use std::collections::BTreeMap; use crate::Error; use serde::{Deserialize, Serialize, Serializer}; /// Placeholder for a property that may be missing from its package, and needs to be copied from a `Workspace`. #[derive(Debug, Copy, Clone, Serialize, Deserialize)] #[serde(untagged, try_from = "InheritableSerdeParser")] pub enum Inheritable { Set(T), #[serde(serialize_with = "workspace_true")] Inherited, } impl TryFrom> for Inheritable { type Error = String; fn try_from(parsed: InheritableSerdeParser) -> Result { match parsed { InheritableSerdeParser::Set(v) => Ok(Self::Set(v)), InheritableSerdeParser::Inherited { workspace: true } => Ok(Self::Inherited), InheritableSerdeParser::Inherited { workspace: false } => Err("inherited field with `workspace = false` is not allowed".into()), InheritableSerdeParser::ParseErrorFallback(s) => Err(format!("Error parsing field content. Expected to deserialize {}, found {s}", std::any::type_name::())), } } } fn workspace_true(serializer: S) -> Result { #[derive(Serialize)] struct Inherited { workspace: bool, } Inherited { workspace: true }.serialize(serializer) } #[derive(Deserialize)] #[serde(untagged)] pub enum InheritableSerdeParser { Set(T), Inherited { /// Always `true` (this is for serde) workspace: bool, }, ParseErrorFallback(toml::Value), } impl PartialEq for Inheritable { fn eq(&self, other: &Self) -> bool { match (self, other) { (Self::Set(a), Self::Set(b)) => a.eq(b), _ => false, } } } impl Inheritable { pub fn as_ref(&self) -> Inheritable<&T> { match self { Self::Set(t) => Inheritable::Set(t), Self::Inherited => Inheritable::Inherited, } } /// You can read the value pub fn is_set(&self) -> bool { matches!(self, Self::Set(_)) } pub fn get(&self) -> Result<&T, Error> { match self { Self::Set(t) => Ok(t), Self::Inherited => Err(Error::InheritedUnknownValue), } } pub fn set(&mut self, val: T) { *self = Self::Set(val); } pub fn as_mut(&mut self) -> Inheritable<&mut T> { match self { Self::Set(t) => Inheritable::Set(t), Self::Inherited => Inheritable::Inherited, } } /// Fails if inherited pub fn get_mut(&mut self) -> Result<&mut T, Error> { match self { Self::Set(t) => Ok(t), Self::Inherited => Err(Error::InheritedUnknownValue), } } /// Panics if inherited #[track_caller] pub fn unwrap(self) -> T { match self { Self::Set(t) => t, Self::Inherited => panic!("inherited workspace value"), } } /// Copy from workspace if needed pub fn inherit(&mut self, other: &T) where T: Clone { if matches!(self, Self::Inherited) { *self = Self::Set(other.clone()); } } } impl Default for Inheritable { fn default() -> Self { Self::Set(T::default()) } } impl Inheritable> { /// False if inherited and unknown #[must_use] pub fn is_empty(&self) -> bool { match self { Self::Inherited => false, Self::Set(v) => v.is_empty(), } } } impl Inheritable> { /// False if inherited and unknown #[must_use] pub fn is_empty(&self) -> bool { match self { Self::Inherited => false, Self::Set(v) => v.is_empty(), } } } impl Inheritable { /// False if inherited and unknown pub fn is_default(&self) -> bool { match self { Self::Inherited => false, Self::Set(v) => T::default() == *v, } } } impl From> for Inheritable { /// Inherits if `None` fn from(val: Option) -> Self { match val { Some(val) => Self::Set(val), None => Self::Inherited, } } } impl From> for Option { /// `None` if inherited fn from(val: Inheritable) -> Self { match val { Inheritable::Inherited => None, Inheritable::Set(val) => Some(val), } } } #[test] fn serializes() { #[derive(Serialize)] struct Foo { bar: Inheritable<&'static str>, } let s = toml::to_string(&Foo { bar: Inheritable::Inherited, }).unwrap(); assert_eq!(s, "[bar]\nworkspace = true\n"); let s = toml::to_string(&Foo { bar: Inheritable::Set("hello"), }).unwrap(); assert_eq!(s, "bar = \"hello\"\n"); }