1use crate::{error, Unit};
2
3#[derive(Clone, Debug)]
4pub(crate) enum Error {
5 ConversionToSecondsFailed { unit: Unit },
6 EmptyDuration,
7 FailedValueSet { unit: Unit },
8 InvalidFraction,
9 InvalidFractionNanos,
10 MissingFractionalDigits,
11 NotAllowedCalendarUnit { unit: Unit },
12 NotAllowedFractionalUnit { found: Unit },
13 NotAllowedNegative,
14 OutOfOrderHMS { found: Unit },
15 OutOfOrderUnits { found: Unit, previous: Unit },
16 OverflowForUnit { unit: Unit },
17 OverflowForUnitFractional { unit: Unit },
18 SignedOverflowForUnit { unit: Unit },
19}
20
21impl error::IntoError for Error {
22 fn into_error(self) -> error::Error {
23 self.into()
24 }
25}
26
27impl From<Error> for error::Error {
28 #[cold]
29 #[inline(never)]
30 fn from(err: Error) -> error::Error {
31 error::ErrorKind::FmtUtil(err).into()
32 }
33}
34
35impl core::fmt::Display for Error {
36 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
37 use self::Error::*;
38
39 match *self {
40 ConversionToSecondsFailed { unit } => write!(
41 f,
42 "converting {unit} to seconds overflows \
43 a signed 64-bit integer",
44 unit = unit.plural(),
45 ),
46 EmptyDuration => f.write_str("no parsed duration components"),
47 FailedValueSet { unit } => write!(
48 f,
49 "failed to set value for {unit} unit on span",
50 unit = unit.singular(),
51 ),
52 InvalidFraction => f.write_str(
53 "failed to parse fractional component \
54 (up to 9 digits, nanosecond precision is allowed)",
55 ),
56 InvalidFractionNanos => f.write_str(
57 "failed to set nanosecond value from fractional component",
58 ),
59 MissingFractionalDigits => f.write_str(
60 "found decimal after seconds component, \
61 but did not find any digits after decimal",
62 ),
63 NotAllowedCalendarUnit { unit } => write!(
64 f,
65 "parsing calendar units ({unit} in this case) \
66 in this context is not supported \
67 (perhaps try parsing into a `jiff::Span` instead)",
68 unit = unit.plural(),
69 ),
70 NotAllowedFractionalUnit { found } => write!(
71 f,
72 "fractional {found} are not supported",
73 found = found.plural(),
74 ),
75 NotAllowedNegative => f.write_str(
76 "cannot parse negative duration into unsigned \
77 `std::time::Duration`",
78 ),
79 OutOfOrderHMS { found } => write!(
80 f,
81 "found `HH:MM:SS` after unit {found}, \
82 but `HH:MM:SS` can only appear after \
83 years, months, weeks or days",
84 found = found.singular(),
85 ),
86 OutOfOrderUnits { found, previous } => write!(
87 f,
88 "found value with unit {found} \
89 after unit {previous}, but units must be \
90 written from largest to smallest \
91 (and they can't be repeated)",
92 found = found.singular(),
93 previous = previous.singular(),
94 ),
95 OverflowForUnit { unit } => write!(
96 f,
97 "accumulated duration \
98 overflowed when adding value to unit {unit}",
99 unit = unit.singular(),
100 ),
101 OverflowForUnitFractional { unit } => write!(
102 f,
103 "accumulated duration \
104 overflowed when adding fractional value to unit {unit}",
105 unit = unit.singular(),
106 ),
107 SignedOverflowForUnit { unit } => write!(
108 f,
109 "value for {unit} is too big (or small) to fit into \
110 a signed 64-bit integer",
111 unit = unit.plural(),
112 ),
113 }
114 }
115}