diff --git a/Cargo.lock b/Cargo.lock index afc846c84..3f469c114 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -654,6 +654,17 @@ dependencies = [ "logos-codegen", ] +[[package]] +name = "macro-string" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59a9dbbfc75d2688ed057456ce8a3ee3f48d12eec09229f560f3643b9f275653" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "memchr" version = "2.7.6" @@ -1604,6 +1615,7 @@ name = "wit-bindgen-rust-macro" version = "0.53.1" dependencies = [ "anyhow", + "macro-string", "prettyplease", "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 86e224127..beb93a8ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ indexmap = "2.0.0" prettyplease = "0.2.20" syn = { version = "2.0.89", features = ["printing"] } futures = "0.3.31" +macro-string = "0.2.0" wat = "1.245.1" wasmparser = "0.245.1" diff --git a/crates/guest-rust/macro/Cargo.toml b/crates/guest-rust/macro/Cargo.toml index 5fb9c1ea6..0764d61b2 100644 --- a/crates/guest-rust/macro/Cargo.toml +++ b/crates/guest-rust/macro/Cargo.toml @@ -23,6 +23,7 @@ wit-bindgen-rust = { workspace = true } anyhow = { workspace = true } syn = { workspace = true } prettyplease = { workspace = true } +macro-string = { workspace = true } [features] async = [] diff --git a/crates/guest-rust/macro/src/lib.rs b/crates/guest-rust/macro/src/lib.rs index 91d4335dd..e5586e0d3 100644 --- a/crates/guest-rust/macro/src/lib.rs +++ b/crates/guest-rust/macro/src/lib.rs @@ -5,8 +5,7 @@ use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; use syn::parse::{Error, Parse, ParseStream, Result}; use syn::punctuated::Punctuated; -use syn::spanned::Spanned; -use syn::{LitStr, Token, braced, token}; +use syn::{Token, braced, token}; use wit_bindgen_core::AsyncFilterSet; use wit_bindgen_core::WorldGenerator; use wit_bindgen_core::wit_parser::{PackageId, Resolve, UnresolvedPackageGroup, WorldId}; @@ -76,7 +75,13 @@ impl Parse for Config { for field in fields.into_pairs() { match field.into_value() { Opt::Path(span, p) => { - let paths = p.into_iter().map(|f| PathBuf::from(f.value())).collect(); + let paths = p + .into_iter() + .map(|f| f.eval()) + .collect::>>()? + .into_iter() + .map(PathBuf::from) + .collect(); source = Some(match source { Some(Source::Paths(_)) | Some(Source::Inline(_, Some(_))) => { @@ -348,7 +353,7 @@ impl From for wit_bindgen_rust::ExportKey { enum Opt { World(syn::LitStr), - Path(Span, Vec), + Path(Span, Vec), Inline(syn::LitStr), UseStdFeature, RawStrings, @@ -387,11 +392,13 @@ impl Parse for Opt { if input.peek(token::Bracket) { let contents; syn::bracketed!(contents in input); + let span = input.span(); let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?; - Ok(Opt::Path(list.span(), list.into_iter().collect())) + Ok(Opt::Path(span, list.into_iter().collect())) } else { - let path: LitStr = input.parse()?; - Ok(Opt::Path(path.span(), vec![path])) + let span = input.span(); + let path: macro_string::MacroString = input.parse()?; + Ok(Opt::Path(span, vec![path])) } } else if l.peek(kw::inline) { input.parse::()?; diff --git a/crates/guest-rust/src/examples.rs b/crates/guest-rust/src/examples.rs index a63996889..653cb717a 100644 --- a/crates/guest-rust/src/examples.rs +++ b/crates/guest-rust/src/examples.rs @@ -53,3 +53,12 @@ pub mod _3_world_exports; #[doc = include_str!("./examples/_4_exported_resources.rs")] /// ``` pub mod _4_exported_resources; + +/// An example of importing a compile-time directory of wit files. +/// +/// The code used to generate this module is: +/// +/// ```rust +#[doc = include_str!("./examples/_5_import_directory.rs")] +/// ``` +pub mod _5_import_directory; diff --git a/crates/guest-rust/src/examples/_5_import_directory.rs b/crates/guest-rust/src/examples/_5_import_directory.rs new file mode 100644 index 000000000..a6905e944 --- /dev/null +++ b/crates/guest-rust/src/examples/_5_import_directory.rs @@ -0,0 +1,6 @@ +crate::generate!({ + path: concat!( + env!("CARGO_MANIFEST_DIR"), + "/wit/" + ), +}); diff --git a/crates/guest-rust/wit/_5_import_directory.wit b/crates/guest-rust/wit/_5_import_directory.wit new file mode 100644 index 000000000..e43771793 --- /dev/null +++ b/crates/guest-rust/wit/_5_import_directory.wit @@ -0,0 +1,5 @@ +package example:import-directory; + +world import-directory { + import hello: func() -> string; +}