Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize on world selection throughout tooling #505

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
55 changes: 9 additions & 46 deletions crates/guest-rust-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Opt, Token![,]>::parse_terminated(&content)?;
let mut document = None;
for field in fields.into_pairs() {
match field.into_value() {
Opt::Path(s) => {
Expand All @@ -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() {
Expand All @@ -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::<syn::LitStr>()?;
world = input.parse::<Option<syn::LitStr>>()?.map(|s| s.value());
if input.parse::<Option<syn::token::In>>()?.is_some() {
source = Some(Source::Path(input.parse::<syn::LitStr>()?.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,
Expand Down Expand Up @@ -140,16 +113,6 @@ fn parse_source(source: &Option<Source>) -> anyhow::Result<(Resolve, PackageId,
Ok((resolve, pkg, files))
}

fn parse_doc(s: &str, world: &mut Option<String>) -> 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<TokenStream> {
let mut files = Default::default();
Expand Down
7 changes: 1 addition & 6 deletions crates/test-helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
41 changes: 2 additions & 39 deletions src/bin/wit-bindgen.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(())
}
Expand Down
7 changes: 1 addition & 6 deletions tests/runtime/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ fn tests(name: &str) -> Result<Vec<PathBuf>> {

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();
Expand Down