time/error/
invalid_format_description.rs

1//! Invalid format description
2
3use alloc::string::String;
4use core::fmt;
5
6use crate::error;
7
8/// The format description provided was not valid.
9#[non_exhaustive]
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum InvalidFormatDescription {
12    /// There was a bracket pair that was opened but not closed.
13    #[non_exhaustive]
14    UnclosedOpeningBracket {
15        /// The zero-based index of the opening bracket.
16        index: usize,
17    },
18    /// A component name is not valid.
19    #[non_exhaustive]
20    InvalidComponentName {
21        /// The name of the invalid component name.
22        name: String,
23        /// The zero-based index the component name starts at.
24        index: usize,
25    },
26    /// A modifier is not valid.
27    #[non_exhaustive]
28    InvalidModifier {
29        /// The value of the invalid modifier.
30        value: String,
31        /// The zero-based index the modifier starts at.
32        index: usize,
33    },
34    /// A component name is missing.
35    #[non_exhaustive]
36    MissingComponentName {
37        /// The zero-based index where the component name should start.
38        index: usize,
39    },
40    /// A required modifier is missing.
41    #[non_exhaustive]
42    MissingRequiredModifier {
43        /// The name of the modifier that is missing.
44        name: &'static str,
45        /// The zero-based index of the component.
46        index: usize,
47    },
48    /// Something was expected, but not found.
49    #[non_exhaustive]
50    Expected {
51        /// What was expected to be present, but wasn't.
52        what: &'static str,
53        /// The zero-based index the item was expected to be found at.
54        index: usize,
55    },
56    /// Certain behavior is not supported in the given context.
57    #[non_exhaustive]
58    NotSupported {
59        /// The behavior that is not supported.
60        what: &'static str,
61        /// The context in which the behavior is not supported.
62        context: &'static str,
63        /// The zero-based index the error occurred at.
64        index: usize,
65    },
66}
67
68impl From<InvalidFormatDescription> for crate::Error {
69    #[inline]
70    fn from(original: InvalidFormatDescription) -> Self {
71        Self::InvalidFormatDescription(original)
72    }
73}
74
75impl TryFrom<crate::Error> for InvalidFormatDescription {
76    type Error = error::DifferentVariant;
77
78    #[inline]
79    fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
80        match err {
81            crate::Error::InvalidFormatDescription(err) => Ok(err),
82            _ => Err(error::DifferentVariant),
83        }
84    }
85}
86
87impl fmt::Display for InvalidFormatDescription {
88    #[inline]
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        use InvalidFormatDescription::*;
91        match self {
92            UnclosedOpeningBracket { index } => {
93                write!(f, "unclosed opening bracket at byte index {index}")
94            }
95            InvalidComponentName { name, index } => {
96                write!(f, "invalid component name `{name}` at byte index {index}")
97            }
98            InvalidModifier { value, index } => {
99                write!(f, "invalid modifier `{value}` at byte index {index}")
100            }
101            MissingComponentName { index } => {
102                write!(f, "missing component name at byte index {index}")
103            }
104            MissingRequiredModifier { name, index } => {
105                write!(
106                    f,
107                    "missing required modifier `{name}` for component at byte index {index}"
108                )
109            }
110            Expected {
111                what: expected,
112                index,
113            } => {
114                write!(f, "expected {expected} at byte index {index}")
115            }
116            NotSupported {
117                what,
118                context,
119                index,
120            } => {
121                if context.is_empty() {
122                    write!(f, "{what} is not supported at byte index {index}")
123                } else {
124                    write!(
125                        f,
126                        "{what} is not supported in {context} at byte index {index}"
127                    )
128                }
129            }
130        }
131    }
132}
133
134impl core::error::Error for InvalidFormatDescription {}