Skip to content

Commit 6e187f8

Browse files
committed
refactor(toml): Make profile validation free methods
1 parent 89bfc4d commit 6e187f8

File tree

2 files changed

+160
-156
lines changed

2 files changed

+160
-156
lines changed

src/cargo/core/profiles.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::util::toml::schema::TomlTrimPathsValue;
3232
use crate::util::toml::schema::{
3333
ProfilePackageSpec, StringOrBool, TomlDebugInfo, TomlProfile, TomlProfiles,
3434
};
35+
use crate::util::toml::validate_profile;
3536
use crate::util::{closest_msg, config, CargoResult, Config};
3637
use anyhow::{bail, Context as _};
3738
use std::collections::{BTreeMap, HashMap, HashSet};
@@ -1235,20 +1236,19 @@ fn get_config_profile(ws: &Workspace<'_>, name: &str) -> CargoResult<Option<Toml
12351236
return Ok(None);
12361237
};
12371238
let mut warnings = Vec::new();
1238-
profile
1239-
.val
1240-
.validate(
1241-
name,
1242-
ws.config().cli_unstable(),
1243-
ws.unstable_features(),
1244-
&mut warnings,
1239+
validate_profile(
1240+
&profile.val,
1241+
name,
1242+
ws.config().cli_unstable(),
1243+
ws.unstable_features(),
1244+
&mut warnings,
1245+
)
1246+
.with_context(|| {
1247+
format!(
1248+
"config profile `{}` is not valid (defined in `{}`)",
1249+
name, profile.definition
12451250
)
1246-
.with_context(|| {
1247-
format!(
1248-
"config profile `{}` is not valid (defined in `{}`)",
1249-
name, profile.definition
1250-
)
1251-
})?;
1251+
})?;
12521252
for warning in warnings {
12531253
ws.config().shell().warn(warning)?;
12541254
}

src/cargo/util/toml/mod.rs

Lines changed: 147 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ impl schema::TomlManifest {
954954
let profiles = me.profile.clone();
955955
if let Some(profiles) = &profiles {
956956
let cli_unstable = config.cli_unstable();
957-
profiles.validate(cli_unstable, &features, &mut warnings)?;
957+
validate_profiles(profiles, cli_unstable, &features, &mut warnings)?;
958958
}
959959

960960
let publish = package.publish.clone().map(|publish| {
@@ -1155,7 +1155,7 @@ impl schema::TomlManifest {
11551155
};
11561156
let profiles = me.profile.clone();
11571157
if let Some(profiles) = &profiles {
1158-
profiles.validate(config.cli_unstable(), &features, &mut warnings)?;
1158+
validate_profiles(profiles, config.cli_unstable(), &features, &mut warnings)?;
11591159
}
11601160
let resolve_behavior = me
11611161
.workspace
@@ -1969,175 +1969,179 @@ impl<P: ResolveToPath + Clone> schema::TomlDetailedDependency<P> {
19691969
}
19701970
}
19711971

1972-
impl schema::TomlProfiles {
1973-
/// Checks syntax validity and unstable feature gate for each profile.
1974-
///
1975-
/// It's a bit unfortunate both `-Z` flags and `cargo-features` are required,
1976-
/// because profiles can now be set in either `Cargo.toml` or `config.toml`.
1977-
fn validate(
1978-
&self,
1979-
cli_unstable: &CliUnstable,
1980-
features: &Features,
1981-
warnings: &mut Vec<String>,
1982-
) -> CargoResult<()> {
1983-
for (name, profile) in &self.0 {
1984-
profile.validate(name, cli_unstable, features, warnings)?;
1985-
}
1986-
Ok(())
1972+
/// Checks syntax validity and unstable feature gate for each profile.
1973+
///
1974+
/// It's a bit unfortunate both `-Z` flags and `cargo-features` are required,
1975+
/// because profiles can now be set in either `Cargo.toml` or `config.toml`.
1976+
fn validate_profiles(
1977+
profiles: &schema::TomlProfiles,
1978+
cli_unstable: &CliUnstable,
1979+
features: &Features,
1980+
warnings: &mut Vec<String>,
1981+
) -> CargoResult<()> {
1982+
for (name, profile) in &profiles.0 {
1983+
validate_profile(profile, name, cli_unstable, features, warnings)?;
19871984
}
1985+
Ok(())
19881986
}
19891987

1990-
impl schema::TomlProfile {
1991-
/// Checks stytax validity and unstable feature gate for a given profile.
1992-
pub fn validate(
1993-
&self,
1994-
name: &str,
1995-
cli_unstable: &CliUnstable,
1996-
features: &Features,
1997-
warnings: &mut Vec<String>,
1998-
) -> CargoResult<()> {
1999-
self.validate_profile(name, cli_unstable, features)?;
2000-
if let Some(ref profile) = self.build_override {
2001-
profile.validate_override("build-override")?;
2002-
profile.validate_profile(&format!("{name}.build-override"), cli_unstable, features)?;
2003-
}
2004-
if let Some(ref packages) = self.package {
2005-
for (override_name, profile) in packages {
2006-
profile.validate_override("package")?;
2007-
profile.validate_profile(
2008-
&format!("{name}.package.{override_name}"),
2009-
cli_unstable,
2010-
features,
2011-
)?;
2012-
}
1988+
/// Checks stytax validity and unstable feature gate for a given profile.
1989+
pub fn validate_profile(
1990+
root: &schema::TomlProfile,
1991+
name: &str,
1992+
cli_unstable: &CliUnstable,
1993+
features: &Features,
1994+
warnings: &mut Vec<String>,
1995+
) -> CargoResult<()> {
1996+
validate_profile_layer(root, name, cli_unstable, features)?;
1997+
if let Some(ref profile) = root.build_override {
1998+
validate_profile_override(profile, "build-override")?;
1999+
validate_profile_layer(
2000+
profile,
2001+
&format!("{name}.build-override"),
2002+
cli_unstable,
2003+
features,
2004+
)?;
2005+
}
2006+
if let Some(ref packages) = root.package {
2007+
for (override_name, profile) in packages {
2008+
validate_profile_override(profile, "package")?;
2009+
validate_profile_layer(
2010+
profile,
2011+
&format!("{name}.package.{override_name}"),
2012+
cli_unstable,
2013+
features,
2014+
)?;
20132015
}
2016+
}
20142017

2015-
// Profile name validation
2016-
restricted_names::validate_profile_name(name)?;
2018+
// Profile name validation
2019+
restricted_names::validate_profile_name(name)?;
20172020

2018-
if let Some(dir_name) = &self.dir_name {
2019-
// This is disabled for now, as we would like to stabilize named
2020-
// profiles without this, and then decide in the future if it is
2021-
// needed. This helps simplify the UI a little.
2022-
bail!(
2023-
"dir-name=\"{}\" in profile `{}` is not currently allowed, \
2021+
if let Some(dir_name) = &root.dir_name {
2022+
// This is disabled for now, as we would like to stabilize named
2023+
// profiles without this, and then decide in the future if it is
2024+
// needed. This helps simplify the UI a little.
2025+
bail!(
2026+
"dir-name=\"{}\" in profile `{}` is not currently allowed, \
20242027
directory names are tied to the profile name for custom profiles",
2025-
dir_name,
2026-
name
2027-
);
2028-
}
2028+
dir_name,
2029+
name
2030+
);
2031+
}
20292032

2030-
// `inherits` validation
2031-
if matches!(self.inherits.as_deref(), Some("debug")) {
2032-
bail!(
2033-
"profile.{}.inherits=\"debug\" should be profile.{}.inherits=\"dev\"",
2034-
name,
2035-
name
2036-
);
2037-
}
2033+
// `inherits` validation
2034+
if matches!(root.inherits.as_deref(), Some("debug")) {
2035+
bail!(
2036+
"profile.{}.inherits=\"debug\" should be profile.{}.inherits=\"dev\"",
2037+
name,
2038+
name
2039+
);
2040+
}
20382041

2039-
match name {
2040-
"doc" => {
2041-
warnings.push("profile `doc` is deprecated and has no effect".to_string());
2042-
}
2043-
"test" | "bench" => {
2044-
if self.panic.is_some() {
2045-
warnings.push(format!("`panic` setting is ignored for `{}` profile", name))
2046-
}
2042+
match name {
2043+
"doc" => {
2044+
warnings.push("profile `doc` is deprecated and has no effect".to_string());
2045+
}
2046+
"test" | "bench" => {
2047+
if root.panic.is_some() {
2048+
warnings.push(format!("`panic` setting is ignored for `{}` profile", name))
20472049
}
2048-
_ => {}
20492050
}
2051+
_ => {}
2052+
}
20502053

2051-
if let Some(panic) = &self.panic {
2052-
if panic != "unwind" && panic != "abort" {
2053-
bail!(
2054-
"`panic` setting of `{}` is not a valid setting, \
2054+
if let Some(panic) = &root.panic {
2055+
if panic != "unwind" && panic != "abort" {
2056+
bail!(
2057+
"`panic` setting of `{}` is not a valid setting, \
20552058
must be `unwind` or `abort`",
2056-
panic
2057-
);
2058-
}
2059+
panic
2060+
);
20592061
}
2062+
}
20602063

2061-
if let Some(schema::StringOrBool::String(arg)) = &self.lto {
2062-
if arg == "true" || arg == "false" {
2063-
bail!(
2064-
"`lto` setting of string `\"{arg}\"` for `{name}` profile is not \
2064+
if let Some(schema::StringOrBool::String(arg)) = &root.lto {
2065+
if arg == "true" || arg == "false" {
2066+
bail!(
2067+
"`lto` setting of string `\"{arg}\"` for `{name}` profile is not \
20652068
a valid setting, must be a boolean (`true`/`false`) or a string \
20662069
(`\"thin\"`/`\"fat\"`/`\"off\"`) or omitted.",
2067-
);
2068-
}
2070+
);
20692071
}
2070-
2071-
Ok(())
20722072
}
20732073

2074-
/// Validates a profile.
2075-
///
2076-
/// This is a shallow check, which is reused for the profile itself and any overrides.
2077-
fn validate_profile(
2078-
&self,
2079-
name: &str,
2080-
cli_unstable: &CliUnstable,
2081-
features: &Features,
2082-
) -> CargoResult<()> {
2083-
if let Some(codegen_backend) = &self.codegen_backend {
2084-
match (
2085-
features.require(Feature::codegen_backend()),
2086-
cli_unstable.codegen_backend,
2087-
) {
2088-
(Err(e), false) => return Err(e),
2089-
_ => {}
2090-
}
2074+
Ok(())
2075+
}
20912076

2092-
if codegen_backend.contains(|c: char| !c.is_ascii_alphanumeric() && c != '_') {
2093-
bail!(
2094-
"`profile.{}.codegen-backend` setting of `{}` is not a valid backend name.",
2095-
name,
2096-
codegen_backend,
2097-
);
2098-
}
2099-
}
2100-
if self.rustflags.is_some() {
2101-
match (
2102-
features.require(Feature::profile_rustflags()),
2103-
cli_unstable.profile_rustflags,
2104-
) {
2105-
(Err(e), false) => return Err(e),
2106-
_ => {}
2107-
}
2108-
}
2109-
if self.trim_paths.is_some() {
2110-
match (
2111-
features.require(Feature::trim_paths()),
2112-
cli_unstable.trim_paths,
2113-
) {
2114-
(Err(e), false) => return Err(e),
2115-
_ => {}
2116-
}
2077+
/// Validates a profile.
2078+
///
2079+
/// This is a shallow check, which is reused for the profile itself and any overrides.
2080+
fn validate_profile_layer(
2081+
profile: &schema::TomlProfile,
2082+
name: &str,
2083+
cli_unstable: &CliUnstable,
2084+
features: &Features,
2085+
) -> CargoResult<()> {
2086+
if let Some(codegen_backend) = &profile.codegen_backend {
2087+
match (
2088+
features.require(Feature::codegen_backend()),
2089+
cli_unstable.codegen_backend,
2090+
) {
2091+
(Err(e), false) => return Err(e),
2092+
_ => {}
21172093
}
2118-
Ok(())
2119-
}
21202094

2121-
/// Validation that is specific to an override.
2122-
fn validate_override(&self, which: &str) -> CargoResult<()> {
2123-
if self.package.is_some() {
2124-
bail!("package-specific profiles cannot be nested");
2125-
}
2126-
if self.build_override.is_some() {
2127-
bail!("build-override profiles cannot be nested");
2128-
}
2129-
if self.panic.is_some() {
2130-
bail!("`panic` may not be specified in a `{}` profile", which)
2095+
if codegen_backend.contains(|c: char| !c.is_ascii_alphanumeric() && c != '_') {
2096+
bail!(
2097+
"`profile.{}.codegen-backend` setting of `{}` is not a valid backend name.",
2098+
name,
2099+
codegen_backend,
2100+
);
21312101
}
2132-
if self.lto.is_some() {
2133-
bail!("`lto` may not be specified in a `{}` profile", which)
2102+
}
2103+
if profile.rustflags.is_some() {
2104+
match (
2105+
features.require(Feature::profile_rustflags()),
2106+
cli_unstable.profile_rustflags,
2107+
) {
2108+
(Err(e), false) => return Err(e),
2109+
_ => {}
21342110
}
2135-
if self.rpath.is_some() {
2136-
bail!("`rpath` may not be specified in a `{}` profile", which)
2111+
}
2112+
if profile.trim_paths.is_some() {
2113+
match (
2114+
features.require(Feature::trim_paths()),
2115+
cli_unstable.trim_paths,
2116+
) {
2117+
(Err(e), false) => return Err(e),
2118+
_ => {}
21372119
}
2138-
Ok(())
21392120
}
2121+
Ok(())
2122+
}
21402123

2124+
/// Validation that is specific to an override.
2125+
fn validate_profile_override(profile: &schema::TomlProfile, which: &str) -> CargoResult<()> {
2126+
if profile.package.is_some() {
2127+
bail!("package-specific profiles cannot be nested");
2128+
}
2129+
if profile.build_override.is_some() {
2130+
bail!("build-override profiles cannot be nested");
2131+
}
2132+
if profile.panic.is_some() {
2133+
bail!("`panic` may not be specified in a `{}` profile", which)
2134+
}
2135+
if profile.lto.is_some() {
2136+
bail!("`lto` may not be specified in a `{}` profile", which)
2137+
}
2138+
if profile.rpath.is_some() {
2139+
bail!("`rpath` may not be specified in a `{}` profile", which)
2140+
}
2141+
Ok(())
2142+
}
2143+
2144+
impl schema::TomlProfile {
21412145
/// Overwrite self's values with the given profile.
21422146
pub fn merge(&mut self, profile: &schema::TomlProfile) {
21432147
if let Some(v) = &profile.opt_level {

0 commit comments

Comments
 (0)