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 Oct 9, 2022
1 parent 6ae2921 commit a62b1ad
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 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,11 @@ 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()?;
if packages.len() == 0 {
anyhow::bail!("no packages found at `{src}`");
}
let package = packages.pop().expect("at least one package");
Dependency::from(package.summary())
};
selected
Expand Down Expand Up @@ -599,10 +602,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 @@ -627,11 +629,7 @@ fn infer_package(
})
.into_iter()
.format("\n "),
if is_git_source {
format!("\nTo disambiguate, run `cargo add --git {src} <crate>`")
} else {
"".to_owned()
}
format!("\nTo disambiguate, run `cargo add --git {src} <crate>`")
);
}
};
Expand Down

0 comments on commit a62b1ad

Please sign in to comment.