Skip to main content

jiff/error/
unit.rs

1use jcore::constants as c;
2
3use crate::{Unit, error};
4
5#[derive(#[automatically_derived]impl ::core::clone::Clone for UnitConfigError {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        match self {
            Self::CalendarUnitsNotAllowed { unit: __self_0 } =>
                UnitConfigError::CalendarUnitsNotAllowed {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::CivilDate { given: __self_0 } =>
                UnitConfigError::CivilDate {
                    given: ::core::clone::Clone::clone(__self_0),
                },
            Self::CivilTime { given: __self_0 } =>
                UnitConfigError::CivilTime {
                    given: ::core::clone::Clone::clone(__self_0),
                },
            Self::IncrementDivide { unit: __self_0, must_divide: __self_1 } =>
                UnitConfigError::IncrementDivide {
                    unit: ::core::clone::Clone::clone(__self_0),
                    must_divide: ::core::clone::Clone::clone(__self_1),
                },
            Self::LargestSmallerThanSmallest {
                smallest: __self_0, largest: __self_1 } =>
                UnitConfigError::LargestSmallerThanSmallest {
                    smallest: ::core::clone::Clone::clone(__self_0),
                    largest: ::core::clone::Clone::clone(__self_1),
                },
            Self::RelativeWeekOrDay { unit: __self_0 } =>
                UnitConfigError::RelativeWeekOrDay {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::RelativeYearOrMonth { unit: __self_0 } =>
                UnitConfigError::RelativeYearOrMonth {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::RelativeYearOrMonthGivenDaysAre24Hours { unit: __self_0 } =>
                UnitConfigError::RelativeYearOrMonthGivenDaysAre24Hours {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::RoundToUnitUnsupported { unit: __self_0 } =>
                UnitConfigError::RoundToUnitUnsupported {
                    unit: ::core::clone::Clone::clone(__self_0),
                },
            Self::SignedDurationCalendarUnit { smallest: __self_0 } =>
                UnitConfigError::SignedDurationCalendarUnit {
                    smallest: ::core::clone::Clone::clone(__self_0),
                },
            Self::TimeZoneOffset { given: __self_0 } =>
                UnitConfigError::TimeZoneOffset {
                    given: ::core::clone::Clone::clone(__self_0),
                },
        }
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for UnitConfigError {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::CalendarUnitsNotAllowed { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "CalendarUnitsNotAllowed unit", &__self_0),
            Self::CivilDate { given: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "CivilDate given", &__self_0),
            Self::CivilTime { given: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "CivilTime given", &__self_0),
            Self::IncrementDivide { unit: __self_0, must_divide: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "IncrementDivide unit must_divide", __self_0, &__self_1),
            Self::LargestSmallerThanSmallest {
                smallest: __self_0, largest: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "LargestSmallerThanSmallest smallest largest", __self_0,
                    &__self_1),
            Self::RelativeWeekOrDay { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "RelativeWeekOrDay unit", &__self_0),
            Self::RelativeYearOrMonth { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "RelativeYearOrMonth unit", &__self_0),
            Self::RelativeYearOrMonthGivenDaysAre24Hours { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "RelativeYearOrMonthGivenDaysAre24Hours unit", &__self_0),
            Self::RoundToUnitUnsupported { unit: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "RoundToUnitUnsupported unit", &__self_0),
            Self::SignedDurationCalendarUnit { smallest: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "SignedDurationCalendarUnit smallest", &__self_0),
            Self::TimeZoneOffset { given: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "TimeZoneOffset given", &__self_0),
        }
    }
}Debug)]
6#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7pub(crate) enum UnitConfigError {
8    CalendarUnitsNotAllowed { unit: Unit },
9    CivilDate { given: Unit },
10    CivilTime { given: Unit },
11    IncrementDivide { unit: Unit, must_divide: MustDivide },
12    LargestSmallerThanSmallest { smallest: Unit, largest: Unit },
13    RelativeWeekOrDay { unit: Unit },
14    RelativeYearOrMonth { unit: Unit },
15    RelativeYearOrMonthGivenDaysAre24Hours { unit: Unit },
16    RoundToUnitUnsupported { unit: Unit },
17    SignedDurationCalendarUnit { smallest: Unit },
18    TimeZoneOffset { given: Unit },
19}
20
21impl From<UnitConfigError> for error::Error {
22    #[cold]
23    #[inline(never)]
24    fn from(err: UnitConfigError) -> error::Error {
25        error::ErrorKind::UnitConfig(err).into()
26    }
27}
28
29impl error::IntoError for UnitConfigError {
30    fn into_error(self) -> error::Error {
31        self.into()
32    }
33}
34
35impl core::fmt::Display for UnitConfigError {
36    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
37        use self::UnitConfigError::*;
38
39        match *self {
40            CalendarUnitsNotAllowed { unit } => f.write_fmt(format_args!("operation can only be performed with units of hours or smaller, but found non-zero \'{0}\' units (operations on `jiff::Timestamp`, `jiff::tz::Offset` and `jiff::civil::Time` don\'t support calendar units in a `jiff::Span`)",
        unit.singular()))write!(
41                f,
42                "operation can only be performed with units of hours \
43                 or smaller, but found non-zero '{unit}' units \
44                 (operations on `jiff::Timestamp`, `jiff::tz::Offset` \
45                  and `jiff::civil::Time` don't support calendar \
46                  units in a `jiff::Span`)",
47                unit = unit.singular(),
48            ),
49            CivilDate { given } => f.write_fmt(format_args!("\'{0}\' not allowed as a date unit for rounding span",
        given.singular()))write!(
50                f,
51                "'{unit}' not allowed as a date unit for rounding span",
52                unit = given.singular()
53            ),
54            CivilTime { given } => f.write_fmt(format_args!("\'{0}\' not allowed as a time unit for rounding span",
        given.singular()))write!(
55                f,
56                "'{unit}' not allowed as a time unit for rounding span",
57                unit = given.singular()
58            ),
59            IncrementDivide { unit, must_divide: MustDivide::Days } => f.write_fmt(format_args!("increment for rounding to \'{0}\' must be equal to `1`",
        unit.plural()))write!(
60                f,
61                "increment for rounding to '{unit}' \
62                 must be equal to `1`",
63                unit = unit.plural(),
64            ),
65            IncrementDivide { unit, must_divide } => f.write_fmt(format_args!("increment for rounding to \'{0}\' must divide into `{1}` evenly",
        unit.plural(), must_divide.as_i64()))write!(
66                f,
67                "increment for rounding to '{unit}' \
68                 must divide into `{must_divide}` evenly",
69                unit = unit.plural(),
70                must_divide = must_divide.as_i64(),
71            ),
72            LargestSmallerThanSmallest { smallest, largest } => {
73                f.write_fmt(format_args!("largest unit (\'{0}\') cannot be smaller than smallest unit (\'{1}\')",
        largest.singular(), smallest.singular()))write!(
74                    f,
75                    "largest unit ('{largest}') cannot be smaller than \
76                     smallest unit ('{smallest}')",
77                    largest = largest.singular(),
78                    smallest = smallest.singular(),
79                )
80            }
81            RelativeWeekOrDay { unit } => f.write_fmt(format_args!("using unit \'{0}\' in a span or configuration requires that either a relative reference time be given or `jiff::SpanRelativeTo::days_are_24_hours()` is used to indicate invariant 24-hour days, but neither were provided",
        unit.singular()))write!(
82                f,
83                "using unit '{unit}' in a span or configuration \
84                 requires that either a relative reference time be given \
85                 or `jiff::SpanRelativeTo::days_are_24_hours()` is used to \
86                 indicate invariant 24-hour days, \
87                 but neither were provided",
88                unit = unit.singular(),
89            ),
90            RelativeYearOrMonth { unit } => f.write_fmt(format_args!("using unit \'{0}\' in a span or configuration requires that a relative reference time be given, but none was provided",
        unit.singular()))write!(
91                f,
92                "using unit '{unit}' in a span or configuration \
93                 requires that a relative reference time be given, \
94                 but none was provided",
95                unit = unit.singular(),
96            ),
97            RelativeYearOrMonthGivenDaysAre24Hours { unit } => f.write_fmt(format_args!("using unit \'{0}\' in span or configuration requires that a relative reference time be given (`jiff::SpanRelativeTo::days_are_24_hours()` was given but this only permits using days and weeks without a relative reference time)",
        unit.singular()))write!(
98                f,
99                "using unit '{unit}' in span or configuration \
100                 requires that a relative reference time be given \
101                 (`jiff::SpanRelativeTo::days_are_24_hours()` was given \
102                 but this only permits using days and weeks \
103                 without a relative reference time)",
104                unit = unit.singular(),
105            ),
106            RoundToUnitUnsupported { unit } => f.write_fmt(format_args!("rounding to \'{0}\' is not supported",
        unit.plural()))write!(
107                f,
108                "rounding to '{unit}' is not supported",
109                unit = unit.plural(),
110            ),
111            SignedDurationCalendarUnit { smallest } => f.write_fmt(format_args!("rounding `jiff::SignedDuration` failed because the smallest unit provided, \'{0}\', is a calendar unit (to round by calendar units, you must use a `jiff::Span`)",
        smallest.plural()))write!(
112                f,
113                "rounding `jiff::SignedDuration` failed because \
114                 the smallest unit provided, '{plural}', is a calendar unit \
115                 (to round by calendar units, you must use a `jiff::Span`)",
116                plural = smallest.plural(),
117            ),
118            TimeZoneOffset { given } => f.write_fmt(format_args!("\'{0}\' not allowed when rounding time zone offset (must use hours, minutes or seconds)",
        given.singular()))write!(
119                f,
120                "'{unit}' not allowed when rounding time zone offset \
121                 (must use hours, minutes or seconds)",
122                unit = given.singular(),
123            ),
124        }
125    }
126}
127
128#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for MustDivide {
}
#[automatically_derived]impl ::core::clone::Clone for MustDivide {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::Copy for MustDivide { }Copy, #[automatically_derived]impl ::core::fmt::Debug for MustDivide {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_c_like_enum_write_str(f,
            "NanosPerCivilDay MicrosPerCivilDay MillisPerCivilDay SecsPerCivilDay MinsPerCivilDay HoursPerCivilDay NanosPerMicro MicrosPerMilli MillisPerSec SecsPerMin MinsPerHour Days",
            match self {
                MustDivide::NanosPerCivilDay => 0usize,
                MustDivide::MicrosPerCivilDay => 17usize,
                MustDivide::MillisPerCivilDay => 35usize,
                MustDivide::SecsPerCivilDay => 53usize,
                MustDivide::MinsPerCivilDay => 69usize,
                MustDivide::HoursPerCivilDay => 85usize,
                MustDivide::NanosPerMicro => 102usize,
                MustDivide::MicrosPerMilli => 116usize,
                MustDivide::MillisPerSec => 131usize,
                MustDivide::SecsPerMin => 144usize,
                MustDivide::MinsPerHour => 155usize,
                MustDivide::Days => 167usize,
            })
    }
}Debug)]
129#[cfg_attr(feature = "defmt", derive(defmt::Format))]
130pub(crate) enum MustDivide {
131    NanosPerCivilDay,
132    MicrosPerCivilDay,
133    MillisPerCivilDay,
134    SecsPerCivilDay,
135    MinsPerCivilDay,
136    HoursPerCivilDay,
137    NanosPerMicro,
138    MicrosPerMilli,
139    MillisPerSec,
140    SecsPerMin,
141    MinsPerHour,
142    // A weird case because we require that the rounding increment, when
143    // rounding to the nearest day for a date or datetime, is always `1`.
144    Days,
145}
146
147impl MustDivide {
148    pub(crate) fn as_i64(&self) -> i64 {
149        use self::MustDivide::*;
150
151        match *self {
152            NanosPerCivilDay => c::NANOS_PER_CIVIL_DAY,
153            MicrosPerCivilDay => c::MICROS_PER_CIVIL_DAY,
154            MillisPerCivilDay => c::MILLIS_PER_CIVIL_DAY,
155            SecsPerCivilDay => c::SECS_PER_CIVIL_DAY,
156            MinsPerCivilDay => c::MINS_PER_CIVIL_DAY,
157            HoursPerCivilDay => c::HOURS_PER_CIVIL_DAY,
158            NanosPerMicro => c::NANOS_PER_MICRO,
159            MicrosPerMilli => c::MICROS_PER_MILLI,
160            MillisPerSec => c::MILLIS_PER_SEC,
161            SecsPerMin => c::SECS_PER_MIN,
162            MinsPerHour => c::MINS_PER_HOUR,
163            Days => 2,
164        }
165    }
166}