Rewrite the ambiguous path error function for readability

This commit is contained in:
Michael-F-Bryan 2022-07-19 00:19:42 +08:00 committed by Hanno Braun
parent 82b5f6c822
commit 72a633a5e6

View File

@ -228,21 +228,21 @@ fn ambiguous_path_error(
metadata: &cargo_metadata::Metadata, metadata: &cargo_metadata::Metadata,
dir: &Path, dir: &Path,
) -> Error { ) -> Error {
let possible_paths = metadata let mut possible_paths = Vec::new();
.workspace_members
.iter() for id in &metadata.workspace_members {
.map(|id| PathBuf::from(&metadata[id].manifest_path)) let cargo_toml = &metadata[id].manifest_path;
.map(|mut cargo_toml_path| { let crate_dir = cargo_toml
let _ = cargo_toml_path.pop(); .parent()
cargo_toml_path .expect("A Cargo.toml always has a parent");
}) // Try to make the path relative to the workspace root so error messages
.map(|crate_dir| { // aren't super long.
crate_dir let simplified_path = crate_dir
.strip_prefix(&metadata.workspace_root) .strip_prefix(&metadata.workspace_root)
.unwrap_or(&crate_dir) .unwrap_or(crate_dir);
.to_path_buf()
}) possible_paths.push(simplified_path.into());
.collect(); }
Error::AmbiguousPath { Error::AmbiguousPath {
dir: dir.to_path_buf(), dir: dir.to_path_buf(),
@ -368,7 +368,8 @@ pub enum Error {
AmbiguousPath { AmbiguousPath {
/// The model directory supplied by the user. /// The model directory supplied by the user.
dir: PathBuf, dir: PathBuf,
/// The directories for each crate in the workspace. /// The directories for each crate in the workspace, relative to the
/// workspace root.
possible_paths: Vec<PathBuf>, possible_paths: Vec<PathBuf>,
}, },
} }