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