Skip to content

Commit

Permalink
Rust: make the path to the bitflags re-export configurable. (#645)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
peterhuene committed Aug 21, 2023
1 parent e32a150 commit f26f5aa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
7 changes: 7 additions & 0 deletions crates/rust-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -210,6 +212,7 @@ enum Opt {
Skip(Vec<syn::LitStr>),
Ownership(Ownership),
RuntimePath(syn::LitStr),
BitflagsPath(syn::LitStr),
Exports(Vec<Export>),
Stubs,
ExportPrefix(syn::LitStr),
Expand Down Expand Up @@ -292,6 +295,10 @@ impl Parse for Opt {
input.parse::<kw::runtime_path>()?;
input.parse::<Token![:]>()?;
Ok(Opt::RuntimePath(input.parse()?))
} else if l.peek(kw::bitflags_path) {
input.parse::<kw::bitflags_path>()?;
input.parse::<Token![:]>()?;
Ok(Opt::BitflagsPath(input.parse()?))
} else if l.peek(kw::stubs) {
input.parse::<kw::stubs>()?;
Ok(Opt::Stubs)
Expand Down
18 changes: 17 additions & 1 deletion crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ pub struct Opts {
/// This defaults to `wit_bindgen::rt`.
#[cfg_attr(feature = "clap", arg(long))]
pub runtime_path: Option<String>,

/// 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<String>,
}

impl Opts {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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!(
Expand Down
30 changes: 30 additions & 0 deletions crates/rust/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit f26f5aa

Please sign in to comment.