Skip to main content

jiff/error/fmt/
util.rs

1use crate::{Unit, error};
2
3#[derive(#[automatically_derived]impl ::core::clone::Clone for Error {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        match self {
            Self::ConversionToSecondsFailed { unit: __self_0 } =>
                Error::ConversionToSecondsFailed {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::EmptyDuration => Error::EmptyDuration,
            Self::FailedValueSet { unit: __self_0 } =>
                Error::FailedValueSet {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::InvalidFraction => Error::InvalidFraction,
            Self::InvalidFractionNanos => Error::InvalidFractionNanos,
            Self::MissingFractionalDigits => Error::MissingFractionalDigits,
            Self::NotAllowedCalendarUnit { unit: __self_0 } =>
                Error::NotAllowedCalendarUnit {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::NotAllowedFractionalUnit { found: __self_0 } =>
                Error::NotAllowedFractionalUnit {
                    found: ::core::clone::Clone::clone(__self_0),
                },
            Self::NotAllowedNegative => Error::NotAllowedNegative,
            Self::OutOfOrderHMS { found: __self_0 } =>
                Error::OutOfOrderHMS {
                    found: ::core::clone::Clone::clone(__self_0),
                },
            Self::OutOfOrderUnits { found: __self_0, previous: __self_1 } =>
                Error::OutOfOrderUnits {
                    found: ::core::clone::Clone::clone(__self_0),
                    previous: ::core::clone::Clone::clone(__self_1),
                },
            Self::OverflowForUnit { unit: __self_0 } =>
                Error::OverflowForUnit {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::OverflowForUnitFractional { unit: __self_0 } =>
                Error::OverflowForUnitFractional {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::SignedOverflowForUnit { unit: __self_0 } =>
                Error::SignedOverflowForUnit {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
        }
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for Error {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::ConversionToSecondsFailed { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "ConversionToSecondsFailed unit", &__self_0),
            Self::EmptyDuration =>
                ::core::fmt::Formatter::write_str(f, "EmptyDuration"),
            Self::FailedValueSet { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "FailedValueSet unit", &__self_0),
            Self::InvalidFraction =>
                ::core::fmt::Formatter::write_str(f, "InvalidFraction"),
            Self::InvalidFractionNanos =>
                ::core::fmt::Formatter::write_str(f, "InvalidFractionNanos"),
            Self::MissingFractionalDigits =>
                ::core::fmt::Formatter::write_str(f,
                    "MissingFractionalDigits"),
            Self::NotAllowedCalendarUnit { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "NotAllowedCalendarUnit unit", &__self_0),
            Self::NotAllowedFractionalUnit { found: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "NotAllowedFractionalUnit found", &__self_0),
            Self::NotAllowedNegative =>
                ::core::fmt::Formatter::write_str(f, "NotAllowedNegative"),
            Self::OutOfOrderHMS { found: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "OutOfOrderHMS found", &__self_0),
            Self::OutOfOrderUnits { found: __self_0, previous: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "OutOfOrderUnits found previous", __self_0, &__self_1),
            Self::OverflowForUnit { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "OverflowForUnit unit", &__self_0),
            Self::OverflowForUnitFractional { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "OverflowForUnitFractional unit", &__self_0),
            Self::SignedOverflowForUnit { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "SignedOverflowForUnit unit", &__self_0),
        }
    }
}Debug)]
4#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5pub(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
22impl error::IntoError for Error {
23    fn into_error(self) -> error::Error {
24        self.into()
25    }
26}
27
28impl 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
36impl 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 } => f.write_fmt(format_args!("converting {0} to seconds overflows a signed 64-bit integer",
        unit.plural()))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 } => f.write_fmt(format_args!("failed to set value for {0} unit on span",
        unit.singular()))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 } => f.write_fmt(format_args!("parsing calendar units ({0} in this case) in this context is not supported (perhaps try parsing into a `jiff::Span` instead)",
        unit.plural()))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 } => f.write_fmt(format_args!("fractional {0} are not supported", found.plural()))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 } => f.write_fmt(format_args!("found `HH:MM:SS` after unit {0}, but `HH:MM:SS` can only appear after years, months, weeks or days",
        found.singular()))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 } => f.write_fmt(format_args!("found value with unit {0} after unit {1}, but units must be written from largest to smallest (and they can\'t be repeated)",
        found.singular(), previous.singular()))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 } => f.write_fmt(format_args!("accumulated duration overflowed when adding value to unit {0}",
        unit.singular()))write!(
97                f,
98                "accumulated duration \
99                 overflowed when adding value to unit {unit}",
100                unit = unit.singular(),
101            ),
102            OverflowForUnitFractional { unit } => f.write_fmt(format_args!("accumulated duration overflowed when adding fractional value to unit {0}",
        unit.singular()))write!(
103                f,
104                "accumulated duration \
105                 overflowed when adding fractional value to unit {unit}",
106                unit = unit.singular(),
107            ),
108            SignedOverflowForUnit { unit } => f.write_fmt(format_args!("value for {0} is too big (or small) to fit into a signed 64-bit integer",
        unit.plural()))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}