Skip to content

Commit 1846165

Browse files
authored
Support multiple documents with a single default world. (#494)
Previously, wit-bindgen only supported using a default world when there was exactly one document. With this change, wit-bindgen will look through all the documents and if only one of them has a default world, it'll use that world. This is a common pattern in WASI proposal repos.
1 parent 864da57 commit 1846165

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/bin/wit-bindgen.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,22 @@ fn gen_world(
162162
}
163163
}
164164
None => {
165-
let mut docs = resolve.packages[pkg].documents.iter();
166-
let (_, doc) = docs
167-
.next()
168-
.ok_or_else(|| anyhow!("no documents found in package"))?;
169-
if docs.next().is_some() {
170-
bail!("multiple documents found in package, specify which to bind with `--world` argument")
165+
if resolve.packages[pkg].documents.is_empty() {
166+
bail!("no documents found in package")
171167
}
172-
resolve.documents[*doc]
173-
.default_world
174-
.ok_or_else(|| anyhow!("no default world in document"))?
168+
169+
let mut unique_default_world = None;
170+
for (_name, doc) in &resolve.documents {
171+
if let Some(default_world) = doc.default_world {
172+
if unique_default_world.is_some() {
173+
bail!("multiple default worlds found in package, specify which to bind with `--world` argument")
174+
} else {
175+
unique_default_world = Some(default_world);
176+
}
177+
}
178+
}
179+
180+
unique_default_world.ok_or_else(|| anyhow!("no default world in package"))?
175181
}
176182
};
177183
generator.generate(&resolve, world, files);

0 commit comments

Comments
 (0)