Files
wasmtime/crates/wasi-common/wig/src/utils.rs
Alex Crichton 968cd76163 Move back to only one WASI submodule (#1434)
* Move back to only one WASI submodule

This commit fixes the issue where we have two WASI submodules for build
reasons in this repository. The fix was to place the submodule in the
`wasi-common` crate, and then anyone using the `wig` crate has to be
sure to define a `WASI_ROOT` env var in a build script to be able to
parse witx files.

With all that in place `wasi-common` becomes the source of truth for the
witx files we're parsing, and crates like `wasmtime-wasi` use
build-scripts shenanigans to read the same witx files. This should
hopefully get us so we're compatible with publishing and still only have
one submodule!

* rustfmt
2020-03-30 14:45:23 -05:00

58 lines
1.6 KiB
Rust

use proc_macro2::{Ident, Literal, TokenStream, TokenTree};
use std::path::PathBuf;
/// Given the input tokens to a macro invocation, return the path to the
/// witx file to process.
pub(crate) fn witx_path_from_args(args: TokenStream) -> PathBuf {
let mut strings = Vec::new();
for arg in args {
if let TokenTree::Literal(literal) = arg {
let parsed = parse_string_literal(literal);
strings.push(parsed);
} else {
panic!("arguments must be string literals");
}
}
if strings.len() != 1 {
panic!("expected one string literals");
}
let root = PathBuf::from(std::env::var("WASI_ROOT").unwrap());
return root.join(&strings[0]);
}
// Convert a `Literal` holding a string literal into the `String`.
//
// FIXME: It feels like there should be an easier way to do this.
fn parse_string_literal(literal: Literal) -> String {
let s = literal.to_string();
assert!(
s.starts_with('"') && s.ends_with('"'),
"string literal must be enclosed in double-quotes"
);
let trimmed = s[1..s.len() - 1].to_owned();
assert!(
!trimmed.contains('"'),
"string literal must not contain embedded quotes for now"
);
assert!(
!trimmed.contains('\\'),
"string literal must not contain embedded backslashes for now"
);
trimmed
}
pub fn param_name(param: &witx::InterfaceFuncParam) -> Ident {
quote::format_ident!(
"{}",
match param.name.as_str() {
"in" | "type" => format!("r#{}", param.name.as_str()),
s => s.to_string(),
}
)
}