Skip to content

Commit

Permalink
refactor(schema): Use dedicated type for 'workspace' field value
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 8, 2024
1 parent 4760ef7 commit e37a04a
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions crates/cargo-util-schemas/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,7 @@ impl<'de> de::Deserialize<'de> for InheritableBtreeMap {
if let Ok(w) = TomlInheritedField::deserialize(
serde_value::ValueDeserializer::<D::Error>::new(value.clone()),
) {
return if w.workspace {
Ok(InheritableField::Inherit(w))
} else {
Err(de::Error::custom("`workspace` cannot be false"))
};
return Ok(InheritableField::Inherit(w));
}
BTreeMap::deserialize(serde_value::ValueDeserializer::<D::Error>::new(value))
.map(InheritableField::Value)
Expand All @@ -424,13 +420,14 @@ impl<'de> de::Deserialize<'de> for InheritableBtreeMap {
#[derive(Deserialize, Serialize, Copy, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct TomlInheritedField {
#[serde(deserialize_with = "bool_no_false")]
workspace: bool,
workspace: WorkspaceValue,
}

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

Expand All @@ -449,6 +446,28 @@ fn bool_no_false<'de, D: de::Deserializer<'de>>(deserializer: D) -> Result<bool,
}
}

#[derive(Deserialize, Serialize, Copy, Clone, Debug)]
#[serde(try_from = "bool")]
#[serde(into = "bool")]
struct WorkspaceValue;

impl TryFrom<bool> for WorkspaceValue {
type Error = String;
fn try_from(other: bool) -> Result<WorkspaceValue, Self::Error> {
if other {
Ok(WorkspaceValue)
} else {
Err("`workspace` cannot be false".to_owned())
}
}
}

impl From<WorkspaceValue> for bool {
fn from(_: WorkspaceValue) -> bool {
true
}
}

#[derive(Serialize, Clone, Debug)]
#[serde(untagged)]
pub enum InheritableDependency {
Expand Down

0 comments on commit e37a04a

Please sign in to comment.