Debian Codegraph @

debian/rust-vendor/jiff/src/error/span.rs

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