Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/epoch_nanoseconds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use num_traits::FromPrimitive;

use crate::{TemporalError, NS_MAX_INSTANT};
use crate::{error::ErrorMessage, TemporalError, NS_MAX_INSTANT};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct EpochNanoseconds(pub(crate) i128);
Expand All @@ -9,8 +9,7 @@ impl TryFrom<i128> for EpochNanoseconds {
type Error = TemporalError;
fn try_from(value: i128) -> Result<Self, Self::Error> {
if !is_valid_epoch_nanos(&value) {
return Err(TemporalError::range()
.with_message("Instant nanoseconds are not within a valid epoch range."));
return Err(TemporalError::range().with_enum(ErrorMessage::InstantOutOfRange));
}
Ok(Self(value))
}
Expand All @@ -20,8 +19,7 @@ impl TryFrom<u128> for EpochNanoseconds {
type Error = TemporalError;
fn try_from(value: u128) -> Result<Self, Self::Error> {
if (NS_MAX_INSTANT as u128) < value {
return Err(TemporalError::range()
.with_message("Instant nanoseconds are not within a valid epoch range."));
return Err(TemporalError::range().with_enum(ErrorMessage::InstantOutOfRange));
}
Ok(Self(value as i128))
}
Expand All @@ -31,8 +29,7 @@ impl TryFrom<f64> for EpochNanoseconds {
type Error = TemporalError;
fn try_from(value: f64) -> Result<Self, Self::Error> {
let Some(value) = i128::from_f64(value) else {
return Err(TemporalError::range()
.with_message("Instant nanoseconds are not within a valid epoch range."));
return Err(TemporalError::range().with_enum(ErrorMessage::InstantOutOfRange));
};
Self::try_from(value)
}
Expand Down
59 changes: 59 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ impl TemporalError {
self
}

/// Add a message enum to the error.
#[inline]
#[must_use]
pub(crate) fn with_enum(mut self, msg: ErrorMessage) -> Self {
Comment thread
nekevss marked this conversation as resolved.
self.msg = msg.to_string().into();
self
}

/// Returns this error's kind.
#[inline]
#[must_use]
Expand Down Expand Up @@ -146,3 +154,54 @@ impl fmt::Display for TemporalError {
Ok(())
}
}

/// The error message
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum ErrorMessage {
InstantOutOfRange,

NumberNotFinite,
NumberNotIntegral,
NumberNotPositive,
NumberOutOfRange,

FractionalDigitsPrecisionInvalid,

SmallestUnitNotTimeUnit,
SmallestUnitLargerThanLargestUnit,
UnitNotDate,
UnitNotTime,
UnitRequired,
UnitNoAutoDuringComparison,
RoundToUnitInvalid,
RoundingModeInvalid,
CalendarNameInvalid,
OffsetOptionInvalid,
TimeZoneNameInvalid,
}

impl ErrorMessage {
pub fn to_string(self) -> &'static str {
match self {
Self::InstantOutOfRange => "Instant nanoseconds are not within a valid epoch range.",
Self::NumberNotFinite => "number value is not a finite value.",
Self::NumberNotIntegral => "value must be integral.",
Self::NumberNotPositive => "integer must be positive.",
Self::NumberOutOfRange => "number exceeded a valid range.",
Self::FractionalDigitsPrecisionInvalid => "Invalid fractionalDigits precision value",
Self::SmallestUnitNotTimeUnit => "smallestUnit must be a valid time unit.",
Self::SmallestUnitLargerThanLargestUnit => {
"smallestUnit was larger than largestunit in DifferenceeSettings"
}
Self::UnitNotDate => "Unit was not part of the date unit group.",
Self::UnitNotTime => "Unit was not part of the time unit group.",
Self::UnitRequired => "Unit is required",
Self::UnitNoAutoDuringComparison => "'auto' units are not allowed during comparison",
Self::RoundToUnitInvalid => "Invalid roundTo unit provided.",
Self::RoundingModeInvalid => "Invalid roundingMode option provided",
Self::CalendarNameInvalid => "Invalid calendarName option provided",
Self::OffsetOptionInvalid => "Invalid offsetOption option provided",
Self::TimeZoneNameInvalid => "Invalid timeZoneName option provided",
}
}
}
118 changes: 56 additions & 62 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! operation may be completed.

use crate::parsers::Precision;
use crate::{TemporalError, TemporalResult, MS_PER_DAY, NS_PER_DAY};
use crate::{error::ErrorMessage, TemporalError, TemporalResult, MS_PER_DAY, NS_PER_DAY};
use core::ops::Add;
use core::{fmt, str::FromStr};

Expand Down Expand Up @@ -71,54 +71,50 @@ impl ToStringRoundingOptions {
rounding_mode,
increment: RoundingIncrement::ONE,
}),
None => {
match self.precision {
Precision::Auto => Ok(ResolvedToStringRoundingOptions {
precision: Precision::Auto,
smallest_unit: Unit::Nanosecond,
None => match self.precision {
Precision::Auto => Ok(ResolvedToStringRoundingOptions {
precision: Precision::Auto,
smallest_unit: Unit::Nanosecond,
rounding_mode,
increment: RoundingIncrement::ONE,
}),
Precision::Digit(0) => Ok(ResolvedToStringRoundingOptions {
precision: Precision::Digit(0),
smallest_unit: Unit::Second,
rounding_mode,
increment: RoundingIncrement::ONE,
}),
Precision::Digit(d) if (1..=3).contains(&d) => {
Ok(ResolvedToStringRoundingOptions {
precision: Precision::Digit(d),
smallest_unit: Unit::Millisecond,
rounding_mode,
increment: RoundingIncrement::ONE,
}),
Precision::Digit(0) => Ok(ResolvedToStringRoundingOptions {
precision: Precision::Digit(0),
smallest_unit: Unit::Second,
increment: RoundingIncrement::try_new(10_u32.pow(3 - d as u32))
.expect("a valid increment"),
})
}
Precision::Digit(d) if (4..=6).contains(&d) => {
Ok(ResolvedToStringRoundingOptions {
precision: Precision::Digit(d),
smallest_unit: Unit::Microsecond,
rounding_mode,
increment: RoundingIncrement::ONE,
}),
Precision::Digit(d) if (1..=3).contains(&d) => {
Ok(ResolvedToStringRoundingOptions {
precision: Precision::Digit(d),
smallest_unit: Unit::Millisecond,
rounding_mode,
increment: RoundingIncrement::try_new(10_u32.pow(3 - d as u32))
.expect("a valid increment"),
})
}
Precision::Digit(d) if (4..=6).contains(&d) => {
Ok(ResolvedToStringRoundingOptions {
precision: Precision::Digit(d),
smallest_unit: Unit::Microsecond,
rounding_mode,
increment: RoundingIncrement::try_new(10_u32.pow(6 - d as u32))
.expect("a valid increment"),
})
}
Precision::Digit(d) if (7..=9).contains(&d) => {
Ok(ResolvedToStringRoundingOptions {
precision: Precision::Digit(d),
smallest_unit: Unit::Nanosecond,
rounding_mode,
increment: RoundingIncrement::try_new(10_u32.pow(9 - d as u32))
.expect("a valid increment"),
})
}
_ => Err(TemporalError::range()
.with_message("Invalid fractionalDigits precision value")),
increment: RoundingIncrement::try_new(10_u32.pow(6 - d as u32))
.expect("a valid increment"),
})
}
}
_ => {
Err(TemporalError::range().with_message("smallestUnit must be a valid time unit."))
}
Precision::Digit(d) if (7..=9).contains(&d) => {
Ok(ResolvedToStringRoundingOptions {
precision: Precision::Digit(d),
smallest_unit: Unit::Nanosecond,
rounding_mode,
increment: RoundingIncrement::try_new(10_u32.pow(9 - d as u32))
.expect("a valid increment"),
})
}
_ => Err(TemporalError::range()
.with_enum(ErrorMessage::FractionalDigitsPrecisionInvalid)),
},
_ => Err(TemporalError::range().with_enum(ErrorMessage::SmallestUnitNotTimeUnit)),
}
}
}
Expand Down Expand Up @@ -212,8 +208,9 @@ impl ResolvedRoundingOptions {
.unwrap_unit_or(smallest_unit.max(fallback_largest));
// 11. If LargerOfTwoUnits(largestUnit, smallestUnit) is not largestUnit, throw a RangeError exception.
if largest_unit < smallest_unit {
return Err(TemporalError::range()
.with_message("smallestUnit was larger than largestunit in DifferenceeSettings"));
return Err(
TemporalError::range().with_enum(ErrorMessage::SmallestUnitLargerThanLargestUnit)
);
}

// 12. Let maximum be MaximumTemporalDurationRoundingIncrement(smallestUnit).
Expand Down Expand Up @@ -243,7 +240,7 @@ impl ResolvedRoundingOptions {
} else {
let maximum = smallest_unit
.to_maximum_rounding_increment()
.ok_or(TemporalError::range().with_message("smallestUnit must be a time unit."))?;
.ok_or(TemporalError::range().with_enum(ErrorMessage::SmallestUnitNotTimeUnit))?;
(maximum, false)
};

Expand All @@ -268,7 +265,7 @@ impl ResolvedRoundingOptions {
Unit::Millisecond => MS_PER_DAY as u64,
Unit::Microsecond => MS_PER_DAY as u64 * 1000,
Unit::Nanosecond => NS_PER_DAY,
_ => return Err(TemporalError::range().with_message("Invalid roundTo unit provided.")),
_ => return Err(TemporalError::range().with_enum(ErrorMessage::RoundToUnitInvalid)),
};

increment.validate(maximum, true)?;
Expand Down Expand Up @@ -302,7 +299,7 @@ impl UnitGroup {
extra_unit: Option<Unit>,
) -> TemporalResult<Unit> {
let Some(unit) = unit else {
return Err(TemporalError::range().with_message("Unit is required."));
return Err(TemporalError::range().with_enum(ErrorMessage::UnitRequired));
};
self.validate_unit(Some(unit), extra_unit)?;
Ok(unit)
Expand All @@ -315,15 +312,13 @@ impl UnitGroup {
Some(unit) if !unit.is_time_unit() => Ok(()),
None => Ok(()),
_ if unit == extra_unit => Ok(()),
_ => Err(TemporalError::range()
.with_message("Unit was not part of the date unit group.")),
_ => Err(TemporalError::range().with_enum(ErrorMessage::UnitNotDate)),
},
UnitGroup::Time => match unit {
Some(unit) if unit.is_time_unit() => Ok(()),
None => Ok(()),
_ if unit == extra_unit => Ok(()),
_ => Err(TemporalError::range()
.with_message("Unit was not part of the time unit group.")),
_ => Err(TemporalError::range().with_enum(ErrorMessage::UnitNotTime)),
},
UnitGroup::DateTime => Ok(()),
}
Expand Down Expand Up @@ -473,7 +468,7 @@ impl Unit {
}

// NOTE(HalidOdat): deviation from specification.
Err(TemporalError::assert().with_message("auto cannot be used for comparison"))
Err(TemporalError::assert().with_enum(ErrorMessage::UnitNoAutoDuringComparison))
}

/// Helper method for getting the index into the [`UNIT_VALUE_TABLE`].
Expand All @@ -494,8 +489,7 @@ impl Unit {
}
}

Err(TemporalError::assert()
.with_message("auto does not exist in the spec Table 21 Value column"))
Err(TemporalError::assert().with_enum(ErrorMessage::UnitNoAutoDuringComparison))
}
}

Expand Down Expand Up @@ -875,7 +869,7 @@ impl FromStr for RoundingMode {
"halfExpand" => Ok(Self::HalfExpand),
"halfTrunc" => Ok(Self::HalfTrunc),
"halfEven" => Ok(Self::HalfEven),
_ => Err(TemporalError::range().with_message("RoundingMode not an accepted value.")),
_ => Err(TemporalError::range().with_enum(ErrorMessage::RoundingModeInvalid)),
}
}
}
Expand Down Expand Up @@ -933,7 +927,7 @@ impl FromStr for DisplayCalendar {
"always" => Ok(Self::Always),
"never" => Ok(Self::Never),
"critical" => Ok(Self::Critical),
_ => Err(TemporalError::range().with_message("Invalid calendarName provided.")),
_ => Err(TemporalError::range().with_enum(ErrorMessage::CalendarNameInvalid)),
}
}
}
Expand Down Expand Up @@ -962,7 +956,7 @@ impl FromStr for DisplayOffset {
match s {
"auto" => Ok(Self::Auto),
"never" => Ok(Self::Never),
_ => Err(TemporalError::range().with_message("Invalid offset option provided.")),
_ => Err(TemporalError::range().with_enum(ErrorMessage::OffsetOptionInvalid)),
}
}
}
Expand Down Expand Up @@ -997,7 +991,7 @@ impl FromStr for DisplayTimeZone {
"auto" => Ok(Self::Auto),
"never" => Ok(Self::Never),
"critical" => Ok(Self::Critical),
_ => Err(TemporalError::range().with_message("Invalid timeZoneName option provided.")),
_ => Err(TemporalError::range().with_enum(ErrorMessage::TimeZoneNameInvalid)),
}
}
}
Loading
Loading