Skip to content

Commit 2a314c7

Browse files
committed
refactor(toml): Pull out profile name validation
It now lives with other name validation logic.
1 parent 90e2995 commit 2a314c7

File tree

3 files changed

+87
-87
lines changed

3 files changed

+87
-87
lines changed

src/cargo/util/command_prelude.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use crate::ops::{CompileFilter, CompileOptions, NewOptions, Packages, VersionCon
66
use crate::util::important_paths::find_root_manifest_for_wd;
77
use crate::util::interning::InternedString;
88
use crate::util::is_rustup;
9-
use crate::util::restricted_names::is_glob_pattern;
10-
use crate::util::toml::schema::{StringOrVec, TomlProfile};
11-
use crate::util::validate_package_name;
9+
use crate::util::restricted_names;
10+
use crate::util::toml::schema::StringOrVec;
1211
use crate::util::{
1312
print_available_benches, print_available_binaries, print_available_examples,
1413
print_available_packages, print_available_tests,
@@ -607,7 +606,7 @@ Run `{cmd}` to see possible targets."
607606
bail!("profile `doc` is reserved and not allowed to be explicitly specified")
608607
}
609608
(_, _, Some(name)) => {
610-
TomlProfile::validate_name(name)?;
609+
restricted_names::validate_profile_name(name)?;
611610
name
612611
}
613612
};
@@ -801,7 +800,7 @@ Run `{cmd}` to see possible targets."
801800
) -> CargoResult<CompileOptions> {
802801
let mut compile_opts = self.compile_options(config, mode, workspace, profile_checking)?;
803802
let spec = self._values_of("package");
804-
if spec.iter().any(is_glob_pattern) {
803+
if spec.iter().any(restricted_names::is_glob_pattern) {
805804
anyhow::bail!("Glob patterns on package selection are not supported.")
806805
}
807806
compile_opts.spec = Packages::Packages(spec);
@@ -835,7 +834,7 @@ Run `{cmd}` to see possible targets."
835834
(None, None) => config.default_registry()?.map(RegistryOrIndex::Registry),
836835
(None, Some(i)) => Some(RegistryOrIndex::Index(i.into_url()?)),
837836
(Some(r), None) => {
838-
validate_package_name(r, "registry name", "")?;
837+
restricted_names::validate_package_name(r, "registry name", "")?;
839838
Some(RegistryOrIndex::Registry(r.to_string()))
840839
}
841840
(Some(_), Some(_)) => {
@@ -850,7 +849,7 @@ Run `{cmd}` to see possible targets."
850849
match self._value_of("registry").map(|s| s.to_string()) {
851850
None => config.default_registry(),
852851
Some(registry) => {
853-
validate_package_name(&registry, "registry name", "")?;
852+
restricted_names::validate_package_name(&registry, "registry name", "")?;
854853
Ok(Some(registry))
855854
}
856855
}

src/cargo/util/restricted_names.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,82 @@ pub fn is_windows_reserved_path(path: &Path) -> bool {
120120
pub fn is_glob_pattern<T: AsRef<str>>(name: T) -> bool {
121121
name.as_ref().contains(&['*', '?', '[', ']'][..])
122122
}
123+
124+
/// Validate dir-names and profile names according to RFC 2678.
125+
pub fn validate_profile_name(name: &str) -> CargoResult<()> {
126+
if let Some(ch) = name
127+
.chars()
128+
.find(|ch| !ch.is_alphanumeric() && *ch != '_' && *ch != '-')
129+
{
130+
bail!(
131+
"invalid character `{}` in profile name `{}`\n\
132+
Allowed characters are letters, numbers, underscore, and hyphen.",
133+
ch,
134+
name
135+
);
136+
}
137+
138+
const SEE_DOCS: &str = "See https://doc.rust-lang.org/cargo/reference/profiles.html \
139+
for more on configuring profiles.";
140+
141+
let lower_name = name.to_lowercase();
142+
if lower_name == "debug" {
143+
bail!(
144+
"profile name `{}` is reserved\n\
145+
To configure the default development profile, use the name `dev` \
146+
as in [profile.dev]\n\
147+
{}",
148+
name,
149+
SEE_DOCS
150+
);
151+
}
152+
if lower_name == "build-override" {
153+
bail!(
154+
"profile name `{}` is reserved\n\
155+
To configure build dependency settings, use [profile.dev.build-override] \
156+
and [profile.release.build-override]\n\
157+
{}",
158+
name,
159+
SEE_DOCS
160+
);
161+
}
162+
163+
// These are some arbitrary reservations. We have no plans to use
164+
// these, but it seems safer to reserve a few just in case we want to
165+
// add more built-in profiles in the future. We can also uses special
166+
// syntax like cargo:foo if needed. But it is unlikely these will ever
167+
// be used.
168+
if matches!(
169+
lower_name.as_str(),
170+
"build"
171+
| "check"
172+
| "clean"
173+
| "config"
174+
| "fetch"
175+
| "fix"
176+
| "install"
177+
| "metadata"
178+
| "package"
179+
| "publish"
180+
| "report"
181+
| "root"
182+
| "run"
183+
| "rust"
184+
| "rustc"
185+
| "rustdoc"
186+
| "target"
187+
| "tmp"
188+
| "uninstall"
189+
) || lower_name.starts_with("cargo")
190+
{
191+
bail!(
192+
"profile name `{}` is reserved\n\
193+
Please choose a different name.\n\
194+
{}",
195+
name,
196+
SEE_DOCS
197+
);
198+
}
199+
200+
Ok(())
201+
}

src/cargo/util/toml/mod.rs

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, Worksp
2323
use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY};
2424
use crate::util::errors::{CargoResult, ManifestError};
2525
use crate::util::interning::InternedString;
26+
use crate::util::restricted_names;
2627
use crate::util::{
2728
self, config::ConfigRelativePath, validate_package_name, Config, IntoUrl, OptVersionReq,
2829
RustVersion,
@@ -2122,7 +2123,7 @@ impl schema::TomlProfile {
21222123
}
21232124

21242125
// Profile name validation
2125-
Self::validate_name(name)?;
2126+
restricted_names::validate_profile_name(name)?;
21262127

21272128
if let Some(dir_name) = &self.dir_name {
21282129
// This is disabled for now, as we would like to stabilize named
@@ -2180,85 +2181,6 @@ impl schema::TomlProfile {
21802181
Ok(())
21812182
}
21822183

2183-
/// Validate dir-names and profile names according to RFC 2678.
2184-
pub fn validate_name(name: &str) -> CargoResult<()> {
2185-
if let Some(ch) = name
2186-
.chars()
2187-
.find(|ch| !ch.is_alphanumeric() && *ch != '_' && *ch != '-')
2188-
{
2189-
bail!(
2190-
"invalid character `{}` in profile name `{}`\n\
2191-
Allowed characters are letters, numbers, underscore, and hyphen.",
2192-
ch,
2193-
name
2194-
);
2195-
}
2196-
2197-
const SEE_DOCS: &str = "See https://doc.rust-lang.org/cargo/reference/profiles.html \
2198-
for more on configuring profiles.";
2199-
2200-
let lower_name = name.to_lowercase();
2201-
if lower_name == "debug" {
2202-
bail!(
2203-
"profile name `{}` is reserved\n\
2204-
To configure the default development profile, use the name `dev` \
2205-
as in [profile.dev]\n\
2206-
{}",
2207-
name,
2208-
SEE_DOCS
2209-
);
2210-
}
2211-
if lower_name == "build-override" {
2212-
bail!(
2213-
"profile name `{}` is reserved\n\
2214-
To configure build dependency settings, use [profile.dev.build-override] \
2215-
and [profile.release.build-override]\n\
2216-
{}",
2217-
name,
2218-
SEE_DOCS
2219-
);
2220-
}
2221-
2222-
// These are some arbitrary reservations. We have no plans to use
2223-
// these, but it seems safer to reserve a few just in case we want to
2224-
// add more built-in profiles in the future. We can also uses special
2225-
// syntax like cargo:foo if needed. But it is unlikely these will ever
2226-
// be used.
2227-
if matches!(
2228-
lower_name.as_str(),
2229-
"build"
2230-
| "check"
2231-
| "clean"
2232-
| "config"
2233-
| "fetch"
2234-
| "fix"
2235-
| "install"
2236-
| "metadata"
2237-
| "package"
2238-
| "publish"
2239-
| "report"
2240-
| "root"
2241-
| "run"
2242-
| "rust"
2243-
| "rustc"
2244-
| "rustdoc"
2245-
| "target"
2246-
| "tmp"
2247-
| "uninstall"
2248-
) || lower_name.starts_with("cargo")
2249-
{
2250-
bail!(
2251-
"profile name `{}` is reserved\n\
2252-
Please choose a different name.\n\
2253-
{}",
2254-
name,
2255-
SEE_DOCS
2256-
);
2257-
}
2258-
2259-
Ok(())
2260-
}
2261-
22622184
/// Validates a profile.
22632185
///
22642186
/// This is a shallow check, which is reused for the profile itself and any overrides.

0 commit comments

Comments
 (0)