Skip to content

Commit

Permalink
Auto merge of #13000 - epage:inherit, r=weihanglo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Nov 20, 2023
2 parents 79acbdb + 7e4d0a6 commit 8e00f03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
7 changes: 0 additions & 7 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,13 +1568,6 @@ impl<T> schema::InheritableField<T> {
}),
}
}

fn as_value(&self) -> Option<&T> {
match self {
schema::InheritableField::Inherit(_) => None,
schema::InheritableField::Value(defined) => Some(defined),
}
}
}

impl schema::InheritableDependency {
Expand Down
23 changes: 22 additions & 1 deletion src/cargo/util/toml/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ pub enum InheritableField<T> {
Inherit(TomlInheritedField),
}

impl<T> InheritableField<T> {
pub fn as_value(&self) -> Option<&T> {
match self {
InheritableField::Inherit(_) => None,
InheritableField::Value(defined) => Some(defined),
}
}
}

//. This already has a `Deserialize` impl from version_trim_whitespace
pub type InheritableSemverVersion = InheritableField<semver::Version>;
impl<'de> de::Deserialize<'de> for InheritableSemverVersion {
Expand Down Expand Up @@ -411,7 +420,19 @@ impl<'de> de::Deserialize<'de> for InheritableBtreeMap {
#[serde(rename_all = "kebab-case")]
pub struct TomlInheritedField {
#[serde(deserialize_with = "bool_no_false")]
pub workspace: bool,
workspace: bool,
}

impl TomlInheritedField {
pub fn new() -> Self {
TomlInheritedField { workspace: true }
}
}

impl Default for TomlInheritedField {
fn default() -> Self {
Self::new()
}
}

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

0 comments on commit 8e00f03

Please sign in to comment.