jiff/error/
span.rs

1use crate::{error, Unit};
2
3#[derive(Clone, Debug)]
4pub(crate) enum Error {
5    ConvertDateTimeToTimestamp,
6    ConvertNanoseconds { unit: Unit },
7    ConvertNegative,
8    ConvertSpanToSignedDuration,
9    FailedSpanBetweenDateTimes { unit: Unit },
10    FailedSpanBetweenZonedDateTimes { unit: Unit },
11    OptionLargest,
12    OptionLargestInSpan,
13    OptionSmallest,
14    ToDurationCivil,
15    ToDurationDaysAre24Hours,
16    ToDurationZoned,
17}
18
19impl From<Error> for error::Error {
20    #[cold]
21    #[inline(never)]
22    fn from(err: Error) -> error::Error {
23        error::ErrorKind::Span(err).into()
24    }
25}
26
27impl error::IntoError for Error {
28    fn into_error(self) -> error::Error {
29        self.into()
30    }
31}
32
33impl core::fmt::Display for Error {
34    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
35        use self::Error::*;
36
37        match *self {
38            ConvertDateTimeToTimestamp => f.write_str(
39                "failed to interpret datetime in UTC \
40                 in order to convert it to a timestamp",
41            ),
42            ConvertNanoseconds { unit } => write!(
43                f,
44                "failed to convert rounded nanoseconds \
45                 to span for largest unit set to '{unit}'",
46                unit = unit.plural(),
47            ),
48            ConvertNegative => f.write_str(
49                "cannot convert negative span \
50                 to unsigned `std::time::Duration`",
51            ),
52            ConvertSpanToSignedDuration => f.write_str(
53                "failed to convert span to duration without relative datetime \
54                 (must use `jiff::Span::to_duration` instead)",
55            ),
56            FailedSpanBetweenDateTimes { unit } => write!(
57                f,
58                "failed to get span between datetimes \
59                 with largest unit set to '{unit}'",
60                unit = unit.plural(),
61            ),
62            FailedSpanBetweenZonedDateTimes { unit } => write!(
63                f,
64                "failed to get span between zoned datetimes \
65                 with largest unit set to '{unit}'",
66                unit = unit.plural(),
67            ),
68            OptionLargest => {
69                f.write_str("error with `largest` rounding option")
70            }
71            OptionLargestInSpan => {
72                f.write_str("error with largest unit in span to be rounded")
73            }
74            OptionSmallest => {
75                f.write_str("error with `smallest` rounding option")
76            }
77            ToDurationCivil => f.write_str(
78                "could not compute normalized relative span \
79                 from civil datetime",
80            ),
81            ToDurationDaysAre24Hours => f.write_str(
82                "could not compute normalized relative span \
83                 when all days are assumed to be 24 hours",
84            ),
85            ToDurationZoned => f.write_str(
86                "could not compute normalized relative span \
87                 from zoned datetime",
88            ),
89        }
90    }
91}