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}
75
76impl From<InvalidFormatDescription> for crate::Error {
77 #[inline]
78 fn from(original: InvalidFormatDescription) -> Self {
79 Self::InvalidFormatDescription(original)
80 }
81}
82
83impl TryFrom<crate::Error> for InvalidFormatDescription {
84 type Error = error::DifferentVariant;
85
86 #[inline]
87 fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
88 match err {
89 crate::Error::InvalidFormatDescription(err) => Ok(err),
90 _ => Err(error::DifferentVariant),
91 }
92 }
93}
94
95impl fmt::Display for InvalidFormatDescription {
96 #[inline]
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 use InvalidFormatDescription::*;
99 match self {
100 UnclosedOpeningBracket { index } => {
101 write!(f, "unclosed opening bracket at byte index {index}")
102 }
103 InvalidComponentName { name, index } => {
104 write!(f, "invalid component name `{name}` at byte index {index}")
105 }
106 InvalidModifier { value, index } => {
107 write!(f, "invalid modifier `{value}` at byte index {index}")
108 }
109 MissingComponentName { index } => {
110 write!(f, "missing component name at byte index {index}")
111 }
112 MissingRequiredModifier { name, index } => {
113 write!(
114 f,
115 "missing required modifier `{name}` for component at byte index {index}"
116 )
117 }
118 Expected {
119 what: expected,
120 index,
121 } => {
122 write!(f, "expected {expected} at byte index {index}")
123 }
124 NotSupported {
125 what,
126 context,
127 index,
128 } => {
129 if context.is_empty() {
130 write!(f, "{what} is not supported at byte index {index}")
131 } else {
132 write!(
133 f,
134 "{what} is not supported in {context} at byte index {index}"
135 )
136 }
137 }
138 DuplicateModifier { name, index } => {
139 write!(f, "duplicate modifier `{name}` at byte index {index}")
140 }
141 }
142 }
143}
144
145impl core::error::Error for InvalidFormatDescription {}