Skip to content
Open
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
2 changes: 2 additions & 0 deletions crates/temporal/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub(crate) enum ErrorMessage {
NumberNotFinite,
NumberNotIntegral,
NumberNotPositive,
NumberNotNonNegative,
NumberOutOfRange,
FractionalDigitsPrecisionInvalid,

Expand Down Expand Up @@ -237,6 +238,7 @@ impl ErrorMessage {
Self::NumberNotFinite => "number value is not a finite value.",
Self::NumberNotIntegral => "value must be integral.",
Self::NumberNotPositive => "integer must be positive.",
Self::NumberNotNonNegative => "integer must be non-negative.",
Self::NumberOutOfRange => "number exceeded a valid range.",
Self::FractionalDigitsPrecisionInvalid => "Invalid fractionalDigits precision value",
Self::SmallestUnitIsRequired => "smallestUnit is required",
Expand Down
57 changes: 56 additions & 1 deletion crates/temporal/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::cmp::Ordering;

use crate::{error::ErrorMessage, TemporalError, TemporalResult};
use num_traits::float::FloatCore;
use num_traits::{AsPrimitive, FromPrimitive, PrimInt};
use num_traits::{AsPrimitive, FromPrimitive, PrimInt, Unsigned};

#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct FiniteF64(pub(crate) f64);
Expand Down Expand Up @@ -110,6 +110,18 @@ impl FiniteF64 {
}
Ok(truncated_value)
}

/// Truncate the current `FiniteF64` to an unsigned integer `T`, throwing an error if the value is negative.
pub fn as_unsigned_integer_with_truncation<T>(&self) -> TemporalResult<T>
where
T: PrimInt + Unsigned + AsPrimitive<f64>,
f64: AsPrimitive<T>,
{
if FloatCore::trunc(self.0) < 0.0 {
return Err(TemporalError::range().with_enum(ErrorMessage::NumberNotNonNegative));
}
Ok(self.as_integer_with_truncation::<T>())
}
}

impl AsPrimitive<i64> for FiniteF64 {
Expand Down Expand Up @@ -384,6 +396,49 @@ mod tests {
assert_eq!(truncated, 1);
}

#[test]
fn finitef64_as_unsigned_integer_with_truncation() {
let zero = FiniteF64::from(0);
assert_eq!(zero.as_unsigned_integer_with_truncation::<u8>(), Ok(0));

for value in [0.5, 0.9, -0.5] {
let finite = FiniteF64::try_from(value).unwrap();
assert_eq!(finite.as_unsigned_integer_with_truncation::<u8>(), Ok(0));
}

let one_point_one = FiniteF64::try_from(1.1).unwrap();
assert_eq!(
one_point_one.as_unsigned_integer_with_truncation::<u8>(),
Ok(1)
);
let one_point_nine = FiniteF64::try_from(1.9).unwrap();
assert_eq!(
one_point_nine.as_unsigned_integer_with_truncation::<u8>(),
Ok(1)
);
let five = FiniteF64::from(5);
assert_eq!(five.as_unsigned_integer_with_truncation::<u8>(), Ok(5));

for value in [-1.2, -4.0] {
let finite = FiniteF64::try_from(value).unwrap();
let truncated = finite.as_unsigned_integer_with_truncation::<u8>();
assert_eq!(
truncated.map_err(|err| err.into_message()),
Err("integer must be non-negative.")
);
}

let above_u8 = FiniteF64::try_from(300.7).unwrap();
assert_eq!(
above_u8.as_unsigned_integer_with_truncation::<u8>(),
Ok(u8::MAX)
);
assert_eq!(
above_u8.as_unsigned_integer_with_truncation::<u16>(),
Ok(300)
);
}

#[test]
fn finitef64_truncate_correct_floor() {
let floors_to_zero = FiniteF64::try_from(0.5).unwrap();
Expand Down