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