Skip to content

Commit 8e00f03

Browse files
committed
Auto merge of #13000 - epage:inherit, r=weihanglo
refactor(toml): Further clean up inheritance ### What does this PR try to resolve? This is a follow up to #12971 that was found as I continued working towards #12801. The first is a more general purpose API cleanup. I was bothered by the idea that a caller could create a `field.workspace = false` when that is disallowed, so I modified the API to prevent that. The second is part of needing to find a home for everything in `toml/mod.rs`. I figured `IneheritableField::as_value` is reasonable in the API, so I carried that forward. It would be reasonable to add other methods, from an API perspective, but I left that for future exploration. ### How should we test and review this PR? ### Additional information
2 parents 79acbdb + 7e4d0a6 commit 8e00f03

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,13 +1568,6 @@ impl<T> schema::InheritableField<T> {
15681568
}),
15691569
}
15701570
}
1571-
1572-
fn as_value(&self) -> Option<&T> {
1573-
match self {
1574-
schema::InheritableField::Inherit(_) => None,
1575-
schema::InheritableField::Value(defined) => Some(defined),
1576-
}
1577-
}
15781571
}
15791572

15801573
impl schema::InheritableDependency {

src/cargo/util/toml/schema.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ pub enum InheritableField<T> {
170170
Inherit(TomlInheritedField),
171171
}
172172

173+
impl<T> InheritableField<T> {
174+
pub fn as_value(&self) -> Option<&T> {
175+
match self {
176+
InheritableField::Inherit(_) => None,
177+
InheritableField::Value(defined) => Some(defined),
178+
}
179+
}
180+
}
181+
173182
//. This already has a `Deserialize` impl from version_trim_whitespace
174183
pub type InheritableSemverVersion = InheritableField<semver::Version>;
175184
impl<'de> de::Deserialize<'de> for InheritableSemverVersion {
@@ -411,7 +420,19 @@ impl<'de> de::Deserialize<'de> for InheritableBtreeMap {
411420
#[serde(rename_all = "kebab-case")]
412421
pub struct TomlInheritedField {
413422
#[serde(deserialize_with = "bool_no_false")]
414-
pub workspace: bool,
423+
workspace: bool,
424+
}
425+
426+
impl TomlInheritedField {
427+
pub fn new() -> Self {
428+
TomlInheritedField { workspace: true }
429+
}
430+
}
431+
432+
impl Default for TomlInheritedField {
433+
fn default() -> Self {
434+
Self::new()
435+
}
415436
}
416437

417438
fn bool_no_false<'de, D: de::Deserializer<'de>>(deserializer: D) -> Result<bool, D::Error> {

0 commit comments

Comments
 (0)