| 1 |
use jcore::constants as c; |
| 2 |
|
| 3 |
use crate::{error, Unit}; |
| 4 |
|
| 5 |
#[derive(Clone, Debug)] |
| 6 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 7 |
pub(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 |
|
| 21 |
impl 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 |
|
| 29 |
impl error::IntoError for UnitConfigError { |
| 30 |
fn into_error(self) -> error::Error { |
| 31 |
self.into() |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl 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 } => 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 } => write!( |
| 50 |
f, |
| 51 |
"'{unit}' not allowed as a date unit for rounding span", |
| 52 |
unit = given.singular() |
| 53 |
), |
| 54 |
CivilTime { given } => 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 } => 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 } => 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 |
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 } => 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 } => 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 } => 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 } => write!( |
| 107 |
f, |
| 108 |
"rounding to '{unit}' is not supported", |
| 109 |
unit = unit.plural(), |
| 110 |
), |
| 111 |
SignedDurationCalendarUnit { smallest } => 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 } => 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(Clone, Copy, Debug)] |
| 129 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 130 |
pub(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 |
|
| 147 |
impl 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 |
} |