Skip to content

Commit 07c67fd

Browse files
authored
Merge pull request #47 from rylev/wit-packages
Allow single file wit packages
2 parents b294ae0 + 1e4af59 commit 07c67fd

3 files changed

Lines changed: 42 additions & 35 deletions

File tree

crates/wac-parser/src/ast/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'a> Parse<'a> for ImportType<'a> {
118118
}
119119
}
120120

121-
/// Represents a package path in the AST.
121+
/// AST representation of a path to an item such as a world in a package (e.g. `foo:bar/qux`).
122122
#[derive(Debug, Clone, Serialize)]
123123
#[serde(rename_all = "camelCase")]
124124
pub struct PackagePath<'a> {

crates/wac-parser/src/resolution/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ impl<'a> AstResolver<'a> {
184184
// If there's a target world in the directive, validate the composition
185185
// conforms to the target
186186
if let Some(path) = &self.document.directive.targets {
187+
log::debug!("validating composition targets world `{}`", path.string);
187188
let item = self.resolve_package_export(&mut state, path)?;
188189
match item {
189190
ItemKind::Type(Type::World(world)) => {
@@ -1563,6 +1564,7 @@ impl<'a> AstResolver<'a> {
15631564
state: &mut State<'a>,
15641565
path: &'a ast::PackagePath<'a>,
15651566
) -> ResolutionResult<ItemKind> {
1567+
log::debug!("resolving package export `{}`", path.string);
15661568
// Check for reference to local item
15671569
if path.name == self.document.directive.package.name {
15681570
return self.resolve_local_export(state, path);

crates/wac-resolver/src/fs.rs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,41 +79,46 @@ impl FileSystemPackageResolver {
7979
// First check to see if a directory exists.
8080
// If so, then treat it as a textual WIT package.
8181
#[cfg(feature = "wit")]
82-
if path.is_dir() {
83-
log::debug!(
84-
"loading WIT package from directory `{path}`",
85-
path = path.display()
86-
);
87-
82+
{
83+
let pkg_res_failure = |e| Error::PackageResolutionFailure {
84+
name: key.name.to_string(),
85+
span: *span,
86+
source: e,
87+
};
8888
let mut resolve = wit_parser::Resolve::new();
89-
let (pkg, _) =
90-
resolve
91-
.push_dir(&path)
92-
.map_err(|e| Error::PackageResolutionFailure {
93-
name: key.name.to_string(),
94-
span: *span,
95-
source: e,
96-
})?;
97-
98-
packages.insert(
99-
*key,
100-
Arc::new(
101-
wit_component::encode(Some(true), &resolve, pkg)
102-
.with_context(|| {
103-
format!(
104-
"failed to encode WIT package from directory `{path}`",
105-
path = path.display()
106-
)
107-
})
108-
.map_err(|e| Error::PackageResolutionFailure {
109-
name: key.name.to_string(),
110-
span: *span,
111-
source: e,
112-
})?,
113-
),
114-
);
115-
116-
continue;
89+
let pkg = if path.is_dir() {
90+
log::debug!(
91+
"loading WIT package from directory `{path}`",
92+
path = path.display()
93+
);
94+
95+
let (pkg, _) = resolve.push_dir(&path).map_err(pkg_res_failure)?;
96+
Some(pkg)
97+
} else if path.extension().and_then(std::ffi::OsStr::to_str) == Some("wit") {
98+
let unresolved = wit_parser::UnresolvedPackage::parse_file(&path)
99+
.map_err(pkg_res_failure)?;
100+
let pkg = resolve.push(unresolved).map_err(pkg_res_failure)?;
101+
Some(pkg)
102+
} else {
103+
None
104+
};
105+
if let Some(pkg) = pkg {
106+
packages.insert(
107+
*key,
108+
Arc::new(
109+
wit_component::encode(Some(true), &resolve, pkg)
110+
.with_context(|| {
111+
format!(
112+
"failed to encode WIT package from `{path}`",
113+
path = path.display()
114+
)
115+
})
116+
.map_err(pkg_res_failure)?,
117+
),
118+
);
119+
120+
continue;
121+
}
117122
}
118123

119124
if !path.is_file() {

0 commit comments

Comments
 (0)