1use jcore::constantsas c;
23use crate::{Unit, error};
45#[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}
2021impl From<UnitConfigError> for error::Error {
22#[cold]
23 #[inline(never)]
24fn from(err: UnitConfigError) -> error::Error {
25 error::ErrorKind::UnitConfig(err).into()
26 }
27}
2829impl error::IntoErrorfor UnitConfigError {
30fn into_error(self) -> error::Error {
31self.into()
32 }
33}
3435impl core::fmt::Displayfor UnitConfigError {
36fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
37use self::UnitConfigError::*;
3839match *self {
40CalendarUnitsNotAllowed { 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!(
41f,
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 ),
49CivilDate { given } => f.write_fmt(format_args!("\'{0}\' not allowed as a date unit for rounding span",
given.singular()))write!(
50f,
51"'{unit}' not allowed as a date unit for rounding span",
52 unit = given.singular()
53 ),
54CivilTime { given } => f.write_fmt(format_args!("\'{0}\' not allowed as a time unit for rounding span",
given.singular()))write!(
55f,
56"'{unit}' not allowed as a time unit for rounding span",
57 unit = given.singular()
58 ),
59IncrementDivide { unit, must_divide: MustDivide::Days } => f.write_fmt(format_args!("increment for rounding to \'{0}\' must be equal to `1`",
unit.plural()))write!(
60f,
61"increment for rounding to '{unit}' \
62 must be equal to `1`",
63 unit = unit.plural(),
64 ),
65IncrementDivide { 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!(
66f,
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 ),
72LargestSmallerThanSmallest { smallest, largest } => {
73f.write_fmt(format_args!("largest unit (\'{0}\') cannot be smaller than smallest unit (\'{1}\')",
largest.singular(), smallest.singular()))write!(
74f,
75"largest unit ('{largest}') cannot be smaller than \
76 smallest unit ('{smallest}')",
77 largest = largest.singular(),
78 smallest = smallest.singular(),
79 )80 }
81RelativeWeekOrDay { 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!(
82f,
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 ),
90RelativeYearOrMonth { 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!(
91f,
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 ),
97RelativeYearOrMonthGivenDaysAre24Hours { 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!(
98f,
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 ),
106RoundToUnitUnsupported { unit } => f.write_fmt(format_args!("rounding to \'{0}\' is not supported",
unit.plural()))write!(
107f,
108"rounding to '{unit}' is not supported",
109 unit = unit.plural(),
110 ),
111SignedDurationCalendarUnit { 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!(
112f,
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 ),
118TimeZoneOffset { given } => f.write_fmt(format_args!("\'{0}\' not allowed when rounding time zone offset (must use hours, minutes or seconds)",
given.singular()))write!(
119f,
120"'{unit}' not allowed when rounding time zone offset \
121 (must use hours, minutes or seconds)",
122 unit = given.singular(),
123 ),
124 }
125 }
126}
127128#[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`.
144Days,
145}
146147impl MustDivide {
148pub(crate) fn as_i64(&self) -> i64 {
149use self::MustDivide::*;
150151match *self {
152NanosPerCivilDay => c::NANOS_PER_CIVIL_DAY,
153MicrosPerCivilDay => c::MICROS_PER_CIVIL_DAY,
154MillisPerCivilDay => c::MILLIS_PER_CIVIL_DAY,
155SecsPerCivilDay => c::SECS_PER_CIVIL_DAY,
156MinsPerCivilDay => c::MINS_PER_CIVIL_DAY,
157HoursPerCivilDay => c::HOURS_PER_CIVIL_DAY,
158NanosPerMicro => c::NANOS_PER_MICRO,
159MicrosPerMilli => c::MICROS_PER_MILLI,
160MillisPerSec => c::MILLIS_PER_SEC,
161SecsPerMin => c::SECS_PER_MIN,
162MinsPerHour => c::MINS_PER_HOUR,
163Days => 2,
164 }
165 }
166}