Skip to content

Commit

Permalink
Updated push_path and push_dir return signature
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslavsky committed Jun 1, 2024
1 parent 9fdcd8c commit a837ff2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions crates/wit-component/tests/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ fn main() -> Result<()> {
fn run_test(path: &Path) -> Result<()> {
let test_case = path.file_stem().unwrap().to_str().unwrap();
let mut resolve = Resolve::default();
let pkgs = resolve.push_dir(&path)?;
let pkg_count = pkgs.len();
let (pkg_ids, _) = resolve.push_dir(&path)?;
let pkg_count = pkg_ids.len();

for (pkg_id, _) in pkgs {
for pkg_id in pkg_ids {
// If this test case contained multiple packages, create separate sub-directories for
// each.
let mut path = path.to_path_buf();
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/tests/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() -> Result<()> {
fn run_test(path: &Path, is_dir: bool) -> Result<()> {
let mut resolve = Resolve::new();
let packages = if is_dir {
resolve.push_dir(path)?.into_iter().map(|x| x.0).collect()
resolve.push_dir(path)?.0
} else {
resolve.append(UnresolvedPackage::parse_file(path)?)?
};
Expand Down
4 changes: 2 additions & 2 deletions crates/wit-component/tests/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn parse_wit_dir() -> Result<()> {
drop(env_logger::try_init());

let mut resolver = Resolve::default();
let package_id = resolver.push_path("tests/wit/parse-dir/wit")?[0].0;
let package_id = resolver.push_path("tests/wit/parse-dir/wit")?.0[0];
assert!(resolver
.select_world(package_id, "foo-world".into())
.is_ok());
Expand All @@ -23,7 +23,7 @@ fn parse_wit_file() -> Result<()> {
drop(env_logger::try_init());

let mut resolver = Resolve::default();
let package_id = resolver.push_path("tests/wit/parse-dir/wit/deps/bar/bar.wit")?[0].0;
let package_id = resolver.push_path("tests/wit/parse-dir/wit/deps/bar/bar.wit")?.0[0];
resolver.select_world(package_id, "bar-world".into())?;
assert!(resolver
.interfaces
Expand Down
24 changes: 13 additions & 11 deletions crates/wit-parser/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ impl Resolve {
///
/// Returns the top-level [`PackageId`] as well as a list of all files read
/// during this parse.
pub fn push_path(&mut self, path: impl AsRef<Path>) -> Result<Vec<(PackageId, Vec<PathBuf>)>> {
pub fn push_path(&mut self, path: impl AsRef<Path>) -> Result<(Vec<PackageId>, Vec<PathBuf>)> {
self._push_path(path.as_ref())
}

fn _push_path(&mut self, path: &Path) -> Result<Vec<(PackageId, Vec<PathBuf>)>> {
fn _push_path(&mut self, path: &Path) -> Result<(Vec<PackageId>, Vec<PathBuf>)> {
if path.is_dir() {
self.push_dir(path).with_context(|| {
format!(
Expand All @@ -171,11 +171,13 @@ impl Resolve {
})
} else {
let ids = self.push_file(path)?;
let mut ret = Vec::new();
let mut pkg_ids = Vec::new();
let mut path_bufs = Vec::new();
for id in ids {
ret.push((id, vec![path.to_path_buf()]));
pkg_ids.push(id);
path_bufs.push(path.to_path_buf());
}
Ok(ret)
Ok((pkg_ids, path_bufs))
}
}

Expand All @@ -198,8 +200,9 @@ impl Resolve {
/// This function returns the [`PackageId`] of the root parsed package at
/// `path`, along with a list of all paths that were consumed during parsing
/// for the root package and all dependency packages, for each package encountered.
pub fn push_dir(&mut self, path: &Path) -> Result<Vec<(PackageId, Vec<PathBuf>)>> {
let mut ret = Vec::new();
pub fn push_dir(&mut self, path: &Path) -> Result<(Vec<PackageId>, Vec<PathBuf>)> {
let mut pkg_ids = Vec::new();
let mut path_bufs = Vec::new();
let pkgs = UnresolvedPackage::parse_dir(path)
.with_context(|| format!("failed to parse package: {}", path.display()))?;

Expand All @@ -222,19 +225,18 @@ impl Resolve {
// Additionally note that the last item visited here is the root
// package, which is the one returned here.
let mut last = None;
let mut files = Vec::new();
let mut pkg = Some(pkg);
for name in order {
let pkg = deps.remove(&name).unwrap_or_else(|| pkg.take().unwrap());
files.extend(pkg.source_files().map(|p| p.to_path_buf()));
path_bufs.extend(pkg.source_files().map(|p| p.to_path_buf()));
let pkgid = self.push(pkg)?;
last = Some(pkgid);
}

ret.push((last.unwrap(), files));
pkg_ids.push(last.unwrap());
}

Ok(ret)
Ok((pkg_ids, path_bufs))
}

// TODO: this is a pretty naive implementation, since for example it doesn't re-use dependencies
Expand Down
6 changes: 3 additions & 3 deletions src/bin/wasm-tools/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl WitResolve {

fn load(&self) -> Result<(Resolve, Vec<PackageId>)> {
let mut resolve = Self::resolve_with_features(&self.features);
let pkg_ids = resolve.push_path(&self.wit)?.iter().map(|p| p.0).collect();
let (pkg_ids, _) = resolve.push_path(&self.wit)?;
Ok((resolve, pkg_ids))
}
}
Expand Down Expand Up @@ -532,8 +532,8 @@ impl WitOpts {
if let Some(input) = &self.input {
if input.is_dir() {
let mut resolve = WitResolve::resolve_with_features(&self.features);
let ids = resolve.push_dir(&input)?.iter().map(|p| p.0).collect();
return Ok(DecodedWasm::WitPackages(resolve, ids));
let (pkg_ids, _) = resolve.push_dir(&input)?;
return Ok(DecodedWasm::WitPackages(resolve, pkg_ids));
}
}

Expand Down

0 comments on commit a837ff2

Please sign in to comment.