diff --git a/crates/wit-component/tests/components.rs b/crates/wit-component/tests/components.rs index 56a659d16d..a0a61a0c06 100644 --- a/crates/wit-component/tests/components.rs +++ b/crates/wit-component/tests/components.rs @@ -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(); diff --git a/crates/wit-component/tests/interfaces.rs b/crates/wit-component/tests/interfaces.rs index 56b75582e4..be1bce46ca 100644 --- a/crates/wit-component/tests/interfaces.rs +++ b/crates/wit-component/tests/interfaces.rs @@ -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)?)? }; diff --git a/crates/wit-component/tests/wit.rs b/crates/wit-component/tests/wit.rs index cb0dc648e9..cebcfef296 100644 --- a/crates/wit-component/tests/wit.rs +++ b/crates/wit-component/tests/wit.rs @@ -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()); @@ -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 diff --git a/crates/wit-parser/src/resolve.rs b/crates/wit-parser/src/resolve.rs index 5e95187af9..4f1e015f4c 100644 --- a/crates/wit-parser/src/resolve.rs +++ b/crates/wit-parser/src/resolve.rs @@ -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) -> Result)>> { + pub fn push_path(&mut self, path: impl AsRef) -> Result<(Vec, Vec)> { self._push_path(path.as_ref()) } - fn _push_path(&mut self, path: &Path) -> Result)>> { + fn _push_path(&mut self, path: &Path) -> Result<(Vec, Vec)> { if path.is_dir() { self.push_dir(path).with_context(|| { format!( @@ -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)) } } @@ -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)>> { - let mut ret = Vec::new(); + pub fn push_dir(&mut self, path: &Path) -> Result<(Vec, Vec)> { + 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()))?; @@ -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 diff --git a/src/bin/wasm-tools/component.rs b/src/bin/wasm-tools/component.rs index 6e68c94524..bd9426c7b0 100644 --- a/src/bin/wasm-tools/component.rs +++ b/src/bin/wasm-tools/component.rs @@ -202,7 +202,7 @@ impl WitResolve { fn load(&self) -> Result<(Resolve, Vec)> { 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)) } } @@ -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)); } }