]> git.proxmox.com Git - rustc.git/blob - vendor/time/src/error/invalid_format_description.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / vendor / time / src / error / invalid_format_description.rs
1 //! Invalid format description
2
3 use alloc::string::String;
4 use core::fmt;
5
6 use crate::error;
7
8 /// The format description provided was not valid.
9 #[non_exhaustive]
10 #[derive(Debug, Clone, PartialEq, Eq)]
11 pub 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 }
41
42 impl From<InvalidFormatDescription> for crate::Error {
43 fn from(original: InvalidFormatDescription) -> Self {
44 Self::InvalidFormatDescription(original)
45 }
46 }
47
48 impl TryFrom<crate::Error> for InvalidFormatDescription {
49 type Error = error::DifferentVariant;
50
51 fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
52 match err {
53 crate::Error::InvalidFormatDescription(err) => Ok(err),
54 _ => Err(error::DifferentVariant),
55 }
56 }
57 }
58
59 impl fmt::Display for InvalidFormatDescription {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 use InvalidFormatDescription::*;
62 match self {
63 UnclosedOpeningBracket { index } => {
64 write!(f, "unclosed opening bracket at byte index {index}")
65 }
66 InvalidComponentName { name, index } => {
67 write!(f, "invalid component name `{name}` at byte index {index}")
68 }
69 InvalidModifier { value, index } => {
70 write!(f, "invalid modifier `{value}` at byte index {index}")
71 }
72 MissingComponentName { index } => {
73 write!(f, "missing component name at byte index {index}")
74 }
75 }
76 }
77 }
78
79 #[cfg(feature = "std")]
80 impl std::error::Error for InvalidFormatDescription {}