Skip to main content

jiff/error/tz/
zic.rs

1#[cfg(any(not(test), not(feature = "alloc")))]
2pub(crate) use self::disabled::*;
3#[cfg(all(test, feature = "alloc"))]
4pub(crate) use self::enabled::*;
5
6#[cfg(any(not(test), not(feature = "alloc")))]
7mod disabled {
8    #[derive(#[automatically_derived]impl ::core::clone::Clone for Error {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { match *self {} }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for Error {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match *self {}
    }
}Debug)]
9    #[cfg_attr(feature = "defmt", derive(defmt::Format))]
10    pub(crate) enum Error {}
11
12    impl core::fmt::Display for Error {
13        fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
14            ::core::panicking::unreachable_default_msg()unreachable!()
15        }
16    }
17}
18
19#[cfg(all(test, feature = "alloc"))]
20mod enabled {
21    use alloc::boxed::Box;
22
23    use crate::error;
24
25    // `man zic` says that the max line length including the line
26    // terminator is 2048. The `core::str::Lines` iterator doesn't include
27    // the terminator, so we subtract 1 to account for that. Note that this
28    // could potentially allow one extra byte in the case of a \r\n line
29    // terminator, but this seems fine.
30    pub(crate) const MAX_LINE_LEN: usize = 2047;
31
32    #[derive(Clone, Debug)]
33    pub(crate) enum Error {
34        DuplicateLink { name: Box<str> },
35        DuplicateLinkZone { name: Box<str> },
36        DuplicateZone { name: Box<str> },
37        DuplicateZoneLink { name: Box<str> },
38        ExpectedCloseQuote,
39        ExpectedColonAfterHour,
40        ExpectedColonAfterMinute,
41        ExpectedContinuationZoneThreeFields,
42        ExpectedContinuationZoneLine { name: Box<str> },
43        ExpectedDotAfterSeconds,
44        ExpectedFirstZoneFourFields,
45        ExpectedLinkTwoFields,
46        ExpectedMinuteAfterHours,
47        ExpectedNameBegin,
48        ExpectedNanosecondDigits,
49        ExpectedNonEmptyAbbreviation,
50        ExpectedNonEmptyAt,
51        ExpectedNonEmptyName,
52        ExpectedNonEmptySave,
53        ExpectedNonEmptyZoneName,
54        ExpectedNothingAfterTime,
55        ExpectedRuleNineFields { got: usize },
56        ExpectedSecondAfterMinutes,
57        ExpectedTimeOneHour,
58        ExpectedUntilYear,
59        ExpectedWhitespaceAfterQuotedField,
60        ExpectedZoneNameComponentNoDots { component: Box<str> },
61        FailedContinuationZone,
62        FailedLinkLine,
63        FailedParseDay,
64        FailedParseFieldAt,
65        FailedParseFieldFormat,
66        FailedParseFieldFrom,
67        FailedParseFieldIn,
68        FailedParseFieldLetters,
69        FailedParseFieldLinkName,
70        FailedParseFieldLinkTarget,
71        FailedParseFieldName,
72        FailedParseFieldOn,
73        FailedParseFieldRules,
74        FailedParseFieldSave,
75        FailedParseFieldStdOff,
76        FailedParseFieldTo,
77        FailedParseFieldUntil,
78        FailedParseHour,
79        FailedParseMinute,
80        FailedParseMonth,
81        FailedParseNanosecond,
82        FailedParseSecond,
83        FailedParseTimeDuration,
84        FailedParseYear,
85        FailedRule { name: Box<str> },
86        FailedRuleLine,
87        FailedZoneFirst,
88        Line { number: usize },
89        LineMaxLength,
90        LineNul,
91        LineOverflow,
92        InvalidAbbreviation,
93        InvalidRuleYear { start: i16, end: i16 },
94        InvalidUtf8,
95        UnrecognizedAtTimeSuffix,
96        UnrecognizedDayOfMonthFormat,
97        UnrecognizedDayOfWeek,
98        UnrecognizedMonthName,
99        UnrecognizedSaveTimeSuffix,
100        UnrecognizedTrailingTimeDuration,
101        UnrecognizedZicLine,
102    }
103
104    // Deriving `defmt::Format` would require `defmt/alloc` (for `Box<str>`),
105    // and this is only used in tests, so just defer to `Debug`.
106    #[cfg(feature = "defmt")]
107    impl defmt::Format for Error {
108        fn format(&self, f: defmt::Formatter) {
109            defmt::write!(f, "{}", defmt::Debug2Format(self))
110        }
111    }
112
113    impl From<Error> for error::Error {
114        #[cold]
115        #[inline(never)]
116        fn from(err: Error) -> error::Error {
117            error::ErrorKind::TzZic(err).into()
118        }
119    }
120
121    impl error::IntoError for Error {
122        fn into_error(self) -> error::Error {
123            self.into()
124        }
125    }
126
127    impl core::fmt::Display for Error {
128        fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
129            use self::Error::*;
130
131            match *self {
132                DuplicateLink { ref name } => {
133                    write!(f, "found duplicate link with name `{name}`")
134                }
135                DuplicateLinkZone { ref name } => write!(
136                    f,
137                    "found link with name `{name}` that conflicts \
138                     with a zone of the same name",
139                ),
140                DuplicateZone { ref name } => {
141                    write!(f, "found duplicate zone with name `{name}`")
142                }
143                DuplicateZoneLink { ref name } => write!(
144                    f,
145                    "found zone with name `{name}` that conflicts \
146                     with a link of the same name",
147                ),
148                ExpectedCloseQuote => {
149                    f.write_str("found unclosed quote for field")
150                }
151                ExpectedColonAfterHour => {
152                    f.write_str("expected `:` after hours")
153                }
154                ExpectedColonAfterMinute => {
155                    f.write_str("expected `:` after minutes")
156                }
157                ExpectedContinuationZoneLine { ref name } => write!(
158                    f,
159                    "expected continuation zone line for `{name}`, \
160                     but found end of data instead",
161                ),
162                ExpectedContinuationZoneThreeFields => f.write_str(
163                    "expected continuation `ZONE` line \
164                     to have at least 3 fields",
165                ),
166                ExpectedDotAfterSeconds => {
167                    f.write_str("expected `.` after seconds")
168                }
169                ExpectedFirstZoneFourFields => f.write_str(
170                    "expected first `ZONE` line to have at least 4 fields",
171                ),
172                ExpectedLinkTwoFields => {
173                    f.write_str("expected exactly 2 fields after `LINK`")
174                }
175                ExpectedMinuteAfterHours => {
176                    f.write_str("expected minute digits after `HH:`")
177                }
178                ExpectedNameBegin => f.write_str(
179                    "`NAME` field cannot begin with a digit, `+` or `-`, \
180                     but found `NAME` that begins with one of those",
181                ),
182                ExpectedNanosecondDigits => {
183                    f.write_str("expected nanosecond digits after `HH:MM:SS.`")
184                }
185                ExpectedNonEmptyAbbreviation => f.write_str(
186                    "empty time zone abbreviations are not allowed",
187                ),
188                ExpectedNonEmptyAt => {
189                    f.write_str("`AT` field for rule cannot be empty")
190                }
191                ExpectedNonEmptyName => {
192                    f.write_str("`NAME` field for rule cannot be empty")
193                }
194                ExpectedNonEmptySave => {
195                    f.write_str("`SAVE` field for rule cannot be empty")
196                }
197                ExpectedNonEmptyZoneName => {
198                    f.write_str("zone names cannot be empty")
199                }
200                ExpectedNothingAfterTime => f.write_str(
201                    "expected no more fields after time of day, \
202                     but found at least one",
203                ),
204                ExpectedRuleNineFields { got } => write!(
205                    f,
206                    "expected exactly 9 fields for rule, \
207                     but found {got} fields",
208                ),
209                ExpectedSecondAfterMinutes => {
210                    f.write_str("expected second digits after `HH:MM:`")
211                }
212                ExpectedTimeOneHour => f.write_str(
213                    "expected time duration to contain \
214                     at least one hour digit",
215                ),
216                ExpectedUntilYear => f.write_str("expected at least a year"),
217                ExpectedWhitespaceAfterQuotedField => {
218                    f.write_str("expected whitespace after quoted field")
219                }
220                ExpectedZoneNameComponentNoDots { ref component } => write!(
221                    f,
222                    "component `{component}` in zone name cannot \
223                     be \".\" or \"..\"",
224                ),
225                FailedContinuationZone => {
226                    f.write_str("failed to parse continuation `Zone` line")
227                }
228                FailedLinkLine => f.write_str("failed to parse `Link` line"),
229                FailedParseDay => f.write_str("failed to parse day"),
230                FailedParseFieldAt => {
231                    f.write_str("failed to parse `AT` field")
232                }
233                FailedParseFieldFormat => {
234                    f.write_str("failed to parse `FORMAT` field")
235                }
236                FailedParseFieldFrom => {
237                    f.write_str("failed to parse `FROM` field")
238                }
239                FailedParseFieldIn => {
240                    f.write_str("failed to parse `IN` field")
241                }
242                FailedParseFieldLetters => {
243                    f.write_str("failed to parse `LETTERS` field")
244                }
245                FailedParseFieldLinkName => {
246                    f.write_str("failed to parse `LINK` name field")
247                }
248                FailedParseFieldLinkTarget => {
249                    f.write_str("failed to parse `LINK` target field")
250                }
251                FailedParseFieldName => {
252                    f.write_str("failed to parse `NAME` field")
253                }
254                FailedParseFieldOn => {
255                    f.write_str("failed to parse `ON` field")
256                }
257                FailedParseFieldRules => {
258                    f.write_str("failed to parse `RULES` field")
259                }
260                FailedParseFieldSave => {
261                    f.write_str("failed to parse `SAVE` field")
262                }
263                FailedParseFieldStdOff => {
264                    f.write_str("failed to parse `STDOFF` field")
265                }
266                FailedParseFieldTo => {
267                    f.write_str("failed to parse `TO` field")
268                }
269                FailedParseFieldUntil => {
270                    f.write_str("failed to parse `UNTIL` field")
271                }
272                FailedParseHour => f.write_str("failed to parse hour"),
273                FailedParseMinute => f.write_str("failed to parse minute"),
274                FailedParseMonth => f.write_str("failed to parse month"),
275                FailedParseNanosecond => {
276                    f.write_str("failed to parse nanosecond")
277                }
278                FailedParseSecond => f.write_str("failed to parse second"),
279                FailedParseTimeDuration => {
280                    f.write_str("failed to parse time duration")
281                }
282                FailedParseYear => f.write_str("failed to parse year"),
283                FailedRule { name: ref rule } => {
284                    write!(f, "failed to parse rule `{rule}`")
285                }
286                FailedRuleLine => f.write_str("failed to parse `Rule` line"),
287                FailedZoneFirst => {
288                    f.write_str("failed to parse first `Zone` line")
289                }
290                InvalidAbbreviation => f.write_str(
291                    "time zone abbreviation \
292                     contains invalid character; only \"+\", \"-\" and \
293                     ASCII alpha-numeric characters are allowed",
294                ),
295                InvalidRuleYear { start, end } => write!(
296                    f,
297                    "found start year={start} \
298                     to be greater than end year={end}"
299                ),
300                InvalidUtf8 => f.write_str("invalid UTF-8"),
301                Line { number } => write!(f, "line {number}"),
302                LineMaxLength => write!(
303                    f,
304                    "found line with length that exceeds \
305                     max length of {MAX_LINE_LEN}",
306                ),
307                LineNul => f.write_str(
308                    "found line with NUL byte, which isn't allowed",
309                ),
310                LineOverflow => f.write_str("line count overflowed"),
311                UnrecognizedAtTimeSuffix => {
312                    f.write_str("unrecognized `AT` time suffix")
313                }
314                UnrecognizedDayOfMonthFormat => {
315                    f.write_str("unrecognized format for day-of-month")
316                }
317                UnrecognizedDayOfWeek => {
318                    f.write_str("unrecognized day of the week")
319                }
320                UnrecognizedMonthName => {
321                    f.write_str("unrecognized month name")
322                }
323                UnrecognizedSaveTimeSuffix => {
324                    f.write_str("unrecognized `SAVE` time suffix")
325                }
326                UnrecognizedTrailingTimeDuration => {
327                    f.write_str("found unrecognized suffix in time duration")
328                }
329                UnrecognizedZicLine => f.write_str("unrecognized zic line"),
330            }
331        }
332    }
333}