cargo_toml/
inheritable.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use crate::Error;
use serde::{Deserialize, Serialize};

/// Placeholder for a property that may be missing from its package, and needs to be copied from a `Workspace`.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Inheritable<T> {
    Set(T),
    Inherited {
        /// Always `true` (this is for serde)
        workspace: bool,
    },
}

impl<T> Inheritable<T> {
    pub fn as_ref(&self) -> Inheritable<&T> {
        match self {
            Self::Set(t) => Inheritable::Set(t),
            Self::Inherited { .. } => Inheritable::Inherited { workspace: true },
        }
    }

    /// 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 { workspace: true },
        }
    }

    /// 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 let Self::Inherited{..} = self {
            *self = Self::Set(other.clone());
        }
    }
}

impl<T: Default> Default for Inheritable<T> {
    fn default() -> Self {
        Self::Set(T::default())
    }
}

impl<T> Inheritable<Vec<T>> {
    /// 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<T: Default + PartialEq> Inheritable<T> {
    /// False if inherited and unknown
    pub fn is_default(&self) -> bool {
        match self {
            Self::Inherited { .. } => false,
            Self::Set(v) => T::default() == *v,
        }
    }
}

impl<T> From<Option<T>> for Inheritable<T> {
    /// Inherits if `None`
    fn from(val: Option<T>) -> Self {
        match val {
            Some(val) => Self::Set(val),
            None => Self::Inherited { workspace: true },
        }
    }
}

impl<T> From<Inheritable<T>> for Option<T> {
    /// `None` if inherited
    fn from(val: Inheritable<T>) -> Self {
        match val {
            Inheritable::Inherited { .. } => None,
            Inheritable::Set(val) => Some(val),
        }
    }
}