jiff/error/fmt/
offset.rs

1use crate::{error, util::escape};
2
3#[derive(Clone, Debug)]
4pub(crate) enum Error {
5    ColonAfterHours,
6    EndOfInput,
7    EndOfInputHour,
8    EndOfInputMinute,
9    EndOfInputNumeric,
10    EndOfInputSecond,
11    InvalidHours,
12    InvalidMinutes,
13    InvalidSeconds,
14    InvalidSecondsFractional,
15    InvalidSign,
16    InvalidSignPlusOrMinus,
17    MissingMinuteAfterHour,
18    MissingSecondAfterMinute,
19    NoColonAfterHours,
20    ParseHours,
21    ParseMinutes,
22    ParseSeconds,
23    PrecisionLoss,
24    SeparatorAfterHours,
25    SeparatorAfterMinutes,
26    SubminutePrecisionNotEnabled,
27    SubsecondPrecisionNotEnabled,
28    UnexpectedLetterOffsetNoZulu(u8),
29}
30
31impl error::IntoError for Error {
32    fn into_error(self) -> error::Error {
33        self.into()
34    }
35}
36
37impl From<Error> for error::Error {
38    #[cold]
39    #[inline(never)]
40    fn from(err: Error) -> error::Error {
41        error::ErrorKind::FmtOffset(err).into()
42    }
43}
44
45impl core::fmt::Display for Error {
46    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
47        use self::Error::*;
48
49        match *self {
50            ColonAfterHours => f.write_str(
51                "parsed hour component of time zone offset, \
52                 but found colon after hours which is not allowed",
53            ),
54            EndOfInput => {
55                f.write_str("expected UTC offset, but found end of input")
56            }
57            EndOfInputHour => f.write_str(
58                "expected two digit hour after sign, but found end of input",
59            ),
60            EndOfInputMinute => f.write_str(
61                "expected two digit minute after hours, \
62                 but found end of input",
63            ),
64            EndOfInputNumeric => f.write_str(
65                "expected UTC numeric offset, but found end of input",
66            ),
67            EndOfInputSecond => f.write_str(
68                "expected two digit second after minutes, \
69                 but found end of input",
70            ),
71            InvalidHours => {
72                f.write_str("failed to parse hours in UTC numeric offset")
73            }
74            InvalidMinutes => {
75                f.write_str("failed to parse minutes in UTC numeric offset")
76            }
77            InvalidSeconds => {
78                f.write_str("failed to parse seconds in UTC numeric offset")
79            }
80            InvalidSecondsFractional => f.write_str(
81                "failed to parse fractional seconds in UTC numeric offset",
82            ),
83            InvalidSign => {
84                f.write_str("failed to parse sign in UTC numeric offset")
85            }
86            InvalidSignPlusOrMinus => f.write_str(
87                "expected `+` or `-` sign at start of UTC numeric offset",
88            ),
89            MissingMinuteAfterHour => f.write_str(
90                "parsed hour component of time zone offset, \
91                 but could not find required minute component",
92            ),
93            MissingSecondAfterMinute => f.write_str(
94                "parsed hour and minute components of time zone offset, \
95                 but could not find required second component",
96            ),
97            NoColonAfterHours => f.write_str(
98                "parsed hour component of time zone offset, \
99                 but could not find required colon separator",
100            ),
101            ParseHours => f.write_str(
102                "failed to parse hours (requires a two digit integer)",
103            ),
104            ParseMinutes => f.write_str(
105                "failed to parse minutes (requires a two digit integer)",
106            ),
107            ParseSeconds => f.write_str(
108                "failed to parse seconds (requires a two digit integer)",
109            ),
110            PrecisionLoss => f.write_str(
111                "due to precision loss from fractional seconds, \
112                 time zone offset is rounded to a value that is out of bounds",
113            ),
114            SeparatorAfterHours => f.write_str(
115                "failed to parse separator after hours in \
116                 UTC numeric offset",
117            ),
118            SeparatorAfterMinutes => f.write_str(
119                "failed to parse separator after minutes in \
120                 UTC numeric offset",
121            ),
122            SubminutePrecisionNotEnabled => f.write_str(
123                "subminute precision for UTC numeric offset \
124                 is not enabled in this context (must provide only \
125                 integral minutes)",
126            ),
127            SubsecondPrecisionNotEnabled => f.write_str(
128                "subsecond precision for UTC numeric offset \
129                 is not enabled in this context (must provide only \
130                 integral minutes or seconds)",
131            ),
132            UnexpectedLetterOffsetNoZulu(byte) => write!(
133                f,
134                "found `{z}` where a numeric UTC offset \
135                 was expected (this context does not permit \
136                 the Zulu offset)",
137                z = escape::Byte(byte),
138            ),
139        }
140    }
141}