miniconf/
error.rs

1/// Errors that can occur when using the Tree traits.
2///
3/// A `usize` member indicates the key depth where the error occurred.
4/// The depth here is the number of names or indices consumed.
5/// It is also the number of separators in a path or the length
6/// of an indices slice.
7///
8/// If multiple errors are applicable simultaneously the precedence
9/// is as per the order in the enum definition (from high to low).
10#[derive(Debug, Copy, Clone, PartialEq, Eq, thiserror::Error)]
11pub enum Traversal {
12    /// A node does not exist at runtime.
13    ///
14    /// An `enum` variant in the tree towards the node is currently absent.
15    /// This is for example the case if an [`Option`] using the `Tree*`
16    /// traits is `None` at runtime. See also [`crate::TreeKey#option`].
17    #[error("Variant absent (depth: {0})")]
18    Absent(usize),
19
20    /// The key ends early and does not reach a leaf node.
21    #[error("Key does not reach a leaf (depth: {0})")]
22    TooShort(usize),
23
24    /// The key was not found (index parse failure or too large,
25    /// name not found or invalid).
26    #[error("Key not found (depth: {0})")]
27    NotFound(usize),
28
29    /// The key is too long and goes beyond a leaf node.
30    #[error("Key goes beyond leaf (depth: {0})")]
31    TooLong(usize),
32
33    /// A node could not be accessed or is invalid.
34    ///
35    /// This is returned from custom implementations.
36    #[error("Access/validation failure (depth: {0}): {1}")]
37    Access(usize, &'static str),
38}
39
40impl Traversal {
41    /// Pass it up one hierarchy depth level, incrementing its usize depth field by one.
42    pub fn increment(self) -> Self {
43        match self {
44            Self::Absent(i) => Self::Absent(i + 1),
45            Self::TooShort(i) => Self::TooShort(i + 1),
46            Self::NotFound(i) => Self::NotFound(i + 1),
47            Self::TooLong(i) => Self::TooLong(i + 1),
48            Self::Access(i, msg) => Self::Access(i + 1, msg),
49        }
50    }
51
52    /// Return the traversal depth
53    #[inline]
54    pub fn depth(&self) -> usize {
55        match self {
56            Self::Absent(i)
57            | Self::TooShort(i)
58            | Self::NotFound(i)
59            | Self::TooLong(i)
60            | Self::Access(i, _) => *i,
61        }
62    }
63}
64
65/// Compound errors
66#[derive(Debug, Copy, Clone, PartialEq, Eq, thiserror::Error)]
67pub enum Error<E> {
68    /// Tree traversal error
69    #[error(transparent)]
70    Traversal(#[from] Traversal),
71
72    /// The value provided could not be serialized or deserialized
73    /// or the traversal callback returned an error.
74    #[error("(De)serialization (depth: {0}): {1}")]
75    Inner(usize, #[source] E),
76
77    /// There was an error during finalization.
78    ///
79    /// This is not to be returned by a TreeSerialize/TreeDeserialize
80    /// implementation but only from a wrapper that creates and finalizes the
81    /// the serializer/deserializer.
82    ///
83    /// The `Deserializer` has encountered an error only after successfully
84    /// deserializing a value. This is the case if there is additional unexpected data.
85    /// The `deserialize_by_key()` update takes place but this
86    /// error will be returned.
87    ///
88    /// A `Serializer` may write checksums or additional framing data and fail with
89    /// this error during finalization after the value has been serialized.
90    #[error("(De)serializer finalization: {0}")]
91    Finalization(#[source] E),
92}
93
94// Try to extract the Traversal from an Error
95impl<E> TryFrom<Error<E>> for Traversal {
96    type Error = Error<E>;
97    #[inline]
98    fn try_from(value: Error<E>) -> Result<Self, Self::Error> {
99        match value {
100            Error::Traversal(e) => Ok(e),
101            e => Err(e),
102        }
103    }
104}
105
106impl<E> Error<E> {
107    /// Pass an `Error<E>` up one hierarchy depth level, incrementing its usize depth field by one.
108    #[inline]
109    pub fn increment(self) -> Self {
110        match self {
111            Self::Traversal(t) => Self::Traversal(t.increment()),
112            Self::Inner(i, e) => Self::Inner(i + 1, e),
113            Self::Finalization(e) => Self::Finalization(e),
114        }
115    }
116}