mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00
refactor: Centralize empty name check
This commit is contained in:
parent
2db5c9dd59
commit
a383185c38
@ -1,6 +1,5 @@
|
||||
//! Crate name parsing.
|
||||
|
||||
use anyhow::bail;
|
||||
use anyhow::Context as _;
|
||||
|
||||
use super::Dependency;
|
||||
@ -29,9 +28,6 @@ impl CrateSpec {
|
||||
.map(|(n, v)| (n, Some(v)))
|
||||
.unwrap_or((pkg_id, None));
|
||||
|
||||
if name.is_empty() {
|
||||
bail!("dependency name cannot be empty");
|
||||
}
|
||||
validate_package_name(name, "dependency name", "")?;
|
||||
|
||||
if let Some(version) = version {
|
||||
|
@ -4,7 +4,7 @@ use crate::util::important_paths::find_root_manifest_for_wd;
|
||||
use crate::util::toml_mut::is_sorted;
|
||||
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
|
||||
use crate::util::{restricted_names, Config};
|
||||
use anyhow::{anyhow, bail, Context};
|
||||
use anyhow::{anyhow, Context};
|
||||
use cargo_util::paths::{self, write_atomic};
|
||||
use serde::de;
|
||||
use serde::Deserialize;
|
||||
@ -197,9 +197,6 @@ fn check_name(
|
||||
}
|
||||
help
|
||||
};
|
||||
if name.is_empty() {
|
||||
bail!("package name cannot be empty");
|
||||
}
|
||||
restricted_names::validate_package_name(name, "package name", &bin_help())?;
|
||||
|
||||
if restricted_names::is_keyword(name) {
|
||||
|
@ -833,9 +833,6 @@ Run `{cmd}` to see possible targets."
|
||||
(None, None) => config.default_registry()?.map(RegistryOrIndex::Registry),
|
||||
(None, Some(i)) => Some(RegistryOrIndex::Index(i.into_url()?)),
|
||||
(Some(r), None) => {
|
||||
if r.is_empty() {
|
||||
bail!("registry name cannot be empty");
|
||||
}
|
||||
restricted_names::validate_package_name(r, "registry name", "")?;
|
||||
Some(RegistryOrIndex::Registry(r.to_string()))
|
||||
}
|
||||
@ -851,9 +848,6 @@ Run `{cmd}` to see possible targets."
|
||||
match self._value_of("registry").map(|s| s.to_string()) {
|
||||
None => config.default_registry(),
|
||||
Some(registry) => {
|
||||
if registry.is_empty() {
|
||||
bail!("registry name cannot be empty");
|
||||
}
|
||||
restricted_names::validate_package_name(®istry, "registry name", "")?;
|
||||
Ok(Some(registry))
|
||||
}
|
||||
|
@ -1551,9 +1551,6 @@ impl Config {
|
||||
|
||||
/// Gets the index for a registry.
|
||||
pub fn get_registry_index(&self, registry: &str) -> CargoResult<Url> {
|
||||
if registry.is_empty() {
|
||||
bail!("registry name cannot be empty");
|
||||
}
|
||||
validate_package_name(registry, "registry name", "")?;
|
||||
if let Some(index) = self.get_string(&format!("registries.{}.index", registry))? {
|
||||
self.resolve_registry_index(&index).with_context(|| {
|
||||
|
@ -43,6 +43,10 @@ pub fn is_conflicting_artifact_name(name: &str) -> bool {
|
||||
/// elsewhere. `cargo new` has a few restrictions, such as checking for
|
||||
/// reserved names. crates.io has even more restrictions.
|
||||
pub fn validate_package_name(name: &str, what: &str, help: &str) -> CargoResult<()> {
|
||||
if name.is_empty() {
|
||||
bail!("{what} cannot be empty");
|
||||
}
|
||||
|
||||
let mut chars = name.chars();
|
||||
if let Some(ch) = chars.next() {
|
||||
if ch.is_digit(10) {
|
||||
|
@ -480,10 +480,6 @@ pub fn to_real_manifest(
|
||||
};
|
||||
|
||||
let package_name = package.name.trim();
|
||||
if package_name.is_empty() {
|
||||
bail!("package name cannot be an empty string")
|
||||
}
|
||||
|
||||
validate_package_name(package_name, "package name", "")?;
|
||||
|
||||
let resolved_path = package_root.join("Cargo.toml");
|
||||
|
@ -97,9 +97,6 @@ impl PackageIdSpec {
|
||||
Some(version) => Some(version.parse::<PartialVersion>()?),
|
||||
None => None,
|
||||
};
|
||||
if name.is_empty() {
|
||||
bail!("package ID specification must have a name: `{spec}`");
|
||||
}
|
||||
validate_package_name(name, "pkgid", "")?;
|
||||
Ok(PackageIdSpec {
|
||||
name: String::from(name),
|
||||
@ -185,9 +182,6 @@ impl PackageIdSpec {
|
||||
None => (String::from(path_name), None),
|
||||
}
|
||||
};
|
||||
if name.is_empty() {
|
||||
bail!("package ID specification must have a name: `{url}`");
|
||||
}
|
||||
validate_package_name(name.as_str(), "pkgid", "")?;
|
||||
Ok(PackageIdSpec {
|
||||
name,
|
||||
|
@ -460,7 +460,7 @@ fn cargo_compile_with_empty_package_name() {
|
||||
[ERROR] failed to parse manifest at `[..]`
|
||||
|
||||
Caused by:
|
||||
package name cannot be an empty string
|
||||
package name cannot be empty
|
||||
",
|
||||
)
|
||||
.run();
|
||||
|
Loading…
x
Reference in New Issue
Block a user