Skip to content

Commit

Permalink
Rename infer_package to infer_package_for_git_source
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Nov 3, 2022
1 parent f53e22b commit ce3f722
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ fn resolve_dependency(
} else {
let mut source = crate::sources::GitSource::new(src.source_id()?, config)?;
let packages = source.read_packages()?;
let package = infer_package(packages, &src, true)?;
let package = infer_package_for_git_source(packages, &src)?;
Dependency::from(package.summary())
};
selected
Expand All @@ -313,8 +313,10 @@ fn resolve_dependency(
selected
} else {
let source = crate::sources::PathSource::new(&path, src.source_id()?, config);
let packages = source.read_packages()?;
let package = infer_package(packages, &src, false)?;
let mut packages = source.read_packages()?;
let package = packages
.pop()
.ok_or(anyhow::anyhow!("no packages found at `{src}`"))?;
Dependency::from(package.summary())
};
selected
Expand Down Expand Up @@ -599,10 +601,9 @@ fn select_package(
}
}

fn infer_package(
fn infer_package_for_git_source(
mut packages: Vec<Package>,
src: &dyn std::fmt::Display,
is_git_source: bool,
) -> CargoResult<Package> {
let package = match packages.len() {
0 => {
Expand All @@ -616,7 +617,7 @@ fn infer_package(
.collect();
names.sort_unstable();
anyhow::bail!(
"multiple packages found at `{src}`:\n {}{}",
"multiple packages found at `{src}`:\n {}\nTo disambiguate, run `cargo add --git {src} <package>`",
names
.iter()
.map(|s| s.to_string())
Expand All @@ -627,11 +628,6 @@ fn infer_package(
})
.into_iter()
.format("\n "),
if is_git_source {
format!("\nTo disambiguate, run `cargo add --git {src} <crate>`")
} else {
"".to_owned()
}
);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
error: multiple packages found at `[ROOTURL]/git-package`:
my-package1, my-package2, my-package3, my-package4, my-package5, my-package6
my-package7, my-package8, my-package9
To disambiguate, run `cargo add --git [ROOTURL]/git-package <crate>`
To disambiguate, run `cargo add --git [ROOTURL]/git-package <package>`

0 comments on commit ce3f722

Please sign in to comment.