jiff/error/fmt/
friendly.rs

1use crate::{error, util::escape};
2
3#[derive(Clone, Debug)]
4pub(crate) enum Error {
5    Empty,
6    ExpectedColonAfterMinute,
7    ExpectedIntegerAfterSign,
8    ExpectedMinuteAfterHour,
9    ExpectedOneMoreUnitAfterComma,
10    ExpectedOneSign,
11    ExpectedSecondAfterMinute,
12    ExpectedUnitSuffix,
13    ExpectedWhitespaceAfterComma { byte: u8 },
14    ExpectedWhitespaceAfterCommaEndOfInput,
15    Failed,
16}
17
18impl error::IntoError for Error {
19    fn into_error(self) -> error::Error {
20        self.into()
21    }
22}
23
24impl From<Error> for error::Error {
25    #[cold]
26    #[inline(never)]
27    fn from(err: Error) -> error::Error {
28        error::ErrorKind::FmtFriendly(err).into()
29    }
30}
31
32impl core::fmt::Display for Error {
33    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
34        use self::Error::*;
35
36        match *self {
37            Empty => f.write_str("an empty string is not valid"),
38            ExpectedColonAfterMinute => f.write_str(
39                "when parsing the `HH:MM:SS` format, \
40                 expected to parse `:` following minute",
41            ),
42            ExpectedIntegerAfterSign => f.write_str(
43                "expected duration to start \
44                 with a unit value (a decimal integer) after an \
45                 optional sign, but no integer was found",
46            ),
47            ExpectedMinuteAfterHour => f.write_str(
48                "when parsing the `HH:MM:SS` format, \
49                 expected to parse minute following hour",
50            ),
51            ExpectedOneMoreUnitAfterComma => f.write_str(
52                "found comma at the end of duration, \
53                 but a comma indicates at least one more \
54                 unit follows",
55            ),
56            ExpectedOneSign => f.write_str(
57                "expected to find either a prefix sign (+/-) or \
58                 a suffix sign (`ago`), but found both",
59            ),
60            ExpectedSecondAfterMinute => f.write_str(
61                "when parsing the `HH:MM:SS` format, \
62                 expected to parse second following minute",
63            ),
64            ExpectedUnitSuffix => f.write_str(
65                "expected to find unit designator suffix \
66                 (e.g., `years` or `secs`) after parsing \
67                 integer",
68            ),
69            ExpectedWhitespaceAfterComma { byte } => write!(
70                f,
71                "expected whitespace after comma, but found `{byte}`",
72                byte = escape::Byte(byte),
73            ),
74            ExpectedWhitespaceAfterCommaEndOfInput => f.write_str(
75                "expected whitespace after comma, but found end of input",
76            ),
77            Failed => f.write_str(
78                "failed to parse input in the \"friendly\" duration format",
79            ),
80        }
81    }
82}