jiff/error/fmt/
rfc9557.rs1use crate::{error, util::escape};
2
3#[derive(Clone, Debug)]
4pub(crate) enum Error {
5 EndOfInputAnnotation,
6 EndOfInputAnnotationClose,
7 EndOfInputAnnotationKey,
8 EndOfInputAnnotationSeparator,
9 EndOfInputAnnotationValue,
10 EndOfInputTzAnnotationClose,
11 UnexpectedByteAnnotation { byte: u8 },
12 UnexpectedByteAnnotationClose { byte: u8 },
13 UnexpectedByteAnnotationKey { byte: u8 },
14 UnexpectedByteAnnotationValue { byte: u8 },
15 UnexpectedByteAnnotationSeparator { byte: u8 },
16 UnexpectedByteTzAnnotationClose { byte: u8 },
17 UnexpectedSlashAnnotationSeparator,
18 UnsupportedAnnotationCritical,
19}
20
21impl error::IntoError for Error {
22 fn into_error(self) -> error::Error {
23 self.into()
24 }
25}
26
27impl From<Error> for error::Error {
28 #[cold]
29 #[inline(never)]
30 fn from(err: Error) -> error::Error {
31 error::ErrorKind::FmtRfc9557(err).into()
32 }
33}
34
35impl core::fmt::Display for Error {
36 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
37 use self::Error::*;
38
39 match *self {
40 EndOfInputAnnotation => f.write_str(
41 "expected the start of an RFC 9557 annotation or IANA \
42 time zone component name, but found end of input instead",
43 ),
44 EndOfInputAnnotationClose => f.write_str(
45 "expected an `]` after parsing an RFC 9557 annotation key \
46 and value, but found end of input instead",
47 ),
48 EndOfInputAnnotationKey => f.write_str(
49 "expected the start of an RFC 9557 annotation key, \
50 but found end of input instead",
51 ),
52 EndOfInputAnnotationSeparator => f.write_str(
53 "expected an `=` after parsing an RFC 9557 annotation key, \
54 but found end of input instead",
55 ),
56 EndOfInputAnnotationValue => f.write_str(
57 "expected the start of an RFC 9557 annotation value, \
58 but found end of input instead",
59 ),
60 EndOfInputTzAnnotationClose => f.write_str(
61 "expected an `]` after parsing an RFC 9557 time zone \
62 annotation, but found end of input instead",
63 ),
64 UnexpectedByteAnnotation { byte } => write!(
65 f,
66 "expected ASCII alphabetic byte (or underscore or period) \
67 at the start of an RFC 9557 annotation or time zone \
68 component name, but found `{byte}` instead",
69 byte = escape::Byte(byte),
70 ),
71 UnexpectedByteAnnotationClose { byte } => write!(
72 f,
73 "expected an `]` after parsing an RFC 9557 annotation key \
74 and value, but found `{byte}` instead",
75 byte = escape::Byte(byte),
76 ),
77 UnexpectedByteAnnotationKey { byte } => write!(
78 f,
79 "expected lowercase alphabetic byte (or underscore) \
80 at the start of an RFC 9557 annotation key, \
81 but found `{byte}` instead",
82 byte = escape::Byte(byte),
83 ),
84 UnexpectedByteAnnotationValue { byte } => write!(
85 f,
86 "expected alphanumeric ASCII byte \
87 at the start of an RFC 9557 annotation value, \
88 but found `{byte}` instead",
89 byte = escape::Byte(byte),
90 ),
91 UnexpectedByteAnnotationSeparator { byte } => write!(
92 f,
93 "expected an `=` after parsing an RFC 9557 annotation \
94 key, but found `{byte}` instead",
95 byte = escape::Byte(byte),
96 ),
97 UnexpectedByteTzAnnotationClose { byte } => write!(
98 f,
99 "expected an `]` after parsing an RFC 9557 time zone \
100 annotation, but found `{byte}` instead",
101 byte = escape::Byte(byte),
102 ),
103 UnexpectedSlashAnnotationSeparator => f.write_str(
104 "expected an `=` after parsing an RFC 9557 annotation \
105 key, but found `/` instead (time zone annotations must \
106 come first)",
107 ),
108 UnsupportedAnnotationCritical => f.write_str(
109 "found unsupported RFC 9557 annotation \
110 with the critical flag (`!`) set",
111 ),
112 }
113 }
114}