refactor(publish): extract "no publishable found" error

This commit is contained in:
Weihang Lo 2025-05-14 01:21:10 -04:00
parent 2421496c92
commit 6f8136488e
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7

View File

@ -93,6 +93,20 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
.filter(|(m, _)| specs.iter().any(|spec| spec.matches(m.package_id())))
.collect();
let (unpublishable, pkgs): (Vec<_>, Vec<_>) = pkgs
.into_iter()
.partition(|(pkg, _)| pkg.publish() == &Some(vec![]));
if !unpublishable.is_empty() {
bail!(
"{} cannot be published.\n\
`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.",
unpublishable
.iter()
.map(|(pkg, _)| format!("`{}`", pkg.name()))
.join(", "),
);
}
let just_pkgs: Vec<_> = pkgs.iter().map(|p| p.0).collect();
let reg_or_index = match opts.reg_or_index.clone() {
Some(r) => {
@ -705,19 +719,6 @@ fn package_list(pkgs: impl IntoIterator<Item = PackageId>, final_sep: &str) -> S
}
fn validate_registry(pkgs: &[&Package], reg_or_index: Option<&RegistryOrIndex>) -> CargoResult<()> {
let unpublishable = pkgs
.iter()
.filter(|pkg| pkg.publish() == &Some(Vec::new()))
.map(|pkg| format!("`{}`", pkg.name()))
.collect::<Vec<_>>();
if !unpublishable.is_empty() {
bail!(
"{} cannot be published.\n\
`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.",
unpublishable.join(", ")
);
}
let reg_name = match reg_or_index {
Some(RegistryOrIndex::Registry(r)) => Some(r.as_str()),
None => Some(CRATES_IO_REGISTRY),