diff --git a/Cargo.lock b/Cargo.lock index 9e134aa95..c54cc2e97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1772,9 +1772,9 @@ dependencies = [ [[package]] name = "wit-parser" -version = "0.6.0" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e0fe225a32528b42a7037add1b5ed1dff83288f21a067007b34565ce87fc2c7" +checksum = "a84965789410bf21087f5a352703142f77b9b4d1478764c3f33a1ea8c7101f40" dependencies = [ "anyhow", "id-arena", diff --git a/Cargo.toml b/Cargo.toml index e04b5b59b..b4d5a91a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ indexmap = "1.9.1" wasm-encoder = "0.24.0" wasm-metadata = "0.2.0" wat = "1.0.59" -wit-parser = "0.6.0" +wit-parser = "0.6.1" wit-component = "0.7.0" wit-bindgen-core = { path = 'crates/bindgen-core', version = '0.3.0' } diff --git a/crates/guest-rust-macro/src/lib.rs b/crates/guest-rust-macro/src/lib.rs index cb60a761f..619ead9b0 100644 --- a/crates/guest-rust-macro/src/lib.rs +++ b/crates/guest-rust-macro/src/lib.rs @@ -33,11 +33,10 @@ impl Parse for Config { let mut world = None; let mut source = None; - let document = if input.peek(token::Brace) { + if input.peek(token::Brace) { let content; syn::braced!(content in input); let fields = Punctuated::::parse_terminated(&content)?; - let mut document = None; for field in fields.into_pairs() { match field.into_value() { Opt::Path(s) => { @@ -47,10 +46,10 @@ impl Parse for Config { source = Some(Source::Path(s.value())); } Opt::World(s) => { - if document.is_some() { - return Err(Error::new(s.span(), "cannot specify second document")); + if world.is_some() { + return Err(Error::new(s.span(), "cannot specify second world")); } - document = Some(parse_doc(&s.value(), &mut world)); + world = Some(s.value()); } Opt::Inline(s) => { if source.is_some() { @@ -67,43 +66,17 @@ impl Parse for Config { Opt::Skip(list) => opts.skip.extend(list.iter().map(|i| i.value())), } } - match (document, &source) { - (Some(doc), _) => doc, - (None, Some(Source::Inline(_))) => "macro-input".to_string(), - _ => { - return Err(Error::new( - call_site, - "must specify a `world` to generate bindings for", - )) - } - } } else { - let document = input.parse::()?; + world = input.parse::>()?.map(|s| s.value()); if input.parse::>()?.is_some() { source = Some(Source::Path(input.parse::()?.value())); } - parse_doc(&document.value(), &mut world) - }; + } let (resolve, pkg, files) = parse_source(&source).map_err(|err| Error::new(call_site, format!("{err:?}")))?; - let doc = resolve.packages[pkg] - .documents - .get(&document) - .copied() - .ok_or_else(|| { - Error::new(call_site, format!("no document named `{document}` found")) - })?; - - let world = match &world { - Some(name) => resolve.documents[doc] - .worlds - .get(name) - .copied() - .ok_or_else(|| Error::new(call_site, format!("no world named `{name}` found")))?, - None => resolve.documents[doc].default_world.ok_or_else(|| { - Error::new(call_site, format!("no default world found in `{document}`")) - })?, - }; + let world = resolve + .select_world(pkg, world.as_deref()) + .map_err(|e| Error::new(call_site, format!("{e:?}")))?; Ok(Config { opts, resolve, @@ -140,16 +113,6 @@ fn parse_source(source: &Option) -> anyhow::Result<(Resolve, PackageId, Ok((resolve, pkg, files)) } -fn parse_doc(s: &str, world: &mut Option) -> String { - match s.find('.') { - Some(pos) => { - *world = Some(s[pos + 1..].to_string()); - s[..pos].to_string() - } - None => s.to_string(), - } -} - impl Config { fn expand(self) -> Result { let mut files = Default::default(); diff --git a/crates/test-helpers/src/lib.rs b/crates/test-helpers/src/lib.rs index 1ec03ec97..361bd7432 100644 --- a/crates/test-helpers/src/lib.rs +++ b/crates/test-helpers/src/lib.rs @@ -135,11 +135,6 @@ fn parse_wit(path: &Path) -> (Resolve, WorldId) { ) .unwrap() }; - let world = resolve.packages[pkg] - .documents - .iter() - .filter_map(|(_, doc)| resolve.documents[*doc].default_world) - .next() - .expect("no `default world` found"); + let world = resolve.select_world(pkg, None).unwrap(); (resolve, world) } diff --git a/src/bin/wit-bindgen.rs b/src/bin/wit-bindgen.rs index fec775d95..080eff496 100644 --- a/src/bin/wit-bindgen.rs +++ b/src/bin/wit-bindgen.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, bail, Context, Result}; +use anyhow::{bail, Context, Result}; use clap::Parser; use std::path::PathBuf; use std::str; @@ -142,44 +142,7 @@ fn gen_world( &Default::default(), )? }; - let world = match &opts.world { - Some(world) => { - let mut parts = world.splitn(2, '.'); - let doc = parts.next().unwrap(); - let world = parts.next(); - let doc = *resolve.packages[pkg] - .documents - .get(doc) - .ok_or_else(|| anyhow!("no document named `{doc}` in package"))?; - match world { - Some(name) => *resolve.documents[doc] - .worlds - .get(name) - .ok_or_else(|| anyhow!("no world named `{name}` in document"))?, - None => resolve.documents[doc] - .default_world - .ok_or_else(|| anyhow!("no default world in document"))?, - } - } - None => { - if resolve.packages[pkg].documents.is_empty() { - bail!("no documents found in package") - } - - let mut unique_default_world = None; - for (_name, doc) in &resolve.documents { - if let Some(default_world) = doc.default_world { - if unique_default_world.is_some() { - bail!("multiple default worlds found in package, specify which to bind with `--world` argument") - } else { - unique_default_world = Some(default_world); - } - } - } - - unique_default_world.ok_or_else(|| anyhow!("no default world in package"))? - } - }; + let world = resolve.select_world(pkg, opts.world.as_deref())?; generator.generate(&resolve, world, files); Ok(()) } diff --git a/tests/runtime/main.rs b/tests/runtime/main.rs index 6f1f94494..4de7745a9 100644 --- a/tests/runtime/main.rs +++ b/tests/runtime/main.rs @@ -76,12 +76,7 @@ fn tests(name: &str) -> Result> { let mut resolve = Resolve::new(); let (pkg, _files) = resolve.push_dir(&dir).unwrap(); - let world = resolve.packages[pkg] - .documents - .iter() - .filter_map(|(_, doc)| resolve.documents[*doc].default_world) - .next() - .expect("no default world found"); + let world = resolve.select_world(pkg, None).unwrap(); let mut rust = Vec::new(); let mut c = Vec::new();