time/error/
invalid_format_description.rs1use alloc::string::String;
4use core::fmt;
5
6use crate::error;
7
8#[non_exhaustive]
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum InvalidFormatDescription {
12 #[non_exhaustive]
14 UnclosedOpeningBracket {
15 index: usize,
17 },
18 #[non_exhaustive]
20 InvalidComponentName {
21 name: String,
23 index: usize,
25 },
26 #[non_exhaustive]
28 InvalidModifier {
29 value: String,
31 index: usize,
33 },
34 #[non_exhaustive]
36 MissingComponentName {
37 index: usize,
39 },
40 #[non_exhaustive]
42 MissingRequiredModifier {
43 name: &'static str,
45 index: usize,
47 },
48 #[non_exhaustive]
50 Expected {
51 what: &'static str,
53 index: usize,
55 },
56 #[non_exhaustive]
58 NotSupported {
59 what: &'static str,
61 context: &'static str,
63 index: usize,
65 },
66 #[non_exhaustive]
68 DuplicateModifier {
69 name: &'static str,
71 index: usize,
73 },
74 #[non_exhaustive]
76 InvalidModifierCombination {
77 modifier: &'static str,
79 context: &'static str,
81 index: usize,
83 },
84}
85
86impl From<InvalidFormatDescription> for crate::Error {
87 #[inline]
88 fn from(original: InvalidFormatDescription) -> Self {
89 Self::InvalidFormatDescription(original)
90 }
91}
92
93impl TryFrom<crate::Error> for InvalidFormatDescription {
94 type Error = error::DifferentVariant;
95
96 #[inline]
97 fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
98 match err {
99 crate::Error::InvalidFormatDescription(err) => Ok(err),
100 _ => Err(error::DifferentVariant),
101 }
102 }
103}
104
105impl fmt::Display for InvalidFormatDescription {
106 #[inline]
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108 use InvalidFormatDescription::*;
109 match self {
110 UnclosedOpeningBracket { index } => {
111 write!(f, "unclosed opening bracket at byte index {index}")
112 }
113 InvalidComponentName { name, index } => {
114 write!(f, "invalid component name `{name}` at byte index {index}")
115 }
116 InvalidModifier { value, index } => {
117 write!(f, "invalid modifier `{value}` at byte index {index}")
118 }
119 MissingComponentName { index } => {
120 write!(f, "missing component name at byte index {index}")
121 }
122 MissingRequiredModifier { name, index } => {
123 write!(
124 f,
125 "missing required modifier `{name}` for component at byte index {index}"
126 )
127 }
128 Expected {
129 what: expected,
130 index,
131 } => {
132 write!(f, "expected {expected} at byte index {index}")
133 }
134 NotSupported {
135 what,
136 context,
137 index,
138 } => {
139 if context.is_empty() {
140 write!(f, "{what} is not supported at byte index {index}")
141 } else {
142 write!(
143 f,
144 "{what} is not supported in {context} at byte index {index}"
145 )
146 }
147 }
148 DuplicateModifier { name, index } => {
149 write!(f, "duplicate modifier `{name}` at byte index {index}")
150 }
151 InvalidModifierCombination {
152 modifier,
153 context,
154 index,
155 } => write!(
156 f,
157 "the `{modifier}` modifier is not valid at byte index {index} {context}"
158 ),
159 }
160 }
161}
162
163impl core::error::Error for InvalidFormatDescription {}