Skip to content

Commit 5eef815

Browse files
committed
Move profile name and dir-name validation and gating to Toml level
1 parent a50be59 commit 5eef815

File tree

3 files changed

+56
-50
lines changed

3 files changed

+56
-50
lines changed

src/cargo/core/profiles.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Deserialize;
66

77
use crate::core::compiler::{CompileMode, ProfileKind};
88
use crate::core::interning::InternedString;
9-
use crate::core::{Feature, Features, PackageId, PackageIdSpec, PackageSet, Shell};
9+
use crate::core::{Features, PackageId, PackageIdSpec, PackageSet, Shell};
1010
use crate::util::errors::CargoResultExt;
1111
use crate::util::toml::{ProfilePackageSpec, StringOrBool, TomlProfile, TomlProfiles, U32OrBool};
1212
use crate::util::{closest_msg, CargoResult, Config};
@@ -54,33 +54,6 @@ impl Profiles {
5454
BTreeMap::new()
5555
};
5656

57-
// Feature gating and name validation
58-
for (profile_name, profile) in &profiles {
59-
match profile_name.as_str() {
60-
"dev" | "release" | "bench" | "test" | "doc" | "check" => {
61-
match &profile.dir_name {
62-
None => {}
63-
Some(dir_name) => {
64-
features.require(Feature::named_profiles())?;
65-
validate_name(&dir_name, "dir-name")?;
66-
}
67-
}
68-
69-
match &profile.inherits {
70-
None => {}
71-
Some(inherits) => {
72-
features.require(Feature::named_profiles())?;
73-
validate_name(&inherits, "inherits")?;
74-
}
75-
}
76-
}
77-
_ => {
78-
features.require(Feature::named_profiles())?;
79-
break;
80-
}
81-
}
82-
}
83-
8457
// Merge with predefined profiles
8558
use std::collections::btree_map::Entry;
8659
for (predef_name, mut predef_prof) in Self::predefined_profiles().into_iter() {
@@ -334,25 +307,6 @@ impl Profiles {
334307
}
335308
}
336309

337-
/// Validate dir-names and profile names according to RFC 2678.
338-
pub fn validate_name(name: &str, what: &str) -> CargoResult<()> {
339-
if let Some(ch) = name
340-
.chars()
341-
.find(|ch| !ch.is_alphanumeric() && *ch != '_' && *ch != '-')
342-
{
343-
failure::bail!("Invalid character `{}` in {}: `{}`", ch, what, name);
344-
}
345-
346-
match name {
347-
"package" | "build" | "debug" => {
348-
failure::bail!("Invalid {}: `{}`", what, name);
349-
}
350-
_ => {}
351-
}
352-
353-
Ok(())
354-
}
355-
356310
/// An object used for handling the profile override hierarchy.
357311
///
358312
/// The precedence of profiles are (first one wins):

src/cargo/util/command_prelude.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::core::compiler::{BuildConfig, MessageFormat};
2-
use crate::core::{profiles, Workspace};
2+
use crate::core::Workspace;
33
use crate::ops::{CompileFilter, CompileOptions, NewOptions, Packages, VersionControl};
44
use crate::sources::CRATES_IO_REGISTRY;
55
use crate::util::important_paths::find_root_manifest_for_wd;
6-
use crate::util::{paths, validate_package_name};
6+
use crate::util::{paths, toml::TomlProfile, validate_package_name};
77
use crate::util::{
88
print_available_benches, print_available_binaries, print_available_examples,
99
print_available_tests,
@@ -305,7 +305,7 @@ pub trait ArgMatchesExt {
305305
Some("dev") => Some(ProfileKind::Dev),
306306
Some("release") => Some(ProfileKind::Release),
307307
Some(name) => {
308-
profiles::validate_name(name, "profile name")?;
308+
TomlProfile::validate_name(name, "profile name")?;
309309
Some(ProfileKind::Custom(name.to_string()))
310310
}
311311
};

src/cargo/util/toml/mod.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,39 @@ impl TomlProfile {
466466
}
467467
}
468468

469+
// Feature gate definition of named profiles
470+
match name {
471+
"dev" | "release" | "bench" | "test" | "doc" | "check" => {}
472+
_ => {
473+
features.require(Feature::named_profiles())?;
474+
}
475+
}
476+
477+
// Feature gate on uses of keys related to named profiles
478+
if self.inherits.is_some() {
479+
features.require(Feature::named_profiles())?;
480+
}
481+
482+
if self.dir_name.is_some() {
483+
features.require(Feature::named_profiles())?;
484+
}
485+
486+
// `dir-name` validation
487+
match &self.dir_name {
488+
None => {}
489+
Some(dir_name) => {
490+
Self::validate_name(&dir_name, "dir-name")?;
491+
}
492+
}
493+
494+
// `inherits` validation
495+
match &self.inherits {
496+
None => {}
497+
Some(inherits) => {
498+
Self::validate_name(&inherits, "inherits")?;
499+
}
500+
}
501+
469502
match name {
470503
"doc" => {
471504
warnings.push("profile `doc` is deprecated and has no effect".to_string());
@@ -490,6 +523,25 @@ impl TomlProfile {
490523
Ok(())
491524
}
492525

526+
/// Validate dir-names and profile names according to RFC 2678.
527+
pub fn validate_name(name: &str, what: &str) -> CargoResult<()> {
528+
if let Some(ch) = name
529+
.chars()
530+
.find(|ch| !ch.is_alphanumeric() && *ch != '_' && *ch != '-')
531+
{
532+
failure::bail!("Invalid character `{}` in {}: `{}`", ch, what, name);
533+
}
534+
535+
match name {
536+
"package" | "build" | "debug" => {
537+
failure::bail!("Invalid {}: `{}`", what, name);
538+
}
539+
_ => {}
540+
}
541+
542+
Ok(())
543+
}
544+
493545
fn validate_override(&self) -> CargoResult<()> {
494546
if self.overrides.is_some() || self.build_override.is_some() {
495547
bail!("Profile overrides cannot be nested.");

0 commit comments

Comments
 (0)