Skip to content

Commit

Permalink
refactor: Centralize empty name check
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 13, 2023
1 parent 2db5c9d commit a383185
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 28 deletions.
4 changes: 0 additions & 4 deletions src/cargo/ops/cargo_add/crate_spec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Crate name parsing.

use anyhow::bail;
use anyhow::Context as _;

use super::Dependency;
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 0 additions & 6 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand All @@ -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(&registry, "registry name", "")?;
Ok(Some(registry))
}
Expand Down
3 changes: 0 additions & 3 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/util/restricted_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 0 additions & 6 deletions src/cargo/util_schemas/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a383185

Please sign in to comment.