From f26f5aa6a96f0c611032bc7d57bc16c536df9280 Mon Sep 17 00:00:00 2001 From: Peter Huene Date: Mon, 21 Aug 2023 07:14:46 -0700 Subject: [PATCH] Rust: make the path to the `bitflags` re-export configurable. (#645) This commit adds a `bitflags_path` option to the macro allowing an alternative path to the `bitflags` crate, similar to the `runtime_path` option. It is primarily intended for crates that wrap wit-bindgen and therefore wit-bindgen might not be a direct dependency from user crates. --- crates/rust-macro/src/lib.rs | 7 +++++++ crates/rust/src/lib.rs | 18 +++++++++++++++++- crates/rust/tests/codegen.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/crates/rust-macro/src/lib.rs b/crates/rust-macro/src/lib.rs index 202e8165b..41a9b0519 100644 --- a/crates/rust-macro/src/lib.rs +++ b/crates/rust-macro/src/lib.rs @@ -63,6 +63,7 @@ impl Parse for Config { Opt::Ownership(ownership) => opts.ownership = ownership, Opt::Skip(list) => opts.skip.extend(list.iter().map(|i| i.value())), Opt::RuntimePath(path) => opts.runtime_path = Some(path.value()), + Opt::BitflagsPath(path) => opts.bitflags_path = Some(path.value()), Opt::Exports(exports) => opts.exports.extend( exports .into_iter() @@ -154,6 +155,7 @@ mod kw { syn::custom_keyword!(inline); syn::custom_keyword!(ownership); syn::custom_keyword!(runtime_path); + syn::custom_keyword!(bitflags_path); syn::custom_keyword!(exports); syn::custom_keyword!(stubs); syn::custom_keyword!(export_prefix); @@ -210,6 +212,7 @@ enum Opt { Skip(Vec), Ownership(Ownership), RuntimePath(syn::LitStr), + BitflagsPath(syn::LitStr), Exports(Vec), Stubs, ExportPrefix(syn::LitStr), @@ -292,6 +295,10 @@ impl Parse for Opt { input.parse::()?; input.parse::()?; Ok(Opt::RuntimePath(input.parse()?)) + } else if l.peek(kw::bitflags_path) { + input.parse::()?; + input.parse::()?; + Ok(Opt::BitflagsPath(input.parse()?)) } else if l.peek(kw::stubs) { input.parse::()?; Ok(Opt::Stubs) diff --git a/crates/rust/src/lib.rs b/crates/rust/src/lib.rs index 54850e6db..4eaac79ec 100644 --- a/crates/rust/src/lib.rs +++ b/crates/rust/src/lib.rs @@ -137,6 +137,12 @@ pub struct Opts { /// This defaults to `wit_bindgen::rt`. #[cfg_attr(feature = "clap", arg(long))] pub runtime_path: Option, + + /// The optional path to the bitflags crate to use. + /// + /// This defaults to `wit_bindgen::bitflags`. + #[cfg_attr(feature = "clap", arg(long))] + pub bitflags_path: Option, } impl Opts { @@ -213,6 +219,13 @@ impl RustWasm { .as_deref() .unwrap_or("wit_bindgen::rt") } + + fn bitflags_path(&self) -> &str { + self.opts + .bitflags_path + .as_deref() + .unwrap_or("wit_bindgen::bitflags") + } } impl WorldGenerator for RustWasm { @@ -1281,7 +1294,10 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> { } fn type_flags(&mut self, _id: TypeId, name: &str, flags: &Flags, docs: &Docs) { - self.src.push_str("wit_bindgen::bitflags::bitflags! {\n"); + self.src.push_str(&format!( + "{bitflags}::bitflags! {{\n", + bitflags = self.gen.bitflags_path() + )); self.rustdoc(docs); let repr = RustFlagsRepr::new(flags); self.src.push_str(&format!( diff --git a/crates/rust/tests/codegen.rs b/crates/rust/tests/codegen.rs index fe7477828..f0de3f039 100644 --- a/crates/rust/tests/codegen.rs +++ b/crates/rust/tests/codegen.rs @@ -195,3 +195,33 @@ mod alternative_runtime_path { fn foobar() {} } } + +mod alternative_bitflags_path { + wit_bindgen::generate!({ + inline: " + package my:inline + world foo { + flags bar { + foo, + bar, + baz + } + export get-flag: func() -> bar + } + ", + bitflags_path: "my_bitflags", + exports: { + world: Component + } + }); + + pub(crate) use wit_bindgen::bitflags as my_bitflags; + + struct Component; + + impl Foo for Component { + fn get_flag() -> Bar { + Bar::BAZ + } + } +}