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 KeyError {
12 /// The key ends early and does not reach a leaf node.
13 #[error("Key does not reach a leaf")]
14 TooShort,
15
16 /// The key was not found (index parse failure or too large,
17 /// name not found or invalid).
18 #[error("Key not found")]
19 NotFound,
20
21 /// The key is too long and goes beyond a leaf node.
22 #[error("Key goes beyond a leaf")]
23 TooLong,
24}
25
26/// Errors that can occur while visting nodes with [`crate::Schema::descend`].
27#[derive(Debug, Copy, Clone, PartialEq, Eq, thiserror::Error)]
28pub enum DescendError<E> {
29 /// The key is invalid.
30 #[error(transparent)]
31 Key(#[from] KeyError),
32
33 /// The visitor callback returned an error.
34 #[error("Visitor failed")]
35 Inner(#[source] E),
36}
37
38/// Errors that can occur while accessing a value.
39#[derive(Debug, Copy, Clone, PartialEq, Eq, thiserror::Error)]
40pub enum ValueError {
41 /// Tree traversal error
42 #[error(transparent)]
43 Key(#[from] KeyError),
44
45 /// A node does not exist at runtime.
46 ///
47 /// An `enum` variant in the tree towards the node is currently absent.
48 /// This is for example the case if an [`Option`] using the `Tree*`
49 /// traits is `None` at runtime. See also [`crate::TreeSchema#option`].
50 #[error("Variant absent")]
51 Absent,
52
53 /// A node could not be accessed or is invalid.
54 ///
55 /// This is returned from custom implementations.
56 #[error("Access/validation failure: {0}")]
57 Access(&'static str),
58}
59
60/// Compound errors
61#[derive(Debug, Copy, Clone, PartialEq, Eq, thiserror::Error)]
62pub enum SerdeError<E> {
63 /// The value could not be accessed.
64 #[error(transparent)]
65 Value(#[from] ValueError),
66
67 /// The value provided could not be serialized or deserialized
68 /// or the traversal callback returned an error.
69 #[error("(De)serialization")]
70 Inner(#[source] E),
71
72 /// There was an error during finalization.
73 ///
74 /// This is not to be returned by a TreeSerialize/TreeDeserialize
75 /// implementation but only from a wrapper that creates and finalizes the
76 /// the serializer/deserializer.
77 ///
78 /// The `Deserializer` has encountered an error only after successfully
79 /// deserializing a value. This is the case if there is additional unexpected data.
80 /// The `deserialize_by_key()` update takes place but this
81 /// error will be returned.
82 ///
83 /// A `Serializer` may write checksums or additional framing data and fail with
84 /// this error during finalization after the value has been serialized.
85 #[error("(De)serializer finalization")]
86 Finalization(#[source] E),
87}
88
89impl<E> From<KeyError> for SerdeError<E> {
90 #[inline]
91 fn from(value: KeyError) -> Self {
92 SerdeError::Value(value.into())
93 }
94}
95
96// Try to extract the Traversal from an Error
97impl<E> TryFrom<SerdeError<E>> for KeyError {
98 type Error = SerdeError<E>;
99 #[inline]
100 fn try_from(value: SerdeError<E>) -> Result<Self, Self::Error> {
101 match value {
102 SerdeError::Value(ValueError::Key(e)) => Ok(e),
103 e => Err(e),
104 }
105 }
106}
107
108// Try to extract the Traversal from an Error
109impl TryFrom<ValueError> for KeyError {
110 type Error = ValueError;
111 #[inline]
112 fn try_from(value: ValueError) -> Result<Self, Self::Error> {
113 match value {
114 ValueError::Key(e) => Ok(e),
115 e => Err(e),
116 }
117 }
118}
119
120// Try to extract the Traversal from an Error
121impl<E> TryFrom<DescendError<E>> for KeyError {
122 type Error = E;
123 #[inline]
124 fn try_from(value: DescendError<E>) -> Result<Self, Self::Error> {
125 match value {
126 DescendError::Key(e) => Ok(e),
127 DescendError::Inner(e) => Err(e),
128 }
129 }
130}
131
132// Try to extract the Traversal from an Error
133impl<E> TryFrom<SerdeError<E>> for ValueError {
134 type Error = E;
135 #[inline]
136 fn try_from(value: SerdeError<E>) -> Result<Self, Self::Error> {
137 match value {
138 SerdeError::Value(e) => Ok(e),
139 SerdeError::Finalization(e) | SerdeError::Inner(e) => Err(e),
140 }
141 }
142}