diff --git a/src/builtins/compiled/duration.rs b/src/builtins/compiled/duration.rs index 5ca94e529..d7695935b 100644 --- a/src/builtins/compiled/duration.rs +++ b/src/builtins/compiled/duration.rs @@ -1,6 +1,6 @@ use crate::{ builtins::TZ_PROVIDER, - options::{RelativeTo, RoundingOptions, TemporalUnit}, + options::{RelativeTo, RoundingOptions, Unit}, primitive::FiniteF64, Duration, TemporalError, TemporalResult, }; @@ -41,11 +41,7 @@ impl Duration { self.compare_with_provider(two, relative_to, &*provider) } - pub fn total( - &self, - unit: TemporalUnit, - relative_to: Option, - ) -> TemporalResult { + pub fn total(&self, unit: Unit, relative_to: Option) -> TemporalResult { let provider = TZ_PROVIDER .lock() .map_err(|_| TemporalError::general("Unable to acquire lock"))?; diff --git a/src/builtins/compiled/duration/tests.rs b/src/builtins/compiled/duration/tests.rs index e6f168a51..56fb8cb30 100644 --- a/src/builtins/compiled/duration/tests.rs +++ b/src/builtins/compiled/duration/tests.rs @@ -2,8 +2,7 @@ use std::string::ToString; use crate::{ options::{ - OffsetDisambiguation, RelativeTo, RoundingIncrement, RoundingOptions, TemporalRoundingMode, - TemporalUnit, + OffsetDisambiguation, RelativeTo, RoundingIncrement, RoundingMode, RoundingOptions, Unit, }, partial::PartialDuration, primitive::FiniteF64, @@ -53,46 +52,46 @@ fn basic_positive_floor_rounding_v2() { largest_unit: None, smallest_unit: None, increment: None, - rounding_mode: Some(TemporalRoundingMode::Floor), + rounding_mode: Some(RoundingMode::Floor), }; - let _ = options.smallest_unit.insert(TemporalUnit::Year); + let _ = options.smallest_unit.insert(Unit::Year); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 0, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Month); + let _ = options.smallest_unit.insert(Unit::Month); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Week); + let _ = options.smallest_unit.insert(Unit::Week); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 3, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Day); + let _ = options.smallest_unit.insert(Unit::Day); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Hour); + let _ = options.smallest_unit.insert(Unit::Hour); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Minute); + let _ = options.smallest_unit.insert(Unit::Minute); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Second); + let _ = options.smallest_unit.insert(Unit::Second); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Millisecond); + let _ = options.smallest_unit.insert(Unit::Millisecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 123, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Microsecond); + let _ = options.smallest_unit.insert(Unit::Microsecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 123, 987, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Nanosecond); + let _ = options.smallest_unit.insert(Unit::Nanosecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 123, 987, 500],); } @@ -122,46 +121,46 @@ fn basic_negative_floor_rounding_v2() { largest_unit: None, smallest_unit: None, increment: None, - rounding_mode: Some(TemporalRoundingMode::Floor), + rounding_mode: Some(RoundingMode::Floor), }; - let _ = options.smallest_unit.insert(TemporalUnit::Year); + let _ = options.smallest_unit.insert(Unit::Year); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-6, 0, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Month); + let _ = options.smallest_unit.insert(Unit::Month); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -8, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Week); + let _ = options.smallest_unit.insert(Unit::Week); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, -4, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Day); + let _ = options.smallest_unit.insert(Unit::Day); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -28, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Hour); + let _ = options.smallest_unit.insert(Unit::Hour); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -17, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Minute); + let _ = options.smallest_unit.insert(Unit::Minute); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -31, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Second); + let _ = options.smallest_unit.insert(Unit::Second); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -21, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Millisecond); + let _ = options.smallest_unit.insert(Unit::Millisecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -124, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Microsecond); + let _ = options.smallest_unit.insert(Unit::Microsecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -123, -988, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Nanosecond); + let _ = options.smallest_unit.insert(Unit::Nanosecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -123, -987, -500],); } @@ -190,46 +189,46 @@ fn basic_positive_ceil_rounding() { largest_unit: None, smallest_unit: None, increment: None, - rounding_mode: Some(TemporalRoundingMode::Ceil), + rounding_mode: Some(RoundingMode::Ceil), }; - let _ = options.smallest_unit.insert(TemporalUnit::Year); + let _ = options.smallest_unit.insert(Unit::Year); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[6, 0, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Month); + let _ = options.smallest_unit.insert(Unit::Month); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 8, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Week); + let _ = options.smallest_unit.insert(Unit::Week); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 4, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Day); + let _ = options.smallest_unit.insert(Unit::Day); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 28, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Hour); + let _ = options.smallest_unit.insert(Unit::Hour); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 17, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Minute); + let _ = options.smallest_unit.insert(Unit::Minute); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 31, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Second); + let _ = options.smallest_unit.insert(Unit::Second); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 21, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Millisecond); + let _ = options.smallest_unit.insert(Unit::Millisecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 124, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Microsecond); + let _ = options.smallest_unit.insert(Unit::Microsecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 123, 988, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Nanosecond); + let _ = options.smallest_unit.insert(Unit::Nanosecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 123, 987, 500],); } @@ -257,46 +256,46 @@ fn basic_negative_ceil_rounding() { largest_unit: None, smallest_unit: None, increment: None, - rounding_mode: Some(TemporalRoundingMode::Ceil), + rounding_mode: Some(RoundingMode::Ceil), }; - let _ = options.smallest_unit.insert(TemporalUnit::Year); + let _ = options.smallest_unit.insert(Unit::Year); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, 0, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Month); + let _ = options.smallest_unit.insert(Unit::Month); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Week); + let _ = options.smallest_unit.insert(Unit::Week); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, -3, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Day); + let _ = options.smallest_unit.insert(Unit::Day); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Hour); + let _ = options.smallest_unit.insert(Unit::Hour); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Minute); + let _ = options.smallest_unit.insert(Unit::Minute); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Second); + let _ = options.smallest_unit.insert(Unit::Second); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Millisecond); + let _ = options.smallest_unit.insert(Unit::Millisecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -123, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Microsecond); + let _ = options.smallest_unit.insert(Unit::Microsecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -123, -987, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Nanosecond); + let _ = options.smallest_unit.insert(Unit::Nanosecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -123, -987, -500],); } @@ -324,46 +323,46 @@ fn basic_positive_expand_rounding() { largest_unit: None, smallest_unit: None, increment: None, - rounding_mode: Some(TemporalRoundingMode::Expand), + rounding_mode: Some(RoundingMode::Expand), }; - let _ = options.smallest_unit.insert(TemporalUnit::Year); + let _ = options.smallest_unit.insert(Unit::Year); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[6, 0, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Month); + let _ = options.smallest_unit.insert(Unit::Month); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 8, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Week); + let _ = options.smallest_unit.insert(Unit::Week); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 4, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Day); + let _ = options.smallest_unit.insert(Unit::Day); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 28, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Hour); + let _ = options.smallest_unit.insert(Unit::Hour); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 17, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Minute); + let _ = options.smallest_unit.insert(Unit::Minute); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 31, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Second); + let _ = options.smallest_unit.insert(Unit::Second); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 21, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Millisecond); + let _ = options.smallest_unit.insert(Unit::Millisecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 124, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Microsecond); + let _ = options.smallest_unit.insert(Unit::Microsecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 123, 988, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Nanosecond); + let _ = options.smallest_unit.insert(Unit::Nanosecond); let result = get_round_result(&test_duration, relative_forward.clone(), options); assert_eq!(&result, &[5, 7, 0, 27, 16, 30, 20, 123, 987, 500],); } @@ -393,46 +392,46 @@ fn basic_negative_expand_rounding() { largest_unit: None, smallest_unit: None, increment: None, - rounding_mode: Some(TemporalRoundingMode::Expand), + rounding_mode: Some(RoundingMode::Expand), }; - let _ = options.smallest_unit.insert(TemporalUnit::Year); + let _ = options.smallest_unit.insert(Unit::Year); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-6, 0, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Month); + let _ = options.smallest_unit.insert(Unit::Month); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -8, 0, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Week); + let _ = options.smallest_unit.insert(Unit::Week); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, -4, 0, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Day); + let _ = options.smallest_unit.insert(Unit::Day); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -28, 0, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Hour); + let _ = options.smallest_unit.insert(Unit::Hour); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -17, 0, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Minute); + let _ = options.smallest_unit.insert(Unit::Minute); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -31, 0, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Second); + let _ = options.smallest_unit.insert(Unit::Second); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -21, 0, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Millisecond); + let _ = options.smallest_unit.insert(Unit::Millisecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -124, 0, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Microsecond); + let _ = options.smallest_unit.insert(Unit::Microsecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -123, -988, 0],); - let _ = options.smallest_unit.insert(TemporalUnit::Nanosecond); + let _ = options.smallest_unit.insert(Unit::Nanosecond); let result = get_round_result(&test_duration.negated(), relative_backward.clone(), options); assert_eq!(&result, &[-5, -7, 0, -27, -16, -30, -20, -123, -987, -500],); } @@ -454,9 +453,9 @@ fn rounding_increment_non_integer() { let mut options = RoundingOptions { largest_unit: None, - smallest_unit: Some(TemporalUnit::Day), + smallest_unit: Some(Unit::Day), increment: None, - rounding_mode: Some(TemporalRoundingMode::Expand), + rounding_mode: Some(RoundingMode::Expand), }; let _ = options @@ -624,7 +623,7 @@ fn round_relative_to_zoned_datetime() { ) .unwrap(); let options = RoundingOptions { - largest_unit: Some(TemporalUnit::Day), + largest_unit: Some(Unit::Day), smallest_unit: None, rounding_mode: None, increment: None, @@ -694,14 +693,11 @@ fn test_duration_total() { ..Default::default() }) .unwrap(); - assert_eq!(d1.total(TemporalUnit::Second, None).unwrap(), 469200.0); + assert_eq!(d1.total(Unit::Second, None).unwrap(), 469200.0); // How many 24-hour days is 123456789 seconds? let d2 = Duration::from_str("PT123456789S").unwrap(); - assert_eq!( - d2.total(TemporalUnit::Day, None).unwrap(), - 1428.8980208333332 - ); + assert_eq!(d2.total(Unit::Day, None).unwrap(), 1428.8980208333332); // Find totals in months, with and without taking DST into account let d3 = Duration::from_partial_duration(PartialDuration { @@ -716,16 +712,13 @@ fn test_duration_total() { ) .unwrap(); assert_eq!( - d3.total( - TemporalUnit::Month, - Some(RelativeTo::ZonedDateTime(relative_to)) - ) - .unwrap(), + d3.total(Unit::Month, Some(RelativeTo::ZonedDateTime(relative_to))) + .unwrap(), 3.7958333333333334 ); assert_eq!( d3.total( - TemporalUnit::Month, + Unit::Month, Some(RelativeTo::PlainDate( PlainDate::new(2020, 1, 1, Calendar::default()).unwrap() )) @@ -746,7 +739,7 @@ fn balance_subseconds() { ..Default::default() }) .unwrap(); - assert_eq!(pos.total(TemporalUnit::Second, None).unwrap(), 2.998998999); + assert_eq!(pos.total(Unit::Second, None).unwrap(), 2.998998999); // Test negative let neg = Duration::from_partial_duration(PartialDuration { @@ -756,7 +749,7 @@ fn balance_subseconds() { ..Default::default() }) .unwrap(); - assert_eq!(neg.total(TemporalUnit::Second, None).unwrap(), -2.998998999); + assert_eq!(neg.total(Unit::Second, None).unwrap(), -2.998998999); } // balances-days-up-to-both-years-and-months.js @@ -774,10 +767,7 @@ fn balance_days_up_to_both_years_and_months() { assert_eq!( two_years - .total( - TemporalUnit::Year, - Some(RelativeTo::PlainDate(relative_to.clone())) - ) + .total(Unit::Year, Some(RelativeTo::PlainDate(relative_to.clone()))) .unwrap(), 2.0 ); @@ -792,10 +782,7 @@ fn balance_days_up_to_both_years_and_months() { assert_eq!( two_years_negative - .total( - TemporalUnit::Year, - Some(RelativeTo::PlainDate(relative_to.clone())) - ) + .total(Unit::Year, Some(RelativeTo::PlainDate(relative_to.clone()))) .unwrap(), -2.0 ); diff --git a/src/builtins/core/calendar.rs b/src/builtins/core/calendar.rs index 3ca01b208..c835a0947 100644 --- a/src/builtins/core/calendar.rs +++ b/src/builtins/core/calendar.rs @@ -9,7 +9,7 @@ use crate::{ Duration, PlainDate, PlainDateTime, PlainMonthDay, PlainYearMonth, }, iso::IsoDate, - options::{ArithmeticOverflow, TemporalUnit}, + options::{ArithmeticOverflow, Unit}, parsers::parse_allowed_calendar_formats, TemporalError, TemporalResult, }; @@ -328,7 +328,7 @@ impl Calendar { // duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]). // 9. Let balanceResult be BalanceTimeDuration(norm, "day"). let (balance_days, _) = - TimeDuration::from_normalized(duration.time().to_normalized(), TemporalUnit::Day)?; + TimeDuration::from_normalized(duration.time().to_normalized(), Unit::Day)?; // 10. Let result be ? AddISODate(date.[[ISOYear]], date.[[ISOMonth]], date.[[ISODay]], duration.[[Years]], // duration.[[Months]], duration.[[Weeks]], duration.[[Days]] + balanceResult.[[Days]], overflow). @@ -353,7 +353,7 @@ impl Calendar { &self, one: &IsoDate, two: &IsoDate, - largest_unit: TemporalUnit, + largest_unit: Unit, ) -> TemporalResult { if self.is_iso() { let date_duration = one.diff_iso_date(two, largest_unit)?; @@ -673,7 +673,7 @@ impl From for Calendar { #[cfg(test)] mod tests { - use crate::{iso::IsoDate, options::TemporalUnit}; + use crate::{iso::IsoDate, options::Unit}; use core::str::FromStr; use super::Calendar; @@ -952,9 +952,7 @@ mod tests { for test in tests { let first = IsoDate::new_unchecked(test.0 .0, test.0 .1, test.0 .2); let second = IsoDate::new_unchecked(test.1 .0, test.1 .1, test.1 .2); - let result = calendar - .date_until(&first, &second, TemporalUnit::Year) - .unwrap(); + let result = calendar.date_until(&first, &second, Unit::Year).unwrap(); assert_eq!( result.years().0 as i32, test.2 .0, diff --git a/src/builtins/core/date.rs b/src/builtins/core/date.rs index 78ab184c2..fbee74cae 100644 --- a/src/builtins/core/date.rs +++ b/src/builtins/core/date.rs @@ -8,7 +8,7 @@ use crate::{ iso::{IsoDate, IsoDateTime, IsoTime}, options::{ ArithmeticOverflow, DifferenceOperation, DifferenceSettings, Disambiguation, - DisplayCalendar, ResolvedRoundingOptions, TemporalUnit, UnitGroup, + DisplayCalendar, ResolvedRoundingOptions, Unit, UnitGroup, }, parsers::{parse_date_time, IxdtfStringBuilder}, primitive::FiniteF64, @@ -236,7 +236,7 @@ impl PlainDate { // 6. Let days be duration.[[Days]] + BalanceTimeDuration(norm, // "day").[[Days]]. let days = duration.days().checked_add( - &TimeDuration::from_normalized(duration.time().to_normalized(), TemporalUnit::Day)?.0, + &TimeDuration::from_normalized(duration.time().to_normalized(), Unit::Day)?.0, )?; // 7. Let result be ? AddISODate(plainDate.[[ISOYear]], plainDate.[[ISOMonth]], plainDate.[[ISODay]], 0, 0, 0, days, overflow). @@ -265,7 +265,7 @@ impl PlainDate { pub(crate) fn internal_diff_date( &self, other: &Self, - largest_unit: TemporalUnit, + largest_unit: Unit, ) -> TemporalResult { if self.iso.year == other.iso.year && self.iso.month == other.iso.month @@ -274,7 +274,7 @@ impl PlainDate { return Ok(Duration::default()); } - if largest_unit == TemporalUnit::Day { + if largest_unit == Unit::Day { let days = self.days_until(other); return Ok(Duration::from(DateDuration::new( FiniteF64::default(), @@ -310,8 +310,8 @@ impl PlainDate { settings, op, UnitGroup::Date, - TemporalUnit::Day, - TemporalUnit::Day, + Unit::Day, + Unit::Day, )?; // 6. If temporalDate.[[ISOYear]] = other.[[ISOYear]], and temporalDate.[[ISOMonth]] = other.[[ISOMonth]], @@ -330,7 +330,7 @@ impl PlainDate { let mut duration = NormalizedDurationRecord::from_date_duration(*result.date())?; // 11. If settings.[[SmallestUnit]] is "day" and settings.[[RoundingIncrement]] = 1, let roundingGranularityIsNoop be true; else let roundingGranularityIsNoop be false. let rounding_granularity_is_noop = - resolved.smallest_unit == TemporalUnit::Day && resolved.increment.get() == 1; + resolved.smallest_unit == Unit::Day && resolved.increment.get() == 1; // 12. If roundingGranularityIsNoop is false, then if !rounding_granularity_is_noop { // a. Let destEpochNs be GetUTCEpochNanoseconds(other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], 0, 0, 0, 0, 0, 0). @@ -348,7 +348,7 @@ impl PlainDate { resolved, )? } - let result = Duration::from_normalized(duration, TemporalUnit::Day)?; + let result = Duration::from_normalized(duration, Unit::Day)?; // 13. Return ! CreateTemporalDuration(sign × duration.[[Years]], sign × duration.[[Months]], sign × duration.[[Weeks]], sign × duration.[[Days]], 0, 0, 0, 0, 0, 0). match op { DifferenceOperation::Until => Ok(result), diff --git a/src/builtins/core/datetime.rs b/src/builtins/core/datetime.rs index 307a48964..63a97a694 100644 --- a/src/builtins/core/datetime.rs +++ b/src/builtins/core/datetime.rs @@ -9,8 +9,8 @@ use crate::{ iso::{IsoDate, IsoDateTime, IsoTime}, options::{ ArithmeticOverflow, DifferenceOperation, DifferenceSettings, Disambiguation, - DisplayCalendar, ResolvedRoundingOptions, RoundingOptions, TemporalUnit, - ToStringRoundingOptions, UnitGroup, + DisplayCalendar, ResolvedRoundingOptions, RoundingOptions, ToStringRoundingOptions, Unit, + UnitGroup, }, parsers::{parse_date_time, IxdtfStringBuilder}, primitive::FiniteF64, @@ -158,8 +158,8 @@ impl PlainDateTime { settings, op, UnitGroup::DateTime, - TemporalUnit::Day, - TemporalUnit::Nanosecond, + Unit::Day, + Unit::Nanosecond, )?; // Step 7-8 combined. @@ -201,7 +201,7 @@ impl PlainDateTime { .diff(&other.iso, &self.calendar, options.largest_unit)?; // 4. If smallestUnit is nanosecond and roundingIncrement = 1, return diff. - if options.smallest_unit == TemporalUnit::Nanosecond && options.increment.get() == 1 { + if options.smallest_unit == Unit::Nanosecond && options.increment.get() == 1 { return Ok(diff); } @@ -217,11 +217,7 @@ impl PlainDateTime { } // 5.5.14 DifferencePlainDateTimeWithTotal ( isoDateTime1, isoDateTime2, calendar, unit ) - pub(crate) fn diff_dt_with_total( - &self, - other: &Self, - unit: TemporalUnit, - ) -> TemporalResult { + pub(crate) fn diff_dt_with_total(&self, other: &Self, unit: Unit) -> TemporalResult { // 1. If CompareISODateTime(isoDateTime1, isoDateTime2) = 0, then // a. Return 0. if matches!(self.iso.cmp(&other.iso), Ordering::Equal) { @@ -234,7 +230,7 @@ impl PlainDateTime { // 3. Let diff be DifferenceISODateTime(isoDateTime1, isoDateTime2, calendar, unit). let diff = self.iso.diff(&other.iso, &self.calendar, unit)?; // 4. If unit is nanosecond, return diff.[[Time]]. - if unit == TemporalUnit::Nanosecond { + if unit == Unit::Nanosecond { return FiniteF64::try_from(diff.normalized_time_duration().0); } // 5. Let destEpochNs be GetUTCEpochNanoseconds(isoDateTime2). @@ -799,8 +795,8 @@ mod tests { }, iso::{IsoDate, IsoDateTime, IsoTime}, options::{ - DifferenceSettings, DisplayCalendar, RoundingIncrement, RoundingOptions, - TemporalRoundingMode, TemporalUnit, ToStringRoundingOptions, + DifferenceSettings, DisplayCalendar, RoundingIncrement, RoundingMode, RoundingOptions, + ToStringRoundingOptions, Unit, }, parsers::Precision, primitive::FiniteF64, @@ -1099,9 +1095,9 @@ mod tests { } fn create_diff_setting( - smallest: TemporalUnit, + smallest: Unit, increment: u32, - rounding_mode: TemporalRoundingMode, + rounding_mode: RoundingMode, ) -> DifferenceSettings { DifferenceSettings { largest_unit: None, @@ -1120,14 +1116,13 @@ mod tests { PlainDateTime::try_new(2021, 9, 7, 12, 39, 40, 987, 654, 321, Calendar::default()) .unwrap(); - let settings = create_diff_setting(TemporalUnit::Hour, 3, TemporalRoundingMode::HalfExpand); + let settings = create_diff_setting(Unit::Hour, 3, RoundingMode::HalfExpand); let result = earlier.until(&later, settings).unwrap(); assert_eq!(result.days(), 973.0); assert_eq!(result.hours(), 3.0); - let settings = - create_diff_setting(TemporalUnit::Minute, 30, TemporalRoundingMode::HalfExpand); + let settings = create_diff_setting(Unit::Minute, 30, RoundingMode::HalfExpand); let result = earlier.until(&later, settings).unwrap(); assert_eq!(result.days(), 973.0); @@ -1144,14 +1139,13 @@ mod tests { PlainDateTime::try_new(2021, 9, 7, 12, 39, 40, 987, 654, 321, Calendar::default()) .unwrap(); - let settings = create_diff_setting(TemporalUnit::Hour, 3, TemporalRoundingMode::HalfExpand); + let settings = create_diff_setting(Unit::Hour, 3, RoundingMode::HalfExpand); let result = later.since(&earlier, settings).unwrap(); assert_eq!(result.days(), 973.0); assert_eq!(result.hours(), 3.0); - let settings = - create_diff_setting(TemporalUnit::Minute, 30, TemporalRoundingMode::HalfExpand); + let settings = create_diff_setting(Unit::Minute, 30, RoundingMode::HalfExpand); let result = later.since(&earlier, settings).unwrap(); assert_eq!(result.days(), 973.0); @@ -1174,7 +1168,7 @@ mod tests { assert_eq!(dt.nanosecond(), expected.8); }; - let gen_rounding_options = |smallest: TemporalUnit, increment: u32| -> RoundingOptions { + let gen_rounding_options = |smallest: Unit, increment: u32| -> RoundingOptions { RoundingOptions { largest_unit: None, smallest_unit: Some(smallest), @@ -1186,33 +1180,27 @@ mod tests { PlainDateTime::try_new(1976, 11, 18, 14, 23, 30, 123, 456, 789, Calendar::default()) .unwrap(); - let result = dt - .round(gen_rounding_options(TemporalUnit::Hour, 4)) - .unwrap(); + let result = dt.round(gen_rounding_options(Unit::Hour, 4)).unwrap(); assert_datetime(result, (1976, 11, 18, 16, 0, 0, 0, 0, 0)); - let result = dt - .round(gen_rounding_options(TemporalUnit::Minute, 15)) - .unwrap(); + let result = dt.round(gen_rounding_options(Unit::Minute, 15)).unwrap(); assert_datetime(result, (1976, 11, 18, 14, 30, 0, 0, 0, 0)); - let result = dt - .round(gen_rounding_options(TemporalUnit::Second, 30)) - .unwrap(); + let result = dt.round(gen_rounding_options(Unit::Second, 30)).unwrap(); assert_datetime(result, (1976, 11, 18, 14, 23, 30, 0, 0, 0)); let result = dt - .round(gen_rounding_options(TemporalUnit::Millisecond, 10)) + .round(gen_rounding_options(Unit::Millisecond, 10)) .unwrap(); assert_datetime(result, (1976, 11, 18, 14, 23, 30, 120, 0, 0)); let result = dt - .round(gen_rounding_options(TemporalUnit::Microsecond, 10)) + .round(gen_rounding_options(Unit::Microsecond, 10)) .unwrap(); assert_datetime(result, (1976, 11, 18, 14, 23, 30, 123, 460, 0)); let result = dt - .round(gen_rounding_options(TemporalUnit::Nanosecond, 10)) + .round(gen_rounding_options(Unit::Nanosecond, 10)) .unwrap(); assert_datetime(result, (1976, 11, 18, 14, 23, 30, 123, 456, 790)); } @@ -1226,7 +1214,7 @@ mod tests { let bad_options = RoundingOptions { largest_unit: None, smallest_unit: None, - rounding_mode: Some(TemporalRoundingMode::Ceil), + rounding_mode: Some(RoundingMode::Ceil), increment: Some(RoundingIncrement::ONE), }; diff --git a/src/builtins/core/duration.rs b/src/builtins/core/duration.rs index 6cfcf12d6..7fc6bf790 100644 --- a/src/builtins/core/duration.rs +++ b/src/builtins/core/duration.rs @@ -5,7 +5,7 @@ use crate::{ iso::{IsoDateTime, IsoTime}, options::{ ArithmeticOverflow, RelativeTo, ResolvedRoundingOptions, RoundingIncrement, - RoundingOptions, TemporalUnit, ToStringRoundingOptions, + RoundingOptions, ToStringRoundingOptions, Unit, }, parsers::{FormattableDateDuration, FormattableDuration, FormattableTimeDuration, Precision}, primitive::FiniteF64, @@ -130,7 +130,7 @@ impl Duration { #[inline] pub(crate) fn from_normalized( duration_record: NormalizedDurationRecord, - largest_unit: TemporalUnit, + largest_unit: Unit, ) -> TemporalResult { let (overflow_day, time) = TimeDuration::from_normalized( duration_record.normalized_time_duration(), @@ -175,15 +175,15 @@ impl Duration { self.date().fields().iter().all(|x| x == &0.0) } - /// Returns the `TemporalUnit` corresponding to the largest non-zero field. + /// Returns the `Unit` corresponding to the largest non-zero field. #[inline] - pub(crate) fn default_largest_unit(&self) -> TemporalUnit { + pub(crate) fn default_largest_unit(&self) -> Unit { self.fields() .iter() .enumerate() .find(|x| x.1 != &0.0) - .map(|x| TemporalUnit::from(10 - x.0)) - .unwrap_or(TemporalUnit::Nanosecond) + .map(|x| Unit::from(10 - x.0)) + .unwrap_or(Unit::Nanosecond) } } @@ -291,7 +291,7 @@ impl Duration { let largest_unit_2 = other.default_largest_unit(); // 10. Let duration1 be ToInternalDurationRecord(one). // 11. Let duration2 be ToInternalDurationRecord(two). - // 12. If zonedRelativeTo is not undefined, and either TemporalUnitCategory(largestUnit1) or TemporalUnitCategory(largestUnit2) is date, then + // 12. If zonedRelativeTo is not undefined, and either UnitCategory(largestUnit1) or UnitCategory(largestUnit2) is date, then if let Some(RelativeTo::ZonedDateTime(zdt)) = relative_to.as_ref() { if largest_unit_1.is_date_unit() || largest_unit_2.is_date_unit() { // a. Let timeZone be zonedRelativeTo.[[TimeZone]]. @@ -470,7 +470,7 @@ impl Duration { let largest_one = self.default_largest_unit(); // 24. Let largestUnit2 be DefaultTemporalLargestUnit(y2, mon2, w2, d2, h2, min2, s2, ms2, mus2). let largest_two = other.default_largest_unit(); - // 25. Let largestUnit be LargerOfTwoTemporalUnits(largestUnit1, largestUnit2). + // 25. Let largestUnit be LargerOfTwoUnits(largestUnit1, largestUnit2). let largest_unit = largest_one.max(largest_two); // 26. Let norm1 be NormalizeTimeDuration(h1, min1, s1, ms1, mus1, ns1). let norm_one = NormalizedTimeDuration::from_time_duration(self.time()); @@ -512,8 +512,8 @@ impl Duration { ) -> TemporalResult { // NOTE: Steps 1-14 seem to be implementation specific steps. // 14. Let roundingIncrement be ? ToTemporalRoundingIncrement(roundTo). - // 15. Let roundingMode be ? ToTemporalRoundingMode(roundTo, "halfExpand"). - // 16. Let smallestUnit be ? GetTemporalUnit(roundTo, "smallestUnit", DATETIME, undefined). + // 15. Let roundingMode be ? ToRoundingMode(roundTo, "halfExpand"). + // 16. Let smallestUnit be ? GetUnit(roundTo, "smallestUnit", DATETIME, undefined). // 17. If smallestUnit is undefined, then // a. Set smallestUnitPresent to false. // b. Set smallestUnit to "nanosecond". @@ -521,13 +521,13 @@ impl Duration { // duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], // duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], // duration.[[Microseconds]]). - // 19. Let defaultLargestUnit be LargerOfTwoTemporalUnits(existingLargestUnit, smallestUnit). + // 19. Let defaultLargestUnit be LargerOfTwoUnits(existingLargestUnit, smallestUnit). // 20. If largestUnit is undefined, then // a. Set largestUnitPresent to false. // b. Set largestUnit to defaultLargestUnit. // 21. Else if largestUnit is "auto", then // a. Set largestUnit to defaultLargestUnit. - // 23. If LargerOfTwoTemporalUnits(largestUnit, smallestUnit) is not largestUnit, throw a RangeError exception. + // 23. If LargerOfTwoUnits(largestUnit, smallestUnit) is not largestUnit, throw a RangeError exception. // 24. Let maximum be MaximumTemporalDurationRoundingIncrement(smallestUnit). // 25. If maximum is not undefined, perform ? ValidateTemporalRoundingIncrement(roundingIncrement, maximum, false). let existing_largest_unit = self.default_largest_unit(); @@ -686,7 +686,7 @@ impl Duration { /// Returns the total of the `Duration` pub fn total_with_provider( &self, - unit: TemporalUnit, + unit: Unit, relative_to: Option, provider: &impl TimeZoneProvider, // Review question what is the return type of duration.prototye.total? @@ -763,8 +763,7 @@ impl Duration { /// Returns the `Duration` as a formatted string pub fn as_temporal_string(&self, options: ToStringRoundingOptions) -> TemporalResult { - if options.smallest_unit == Some(TemporalUnit::Hour) - || options.smallest_unit == Some(TemporalUnit::Minute) + if options.smallest_unit == Some(Unit::Hour) || options.smallest_unit == Some(Unit::Minute) { return Err(TemporalError::range().with_message( "string rounding options cannot have hour or minute smallest unit.", @@ -772,7 +771,7 @@ impl Duration { } let resolved_options = options.resolve()?; - if resolved_options.smallest_unit == TemporalUnit::Nanosecond + if resolved_options.smallest_unit == Unit::Nanosecond && resolved_options.increment == RoundingIncrement::ONE { let duration = duration_to_formattable(self, resolved_options.precision)?; @@ -794,8 +793,8 @@ impl Duration { .round(FiniteF64::default(), rounding_options)?; // 14. Set internalDuration to CombineDateAndTimeDuration(internalDuration.[[Date]], timeDuration). let norm = NormalizedDurationRecord::new(norm.date(), rounded.normalized_time_duration())?; - // 15. Let roundedLargestUnit be LargerOfTwoTemporalUnits(largestUnit, second). - let rounded_largest = largest.max(TemporalUnit::Second); + // 15. Let roundedLargestUnit be LargerOfTwoUnits(largestUnit, second). + let rounded_largest = largest.max(Unit::Second); // 16. Let roundedDuration be ? TemporalDurationFromInternal(internalDuration, roundedLargestUnit). let rounded = Self::from_normalized(norm, rounded_largest)?; diff --git a/src/builtins/core/duration/normalized.rs b/src/builtins/core/duration/normalized.rs index 92f212845..40c050ea2 100644 --- a/src/builtins/core/duration/normalized.rs +++ b/src/builtins/core/duration/normalized.rs @@ -9,7 +9,7 @@ use crate::{ iso::{IsoDate, IsoDateTime}, options::{ ArithmeticOverflow, Disambiguation, ResolvedRoundingOptions, RoundingIncrement, - TemporalRoundingMode, TemporalUnit, + RoundingMode, Unit, }, primitive::FiniteF64, provider::TimeZoneProvider, @@ -140,7 +140,7 @@ impl NormalizedTimeDuration { // 1. Assert: IsCalendarUnit(unit) is false. let (days, norm, total) = match options.smallest_unit { // 2. If unit is "day", then - TemporalUnit::Day => { + Unit::Day => { // a. Let fractionalDays be days + DivideNormalizedTimeDuration(norm, nsPerDay). let fractional_days = days.checked_add(&FiniteF64(self.as_fractional_days()))?; // b. Set days to RoundNumberToIncrement(fractionalDays, increment, roundingMode). @@ -158,12 +158,12 @@ impl NormalizedTimeDuration { ) } // 3. Else, - TemporalUnit::Hour - | TemporalUnit::Minute - | TemporalUnit::Second - | TemporalUnit::Millisecond - | TemporalUnit::Microsecond - | TemporalUnit::Nanosecond => { + Unit::Hour + | Unit::Minute + | Unit::Second + | Unit::Millisecond + | Unit::Microsecond + | Unit::Nanosecond => { // a. Assert: The value in the "Category" column of the row of Table 22 whose "Singular" column contains unit, is time. // b. Let divisor be the value in the "Length in Nanoseconds" column of the row of Table 22 whose "Singular" column contains unit. let divisor = options.smallest_unit.as_nanoseconds().temporal_unwrap()?; @@ -199,7 +199,7 @@ impl NormalizedTimeDuration { /// Equivalent: 7.5.31 TotalTimeDuration ( timeDuration, unit ) /// TODO Fix: Arithemtic on floating point numbers is not safe. According to NOTE 2 in the spec - pub(crate) fn total(&self, unit: TemporalUnit) -> TemporalResult { + pub(crate) fn total(&self, unit: Unit) -> TemporalResult { let time_duration = self.0; // 1. Let divisor be the value in the "Length in Nanoseconds" column of the row of Table 21 whose "Value" column contains unit. let unit_nanoseconds = unit.as_nanoseconds().temporal_unwrap()?; @@ -213,7 +213,7 @@ impl NormalizedTimeDuration { pub(super) fn round_inner( &self, increment: NonZeroU128, - mode: TemporalRoundingMode, + mode: RoundingMode, ) -> TemporalResult { let rounded = IncrementRounder::::from_signed_num(self.0, increment)?.round(mode); if rounded.abs() > MAX_TIME_DURATION { @@ -346,13 +346,13 @@ impl NormalizedDurationRecord { // NOTE: r2 may never be used...need to test. let (r1, r2, start_duration, end_duration) = match options.smallest_unit { // 1. If unit is "year", then - TemporalUnit::Year => { + Unit::Year => { // a. Let years be RoundNumberToIncrement(duration.[[Years]], increment, "trunc"). let years = IncrementRounder::from_signed_num( self.date().years.0, options.increment.as_extended_increment(), )? - .round(TemporalRoundingMode::Trunc); + .round(RoundingMode::Trunc); // b. Let r1 be years. let r1 = years; // c. Let r2 be years + increment × sign. @@ -378,13 +378,13 @@ impl NormalizedDurationRecord { ) } // 2. Else if unit is "month", then - TemporalUnit::Month => { + Unit::Month => { // a. Let months be RoundNumberToIncrement(duration.[[Months]], increment, "trunc"). let months = IncrementRounder::from_signed_num( self.date().months.0, options.increment.as_extended_increment(), )? - .round(TemporalRoundingMode::Trunc); + .round(RoundingMode::Trunc); // b. Let r1 be months. let r1 = months; // c. Let r2 be months + increment × sign. @@ -410,7 +410,7 @@ impl NormalizedDurationRecord { ) } // 3. Else if unit is "week", then - TemporalUnit::Week => { + Unit::Week => { // TODO: Reconcile potential overflow on years as i32. `ValidateDuration` requires years, months, weeks to be abs(x) <= 2^32 // a. Let isoResult1 be BalanceISODate(dateTime.[[Year]] + duration.[[Years]], // dateTime.[[Month]] + duration.[[Months]], dateTime.[[Day]]). @@ -449,15 +449,14 @@ impl NormalizedDurationRecord { // e. Let untilOptions be OrdinaryObjectCreate(null). // f. Perform ! CreateDataPropertyOrThrow(untilOptions, "largestUnit", "week"). // g. Let untilResult be ? DifferenceDate(calendarRec, weeksStart, weeksEnd, untilOptions). - let until_result = - weeks_start.internal_diff_date(&weeks_end, TemporalUnit::Week)?; + let until_result = weeks_start.internal_diff_date(&weeks_end, Unit::Week)?; // h. Let weeks be RoundNumberToIncrement(duration.[[Weeks]] + untilResult.[[Weeks]], increment, "trunc"). let weeks = IncrementRounder::from_signed_num( self.date().weeks.checked_add(&until_result.weeks())?.0, options.increment.as_extended_increment(), )? - .round(TemporalRoundingMode::Trunc); + .round(RoundingMode::Trunc); // i. Let r1 be weeks. let r1 = weeks; @@ -483,7 +482,7 @@ impl NormalizedDurationRecord { )?, ) } - TemporalUnit::Day => { + Unit::Day => { // 4. Else, // a. Assert: unit is "day". // b. Let days be RoundNumberToIncrement(duration.[[Days]], increment, "trunc"). @@ -491,7 +490,7 @@ impl NormalizedDurationRecord { self.date().days.0, options.increment.as_extended_increment(), )? - .round(TemporalRoundingMode::Trunc); + .round(RoundingMode::Trunc); // c. Let r1 be days. let r1 = days; // d. Let r2 be days + increment × sign. @@ -765,8 +764,8 @@ impl NormalizedDurationRecord { let mut days = 0; // 15. Let remainder be roundedNorm. let mut remainder = rounded_norm; - // 16. If LargerOfTwoTemporalUnits(largestUnit, "day") is largestUnit, then - if options.largest_unit.max(TemporalUnit::Day) == options.largest_unit { + // 16. If LargerOfTwoUnits(largestUnit, "day") is largestUnit, then + if options.largest_unit.max(Unit::Day) == options.largest_unit { // a. Set days to roundedWholeDays. days = rounded_whole_days; // b. Set remainder to remainder(roundedFractionalDays, 1) × nsPerDay. @@ -801,14 +800,14 @@ impl NormalizedDurationRecord { nudge_epoch_ns: i128, date_time: &PlainDateTime, tz: Option<(&TimeZone, &impl TimeZoneProvider)>, - largest_unit: TemporalUnit, - smallest_unit: TemporalUnit, + largest_unit: Unit, + smallest_unit: Unit, ) -> TemporalResult { // Assert: The value in the "Category" column of the row of Table 22 whose "Singular" column contains largestUnit, is date. // 2. Assert: The value in the "Category" column of the row of Table 22 whose "Singular" column contains smallestUnit, is date. let mut duration = *self; // 3. If smallestUnit is "year", return duration. - if smallest_unit == TemporalUnit::Year { + if smallest_unit == Unit::Year { return Ok(duration); } @@ -819,17 +818,17 @@ impl NormalizedDurationRecord { let mut smallest_unit = smallest_unit + 1; // 7. Let done be false. // 8. Repeat, while unitIndex ≤ largestUnitIndex and done is false, - while smallest_unit != TemporalUnit::Auto && largest_unit < smallest_unit { + while smallest_unit != Unit::Auto && largest_unit < smallest_unit { // a. Let unit be the value in the "Singular" column of Table 22 in the row whose ordinal index is unitIndex. // b. If unit is not "week", or largestUnit is "week", then - if smallest_unit == TemporalUnit::Week || largest_unit != TemporalUnit::Week { + if smallest_unit == Unit::Week || largest_unit != Unit::Week { smallest_unit = smallest_unit + 1; continue; } let end_duration = match smallest_unit { // i. If unit is "year", then - TemporalUnit::Year => { + Unit::Year => { // 1. Let years be duration.[[Years]] + sign. // 2. Let endDuration be ? CreateNormalizedDurationRecord(years, 0, 0, 0, ZeroTimeDuration()). DateDuration::new( @@ -843,7 +842,7 @@ impl NormalizedDurationRecord { )? } // ii. Else if unit is "month", then - TemporalUnit::Month => { + Unit::Month => { // 1. Let months be duration.[[Months]] + sign. // 2. Let endDuration be ? CreateNormalizedDurationRecord(duration.[[Years]], months, 0, 0, ZeroTimeDuration()). DateDuration::new( @@ -857,7 +856,7 @@ impl NormalizedDurationRecord { )? } // iii. Else if unit is "week", then - TemporalUnit::Week => { + Unit::Week => { // 1. Let weeks be duration.[[Weeks]] + sign. // 2. Let endDuration be ? CreateNormalizedDurationRecord(duration.[[Years]], duration.[[Months]], weeks, 0, ZeroTimeDuration()). DateDuration::new( @@ -871,7 +870,7 @@ impl NormalizedDurationRecord { )? } // iv. Else, - TemporalUnit::Day => { + Unit::Day => { // 1. Assert: unit is "day". // 2. Let days be duration.[[Days]] + sign. // 3. Let endDuration be ? CreateNormalizedDurationRecord(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], days, ZeroTimeDuration()). @@ -945,7 +944,7 @@ impl NormalizedDurationRecord { // 2. If IsCalendarUnit(smallestUnit) is true, set irregularLengthUnit to true. // 3. If timeZoneRec is not unset and smallestUnit is "day", set irregularLengthUnit to true. let irregular_unit = options.smallest_unit.is_calendar_unit() - || (timezone_record.is_some() && options.smallest_unit == TemporalUnit::Day); + || (timezone_record.is_some() && options.smallest_unit == Unit::Day); // 4. If DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], NormalizedTimeDurationSign(duration.[[NormalizedTime]]), 0, 0, 0, 0, 0) < 0, let sign be -1; else let sign be 1. let sign = self.sign()?; @@ -968,9 +967,9 @@ impl NormalizedDurationRecord { let mut duration = nudge_result.normalized; // 9. If nudgeResult.[[DidExpandCalendarUnit]] is true and smallestUnit is not "week", then - if nudge_result.expanded && options.smallest_unit != TemporalUnit::Week { - // a. Let startUnit be LargerOfTwoTemporalUnits(smallestUnit, "day"). - let start_unit = options.smallest_unit.max(TemporalUnit::Day); + if nudge_result.expanded && options.smallest_unit != Unit::Week { + // a. Let startUnit be LargerOfTwoUnits(smallestUnit, "day"). + let start_unit = options.smallest_unit.max(Unit::Day); // b. Set duration to ? BubbleRelativeDuration(sign, duration, nudgeResult.[[NudgedEpochNs]], dateTime, calendarRec, timeZoneRec, largestUnit, startUnit). duration = duration.bubble_relative_duration( sign, @@ -991,10 +990,10 @@ impl NormalizedDurationRecord { dest_epoch_ns: i128, dt: &PlainDateTime, tz: Option<(&TimeZone, &impl TimeZoneProvider)>, - unit: TemporalUnit, + unit: Unit, ) -> TemporalResult { // 1. If IsCalendarUnit(unit) is true, or timeZone is not unset and unit is day, then - if unit.is_calendar_unit() || (tz.is_some() && unit == TemporalUnit::Day) { + if unit.is_calendar_unit() || (tz.is_some() && unit == Unit::Day) { // a. Let sign be InternalDurationSign(duration). let sign = self.sign()?; // b. Let record be ? NudgeToCalendarUnit(sign, duration, destEpochNs, isoDateTime, timeZone, calendar, 1, unit, trunc). @@ -1007,7 +1006,7 @@ impl NormalizedDurationRecord { largest_unit: unit, smallest_unit: unit, increment: RoundingIncrement::default(), - rounding_mode: TemporalRoundingMode::Trunc, + rounding_mode: RoundingMode::Trunc, }, )?; diff --git a/src/builtins/core/duration/time.rs b/src/builtins/core/duration/time.rs index b45834192..dc742f638 100644 --- a/src/builtins/core/duration/time.rs +++ b/src/builtins/core/duration/time.rs @@ -1,8 +1,7 @@ //! An implementation of `TimeDuration` and it's methods. use crate::{ - options::TemporalUnit, primitive::FiniteF64, temporal_assert, Sign, TemporalError, - TemporalResult, + options::Unit, primitive::FiniteF64, temporal_assert, Sign, TemporalError, TemporalResult, }; use super::{duration_sign, is_valid_duration, normalized::NormalizedTimeDuration}; @@ -64,7 +63,7 @@ impl TimeDuration { /// - Will error if provided duration is invalid pub(crate) fn from_normalized( norm: NormalizedTimeDuration, - largest_unit: TemporalUnit, + largest_unit: Unit, ) -> TemporalResult<(FiniteF64, Self)> { // 1. Let days, hours, minutes, seconds, milliseconds, and microseconds be 0. let mut days = 0; @@ -81,7 +80,7 @@ impl TimeDuration { match largest_unit { // 4. If largestUnit is "year", "month", "week", or "day", then - TemporalUnit::Year | TemporalUnit::Month | TemporalUnit::Week | TemporalUnit::Day => { + Unit::Year | Unit::Month | Unit::Week | Unit::Day => { // a. Set microseconds to floor(nanoseconds / 1000). // b. Set nanoseconds to nanoseconds modulo 1000. (microseconds, nanoseconds) = nanoseconds.div_rem_euclid(&1_000); @@ -107,7 +106,7 @@ impl TimeDuration { (days, hours) = hours.div_rem_euclid(&24); } // 5. Else if largestUnit is "hour", then - TemporalUnit::Hour => { + Unit::Hour => { // a. Set microseconds to floor(nanoseconds / 1000). // b. Set nanoseconds to nanoseconds modulo 1000. (microseconds, nanoseconds) = nanoseconds.div_rem_euclid(&1_000); @@ -129,7 +128,7 @@ impl TimeDuration { (hours, minutes) = minutes.div_rem_euclid(&60); } // 6. Else if largestUnit is "minute", then - TemporalUnit::Minute => { + Unit::Minute => { // a. Set microseconds to floor(nanoseconds / 1000). // b. Set nanoseconds to nanoseconds modulo 1000. (microseconds, nanoseconds) = nanoseconds.div_rem_euclid(&1_000); @@ -147,7 +146,7 @@ impl TimeDuration { (minutes, seconds) = seconds.div_rem_euclid(&60); } // 7. Else if largestUnit is "second", then - TemporalUnit::Second => { + Unit::Second => { // a. Set microseconds to floor(nanoseconds / 1000). // b. Set nanoseconds to nanoseconds modulo 1000. (microseconds, nanoseconds) = nanoseconds.div_rem_euclid(&1_000); @@ -161,7 +160,7 @@ impl TimeDuration { (seconds, milliseconds) = milliseconds.div_rem_euclid(&1_000); } // 8. Else if largestUnit is "millisecond", then - TemporalUnit::Millisecond => { + Unit::Millisecond => { // a. Set microseconds to floor(nanoseconds / 1000). // b. Set nanoseconds to nanoseconds modulo 1000. (microseconds, nanoseconds) = nanoseconds.div_rem_euclid(&1_000); @@ -171,14 +170,14 @@ impl TimeDuration { (milliseconds, microseconds) = microseconds.div_rem_euclid(&1_000); } // 9. Else if largestUnit is "microsecond", then - TemporalUnit::Microsecond => { + Unit::Microsecond => { // a. Set microseconds to floor(nanoseconds / 1000). // b. Set nanoseconds to nanoseconds modulo 1000. (microseconds, nanoseconds) = nanoseconds.div_rem_euclid(&1_000); } // 10. Else, // a. Assert: largestUnit is "nanosecond". - _ => temporal_assert!(largest_unit == TemporalUnit::Nanosecond), + _ => temporal_assert!(largest_unit == Unit::Nanosecond), } // NOTE(nekevss): `mul_add` is essentially the Rust's implementation of `std::fma()`, so that's handy, but diff --git a/src/builtins/core/instant.rs b/src/builtins/core/instant.rs index 86b959e09..df78cc5c3 100644 --- a/src/builtins/core/instant.rs +++ b/src/builtins/core/instant.rs @@ -10,7 +10,7 @@ use crate::{ iso::IsoDateTime, options::{ DifferenceOperation, DifferenceSettings, DisplayOffset, ResolvedRoundingOptions, - RoundingOptions, TemporalUnit, ToStringRoundingOptions, UnitGroup, + RoundingOptions, ToStringRoundingOptions, Unit, UnitGroup, }, parsers::{parse_instant, IxdtfStringBuilder}, primitive::FiniteF64, @@ -89,8 +89,8 @@ impl Instant { options, op, UnitGroup::Time, - TemporalUnit::Second, - TemporalUnit::Nanosecond, + Unit::Second, + Unit::Nanosecond, )?; // Below are the steps from Difference Instant. @@ -116,19 +116,17 @@ impl Instant { ) -> TemporalResult { let increment = resolved_options.increment.as_extended_increment(); let increment = match resolved_options.smallest_unit { - TemporalUnit::Hour => increment + Unit::Hour => increment .checked_mul(NonZeroU128::new(NANOSECONDS_PER_HOUR as u128).temporal_unwrap()?), - TemporalUnit::Minute => increment + Unit::Minute => increment .checked_mul(NonZeroU128::new(NANOSECONDS_PER_MINUTE as u128).temporal_unwrap()?), - TemporalUnit::Second => increment + Unit::Second => increment .checked_mul(NonZeroU128::new(NANOSECONDS_PER_SECOND as u128).temporal_unwrap()?), - TemporalUnit::Millisecond => { + Unit::Millisecond => { increment.checked_mul(NonZeroU128::new(1_000_000).temporal_unwrap()?) } - TemporalUnit::Microsecond => { - increment.checked_mul(NonZeroU128::new(1_000).temporal_unwrap()?) - } - TemporalUnit::Nanosecond => Some(increment), + Unit::Microsecond => increment.checked_mul(NonZeroU128::new(1_000).temporal_unwrap()?), + Unit::Nanosecond => Some(increment), _ => { return Err(TemporalError::range() .with_message("Invalid unit provided for Instant::round.")) @@ -334,7 +332,7 @@ mod tests { use crate::{ builtins::core::{duration::TimeDuration, Instant}, - options::{DifferenceSettings, TemporalRoundingMode, TemporalUnit}, + options::{DifferenceSettings, RoundingMode, Unit}, primitive::FiniteF64, time::EpochNanoseconds, NS_MAX_INSTANT, NS_MIN_INSTANT, @@ -429,10 +427,10 @@ mod tests { #[test] fn basic_instant_until() { - let init_diff_setting = |unit: TemporalUnit| -> DifferenceSettings { + let init_diff_setting = |unit: Unit| -> DifferenceSettings { DifferenceSettings { - largest_unit: Some(TemporalUnit::Hour), - rounding_mode: Some(TemporalRoundingMode::Ceil), + largest_unit: Some(Unit::Hour), + rounding_mode: Some(RoundingMode::Ceil), increment: None, smallest_unit: Some(unit), } @@ -462,20 +460,20 @@ mod tests { .unwrap(); let positive_result = earlier - .until(&later, init_diff_setting(TemporalUnit::Hour)) + .until(&later, init_diff_setting(Unit::Hour)) .unwrap(); assert_time_duration(positive_result.time(), (376436.0, 0.0, 0.0, 0.0, 0.0, 0.0)); let negative_result = later - .until(&earlier, init_diff_setting(TemporalUnit::Hour)) + .until(&earlier, init_diff_setting(Unit::Hour)) .unwrap(); assert_time_duration(negative_result.time(), (-376435.0, 0.0, 0.0, 0.0, 0.0, 0.0)); let positive_result = earlier - .until(&later, init_diff_setting(TemporalUnit::Minute)) + .until(&later, init_diff_setting(Unit::Minute)) .unwrap(); assert_time_duration(positive_result.time(), (376435.0, 24.0, 0.0, 0.0, 0.0, 0.0)); let negative_result = later - .until(&earlier, init_diff_setting(TemporalUnit::Minute)) + .until(&earlier, init_diff_setting(Unit::Minute)) .unwrap(); assert_time_duration( negative_result.time(), @@ -485,14 +483,14 @@ mod tests { // ... Skip to lower units ... let positive_result = earlier - .until(&later, init_diff_setting(TemporalUnit::Microsecond)) + .until(&later, init_diff_setting(Unit::Microsecond)) .unwrap(); assert_time_duration( positive_result.time(), (376435.0, 23.0, 8.0, 148.0, 530.0, 0.0), ); let negative_result = later - .until(&earlier, init_diff_setting(TemporalUnit::Microsecond)) + .until(&earlier, init_diff_setting(Unit::Microsecond)) .unwrap(); assert_time_duration( negative_result.time(), @@ -500,14 +498,14 @@ mod tests { ); let positive_result = earlier - .until(&later, init_diff_setting(TemporalUnit::Nanosecond)) + .until(&later, init_diff_setting(Unit::Nanosecond)) .unwrap(); assert_time_duration( positive_result.time(), (376435.0, 23.0, 8.0, 148.0, 529.0, 500.0), ); let negative_result = later - .until(&earlier, init_diff_setting(TemporalUnit::Nanosecond)) + .until(&earlier, init_diff_setting(Unit::Nanosecond)) .unwrap(); assert_time_duration( negative_result.time(), @@ -517,10 +515,10 @@ mod tests { #[test] fn basic_instant_since() { - let init_diff_setting = |unit: TemporalUnit| -> DifferenceSettings { + let init_diff_setting = |unit: Unit| -> DifferenceSettings { DifferenceSettings { - largest_unit: Some(TemporalUnit::Hour), - rounding_mode: Some(TemporalRoundingMode::Ceil), + largest_unit: Some(Unit::Hour), + rounding_mode: Some(RoundingMode::Ceil), increment: None, smallest_unit: Some(unit), } @@ -550,20 +548,20 @@ mod tests { .unwrap(); let positive_result = later - .since(&earlier, init_diff_setting(TemporalUnit::Hour)) + .since(&earlier, init_diff_setting(Unit::Hour)) .unwrap(); assert_time_duration(positive_result.time(), (376436.0, 0.0, 0.0, 0.0, 0.0, 0.0)); let negative_result = earlier - .since(&later, init_diff_setting(TemporalUnit::Hour)) + .since(&later, init_diff_setting(Unit::Hour)) .unwrap(); assert_time_duration(negative_result.time(), (-376435.0, 0.0, 0.0, 0.0, 0.0, 0.0)); let positive_result = later - .since(&earlier, init_diff_setting(TemporalUnit::Minute)) + .since(&earlier, init_diff_setting(Unit::Minute)) .unwrap(); assert_time_duration(positive_result.time(), (376435.0, 24.0, 0.0, 0.0, 0.0, 0.0)); let negative_result = earlier - .since(&later, init_diff_setting(TemporalUnit::Minute)) + .since(&later, init_diff_setting(Unit::Minute)) .unwrap(); assert_time_duration( negative_result.time(), @@ -573,14 +571,14 @@ mod tests { // ... Skip to lower units ... let positive_result = later - .since(&earlier, init_diff_setting(TemporalUnit::Microsecond)) + .since(&earlier, init_diff_setting(Unit::Microsecond)) .unwrap(); assert_time_duration( positive_result.time(), (376435.0, 23.0, 8.0, 148.0, 530.0, 0.0), ); let negative_result = earlier - .since(&later, init_diff_setting(TemporalUnit::Microsecond)) + .since(&later, init_diff_setting(Unit::Microsecond)) .unwrap(); assert_time_duration( negative_result.time(), @@ -588,14 +586,14 @@ mod tests { ); let positive_result = later - .since(&earlier, init_diff_setting(TemporalUnit::Nanosecond)) + .since(&earlier, init_diff_setting(Unit::Nanosecond)) .unwrap(); assert_time_duration( positive_result.time(), (376435.0, 23.0, 8.0, 148.0, 529.0, 500.0), ); let negative_result = earlier - .since(&later, init_diff_setting(TemporalUnit::Nanosecond)) + .since(&later, init_diff_setting(Unit::Nanosecond)) .unwrap(); assert_time_duration( negative_result.time(), diff --git a/src/builtins/core/time.rs b/src/builtins/core/time.rs index 53d5b5ccd..c452fb8e2 100644 --- a/src/builtins/core/time.rs +++ b/src/builtins/core/time.rs @@ -5,7 +5,7 @@ use crate::{ iso::IsoTime, options::{ ArithmeticOverflow, DifferenceOperation, DifferenceSettings, ResolvedRoundingOptions, - RoundingIncrement, TemporalRoundingMode, TemporalUnit, ToStringRoundingOptions, UnitGroup, + RoundingIncrement, RoundingMode, ToStringRoundingOptions, Unit, UnitGroup, }, parsers::{parse_time, IxdtfStringBuilder}, primitive::FiniteF64, @@ -172,8 +172,8 @@ impl PlainTime { settings, op, UnitGroup::Time, - TemporalUnit::Hour, - TemporalUnit::Nanosecond, + Unit::Hour, + Unit::Nanosecond, )?; // 5. Let norm be ! DifferenceTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], @@ -183,7 +183,7 @@ impl PlainTime { let mut normalized_time = self.iso.diff(&other.iso).to_normalized(); // 6. If settings.[[SmallestUnit]] is not "nanosecond" or settings.[[RoundingIncrement]] ≠ 1, then - if resolved.smallest_unit != TemporalUnit::Nanosecond + if resolved.smallest_unit != Unit::Nanosecond || resolved.increment != RoundingIncrement::ONE { // a. Let roundRecord be ! RoundDuration(0, 0, 0, 0, norm, settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). @@ -451,12 +451,12 @@ impl PlainTime { /// Rounds the current `Time` according to provided options. pub fn round( &self, - smallest_unit: TemporalUnit, + smallest_unit: Unit, rounding_increment: Option, - rounding_mode: Option, + rounding_mode: Option, ) -> TemporalResult { let increment = RoundingIncrement::try_from(rounding_increment.unwrap_or(1.0))?; - let rounding_mode = rounding_mode.unwrap_or(TemporalRoundingMode::HalfExpand); + let rounding_mode = rounding_mode.unwrap_or(RoundingMode::HalfExpand); let max = smallest_unit .to_maximum_rounding_increment() @@ -468,7 +468,7 @@ impl PlainTime { increment.validate(u64::from(max), false)?; let resolved = ResolvedRoundingOptions { - largest_unit: TemporalUnit::Auto, + largest_unit: Unit::Auto, increment, smallest_unit, rounding_mode, @@ -516,7 +516,7 @@ mod tests { use crate::{ builtins::core::Duration, iso::IsoTime, - options::{ArithmeticOverflow, DifferenceSettings, RoundingIncrement, TemporalUnit}, + options::{ArithmeticOverflow, DifferenceSettings, RoundingIncrement, Unit}, }; use num_traits::FromPrimitive; @@ -586,24 +586,16 @@ mod tests { fn time_round_millisecond() { let base = PlainTime::new_unchecked(IsoTime::new_unchecked(3, 34, 56, 987, 654, 321)); - let result_1 = base - .round(TemporalUnit::Millisecond, Some(1.0), None) - .unwrap(); + let result_1 = base.round(Unit::Millisecond, Some(1.0), None).unwrap(); assert_time(result_1, (3, 34, 56, 988, 0, 0)); - let result_2 = base - .round(TemporalUnit::Millisecond, Some(2.0), None) - .unwrap(); + let result_2 = base.round(Unit::Millisecond, Some(2.0), None).unwrap(); assert_time(result_2, (3, 34, 56, 988, 0, 0)); - let result_3 = base - .round(TemporalUnit::Millisecond, Some(4.0), None) - .unwrap(); + let result_3 = base.round(Unit::Millisecond, Some(4.0), None).unwrap(); assert_time(result_3, (3, 34, 56, 988, 0, 0)); - let result_4 = base - .round(TemporalUnit::Millisecond, Some(5.0), None) - .unwrap(); + let result_4 = base.round(Unit::Millisecond, Some(5.0), None).unwrap(); assert_time(result_4, (3, 34, 56, 990, 0, 0)); } @@ -611,24 +603,16 @@ mod tests { fn time_round_microsecond() { let base = PlainTime::new_unchecked(IsoTime::new_unchecked(3, 34, 56, 987, 654, 321)); - let result_1 = base - .round(TemporalUnit::Microsecond, Some(1.0), None) - .unwrap(); + let result_1 = base.round(Unit::Microsecond, Some(1.0), None).unwrap(); assert_time(result_1, (3, 34, 56, 987, 654, 0)); - let result_2 = base - .round(TemporalUnit::Microsecond, Some(2.0), None) - .unwrap(); + let result_2 = base.round(Unit::Microsecond, Some(2.0), None).unwrap(); assert_time(result_2, (3, 34, 56, 987, 654, 0)); - let result_3 = base - .round(TemporalUnit::Microsecond, Some(4.0), None) - .unwrap(); + let result_3 = base.round(Unit::Microsecond, Some(4.0), None).unwrap(); assert_time(result_3, (3, 34, 56, 987, 656, 0)); - let result_4 = base - .round(TemporalUnit::Microsecond, Some(5.0), None) - .unwrap(); + let result_4 = base.round(Unit::Microsecond, Some(5.0), None).unwrap(); assert_time(result_4, (3, 34, 56, 987, 655, 0)); } @@ -636,24 +620,16 @@ mod tests { fn time_round_nanoseconds() { let base = PlainTime::new_unchecked(IsoTime::new_unchecked(3, 34, 56, 987, 654, 321)); - let result_1 = base - .round(TemporalUnit::Nanosecond, Some(1.0), None) - .unwrap(); + let result_1 = base.round(Unit::Nanosecond, Some(1.0), None).unwrap(); assert_time(result_1, (3, 34, 56, 987, 654, 321)); - let result_2 = base - .round(TemporalUnit::Nanosecond, Some(2.0), None) - .unwrap(); + let result_2 = base.round(Unit::Nanosecond, Some(2.0), None).unwrap(); assert_time(result_2, (3, 34, 56, 987, 654, 322)); - let result_3 = base - .round(TemporalUnit::Nanosecond, Some(4.0), None) - .unwrap(); + let result_3 = base.round(Unit::Nanosecond, Some(4.0), None).unwrap(); assert_time(result_3, (3, 34, 56, 987, 654, 320)); - let result_4 = base - .round(TemporalUnit::Nanosecond, Some(5.0), None) - .unwrap(); + let result_4 = base.round(Unit::Nanosecond, Some(5.0), None).unwrap(); assert_time(result_4, (3, 34, 56, 987, 654, 320)); } @@ -725,7 +701,7 @@ mod tests { let later = PlainTime::new(13, 47, 57, 988, 655, 322).unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Second), + smallest_unit: Some(Unit::Second), increment: Some(RoundingIncrement::try_new(1).unwrap()), ..Default::default() }; @@ -735,7 +711,7 @@ mod tests { ); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Second), + smallest_unit: Some(Unit::Second), increment: Some(RoundingIncrement::try_new(4).unwrap()), ..Default::default() }; @@ -753,92 +729,77 @@ mod tests { .unwrap(); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(1.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(1.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 321, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(2.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(2.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 322, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(4.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(4.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 320, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(5.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(5.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 320, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(8.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(8.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 320, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(10.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(10.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 320, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(20.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(20.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 320, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(25.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(25.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 325, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(40.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(40.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 320, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(50.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(50.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 300, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(100.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(100.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 300, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(125.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(125.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 375, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(200.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(200.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 400, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(250.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(250.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 250, ArithmeticOverflow::Constrain) .unwrap() ); assert_eq!( - time.round(TemporalUnit::Nanosecond, Some(500.0), None) - .unwrap(), + time.round(Unit::Nanosecond, Some(500.0), None).unwrap(), PlainTime::new_with_overflow(3, 34, 56, 987, 654, 500, ArithmeticOverflow::Constrain) .unwrap() ); diff --git a/src/builtins/core/year_month.rs b/src/builtins/core/year_month.rs index 104173897..1c9502f34 100644 --- a/src/builtins/core/year_month.rs +++ b/src/builtins/core/year_month.rs @@ -9,7 +9,7 @@ use crate::{ iso::{year_month_within_limits, IsoDate, IsoDateTime, IsoTime}, options::{ ArithmeticOverflow, DifferenceOperation, DifferenceSettings, DisplayCalendar, - ResolvedRoundingOptions, RoundingIncrement, TemporalUnit, UnitGroup, + ResolvedRoundingOptions, RoundingIncrement, Unit, UnitGroup, }, parsers::{FormattableCalendar, FormattableDate, FormattableYearMonth}, provider::NeverProvider, @@ -78,13 +78,9 @@ impl PlainYearMonth { } // Check if weeks or days are disallowed in this operation - if matches!( - settings.largest_unit, - Some(TemporalUnit::Week) | Some(TemporalUnit::Day) - ) || matches!( - settings.smallest_unit, - Some(TemporalUnit::Week) | Some(TemporalUnit::Day) - ) { + if matches!(settings.largest_unit, Some(Unit::Week) | Some(Unit::Day)) + || matches!(settings.smallest_unit, Some(Unit::Week) | Some(Unit::Day)) + { return Err(TemporalError::range() .with_message("Weeks and days are not allowed in this operation.")); } @@ -95,8 +91,8 @@ impl PlainYearMonth { settings, op, UnitGroup::Date, - TemporalUnit::Year, - TemporalUnit::Month, + Unit::Year, + Unit::Month, )?; // 6. If CompareISODate(yearMonth.[[ISODate]], other.[[ISODate]]) = 0, then @@ -121,9 +117,7 @@ impl PlainYearMonth { let mut duration = NormalizedDurationRecord::from_date_duration(*result.date())?; // 16. If settings.[[SmallestUnit]] is not month or settings.[[RoundingIncrement]] ≠ 1, then - if resolved.smallest_unit != TemporalUnit::Month - || resolved.increment != RoundingIncrement::ONE - { + if resolved.smallest_unit != Unit::Month || resolved.increment != RoundingIncrement::ONE { // a. Let isoDateTime be CombineISODateAndTimeRecord(thisDate, MidnightTimeRecord()). let iso_date_time = IsoDateTime::new_unchecked(self.iso, IsoTime::default()); // b. Let isoDateTimeOther be CombineISODateAndTimeRecord(otherDate, MidnightTimeRecord()). @@ -140,7 +134,7 @@ impl PlainYearMonth { } // 17. Let result be ! TemporalDurationFromInternal(duration, day). - let result = Duration::from_normalized(duration, TemporalUnit::Day)?; + let result = Duration::from_normalized(duration, Unit::Day)?; // 18. If operation is since, set result to CreateNegatedTemporalDuration(result). // 19. Return result. @@ -406,7 +400,7 @@ mod tests { let earlier = PlainYearMonth::from_str("2024-03").unwrap(); let later = PlainYearMonth::from_str("2024-03").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Month), + smallest_unit: Some(Unit::Month), ..Default::default() }; @@ -427,7 +421,7 @@ mod tests { let earlier = PlainYearMonth::from_str("2023-01").unwrap(); let later = PlainYearMonth::from_str("2023-02").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Month), + smallest_unit: Some(Unit::Month), ..Default::default() }; @@ -446,7 +440,7 @@ mod tests { let earlier = PlainYearMonth::from_str("2022-11").unwrap(); let later = PlainYearMonth::from_str("2023-02").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Month), + smallest_unit: Some(Unit::Month), ..Default::default() }; @@ -465,7 +459,7 @@ mod tests { let earlier = PlainYearMonth::from_str("2002-05").unwrap(); let later = PlainYearMonth::from_str("2003-06").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Month), + smallest_unit: Some(Unit::Month), ..Default::default() }; @@ -486,7 +480,7 @@ mod tests { let earlier = PlainYearMonth::from_str("2022-06").unwrap(); let later = PlainYearMonth::from_str("2023-06").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Year), + smallest_unit: Some(Unit::Year), ..Default::default() }; @@ -505,7 +499,7 @@ mod tests { let earlier = PlainYearMonth::from_str("1000-01").unwrap(); let later = PlainYearMonth::from_str("2000-01").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Year), + smallest_unit: Some(Unit::Year), ..Default::default() }; @@ -521,7 +515,7 @@ mod tests { let earlier = PlainYearMonth::from_str("-271821-04").unwrap(); let later = PlainYearMonth::from_str("-271820-04").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Year), + smallest_unit: Some(Unit::Year), ..Default::default() }; @@ -553,7 +547,7 @@ mod tests { .unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Month), + smallest_unit: Some(Unit::Month), ..Default::default() }; @@ -569,7 +563,7 @@ mod tests { let ym2 = PlainYearMonth::from_str("2023-02").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Month), + smallest_unit: Some(Unit::Month), increment: Some(RoundingIncrement::ONE), ..Default::default() }; @@ -584,7 +578,7 @@ mod tests { let ym2 = PlainYearMonth::from_str("2023-02").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Year), + smallest_unit: Some(Unit::Year), ..Default::default() }; @@ -599,7 +593,7 @@ mod tests { let ym2 = PlainYearMonth::from_str("2023-02").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Day), + smallest_unit: Some(Unit::Day), ..Default::default() }; @@ -616,7 +610,7 @@ mod tests { let ym2 = PlainYearMonth::from_str("2023-02").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Week), + smallest_unit: Some(Unit::Week), ..Default::default() }; @@ -633,7 +627,7 @@ mod tests { let ym2 = PlainYearMonth::from_str("2023-02").unwrap(); let settings = DifferenceSettings { - smallest_unit: Some(TemporalUnit::Month), + smallest_unit: Some(Unit::Month), increment: None, // No rounding increment ..Default::default() }; diff --git a/src/builtins/core/zoneddatetime.rs b/src/builtins/core/zoneddatetime.rs index 9b76858f0..d28790505 100644 --- a/src/builtins/core/zoneddatetime.rs +++ b/src/builtins/core/zoneddatetime.rs @@ -17,8 +17,8 @@ use crate::{ options::{ ArithmeticOverflow, DifferenceOperation, DifferenceSettings, Disambiguation, DisplayCalendar, DisplayOffset, DisplayTimeZone, OffsetDisambiguation, - ResolvedRoundingOptions, RoundingIncrement, TemporalRoundingMode, TemporalUnit, - ToStringRoundingOptions, UnitGroup, + ResolvedRoundingOptions, RoundingIncrement, RoundingMode, ToStringRoundingOptions, Unit, + UnitGroup, }, parsers::{self, FormattableOffset, FormattableTime, IxdtfStringBuilder, Precision}, partial::{PartialDate, PartialTime}, @@ -213,7 +213,7 @@ impl ZonedDateTime { resolved_options: ResolvedRoundingOptions, provider: &impl TimeZoneProvider, ) -> TemporalResult { - // 1. If TemporalUnitCategory(largestUnit) is time, then + // 1. If UnitCategory(largestUnit) is time, then if resolved_options.largest_unit.is_time_unit() { // a. Return DifferenceInstant(ns1, ns2, roundingIncrement, smallestUnit, roundingMode). return self @@ -223,7 +223,7 @@ impl ZonedDateTime { // 2. let difference be ? differencezoneddatetime(ns1, ns2, timezone, calendar, largestunit). let diff = self.diff_zoned_datetime(other, resolved_options.largest_unit, provider)?; // 3. if smallestunit is nanosecond and roundingincrement = 1, return difference. - if resolved_options.smallest_unit == TemporalUnit::Nanosecond + if resolved_options.smallest_unit == Unit::Nanosecond && resolved_options.increment == RoundingIncrement::ONE { return Ok(diff); @@ -245,10 +245,10 @@ impl ZonedDateTime { pub(crate) fn diff_with_total( &self, other: &Self, - unit: TemporalUnit, + unit: Unit, provider: &impl TimeZoneProvider, ) -> TemporalResult { - // 1. If TemporalUnitCategory(unit) is time, then + // 1. If UnitCategory(unit) is time, then if unit.is_time_unit() { // a. Let difference be TimeDurationFromEpochNanosecondsDifference(ns2, ns1). let diff = NormalizedTimeDuration::from_nanosecond_difference( @@ -277,7 +277,7 @@ impl ZonedDateTime { pub(crate) fn diff_zoned_datetime( &self, other: &Self, - largest_unit: TemporalUnit, + largest_unit: Unit, provider: &impl TimeZoneProvider, ) -> TemporalResult { // 1. If ns1 = ns2, return CombineDateAndTimeDuration(ZeroDateDuration(), 0). @@ -343,8 +343,8 @@ impl ZonedDateTime { day_correction += 1; } // 11. Assert: success is true. - // 12. Let dateLargestUnit be LargerOfTwoTemporalUnits(largestUnit, day). - let date_largest = largest_unit.max(TemporalUnit::Day); + // 12. Let dateLargestUnit be LargerOfTwoUnits(largestUnit, day). + let date_largest = largest_unit.max(Unit::Day); // 13. Let dateDifference be CalendarDateUntil(calendar, startDateTime.[[ISODate]], intermediateDateTime.[[ISODate]], dateLargestUnit). // 14. Return CombineDateAndTimeDuration(dateDifference, timeDuration). let date_diff = @@ -373,11 +373,11 @@ impl ZonedDateTime { options, op, UnitGroup::DateTime, - TemporalUnit::Hour, - TemporalUnit::Nanosecond, + Unit::Hour, + Unit::Nanosecond, )?; - // 5. If TemporalUnitCategory(settings.[[LargestUnit]]) is time, then + // 5. If UnitCategory(settings.[[LargestUnit]]) is time, then if resolved_options.largest_unit.is_time_unit() { // a. Let internalDuration be DifferenceInstant(zonedDateTime.[[EpochNanoseconds]], other.[[EpochNanoseconds]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). let internal = self @@ -412,7 +412,7 @@ impl ZonedDateTime { // 9. Let internalDuration be ? DifferenceZonedDateTimeWithRounding(zonedDateTime.[[EpochNanoseconds]], other.[[EpochNanoseconds]], zonedDateTime.[[TimeZone]], zonedDateTime.[[Calendar]], settings.[[LargestUnit]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]). let internal = self.diff_with_rounding(other, resolved_options, provider)?; // 10. Let result be ! TemporalDurationFromInternal(internalDuration, hour). - let result = Duration::from_normalized(internal, TemporalUnit::Hour)?; + let result = Duration::from_normalized(internal, Unit::Hour)?; // 11. If operation is since, set result to CreateNegatedTemporalDuration(result). // 12. Return result. match op { @@ -1169,7 +1169,7 @@ pub(crate) fn interpret_isodatetime_offset( candidate_offset, NonZeroU128::new(60_000_000_000).expect("cannot be zero"), // TODO: Add back const { } after MSRV can be bumped )? - .round(TemporalRoundingMode::HalfExpand); + .round(RoundingMode::HalfExpand); // ii. If roundedCandidateNanoseconds = offsetNanoseconds, then if rounded_candidate == offset.into() { // 1. Return candidate. @@ -1211,7 +1211,7 @@ pub(crate) fn nanoseconds_to_formattable_offset_minutes( let nanoseconds = IncrementRounder::from_signed_num(nanoseconds, unsafe { NonZeroU128::new_unchecked(NS_PER_MINUTE as u128) })? - .round(TemporalRoundingMode::HalfExpand); + .round(RoundingMode::HalfExpand); let offset_minutes = (nanoseconds / NS_PER_MINUTE) as i32; let sign = if offset_minutes < 0 { Sign::Negative @@ -1227,7 +1227,7 @@ pub(crate) fn nanoseconds_to_formattable_offset_minutes( mod tests { use super::ZonedDateTime; use crate::{ - options::{DifferenceSettings, Disambiguation, OffsetDisambiguation, TemporalUnit}, + options::{DifferenceSettings, Disambiguation, OffsetDisambiguation, Unit}, partial::{PartialDate, PartialTime, PartialZonedDateTime}, primitive::FiniteF64, time::EpochNanoseconds, @@ -1350,8 +1350,8 @@ mod tests { .until( &midnight_disambiguated.instant, DifferenceSettings { - largest_unit: Some(TemporalUnit::Hour), - smallest_unit: Some(TemporalUnit::Nanosecond), + largest_unit: Some(Unit::Hour), + smallest_unit: Some(Unit::Nanosecond), ..Default::default() }, ) diff --git a/src/iso.rs b/src/iso.rs index 5b94c491a..c8db769af 100644 --- a/src/iso.rs +++ b/src/iso.rs @@ -35,7 +35,7 @@ use crate::{ Duration, PartialTime, PlainDate, }, error::TemporalError, - options::{ArithmeticOverflow, ResolvedRoundingOptions, TemporalUnit}, + options::{ArithmeticOverflow, ResolvedRoundingOptions, Unit}, primitive::FiniteF64, rounding::{IncrementRounder, Round}, temporal_assert, @@ -202,11 +202,11 @@ impl IsoDateTime { &self, other: &Self, calendar: &Calendar, - largest_unit: TemporalUnit, + largest_unit: Unit, ) -> TemporalResult { // 1. Assert: ISODateTimeWithinLimits(y1, mon1, d1, h1, min1, s1, ms1, mus1, ns1) is true. // 2. Assert: ISODateTimeWithinLimits(y2, mon2, d2, h2, min2, s2, ms2, mus2, ns2) is true. - // 3. Assert: If y1 ≠ y2, and mon1 ≠ mon2, and d1 ≠ d2, and LargerOfTwoTemporalUnits(largestUnit, "day") + // 3. Assert: If y1 ≠ y2, and mon1 ≠ mon2, and d1 ≠ d2, and LargerOfTwoUnits(largestUnit, "day") // is not "day", CalendarMethodsRecordHasLookedUp(calendarRec, date-until) is true. // 4. Let timeDuration be DifferenceTime(h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2). @@ -244,9 +244,9 @@ impl IsoDateTime { calendar.clone(), )?; - // 11. Let dateLargestUnit be LargerOfTwoTemporalUnits("day", largestUnit). + // 11. Let dateLargestUnit be LargerOfTwoUnits("day", largestUnit). // 12. Let untilOptions be ! SnapshotOwnProperties(options, null). - let date_largest_unit = largest_unit.max(TemporalUnit::Day); + let date_largest_unit = largest_unit.max(Unit::Day); // 13. Perform ! CreateDataPropertyOrThrow(untilOptions, "largestUnit", dateLargestUnit). // 14. Let dateDifference be ? DifferenceDate(calendarRec, date1, date2, untilOptions). @@ -409,7 +409,7 @@ impl IsoDate { pub(crate) fn diff_iso_date( &self, other: &Self, - largest_unit: TemporalUnit, + largest_unit: Unit, ) -> TemporalResult { // 1. Assert: IsValidISODate(y1, m1, d1) is true. // 2. Assert: IsValidISODate(y2, m2, d2) is true. @@ -424,7 +424,7 @@ impl IsoDate { let mut years = 0; let mut months = 0; // 6. If largestUnit is "year", then - if largest_unit == TemporalUnit::Year || largest_unit == TemporalUnit::Month { + if largest_unit == Unit::Year || largest_unit == Unit::Month { // others.year - self.year is adopted from temporal-proposal/polyfill as it saves iterations. // a. Let candidateYears be sign. let mut candidate_years: i32 = other.year - self.year; @@ -468,7 +468,7 @@ impl IsoDate { ); } - if largest_unit == TemporalUnit::Month { + if largest_unit == Unit::Month { months += years * 12; years = 0; } @@ -494,7 +494,7 @@ impl IsoDate { constrained.day, ); - let (weeks, days) = if largest_unit == TemporalUnit::Week { + let (weeks, days) = if largest_unit == Unit::Week { (days / 7, days % 7) } else { (0, days) @@ -736,7 +736,7 @@ impl IsoTime { ) -> TemporalResult<(i32, Self)> { // 1. If unit is "day" or "hour", then let quantity = match resolved_options.smallest_unit { - TemporalUnit::Day | TemporalUnit::Hour => { + Unit::Day | Unit::Hour => { // a. Let quantity be ((((hour × 60 + minute) × 60 + second) × 1000 + millisecond) // × 1000 + microsecond) × 1000 + nanosecond. let minutes = i128::from(self.hour) * 60 + i128::from(self.minute); @@ -746,7 +746,7 @@ impl IsoTime { micros * 1000 + i128::from(self.nanosecond) } // 2. Else if unit is "minute", then - TemporalUnit::Minute => { + Unit::Minute => { // a. Let quantity be (((minute × 60 + second) × 1000 + millisecond) × 1000 + microsecond) × 1000 + nanosecond. let seconds = i128::from(self.minute) * 60 + i128::from(self.second); let millis = seconds * 1000 + i128::from(self.millisecond); @@ -754,25 +754,25 @@ impl IsoTime { micros * 1000 + i128::from(self.nanosecond) } // 3. Else if unit is "second", then - TemporalUnit::Second => { + Unit::Second => { // a. Let quantity be ((second × 1000 + millisecond) × 1000 + microsecond) × 1000 + nanosecond. let millis = i128::from(self.second) * 1000 + i128::from(self.millisecond); let micros = millis * 1000 + i128::from(self.microsecond); micros * 1000 + i128::from(self.nanosecond) } // 4. Else if unit is "millisecond", then - TemporalUnit::Millisecond => { + Unit::Millisecond => { // a. Let quantity be (millisecond × 1000 + microsecond) × 1000 + nanosecond. let micros = i128::from(self.millisecond) * 1000 + i128::from(self.microsecond); micros * 1000 + i128::from(self.nanosecond) } // 5. Else if unit is "microsecond", then - TemporalUnit::Microsecond => { + Unit::Microsecond => { // a. Let quantity be microsecond × 1000 + nanosecond. i128::from(self.microsecond) * 1000 + i128::from(self.nanosecond) } // 6. Else, - TemporalUnit::Nanosecond => { + Unit::Nanosecond => { // a. Assert: unit is "nanosecond". // b. Let quantity be nanosecond. i128::from(self.nanosecond) @@ -809,16 +809,16 @@ impl IsoTime { match resolved_options.smallest_unit { // 9. If unit is "day", then // a. Return Time Record { [[Days]]: result, [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }. - TemporalUnit::Day => Ok((result_i64 as i32, Self::default())), + Unit::Day => Ok((result_i64 as i32, Self::default())), // 10. If unit is "hour", then // a. Return BalanceTime(result, 0, 0, 0, 0, 0). - TemporalUnit::Hour => Ok(Self::balance(result_i64, 0, 0, 0, 0, 0)), + Unit::Hour => Ok(Self::balance(result_i64, 0, 0, 0, 0, 0)), // 11. If unit is "minute", then // a. Return BalanceTime(hour, result, 0.0, 0.0, 0.0, 0). - TemporalUnit::Minute => Ok(Self::balance(self.hour.into(), result_i64, 0, 0, 0, 0)), + Unit::Minute => Ok(Self::balance(self.hour.into(), result_i64, 0, 0, 0, 0)), // 12. If unit is "second", then // a. Return BalanceTime(hour, minute, result, 0.0, 0.0, 0). - TemporalUnit::Second => Ok(Self::balance( + Unit::Second => Ok(Self::balance( self.hour.into(), self.minute.into(), result_i64, @@ -828,7 +828,7 @@ impl IsoTime { )), // 13. If unit is "millisecond", then // a. Return BalanceTime(hour, minute, second, result, 0.0, 0). - TemporalUnit::Millisecond => Ok(Self::balance( + Unit::Millisecond => Ok(Self::balance( self.hour.into(), self.minute.into(), self.second.into(), @@ -838,7 +838,7 @@ impl IsoTime { )), // 14. If unit is "microsecond", then // a. Return BalanceTime(hour, minute, second, millisecond, result, 0). - TemporalUnit::Microsecond => Ok(Self::balance( + Unit::Microsecond => Ok(Self::balance( self.hour.into(), self.minute.into(), self.second.into(), @@ -848,7 +848,7 @@ impl IsoTime { )), // 15. Assert: unit is "nanosecond". // 16. Return BalanceTime(hour, minute, second, millisecond, microsecond, result). - TemporalUnit::Nanosecond => Ok(Self::balance( + Unit::Nanosecond => Ok(Self::balance( self.hour.into(), self.minute.into(), self.second.into(), diff --git a/src/options.rs b/src/options.rs index 7b21ba57b..98086d6a0 100644 --- a/src/options.rs +++ b/src/options.rs @@ -25,49 +25,49 @@ pub(crate) enum DifferenceOperation { #[derive(Debug, Default)] pub struct ToStringRoundingOptions { pub precision: Precision, - pub smallest_unit: Option, - pub rounding_mode: Option, + pub smallest_unit: Option, + pub rounding_mode: Option, } #[derive(Debug)] pub(crate) struct ResolvedToStringRoundingOptions { pub(crate) precision: Precision, - pub(crate) smallest_unit: TemporalUnit, - pub(crate) rounding_mode: TemporalRoundingMode, + pub(crate) smallest_unit: Unit, + pub(crate) rounding_mode: RoundingMode, pub(crate) increment: RoundingIncrement, } impl ToStringRoundingOptions { pub(crate) fn resolve(&self) -> TemporalResult { - let rounding_mode = self.rounding_mode.unwrap_or(TemporalRoundingMode::Trunc); + let rounding_mode = self.rounding_mode.unwrap_or(RoundingMode::Trunc); match self.smallest_unit { - Some(TemporalUnit::Minute) => Ok(ResolvedToStringRoundingOptions { + Some(Unit::Minute) => Ok(ResolvedToStringRoundingOptions { precision: Precision::Minute, - smallest_unit: TemporalUnit::Minute, + smallest_unit: Unit::Minute, rounding_mode, increment: RoundingIncrement::ONE, }), - Some(TemporalUnit::Second) => Ok(ResolvedToStringRoundingOptions { + Some(Unit::Second) => Ok(ResolvedToStringRoundingOptions { precision: Precision::Digit(0), - smallest_unit: TemporalUnit::Second, + smallest_unit: Unit::Second, rounding_mode, increment: RoundingIncrement::ONE, }), - Some(TemporalUnit::Millisecond) => Ok(ResolvedToStringRoundingOptions { + Some(Unit::Millisecond) => Ok(ResolvedToStringRoundingOptions { precision: Precision::Digit(3), - smallest_unit: TemporalUnit::Millisecond, + smallest_unit: Unit::Millisecond, rounding_mode, increment: RoundingIncrement::ONE, }), - Some(TemporalUnit::Microsecond) => Ok(ResolvedToStringRoundingOptions { + Some(Unit::Microsecond) => Ok(ResolvedToStringRoundingOptions { precision: Precision::Digit(6), - smallest_unit: TemporalUnit::Microsecond, + smallest_unit: Unit::Microsecond, rounding_mode, increment: RoundingIncrement::ONE, }), - Some(TemporalUnit::Nanosecond) => Ok(ResolvedToStringRoundingOptions { + Some(Unit::Nanosecond) => Ok(ResolvedToStringRoundingOptions { precision: Precision::Digit(9), - smallest_unit: TemporalUnit::Nanosecond, + smallest_unit: Unit::Nanosecond, rounding_mode, increment: RoundingIncrement::ONE, }), @@ -75,20 +75,20 @@ impl ToStringRoundingOptions { match self.precision { Precision::Auto => Ok(ResolvedToStringRoundingOptions { precision: Precision::Auto, - smallest_unit: TemporalUnit::Nanosecond, + smallest_unit: Unit::Nanosecond, rounding_mode, increment: RoundingIncrement::ONE, }), Precision::Digit(0) => Ok(ResolvedToStringRoundingOptions { precision: Precision::Digit(0), - smallest_unit: TemporalUnit::Second, + 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: TemporalUnit::Millisecond, + smallest_unit: Unit::Millisecond, rounding_mode, increment: RoundingIncrement::try_new(10_u32.pow(3 - d as u32)) .expect("a valid increment"), @@ -97,7 +97,7 @@ impl ToStringRoundingOptions { Precision::Digit(d) if (4..=6).contains(&d) => { Ok(ResolvedToStringRoundingOptions { precision: Precision::Digit(d), - smallest_unit: TemporalUnit::Microsecond, + smallest_unit: Unit::Microsecond, rounding_mode, increment: RoundingIncrement::try_new(10_u32.pow(6 - d as u32)) .expect("a valid increment"), @@ -106,7 +106,7 @@ impl ToStringRoundingOptions { Precision::Digit(d) if (7..=9).contains(&d) => { Ok(ResolvedToStringRoundingOptions { precision: Precision::Digit(d), - smallest_unit: TemporalUnit::Nanosecond, + smallest_unit: Unit::Nanosecond, rounding_mode, increment: RoundingIncrement::try_new(10_u32.pow(9 - d as u32)) .expect("a valid increment"), @@ -126,18 +126,18 @@ impl ToStringRoundingOptions { #[non_exhaustive] #[derive(Debug, Default, Clone, Copy)] pub struct DifferenceSettings { - pub largest_unit: Option, - pub smallest_unit: Option, - pub rounding_mode: Option, + pub largest_unit: Option, + pub smallest_unit: Option, + pub rounding_mode: Option, pub increment: Option, } #[non_exhaustive] #[derive(Debug, Clone, Copy)] pub struct RoundingOptions { - pub largest_unit: Option, - pub smallest_unit: Option, - pub rounding_mode: Option, + pub largest_unit: Option, + pub smallest_unit: Option, + pub rounding_mode: Option, pub increment: Option, } @@ -147,7 +147,7 @@ pub struct RoundingOptions { impl Default for RoundingOptions { fn default() -> Self { Self { - largest_unit: Some(TemporalUnit::Auto), + largest_unit: Some(Unit::Auto), smallest_unit: None, rounding_mode: None, increment: None, @@ -158,16 +158,16 @@ impl Default for RoundingOptions { /// Internal options object that represents the resolved rounding options. #[derive(Debug, Clone, Copy)] pub(crate) struct ResolvedRoundingOptions { - pub(crate) largest_unit: TemporalUnit, - pub(crate) smallest_unit: TemporalUnit, + pub(crate) largest_unit: Unit, + pub(crate) smallest_unit: Unit, pub(crate) increment: RoundingIncrement, - pub(crate) rounding_mode: TemporalRoundingMode, + pub(crate) rounding_mode: RoundingMode, } impl ResolvedRoundingOptions { pub(crate) fn from_to_string_options(options: &ResolvedToStringRoundingOptions) -> Self { Self { - largest_unit: TemporalUnit::Auto, + largest_unit: Unit::Auto, smallest_unit: options.smallest_unit, increment: options.increment, rounding_mode: options.rounding_mode, @@ -178,11 +178,11 @@ impl ResolvedRoundingOptions { options: DifferenceSettings, operation: DifferenceOperation, unit_group: UnitGroup, - //dissallow_week_and_day: bool, - fallback_largest: TemporalUnit, - fallback_smallest: TemporalUnit, + fallback_largest: Unit, + fallback_smallest: Unit, ) -> TemporalResult { - // Week and day is the only use case where any of the units are not allowed. + // 1. NOTE: The following steps read options and perform independent validation in alphabetical order. + // 2. Let largestUnit be ? GetUnitValuedOption(options, "largestUnit", unitGroup, auto). // 4. Let resolvedOptions be ? SnapshotOwnProperties(? GetOptionsObject(options), null). // 5. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, DATE, « », "day", "day"). @@ -196,22 +196,20 @@ impl ResolvedRoundingOptions { let rounding_mode = match operation { DifferenceOperation::Since => options .rounding_mode - .unwrap_or(TemporalRoundingMode::Trunc) + .unwrap_or(RoundingMode::Trunc) .negate(), - DifferenceOperation::Until => { - options.rounding_mode.unwrap_or(TemporalRoundingMode::Trunc) - } + DifferenceOperation::Until => options.rounding_mode.unwrap_or(RoundingMode::Trunc), }; - // 7. Let smallestUnit be ? GetTemporalUnitValuedOption(options, "smallestUnit", unitGroup, fallbackSmallestUnit). + // 7. Let smallestUnit be ? GetUnitValuedOption(options, "smallestUnit", unitGroup, fallbackSmallestUnit). unit_group.validate_unit(options.smallest_unit, None)?; let smallest_unit = options.smallest_unit.unwrap_or(fallback_smallest); // 8. If disallowedUnits contains smallestUnit, throw a RangeError exception. - // 9. Let defaultLargestUnit be LargerOfTwoTemporalUnits(smallestLargestDefaultUnit, smallestUnit). + // 9. Let defaultLargestUnit be LargerOfTwoUnits(smallestLargestDefaultUnit, smallestUnit). // 10. If largestUnit is auto, set largestUnit to defaultLargestUnit. let largest_unit = options .largest_unit .unwrap_unit_or(smallest_unit.max(fallback_largest)); - // 11. If LargerOfTwoTemporalUnits(largestUnit, smallestUnit) is not largestUnit, throw a RangeError exception. + // 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")); @@ -235,7 +233,7 @@ impl ResolvedRoundingOptions { pub(crate) fn from_duration_options( options: RoundingOptions, - existing_largest: TemporalUnit, + existing_largest: Unit, ) -> TemporalResult { // 22. If smallestUnitPresent is false and largestUnitPresent is false, then if options.largest_unit.is_none() && options.smallest_unit.is_none() { @@ -246,10 +244,10 @@ impl ResolvedRoundingOptions { // 14. Let roundingIncrement be ? ToTemporalRoundingIncrement(roundTo). let increment = options.increment.unwrap_or_default(); - // 15. Let roundingMode be ? ToTemporalRoundingMode(roundTo, "halfExpand"). + // 15. Let roundingMode be ? ToRoundingMode(roundTo, "halfExpand"). let rounding_mode = options.rounding_mode.unwrap_or_default(); - // 16. Let smallestUnit be ? GetTemporalUnit(roundTo, "smallestUnit", DATETIME, undefined). - UnitGroup::DateTime.validate_unit(options.largest_unit, Some(TemporalUnit::Auto))?; + // 16. Let smallestUnit be ? GetUnit(roundTo, "smallestUnit", DATETIME, undefined). + UnitGroup::DateTime.validate_unit(options.largest_unit, Some(Unit::Auto))?; UnitGroup::DateTime.validate_unit(options.smallest_unit, None)?; // 17. If smallestUnit is undefined, then // a. Set smallestUnitPresent to false. @@ -258,21 +256,21 @@ impl ResolvedRoundingOptions { // duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], // duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], // duration.[[Microseconds]]). - // 19. Let defaultLargestUnit be LargerOfTwoTemporalUnits(existingLargestUnit, smallestUnit). + // 19. Let defaultLargestUnit be LargerOfTwoUnits(existingLargestUnit, smallestUnit). // 20. If largestUnit is undefined, then // a. Set largestUnitPresent to false. // b. Set largestUnit to defaultLargestUnit. // 21. Else if largestUnit is "auto", then // a. Set largestUnit to defaultLargestUnit. - // 23. If LargerOfTwoTemporalUnits(largestUnit, smallestUnit) is not largestUnit, throw a RangeError exception. + // 23. If LargerOfTwoUnits(largestUnit, smallestUnit) is not largestUnit, throw a RangeError exception. // 24. Let maximum be MaximumTemporalDurationRoundingIncrement(smallestUnit). // 25. If maximum is not undefined, perform ? ValidateTemporalRoundingIncrement(roundingIncrement, maximum, false). - let smallest_unit = options.smallest_unit.unwrap_or(TemporalUnit::Nanosecond); + let smallest_unit = options.smallest_unit.unwrap_or(Unit::Nanosecond); let default_largest = existing_largest.max(smallest_unit); let largest_unit = match options.largest_unit { - Some(TemporalUnit::Auto) | None => default_largest, + Some(Unit::Auto) | None => default_largest, Some(unit) => unit, }; @@ -296,13 +294,13 @@ impl ResolvedRoundingOptions { }) } - // NOTE: Should the GetTemporalUnitValuedOption check be integrated into these validations. + // NOTE: Should the GetUnitValuedOption check be integrated into these validations. pub(crate) fn from_datetime_options(options: RoundingOptions) -> TemporalResult { let increment = options.increment.unwrap_or_default(); let rounding_mode = options.rounding_mode.unwrap_or_default(); - let smallest_unit = UnitGroup::Time - .validate_required_unit(options.smallest_unit, Some(TemporalUnit::Day))?; - let (maximum, inclusive) = if smallest_unit == TemporalUnit::Day { + let smallest_unit = + UnitGroup::Time.validate_required_unit(options.smallest_unit, Some(Unit::Day))?; + let (maximum, inclusive) = if smallest_unit == Unit::Day { (1, true) } else { let maximum = smallest_unit @@ -314,7 +312,7 @@ impl ResolvedRoundingOptions { increment.validate(maximum.into(), inclusive)?; Ok(Self { - largest_unit: TemporalUnit::Auto, + largest_unit: Unit::Auto, smallest_unit, increment, rounding_mode, @@ -326,19 +324,19 @@ impl ResolvedRoundingOptions { let rounding_mode = options.rounding_mode.unwrap_or_default(); let smallest_unit = UnitGroup::Time.validate_required_unit(options.smallest_unit, None)?; let maximum = match smallest_unit { - TemporalUnit::Hour => 24u64, - TemporalUnit::Minute => 24 * 60, - TemporalUnit::Second => 24 * 3600, - TemporalUnit::Millisecond => MS_PER_DAY as u64, - TemporalUnit::Microsecond => MS_PER_DAY as u64 * 1000, - TemporalUnit::Nanosecond => NS_PER_DAY, + Unit::Hour => 24u64, + Unit::Minute => 24 * 60, + Unit::Second => 24 * 3600, + 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.")), }; increment.validate(maximum, true)?; Ok(Self { - largest_unit: TemporalUnit::Auto, + largest_unit: Unit::Auto, smallest_unit, increment, rounding_mode, @@ -346,7 +344,7 @@ impl ResolvedRoundingOptions { } pub(crate) fn is_noop(&self) -> bool { - self.smallest_unit == TemporalUnit::Nanosecond && self.increment == RoundingIncrement::ONE + self.smallest_unit == Unit::Nanosecond && self.increment == RoundingIncrement::ONE } } @@ -362,9 +360,9 @@ pub enum UnitGroup { impl UnitGroup { pub fn validate_required_unit( self, - unit: Option, - extra_unit: Option, - ) -> TemporalResult { + unit: Option, + extra_unit: Option, + ) -> TemporalResult { let Some(unit) = unit else { return Err(TemporalError::range().with_message("Unit is required.")); }; @@ -372,11 +370,7 @@ impl UnitGroup { Ok(unit) } - pub fn validate_unit( - self, - unit: Option, - extra_unit: Option, - ) -> TemporalResult<()> { + pub fn validate_unit(self, unit: Option, extra_unit: Option) -> TemporalResult<()> { // TODO: Determine proper handling of Auto. match self { UnitGroup::Date => match unit { @@ -403,7 +397,7 @@ impl UnitGroup { /// The relevant unit that should be used for the operation that /// this option is provided as a value. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub enum TemporalUnit { +pub enum Unit { /// The `Auto` unit Auto = 0, /// The `Nanosecond` unit @@ -428,12 +422,12 @@ pub enum TemporalUnit { Year, } -impl TemporalUnit { +impl Unit { #[inline] #[must_use] - /// Returns the `MaximumRoundingIncrement` for the current `TemporalUnit`. + /// Returns the `MaximumRoundingIncrement` for the current `Unit`. pub fn to_maximum_rounding_increment(self) -> Option { - use TemporalUnit::{ + use Unit::{ Auto, Day, Hour, Microsecond, Millisecond, Minute, Month, Nanosecond, Second, Week, Year, }; @@ -460,7 +454,7 @@ impl TemporalUnit { /// Returns the `Nanosecond amount for any given value.` #[must_use] pub fn as_nanoseconds(&self) -> Option { - use TemporalUnit::{ + use Unit::{ Auto, Day, Hour, Microsecond, Millisecond, Minute, Month, Nanosecond, Second, Week, Year, }; @@ -479,21 +473,21 @@ impl TemporalUnit { #[inline] #[must_use] pub fn is_calendar_unit(&self) -> bool { - use TemporalUnit::{Month, Week, Year}; + use Unit::{Month, Week, Year}; matches!(self, Year | Month | Week) } #[inline] #[must_use] pub fn is_date_unit(&self) -> bool { - use TemporalUnit::{Day, Month, Week, Year}; + use Unit::{Day, Month, Week, Year}; matches!(self, Day | Year | Month | Week) } #[inline] #[must_use] pub fn is_time_unit(&self) -> bool { - use TemporalUnit::{Hour, Microsecond, Millisecond, Minute, Nanosecond, Second}; + use Unit::{Hour, Microsecond, Millisecond, Minute, Nanosecond, Second}; matches!( self, Hour | Minute | Second | Millisecond | Microsecond | Nanosecond @@ -503,20 +497,20 @@ impl TemporalUnit { trait UnwrapUnit { type Result; - fn unwrap_unit_or(self, unit: TemporalUnit) -> Self::Result; + fn unwrap_unit_or(self, unit: Unit) -> Self::Result; } -impl UnwrapUnit for Option { - type Result = TemporalUnit; - fn unwrap_unit_or(self, unit: TemporalUnit) -> Self::Result { - if self == Some(TemporalUnit::Auto) { +impl UnwrapUnit for Option { + type Result = Unit; + fn unwrap_unit_or(self, unit: Unit) -> Self::Result { + if self == Some(Unit::Auto) { return unit; } self.unwrap_or(unit) } } -impl From for TemporalUnit { +impl From for Unit { fn from(value: usize) -> Self { match value { 10 => Self::Year, @@ -534,26 +528,26 @@ impl From for TemporalUnit { } } -impl Add for TemporalUnit { - type Output = TemporalUnit; +impl Add for Unit { + type Output = Unit; fn add(self, rhs: usize) -> Self::Output { - TemporalUnit::from(self as usize + rhs) + Unit::from(self as usize + rhs) } } -/// A parsing error for `TemporalUnit` +/// A parsing error for `Unit` #[derive(Debug, Clone, Copy)] -pub struct ParseTemporalUnitError; +pub struct ParseUnitError; -impl fmt::Display for ParseTemporalUnitError { +impl fmt::Display for ParseUnitError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.write_str("provided string was not a valid TemporalUnit") + f.write_str("provided string was not a valid Unit") } } -impl FromStr for TemporalUnit { - type Err = ParseTemporalUnitError; +impl FromStr for Unit { + type Err = ParseUnitError; fn from_str(s: &str) -> Result { match s { @@ -568,12 +562,12 @@ impl FromStr for TemporalUnit { "millisecond" | "milliseconds" => Ok(Self::Millisecond), "microsecond" | "microseconds" => Ok(Self::Microsecond), "nanosecond" | "nanoseconds" => Ok(Self::Nanosecond), - _ => Err(ParseTemporalUnitError), + _ => Err(ParseUnitError), } } } -impl fmt::Display for TemporalUnit { +impl fmt::Display for Unit { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { Self::Auto => "auto", @@ -782,7 +776,7 @@ impl fmt::Display for OffsetDisambiguation { /// Declares the specified `RoundingMode` for the operation. #[derive(Debug, Copy, Clone, Default)] -pub enum TemporalRoundingMode { +pub enum RoundingMode { /// Ceil RoundingMode Ceil, /// Floor RoundingMode @@ -806,7 +800,7 @@ pub enum TemporalRoundingMode { /// The `UnsignedRoundingMode` #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum TemporalUnsignedRoundingMode { +pub enum UnsignedRoundingMode { /// `Infinity` `RoundingMode` Infinity, /// `Zero` `RoundingMode` @@ -819,12 +813,12 @@ pub enum TemporalUnsignedRoundingMode { HalfEven, } -impl TemporalRoundingMode { +impl RoundingMode { #[inline] #[must_use] /// Negates the current `RoundingMode`. pub const fn negate(self) -> Self { - use TemporalRoundingMode::{ + use RoundingMode::{ Ceil, Expand, Floor, HalfCeil, HalfEven, HalfExpand, HalfFloor, HalfTrunc, Trunc, }; @@ -844,26 +838,26 @@ impl TemporalRoundingMode { #[inline] #[must_use] /// Returns the `UnsignedRoundingMode` - pub const fn get_unsigned_round_mode(self, is_positive: bool) -> TemporalUnsignedRoundingMode { - use TemporalRoundingMode::{ + pub const fn get_unsigned_round_mode(self, is_positive: bool) -> UnsignedRoundingMode { + use RoundingMode::{ Ceil, Expand, Floor, HalfCeil, HalfEven, HalfExpand, HalfFloor, HalfTrunc, Trunc, }; match self { - Ceil if is_positive => TemporalUnsignedRoundingMode::Infinity, - Ceil | Trunc => TemporalUnsignedRoundingMode::Zero, - Floor if is_positive => TemporalUnsignedRoundingMode::Zero, - Floor | Expand => TemporalUnsignedRoundingMode::Infinity, - HalfCeil if is_positive => TemporalUnsignedRoundingMode::HalfInfinity, - HalfCeil | HalfTrunc => TemporalUnsignedRoundingMode::HalfZero, - HalfFloor if is_positive => TemporalUnsignedRoundingMode::HalfZero, - HalfFloor | HalfExpand => TemporalUnsignedRoundingMode::HalfInfinity, - HalfEven => TemporalUnsignedRoundingMode::HalfEven, + Ceil if is_positive => UnsignedRoundingMode::Infinity, + Ceil | Trunc => UnsignedRoundingMode::Zero, + Floor if is_positive => UnsignedRoundingMode::Zero, + Floor | Expand => UnsignedRoundingMode::Infinity, + HalfCeil if is_positive => UnsignedRoundingMode::HalfInfinity, + HalfCeil | HalfTrunc => UnsignedRoundingMode::HalfZero, + HalfFloor if is_positive => UnsignedRoundingMode::HalfZero, + HalfFloor | HalfExpand => UnsignedRoundingMode::HalfInfinity, + HalfEven => UnsignedRoundingMode::HalfEven, } } } -impl FromStr for TemporalRoundingMode { +impl FromStr for RoundingMode { type Err = TemporalError; fn from_str(s: &str) -> Result { @@ -882,7 +876,7 @@ impl FromStr for TemporalRoundingMode { } } -impl fmt::Display for TemporalRoundingMode { +impl fmt::Display for RoundingMode { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { Self::Ceil => "ceil", diff --git a/src/rounding.rs b/src/rounding.rs index cc99f03e1..63dd7df54 100644 --- a/src/rounding.rs +++ b/src/rounding.rs @@ -1,7 +1,7 @@ //! Implementation of increment rounding functionality use crate::{ - options::{TemporalRoundingMode, TemporalUnsignedRoundingMode}, + options::{RoundingMode, UnsignedRoundingMode}, TemporalResult, TemporalUnwrap, }; @@ -30,7 +30,7 @@ pub(crate) trait Roundable: } pub(crate) trait Round { - fn round(&self, mode: TemporalRoundingMode) -> i128; + fn round(&self, mode: RoundingMode) -> i128; } #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] @@ -54,7 +54,7 @@ impl IncrementRounder { impl Round for IncrementRounder { #[inline] - fn round(&self, mode: TemporalRoundingMode) -> i128 { + fn round(&self, mode: RoundingMode) -> i128 { let unsigned_rounding_mode = mode.get_unsigned_round_mode(self.sign); let mut rounded = apply_unsigned_rounding_mode(self.dividend, self.divisor, unsigned_rounding_mode) @@ -121,7 +121,7 @@ impl Roundable for f64 { fn apply_unsigned_rounding_mode( dividend: T, divisor: T, - unsigned_rounding_mode: TemporalUnsignedRoundingMode, + unsigned_rounding_mode: UnsignedRoundingMode, ) -> u128 { // is_floor // 1. If x is equal to r1, return r1. @@ -132,11 +132,11 @@ fn apply_unsigned_rounding_mode( // 3. Assert: unsignedRoundingMode is not undefined. // 4. If unsignedRoundingMode is zero, return r1. - if unsigned_rounding_mode == TemporalUnsignedRoundingMode::Zero { + if unsigned_rounding_mode == UnsignedRoundingMode::Zero { return Roundable::result_floor(dividend, divisor); }; // 5. If unsignedRoundingMode is infinity, return r2. - if unsigned_rounding_mode == TemporalUnsignedRoundingMode::Infinity { + if unsigned_rounding_mode == UnsignedRoundingMode::Infinity { return Roundable::result_ceil(dividend, divisor); }; @@ -150,15 +150,15 @@ fn apply_unsigned_rounding_mode( Some(Ordering::Equal) => { // 10. Assert: d1 is equal to d2. // 11. If unsignedRoundingMode is half-zero, return r1. - if unsigned_rounding_mode == TemporalUnsignedRoundingMode::HalfZero { + if unsigned_rounding_mode == UnsignedRoundingMode::HalfZero { return Roundable::result_floor(dividend, divisor); }; // 12. If unsignedRoundingMode is half-infinity, return r2. - if unsigned_rounding_mode == TemporalUnsignedRoundingMode::HalfInfinity { + if unsigned_rounding_mode == UnsignedRoundingMode::HalfInfinity { return Roundable::result_ceil(dividend, divisor); }; // 13. Assert: unsignedRoundingMode is half-even. - debug_assert!(unsigned_rounding_mode == TemporalUnsignedRoundingMode::HalfEven); + debug_assert!(unsigned_rounding_mode == UnsignedRoundingMode::HalfEven); // 14. Let cardinality be (r1 / (r2 – r1)) modulo 2. // 15. If cardinality is 0, return r1. if Roundable::is_even_cardinal(dividend, divisor) { @@ -175,23 +175,23 @@ fn apply_unsigned_rounding_mode( mod tests { use core::num::NonZeroU128; - use super::{IncrementRounder, Round, TemporalRoundingMode}; + use super::{IncrementRounder, Round, RoundingMode}; #[test] fn neg_i128_rounding() { let result = IncrementRounder::::from_signed_num(-9, NonZeroU128::new(2).unwrap()) .unwrap() - .round(TemporalRoundingMode::Ceil); + .round(RoundingMode::Ceil); assert_eq!(result, -8); let result = IncrementRounder::::from_signed_num(-9, NonZeroU128::new(2).unwrap()) .unwrap() - .round(TemporalRoundingMode::Floor); + .round(RoundingMode::Floor); assert_eq!(result, -10); let result = IncrementRounder::::from_signed_num(-14, NonZeroU128::new(3).unwrap()) .unwrap() - .round(TemporalRoundingMode::HalfExpand); + .round(RoundingMode::HalfExpand); assert_eq!(result, -15); } @@ -199,12 +199,12 @@ mod tests { fn neg_f64_rounding() { let result = IncrementRounder::::from_signed_num(-8.5, NonZeroU128::new(1).unwrap()) .unwrap() - .round(TemporalRoundingMode::Ceil); + .round(RoundingMode::Ceil); assert_eq!(result, -8); let result = IncrementRounder::::from_signed_num(-8.5, NonZeroU128::new(1).unwrap()) .unwrap() - .round(TemporalRoundingMode::Floor); + .round(RoundingMode::Floor); assert_eq!(result, -9); } @@ -215,7 +215,7 @@ mod tests { NonZeroU128::new(1800000000000).unwrap(), ) .unwrap() - .round(TemporalRoundingMode::HalfExpand); + .round(RoundingMode::HalfExpand); assert_eq!(result, -84083400000000000); } diff --git a/temporal_capi/bindings/cpp/temporal_rs/Calendar.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/Calendar.d.hpp index 08ef82a9c..aee55af4a 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/Calendar.d.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/Calendar.d.hpp @@ -26,7 +26,7 @@ struct PartialDate; struct TemporalError; class AnyCalendarKind; class ArithmeticOverflow; -class TemporalUnit; +class Unit; } @@ -56,7 +56,7 @@ class Calendar { inline diplomat::result, temporal_rs::TemporalError> date_add(temporal_rs::IsoDate date, const temporal_rs::Duration& duration, temporal_rs::ArithmeticOverflow overflow) const; - inline diplomat::result, temporal_rs::TemporalError> date_until(temporal_rs::IsoDate one, temporal_rs::IsoDate two, temporal_rs::TemporalUnit largest_unit) const; + inline diplomat::result, temporal_rs::TemporalError> date_until(temporal_rs::IsoDate one, temporal_rs::IsoDate two, temporal_rs::Unit largest_unit) const; inline diplomat::result era(temporal_rs::IsoDate date) const; diff --git a/temporal_capi/bindings/cpp/temporal_rs/Calendar.hpp b/temporal_capi/bindings/cpp/temporal_rs/Calendar.hpp index 838148179..101a127e2 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/Calendar.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/Calendar.hpp @@ -20,7 +20,7 @@ #include "PlainMonthDay.hpp" #include "PlainYearMonth.hpp" #include "TemporalError.hpp" -#include "TemporalUnit.hpp" +#include "Unit.hpp" namespace temporal_rs { @@ -49,7 +49,7 @@ namespace capi { temporal_rs_Calendar_date_add_result temporal_rs_Calendar_date_add(const temporal_rs::capi::Calendar* self, temporal_rs::capi::IsoDate date, const temporal_rs::capi::Duration* duration, temporal_rs::capi::ArithmeticOverflow overflow); typedef struct temporal_rs_Calendar_date_until_result {union {temporal_rs::capi::Duration* ok; temporal_rs::capi::TemporalError err;}; bool is_ok;} temporal_rs_Calendar_date_until_result; - temporal_rs_Calendar_date_until_result temporal_rs_Calendar_date_until(const temporal_rs::capi::Calendar* self, temporal_rs::capi::IsoDate one, temporal_rs::capi::IsoDate two, temporal_rs::capi::TemporalUnit largest_unit); + temporal_rs_Calendar_date_until_result temporal_rs_Calendar_date_until(const temporal_rs::capi::Calendar* self, temporal_rs::capi::IsoDate one, temporal_rs::capi::IsoDate two, temporal_rs::capi::Unit largest_unit); typedef struct temporal_rs_Calendar_era_result {union { temporal_rs::capi::TemporalError err;}; bool is_ok;} temporal_rs_Calendar_era_result; temporal_rs_Calendar_era_result temporal_rs_Calendar_era(const temporal_rs::capi::Calendar* self, temporal_rs::capi::IsoDate date, diplomat::capi::DiplomatWrite* write); @@ -143,7 +143,7 @@ inline diplomat::result, temporal_rs::Te return result.is_ok ? diplomat::result, temporal_rs::TemporalError>(diplomat::Ok>(std::unique_ptr(temporal_rs::PlainDate::FromFFI(result.ok)))) : diplomat::result, temporal_rs::TemporalError>(diplomat::Err(temporal_rs::TemporalError::FromFFI(result.err))); } -inline diplomat::result, temporal_rs::TemporalError> temporal_rs::Calendar::date_until(temporal_rs::IsoDate one, temporal_rs::IsoDate two, temporal_rs::TemporalUnit largest_unit) const { +inline diplomat::result, temporal_rs::TemporalError> temporal_rs::Calendar::date_until(temporal_rs::IsoDate one, temporal_rs::IsoDate two, temporal_rs::Unit largest_unit) const { auto result = temporal_rs::capi::temporal_rs_Calendar_date_until(this->AsFFI(), one.AsFFI(), two.AsFFI(), diff --git a/temporal_capi/bindings/cpp/temporal_rs/DifferenceSettings.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/DifferenceSettings.d.hpp index e6232bef2..492121215 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/DifferenceSettings.d.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/DifferenceSettings.d.hpp @@ -9,21 +9,21 @@ #include #include #include "../diplomat_runtime.hpp" -#include "TemporalRoundingMode.d.hpp" -#include "TemporalUnit.d.hpp" +#include "RoundingMode.d.hpp" +#include "Unit.d.hpp" namespace temporal_rs { -class TemporalRoundingMode; -class TemporalUnit; +class RoundingMode; +class Unit; } namespace temporal_rs { namespace capi { struct DifferenceSettings { - temporal_rs::capi::TemporalUnit_option largest_unit; - temporal_rs::capi::TemporalUnit_option smallest_unit; - temporal_rs::capi::TemporalRoundingMode_option rounding_mode; + temporal_rs::capi::Unit_option largest_unit; + temporal_rs::capi::Unit_option smallest_unit; + temporal_rs::capi::RoundingMode_option rounding_mode; diplomat::capi::OptionU32 increment; }; @@ -34,9 +34,9 @@ namespace capi { namespace temporal_rs { struct DifferenceSettings { - std::optional largest_unit; - std::optional smallest_unit; - std::optional rounding_mode; + std::optional largest_unit; + std::optional smallest_unit; + std::optional rounding_mode; std::optional increment; inline temporal_rs::capi::DifferenceSettings AsFFI() const; diff --git a/temporal_capi/bindings/cpp/temporal_rs/DifferenceSettings.hpp b/temporal_capi/bindings/cpp/temporal_rs/DifferenceSettings.hpp index d09929f4a..228837bd0 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/DifferenceSettings.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/DifferenceSettings.hpp @@ -11,8 +11,8 @@ #include #include #include "../diplomat_runtime.hpp" -#include "TemporalRoundingMode.hpp" -#include "TemporalUnit.hpp" +#include "RoundingMode.hpp" +#include "Unit.hpp" namespace temporal_rs { @@ -27,18 +27,18 @@ namespace capi { inline temporal_rs::capi::DifferenceSettings temporal_rs::DifferenceSettings::AsFFI() const { return temporal_rs::capi::DifferenceSettings { - /* .largest_unit = */ largest_unit.has_value() ? (temporal_rs::capi::TemporalUnit_option{ { largest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalUnit_option{ {}, false }), - /* .smallest_unit = */ smallest_unit.has_value() ? (temporal_rs::capi::TemporalUnit_option{ { smallest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalUnit_option{ {}, false }), - /* .rounding_mode = */ rounding_mode.has_value() ? (temporal_rs::capi::TemporalRoundingMode_option{ { rounding_mode.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalRoundingMode_option{ {}, false }), + /* .largest_unit = */ largest_unit.has_value() ? (temporal_rs::capi::Unit_option{ { largest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::Unit_option{ {}, false }), + /* .smallest_unit = */ smallest_unit.has_value() ? (temporal_rs::capi::Unit_option{ { smallest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::Unit_option{ {}, false }), + /* .rounding_mode = */ rounding_mode.has_value() ? (temporal_rs::capi::RoundingMode_option{ { rounding_mode.value().AsFFI() }, true }) : (temporal_rs::capi::RoundingMode_option{ {}, false }), /* .increment = */ increment.has_value() ? (diplomat::capi::OptionU32{ { increment.value() }, true }) : (diplomat::capi::OptionU32{ {}, false }), }; } inline temporal_rs::DifferenceSettings temporal_rs::DifferenceSettings::FromFFI(temporal_rs::capi::DifferenceSettings c_struct) { return temporal_rs::DifferenceSettings { - /* .largest_unit = */ c_struct.largest_unit.is_ok ? std::optional(temporal_rs::TemporalUnit::FromFFI(c_struct.largest_unit.ok)) : std::nullopt, - /* .smallest_unit = */ c_struct.smallest_unit.is_ok ? std::optional(temporal_rs::TemporalUnit::FromFFI(c_struct.smallest_unit.ok)) : std::nullopt, - /* .rounding_mode = */ c_struct.rounding_mode.is_ok ? std::optional(temporal_rs::TemporalRoundingMode::FromFFI(c_struct.rounding_mode.ok)) : std::nullopt, + /* .largest_unit = */ c_struct.largest_unit.is_ok ? std::optional(temporal_rs::Unit::FromFFI(c_struct.largest_unit.ok)) : std::nullopt, + /* .smallest_unit = */ c_struct.smallest_unit.is_ok ? std::optional(temporal_rs::Unit::FromFFI(c_struct.smallest_unit.ok)) : std::nullopt, + /* .rounding_mode = */ c_struct.rounding_mode.is_ok ? std::optional(temporal_rs::RoundingMode::FromFFI(c_struct.rounding_mode.ok)) : std::nullopt, /* .increment = */ c_struct.increment.is_ok ? std::optional(c_struct.increment.ok) : std::nullopt, }; } diff --git a/temporal_capi/bindings/cpp/temporal_rs/PlainTime.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/PlainTime.d.hpp index eddb36757..312f22f57 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/PlainTime.d.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/PlainTime.d.hpp @@ -22,8 +22,8 @@ struct PartialTime; struct TemporalError; struct ToStringRoundingOptions; class ArithmeticOverflow; -class TemporalRoundingMode; -class TemporalUnit; +class RoundingMode; +class Unit; } @@ -69,7 +69,7 @@ class PlainTime { inline diplomat::result, temporal_rs::TemporalError> since(const temporal_rs::PlainTime& other, temporal_rs::DifferenceSettings settings) const; - inline diplomat::result, temporal_rs::TemporalError> round(temporal_rs::TemporalUnit smallest_unit, std::optional rounding_increment, std::optional rounding_mode) const; + inline diplomat::result, temporal_rs::TemporalError> round(temporal_rs::Unit smallest_unit, std::optional rounding_increment, std::optional rounding_mode) const; inline diplomat::result to_ixdtf_string(temporal_rs::ToStringRoundingOptions options) const; diff --git a/temporal_capi/bindings/cpp/temporal_rs/PlainTime.hpp b/temporal_capi/bindings/cpp/temporal_rs/PlainTime.hpp index d4caeca64..50609376b 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/PlainTime.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/PlainTime.hpp @@ -15,11 +15,11 @@ #include "DifferenceSettings.hpp" #include "Duration.hpp" #include "PartialTime.hpp" +#include "RoundingMode.hpp" #include "TemporalError.hpp" -#include "TemporalRoundingMode.hpp" -#include "TemporalUnit.hpp" #include "TimeDuration.hpp" #include "ToStringRoundingOptions.hpp" +#include "Unit.hpp" namespace temporal_rs { @@ -69,7 +69,7 @@ namespace capi { temporal_rs_PlainTime_since_result temporal_rs_PlainTime_since(const temporal_rs::capi::PlainTime* self, const temporal_rs::capi::PlainTime* other, temporal_rs::capi::DifferenceSettings settings); typedef struct temporal_rs_PlainTime_round_result {union {temporal_rs::capi::PlainTime* ok; temporal_rs::capi::TemporalError err;}; bool is_ok;} temporal_rs_PlainTime_round_result; - temporal_rs_PlainTime_round_result temporal_rs_PlainTime_round(const temporal_rs::capi::PlainTime* self, temporal_rs::capi::TemporalUnit smallest_unit, diplomat::capi::OptionF64 rounding_increment, temporal_rs::capi::TemporalRoundingMode_option rounding_mode); + temporal_rs_PlainTime_round_result temporal_rs_PlainTime_round(const temporal_rs::capi::PlainTime* self, temporal_rs::capi::Unit smallest_unit, diplomat::capi::OptionF64 rounding_increment, temporal_rs::capi::RoundingMode_option rounding_mode); typedef struct temporal_rs_PlainTime_to_ixdtf_string_result {union { temporal_rs::capi::TemporalError err;}; bool is_ok;} temporal_rs_PlainTime_to_ixdtf_string_result; temporal_rs_PlainTime_to_ixdtf_string_result temporal_rs_PlainTime_to_ixdtf_string(const temporal_rs::capi::PlainTime* self, temporal_rs::capi::ToStringRoundingOptions options, diplomat::capi::DiplomatWrite* write); @@ -182,11 +182,11 @@ inline diplomat::result, temporal_rs::Tem return result.is_ok ? diplomat::result, temporal_rs::TemporalError>(diplomat::Ok>(std::unique_ptr(temporal_rs::Duration::FromFFI(result.ok)))) : diplomat::result, temporal_rs::TemporalError>(diplomat::Err(temporal_rs::TemporalError::FromFFI(result.err))); } -inline diplomat::result, temporal_rs::TemporalError> temporal_rs::PlainTime::round(temporal_rs::TemporalUnit smallest_unit, std::optional rounding_increment, std::optional rounding_mode) const { +inline diplomat::result, temporal_rs::TemporalError> temporal_rs::PlainTime::round(temporal_rs::Unit smallest_unit, std::optional rounding_increment, std::optional rounding_mode) const { auto result = temporal_rs::capi::temporal_rs_PlainTime_round(this->AsFFI(), smallest_unit.AsFFI(), rounding_increment.has_value() ? (diplomat::capi::OptionF64{ { rounding_increment.value() }, true }) : (diplomat::capi::OptionF64{ {}, false }), - rounding_mode.has_value() ? (temporal_rs::capi::TemporalRoundingMode_option{ { rounding_mode.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalRoundingMode_option{ {}, false })); + rounding_mode.has_value() ? (temporal_rs::capi::RoundingMode_option{ { rounding_mode.value().AsFFI() }, true }) : (temporal_rs::capi::RoundingMode_option{ {}, false })); return result.is_ok ? diplomat::result, temporal_rs::TemporalError>(diplomat::Ok>(std::unique_ptr(temporal_rs::PlainTime::FromFFI(result.ok)))) : diplomat::result, temporal_rs::TemporalError>(diplomat::Err(temporal_rs::TemporalError::FromFFI(result.err))); } diff --git a/temporal_capi/bindings/cpp/temporal_rs/RoundingMode.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/RoundingMode.d.hpp new file mode 100644 index 000000000..047f00fd1 --- /dev/null +++ b/temporal_capi/bindings/cpp/temporal_rs/RoundingMode.d.hpp @@ -0,0 +1,61 @@ +#ifndef temporal_rs_RoundingMode_D_HPP +#define temporal_rs_RoundingMode_D_HPP + +#include +#include +#include +#include +#include +#include +#include +#include "../diplomat_runtime.hpp" + + +namespace temporal_rs { +namespace capi { + enum RoundingMode { + RoundingMode_Ceil = 0, + RoundingMode_Floor = 1, + RoundingMode_Expand = 2, + RoundingMode_Trunc = 3, + RoundingMode_HalfCeil = 4, + RoundingMode_HalfFloor = 5, + RoundingMode_HalfExpand = 6, + RoundingMode_HalfTrunc = 7, + RoundingMode_HalfEven = 8, + }; + + typedef struct RoundingMode_option {union { RoundingMode ok; }; bool is_ok; } RoundingMode_option; +} // namespace capi +} // namespace + +namespace temporal_rs { +class RoundingMode { +public: + enum Value { + Ceil = 0, + Floor = 1, + Expand = 2, + Trunc = 3, + HalfCeil = 4, + HalfFloor = 5, + HalfExpand = 6, + HalfTrunc = 7, + HalfEven = 8, + }; + + RoundingMode() = default; + // Implicit conversions between enum and ::Value + constexpr RoundingMode(Value v) : value(v) {} + constexpr operator Value() const { return value; } + // Prevent usage as boolean value + explicit operator bool() const = delete; + + inline temporal_rs::capi::RoundingMode AsFFI() const; + inline static temporal_rs::RoundingMode FromFFI(temporal_rs::capi::RoundingMode c_enum); +private: + Value value; +}; + +} // namespace +#endif // temporal_rs_RoundingMode_D_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/RoundingMode.hpp b/temporal_capi/bindings/cpp/temporal_rs/RoundingMode.hpp new file mode 100644 index 000000000..1efda5a3d --- /dev/null +++ b/temporal_capi/bindings/cpp/temporal_rs/RoundingMode.hpp @@ -0,0 +1,45 @@ +#ifndef temporal_rs_RoundingMode_HPP +#define temporal_rs_RoundingMode_HPP + +#include "RoundingMode.d.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include "../diplomat_runtime.hpp" + + +namespace temporal_rs { +namespace capi { + extern "C" { + + + } // extern "C" +} // namespace capi +} // namespace + +inline temporal_rs::capi::RoundingMode temporal_rs::RoundingMode::AsFFI() const { + return static_cast(value); +} + +inline temporal_rs::RoundingMode temporal_rs::RoundingMode::FromFFI(temporal_rs::capi::RoundingMode c_enum) { + switch (c_enum) { + case temporal_rs::capi::RoundingMode_Ceil: + case temporal_rs::capi::RoundingMode_Floor: + case temporal_rs::capi::RoundingMode_Expand: + case temporal_rs::capi::RoundingMode_Trunc: + case temporal_rs::capi::RoundingMode_HalfCeil: + case temporal_rs::capi::RoundingMode_HalfFloor: + case temporal_rs::capi::RoundingMode_HalfExpand: + case temporal_rs::capi::RoundingMode_HalfTrunc: + case temporal_rs::capi::RoundingMode_HalfEven: + return static_cast(c_enum); + default: + abort(); + } +} +#endif // temporal_rs_RoundingMode_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/RoundingOptions.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/RoundingOptions.d.hpp index f73110f35..530a6533c 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/RoundingOptions.d.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/RoundingOptions.d.hpp @@ -9,21 +9,21 @@ #include #include #include "../diplomat_runtime.hpp" -#include "TemporalRoundingMode.d.hpp" -#include "TemporalUnit.d.hpp" +#include "RoundingMode.d.hpp" +#include "Unit.d.hpp" namespace temporal_rs { -class TemporalRoundingMode; -class TemporalUnit; +class RoundingMode; +class Unit; } namespace temporal_rs { namespace capi { struct RoundingOptions { - temporal_rs::capi::TemporalUnit_option largest_unit; - temporal_rs::capi::TemporalUnit_option smallest_unit; - temporal_rs::capi::TemporalRoundingMode_option rounding_mode; + temporal_rs::capi::Unit_option largest_unit; + temporal_rs::capi::Unit_option smallest_unit; + temporal_rs::capi::RoundingMode_option rounding_mode; diplomat::capi::OptionU32 increment; }; @@ -34,9 +34,9 @@ namespace capi { namespace temporal_rs { struct RoundingOptions { - std::optional largest_unit; - std::optional smallest_unit; - std::optional rounding_mode; + std::optional largest_unit; + std::optional smallest_unit; + std::optional rounding_mode; std::optional increment; inline temporal_rs::capi::RoundingOptions AsFFI() const; diff --git a/temporal_capi/bindings/cpp/temporal_rs/RoundingOptions.hpp b/temporal_capi/bindings/cpp/temporal_rs/RoundingOptions.hpp index 275fcf35e..1c18e28e9 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/RoundingOptions.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/RoundingOptions.hpp @@ -11,8 +11,8 @@ #include #include #include "../diplomat_runtime.hpp" -#include "TemporalRoundingMode.hpp" -#include "TemporalUnit.hpp" +#include "RoundingMode.hpp" +#include "Unit.hpp" namespace temporal_rs { @@ -27,18 +27,18 @@ namespace capi { inline temporal_rs::capi::RoundingOptions temporal_rs::RoundingOptions::AsFFI() const { return temporal_rs::capi::RoundingOptions { - /* .largest_unit = */ largest_unit.has_value() ? (temporal_rs::capi::TemporalUnit_option{ { largest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalUnit_option{ {}, false }), - /* .smallest_unit = */ smallest_unit.has_value() ? (temporal_rs::capi::TemporalUnit_option{ { smallest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalUnit_option{ {}, false }), - /* .rounding_mode = */ rounding_mode.has_value() ? (temporal_rs::capi::TemporalRoundingMode_option{ { rounding_mode.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalRoundingMode_option{ {}, false }), + /* .largest_unit = */ largest_unit.has_value() ? (temporal_rs::capi::Unit_option{ { largest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::Unit_option{ {}, false }), + /* .smallest_unit = */ smallest_unit.has_value() ? (temporal_rs::capi::Unit_option{ { smallest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::Unit_option{ {}, false }), + /* .rounding_mode = */ rounding_mode.has_value() ? (temporal_rs::capi::RoundingMode_option{ { rounding_mode.value().AsFFI() }, true }) : (temporal_rs::capi::RoundingMode_option{ {}, false }), /* .increment = */ increment.has_value() ? (diplomat::capi::OptionU32{ { increment.value() }, true }) : (diplomat::capi::OptionU32{ {}, false }), }; } inline temporal_rs::RoundingOptions temporal_rs::RoundingOptions::FromFFI(temporal_rs::capi::RoundingOptions c_struct) { return temporal_rs::RoundingOptions { - /* .largest_unit = */ c_struct.largest_unit.is_ok ? std::optional(temporal_rs::TemporalUnit::FromFFI(c_struct.largest_unit.ok)) : std::nullopt, - /* .smallest_unit = */ c_struct.smallest_unit.is_ok ? std::optional(temporal_rs::TemporalUnit::FromFFI(c_struct.smallest_unit.ok)) : std::nullopt, - /* .rounding_mode = */ c_struct.rounding_mode.is_ok ? std::optional(temporal_rs::TemporalRoundingMode::FromFFI(c_struct.rounding_mode.ok)) : std::nullopt, + /* .largest_unit = */ c_struct.largest_unit.is_ok ? std::optional(temporal_rs::Unit::FromFFI(c_struct.largest_unit.ok)) : std::nullopt, + /* .smallest_unit = */ c_struct.smallest_unit.is_ok ? std::optional(temporal_rs::Unit::FromFFI(c_struct.smallest_unit.ok)) : std::nullopt, + /* .rounding_mode = */ c_struct.rounding_mode.is_ok ? std::optional(temporal_rs::RoundingMode::FromFFI(c_struct.rounding_mode.ok)) : std::nullopt, /* .increment = */ c_struct.increment.is_ok ? std::optional(c_struct.increment.ok) : std::nullopt, }; } diff --git a/temporal_capi/bindings/cpp/temporal_rs/TemporalRoundingMode.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/TemporalRoundingMode.d.hpp deleted file mode 100644 index a316ee677..000000000 --- a/temporal_capi/bindings/cpp/temporal_rs/TemporalRoundingMode.d.hpp +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef temporal_rs_TemporalRoundingMode_D_HPP -#define temporal_rs_TemporalRoundingMode_D_HPP - -#include -#include -#include -#include -#include -#include -#include -#include "../diplomat_runtime.hpp" - - -namespace temporal_rs { -namespace capi { - enum TemporalRoundingMode { - TemporalRoundingMode_Ceil = 0, - TemporalRoundingMode_Floor = 1, - TemporalRoundingMode_Expand = 2, - TemporalRoundingMode_Trunc = 3, - TemporalRoundingMode_HalfCeil = 4, - TemporalRoundingMode_HalfFloor = 5, - TemporalRoundingMode_HalfExpand = 6, - TemporalRoundingMode_HalfTrunc = 7, - TemporalRoundingMode_HalfEven = 8, - }; - - typedef struct TemporalRoundingMode_option {union { TemporalRoundingMode ok; }; bool is_ok; } TemporalRoundingMode_option; -} // namespace capi -} // namespace - -namespace temporal_rs { -class TemporalRoundingMode { -public: - enum Value { - Ceil = 0, - Floor = 1, - Expand = 2, - Trunc = 3, - HalfCeil = 4, - HalfFloor = 5, - HalfExpand = 6, - HalfTrunc = 7, - HalfEven = 8, - }; - - TemporalRoundingMode() = default; - // Implicit conversions between enum and ::Value - constexpr TemporalRoundingMode(Value v) : value(v) {} - constexpr operator Value() const { return value; } - // Prevent usage as boolean value - explicit operator bool() const = delete; - - inline temporal_rs::capi::TemporalRoundingMode AsFFI() const; - inline static temporal_rs::TemporalRoundingMode FromFFI(temporal_rs::capi::TemporalRoundingMode c_enum); -private: - Value value; -}; - -} // namespace -#endif // temporal_rs_TemporalRoundingMode_D_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/TemporalRoundingMode.hpp b/temporal_capi/bindings/cpp/temporal_rs/TemporalRoundingMode.hpp deleted file mode 100644 index 185ccd6ef..000000000 --- a/temporal_capi/bindings/cpp/temporal_rs/TemporalRoundingMode.hpp +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef temporal_rs_TemporalRoundingMode_HPP -#define temporal_rs_TemporalRoundingMode_HPP - -#include "TemporalRoundingMode.d.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include "../diplomat_runtime.hpp" - - -namespace temporal_rs { -namespace capi { - extern "C" { - - - } // extern "C" -} // namespace capi -} // namespace - -inline temporal_rs::capi::TemporalRoundingMode temporal_rs::TemporalRoundingMode::AsFFI() const { - return static_cast(value); -} - -inline temporal_rs::TemporalRoundingMode temporal_rs::TemporalRoundingMode::FromFFI(temporal_rs::capi::TemporalRoundingMode c_enum) { - switch (c_enum) { - case temporal_rs::capi::TemporalRoundingMode_Ceil: - case temporal_rs::capi::TemporalRoundingMode_Floor: - case temporal_rs::capi::TemporalRoundingMode_Expand: - case temporal_rs::capi::TemporalRoundingMode_Trunc: - case temporal_rs::capi::TemporalRoundingMode_HalfCeil: - case temporal_rs::capi::TemporalRoundingMode_HalfFloor: - case temporal_rs::capi::TemporalRoundingMode_HalfExpand: - case temporal_rs::capi::TemporalRoundingMode_HalfTrunc: - case temporal_rs::capi::TemporalRoundingMode_HalfEven: - return static_cast(c_enum); - default: - abort(); - } -} -#endif // temporal_rs_TemporalRoundingMode_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/TemporalUnit.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/TemporalUnit.d.hpp deleted file mode 100644 index dfb30e762..000000000 --- a/temporal_capi/bindings/cpp/temporal_rs/TemporalUnit.d.hpp +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef temporal_rs_TemporalUnit_D_HPP -#define temporal_rs_TemporalUnit_D_HPP - -#include -#include -#include -#include -#include -#include -#include -#include "../diplomat_runtime.hpp" - - -namespace temporal_rs { -namespace capi { - enum TemporalUnit { - TemporalUnit_Auto = 0, - TemporalUnit_Nanosecond = 1, - TemporalUnit_Microsecond = 2, - TemporalUnit_Millisecond = 3, - TemporalUnit_Second = 4, - TemporalUnit_Minute = 5, - TemporalUnit_Hour = 6, - TemporalUnit_Day = 7, - TemporalUnit_Week = 8, - TemporalUnit_Month = 9, - TemporalUnit_Year = 10, - }; - - typedef struct TemporalUnit_option {union { TemporalUnit ok; }; bool is_ok; } TemporalUnit_option; -} // namespace capi -} // namespace - -namespace temporal_rs { -class TemporalUnit { -public: - enum Value { - Auto = 0, - Nanosecond = 1, - Microsecond = 2, - Millisecond = 3, - Second = 4, - Minute = 5, - Hour = 6, - Day = 7, - Week = 8, - Month = 9, - Year = 10, - }; - - TemporalUnit() = default; - // Implicit conversions between enum and ::Value - constexpr TemporalUnit(Value v) : value(v) {} - constexpr operator Value() const { return value; } - // Prevent usage as boolean value - explicit operator bool() const = delete; - - inline temporal_rs::capi::TemporalUnit AsFFI() const; - inline static temporal_rs::TemporalUnit FromFFI(temporal_rs::capi::TemporalUnit c_enum); -private: - Value value; -}; - -} // namespace -#endif // temporal_rs_TemporalUnit_D_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/TemporalUnit.hpp b/temporal_capi/bindings/cpp/temporal_rs/TemporalUnit.hpp deleted file mode 100644 index 633cd5375..000000000 --- a/temporal_capi/bindings/cpp/temporal_rs/TemporalUnit.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef temporal_rs_TemporalUnit_HPP -#define temporal_rs_TemporalUnit_HPP - -#include "TemporalUnit.d.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include "../diplomat_runtime.hpp" - - -namespace temporal_rs { -namespace capi { - extern "C" { - - - } // extern "C" -} // namespace capi -} // namespace - -inline temporal_rs::capi::TemporalUnit temporal_rs::TemporalUnit::AsFFI() const { - return static_cast(value); -} - -inline temporal_rs::TemporalUnit temporal_rs::TemporalUnit::FromFFI(temporal_rs::capi::TemporalUnit c_enum) { - switch (c_enum) { - case temporal_rs::capi::TemporalUnit_Auto: - case temporal_rs::capi::TemporalUnit_Nanosecond: - case temporal_rs::capi::TemporalUnit_Microsecond: - case temporal_rs::capi::TemporalUnit_Millisecond: - case temporal_rs::capi::TemporalUnit_Second: - case temporal_rs::capi::TemporalUnit_Minute: - case temporal_rs::capi::TemporalUnit_Hour: - case temporal_rs::capi::TemporalUnit_Day: - case temporal_rs::capi::TemporalUnit_Week: - case temporal_rs::capi::TemporalUnit_Month: - case temporal_rs::capi::TemporalUnit_Year: - return static_cast(c_enum); - default: - abort(); - } -} -#endif // temporal_rs_TemporalUnit_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/TemporalUnsignedRoundingMode.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/TemporalUnsignedRoundingMode.d.hpp deleted file mode 100644 index cead5a5e5..000000000 --- a/temporal_capi/bindings/cpp/temporal_rs/TemporalUnsignedRoundingMode.d.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef temporal_rs_TemporalUnsignedRoundingMode_D_HPP -#define temporal_rs_TemporalUnsignedRoundingMode_D_HPP - -#include -#include -#include -#include -#include -#include -#include -#include "../diplomat_runtime.hpp" - - -namespace temporal_rs { -namespace capi { - enum TemporalUnsignedRoundingMode { - TemporalUnsignedRoundingMode_Infinity = 0, - TemporalUnsignedRoundingMode_Zero = 1, - TemporalUnsignedRoundingMode_HalfInfinity = 2, - TemporalUnsignedRoundingMode_HalfZero = 3, - TemporalUnsignedRoundingMode_HalfEven = 4, - }; - - typedef struct TemporalUnsignedRoundingMode_option {union { TemporalUnsignedRoundingMode ok; }; bool is_ok; } TemporalUnsignedRoundingMode_option; -} // namespace capi -} // namespace - -namespace temporal_rs { -class TemporalUnsignedRoundingMode { -public: - enum Value { - Infinity = 0, - Zero = 1, - HalfInfinity = 2, - HalfZero = 3, - HalfEven = 4, - }; - - TemporalUnsignedRoundingMode() = default; - // Implicit conversions between enum and ::Value - constexpr TemporalUnsignedRoundingMode(Value v) : value(v) {} - constexpr operator Value() const { return value; } - // Prevent usage as boolean value - explicit operator bool() const = delete; - - inline temporal_rs::capi::TemporalUnsignedRoundingMode AsFFI() const; - inline static temporal_rs::TemporalUnsignedRoundingMode FromFFI(temporal_rs::capi::TemporalUnsignedRoundingMode c_enum); -private: - Value value; -}; - -} // namespace -#endif // temporal_rs_TemporalUnsignedRoundingMode_D_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/TemporalUnsignedRoundingMode.hpp b/temporal_capi/bindings/cpp/temporal_rs/TemporalUnsignedRoundingMode.hpp deleted file mode 100644 index a03bca4d4..000000000 --- a/temporal_capi/bindings/cpp/temporal_rs/TemporalUnsignedRoundingMode.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef temporal_rs_TemporalUnsignedRoundingMode_HPP -#define temporal_rs_TemporalUnsignedRoundingMode_HPP - -#include "TemporalUnsignedRoundingMode.d.hpp" - -#include -#include -#include -#include -#include -#include -#include -#include "../diplomat_runtime.hpp" - - -namespace temporal_rs { -namespace capi { - extern "C" { - - - } // extern "C" -} // namespace capi -} // namespace - -inline temporal_rs::capi::TemporalUnsignedRoundingMode temporal_rs::TemporalUnsignedRoundingMode::AsFFI() const { - return static_cast(value); -} - -inline temporal_rs::TemporalUnsignedRoundingMode temporal_rs::TemporalUnsignedRoundingMode::FromFFI(temporal_rs::capi::TemporalUnsignedRoundingMode c_enum) { - switch (c_enum) { - case temporal_rs::capi::TemporalUnsignedRoundingMode_Infinity: - case temporal_rs::capi::TemporalUnsignedRoundingMode_Zero: - case temporal_rs::capi::TemporalUnsignedRoundingMode_HalfInfinity: - case temporal_rs::capi::TemporalUnsignedRoundingMode_HalfZero: - case temporal_rs::capi::TemporalUnsignedRoundingMode_HalfEven: - return static_cast(c_enum); - default: - abort(); - } -} -#endif // temporal_rs_TemporalUnsignedRoundingMode_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/ToStringRoundingOptions.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/ToStringRoundingOptions.d.hpp index da1940d26..1f04fff60 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/ToStringRoundingOptions.d.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/ToStringRoundingOptions.d.hpp @@ -10,13 +10,13 @@ #include #include "../diplomat_runtime.hpp" #include "Precision.d.hpp" -#include "TemporalRoundingMode.d.hpp" -#include "TemporalUnit.d.hpp" +#include "RoundingMode.d.hpp" +#include "Unit.d.hpp" namespace temporal_rs { struct Precision; -class TemporalRoundingMode; -class TemporalUnit; +class RoundingMode; +class Unit; } @@ -24,8 +24,8 @@ namespace temporal_rs { namespace capi { struct ToStringRoundingOptions { temporal_rs::capi::Precision precision; - temporal_rs::capi::TemporalUnit_option smallest_unit; - temporal_rs::capi::TemporalRoundingMode_option rounding_mode; + temporal_rs::capi::Unit_option smallest_unit; + temporal_rs::capi::RoundingMode_option rounding_mode; }; typedef struct ToStringRoundingOptions_option {union { ToStringRoundingOptions ok; }; bool is_ok; } ToStringRoundingOptions_option; @@ -36,8 +36,8 @@ namespace capi { namespace temporal_rs { struct ToStringRoundingOptions { temporal_rs::Precision precision; - std::optional smallest_unit; - std::optional rounding_mode; + std::optional smallest_unit; + std::optional rounding_mode; inline temporal_rs::capi::ToStringRoundingOptions AsFFI() const; inline static temporal_rs::ToStringRoundingOptions FromFFI(temporal_rs::capi::ToStringRoundingOptions c_struct); diff --git a/temporal_capi/bindings/cpp/temporal_rs/ToStringRoundingOptions.hpp b/temporal_capi/bindings/cpp/temporal_rs/ToStringRoundingOptions.hpp index 2a2166c09..e28ca00ec 100644 --- a/temporal_capi/bindings/cpp/temporal_rs/ToStringRoundingOptions.hpp +++ b/temporal_capi/bindings/cpp/temporal_rs/ToStringRoundingOptions.hpp @@ -12,8 +12,8 @@ #include #include "../diplomat_runtime.hpp" #include "Precision.hpp" -#include "TemporalRoundingMode.hpp" -#include "TemporalUnit.hpp" +#include "RoundingMode.hpp" +#include "Unit.hpp" namespace temporal_rs { @@ -29,16 +29,16 @@ namespace capi { inline temporal_rs::capi::ToStringRoundingOptions temporal_rs::ToStringRoundingOptions::AsFFI() const { return temporal_rs::capi::ToStringRoundingOptions { /* .precision = */ precision.AsFFI(), - /* .smallest_unit = */ smallest_unit.has_value() ? (temporal_rs::capi::TemporalUnit_option{ { smallest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalUnit_option{ {}, false }), - /* .rounding_mode = */ rounding_mode.has_value() ? (temporal_rs::capi::TemporalRoundingMode_option{ { rounding_mode.value().AsFFI() }, true }) : (temporal_rs::capi::TemporalRoundingMode_option{ {}, false }), + /* .smallest_unit = */ smallest_unit.has_value() ? (temporal_rs::capi::Unit_option{ { smallest_unit.value().AsFFI() }, true }) : (temporal_rs::capi::Unit_option{ {}, false }), + /* .rounding_mode = */ rounding_mode.has_value() ? (temporal_rs::capi::RoundingMode_option{ { rounding_mode.value().AsFFI() }, true }) : (temporal_rs::capi::RoundingMode_option{ {}, false }), }; } inline temporal_rs::ToStringRoundingOptions temporal_rs::ToStringRoundingOptions::FromFFI(temporal_rs::capi::ToStringRoundingOptions c_struct) { return temporal_rs::ToStringRoundingOptions { /* .precision = */ temporal_rs::Precision::FromFFI(c_struct.precision), - /* .smallest_unit = */ c_struct.smallest_unit.is_ok ? std::optional(temporal_rs::TemporalUnit::FromFFI(c_struct.smallest_unit.ok)) : std::nullopt, - /* .rounding_mode = */ c_struct.rounding_mode.is_ok ? std::optional(temporal_rs::TemporalRoundingMode::FromFFI(c_struct.rounding_mode.ok)) : std::nullopt, + /* .smallest_unit = */ c_struct.smallest_unit.is_ok ? std::optional(temporal_rs::Unit::FromFFI(c_struct.smallest_unit.ok)) : std::nullopt, + /* .rounding_mode = */ c_struct.rounding_mode.is_ok ? std::optional(temporal_rs::RoundingMode::FromFFI(c_struct.rounding_mode.ok)) : std::nullopt, }; } diff --git a/temporal_capi/bindings/cpp/temporal_rs/Unit.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/Unit.d.hpp new file mode 100644 index 000000000..23a3fc4b1 --- /dev/null +++ b/temporal_capi/bindings/cpp/temporal_rs/Unit.d.hpp @@ -0,0 +1,65 @@ +#ifndef temporal_rs_Unit_D_HPP +#define temporal_rs_Unit_D_HPP + +#include +#include +#include +#include +#include +#include +#include +#include "../diplomat_runtime.hpp" + + +namespace temporal_rs { +namespace capi { + enum Unit { + Unit_Auto = 0, + Unit_Nanosecond = 1, + Unit_Microsecond = 2, + Unit_Millisecond = 3, + Unit_Second = 4, + Unit_Minute = 5, + Unit_Hour = 6, + Unit_Day = 7, + Unit_Week = 8, + Unit_Month = 9, + Unit_Year = 10, + }; + + typedef struct Unit_option {union { Unit ok; }; bool is_ok; } Unit_option; +} // namespace capi +} // namespace + +namespace temporal_rs { +class Unit { +public: + enum Value { + Auto = 0, + Nanosecond = 1, + Microsecond = 2, + Millisecond = 3, + Second = 4, + Minute = 5, + Hour = 6, + Day = 7, + Week = 8, + Month = 9, + Year = 10, + }; + + Unit() = default; + // Implicit conversions between enum and ::Value + constexpr Unit(Value v) : value(v) {} + constexpr operator Value() const { return value; } + // Prevent usage as boolean value + explicit operator bool() const = delete; + + inline temporal_rs::capi::Unit AsFFI() const; + inline static temporal_rs::Unit FromFFI(temporal_rs::capi::Unit c_enum); +private: + Value value; +}; + +} // namespace +#endif // temporal_rs_Unit_D_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/Unit.hpp b/temporal_capi/bindings/cpp/temporal_rs/Unit.hpp new file mode 100644 index 000000000..eab815f5b --- /dev/null +++ b/temporal_capi/bindings/cpp/temporal_rs/Unit.hpp @@ -0,0 +1,47 @@ +#ifndef temporal_rs_Unit_HPP +#define temporal_rs_Unit_HPP + +#include "Unit.d.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include "../diplomat_runtime.hpp" + + +namespace temporal_rs { +namespace capi { + extern "C" { + + + } // extern "C" +} // namespace capi +} // namespace + +inline temporal_rs::capi::Unit temporal_rs::Unit::AsFFI() const { + return static_cast(value); +} + +inline temporal_rs::Unit temporal_rs::Unit::FromFFI(temporal_rs::capi::Unit c_enum) { + switch (c_enum) { + case temporal_rs::capi::Unit_Auto: + case temporal_rs::capi::Unit_Nanosecond: + case temporal_rs::capi::Unit_Microsecond: + case temporal_rs::capi::Unit_Millisecond: + case temporal_rs::capi::Unit_Second: + case temporal_rs::capi::Unit_Minute: + case temporal_rs::capi::Unit_Hour: + case temporal_rs::capi::Unit_Day: + case temporal_rs::capi::Unit_Week: + case temporal_rs::capi::Unit_Month: + case temporal_rs::capi::Unit_Year: + return static_cast(c_enum); + default: + abort(); + } +} +#endif // temporal_rs_Unit_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/UnsignedRoundingMode.d.hpp b/temporal_capi/bindings/cpp/temporal_rs/UnsignedRoundingMode.d.hpp new file mode 100644 index 000000000..8e97866a1 --- /dev/null +++ b/temporal_capi/bindings/cpp/temporal_rs/UnsignedRoundingMode.d.hpp @@ -0,0 +1,53 @@ +#ifndef temporal_rs_UnsignedRoundingMode_D_HPP +#define temporal_rs_UnsignedRoundingMode_D_HPP + +#include +#include +#include +#include +#include +#include +#include +#include "../diplomat_runtime.hpp" + + +namespace temporal_rs { +namespace capi { + enum UnsignedRoundingMode { + UnsignedRoundingMode_Infinity = 0, + UnsignedRoundingMode_Zero = 1, + UnsignedRoundingMode_HalfInfinity = 2, + UnsignedRoundingMode_HalfZero = 3, + UnsignedRoundingMode_HalfEven = 4, + }; + + typedef struct UnsignedRoundingMode_option {union { UnsignedRoundingMode ok; }; bool is_ok; } UnsignedRoundingMode_option; +} // namespace capi +} // namespace + +namespace temporal_rs { +class UnsignedRoundingMode { +public: + enum Value { + Infinity = 0, + Zero = 1, + HalfInfinity = 2, + HalfZero = 3, + HalfEven = 4, + }; + + UnsignedRoundingMode() = default; + // Implicit conversions between enum and ::Value + constexpr UnsignedRoundingMode(Value v) : value(v) {} + constexpr operator Value() const { return value; } + // Prevent usage as boolean value + explicit operator bool() const = delete; + + inline temporal_rs::capi::UnsignedRoundingMode AsFFI() const; + inline static temporal_rs::UnsignedRoundingMode FromFFI(temporal_rs::capi::UnsignedRoundingMode c_enum); +private: + Value value; +}; + +} // namespace +#endif // temporal_rs_UnsignedRoundingMode_D_HPP diff --git a/temporal_capi/bindings/cpp/temporal_rs/UnsignedRoundingMode.hpp b/temporal_capi/bindings/cpp/temporal_rs/UnsignedRoundingMode.hpp new file mode 100644 index 000000000..5225184cf --- /dev/null +++ b/temporal_capi/bindings/cpp/temporal_rs/UnsignedRoundingMode.hpp @@ -0,0 +1,41 @@ +#ifndef temporal_rs_UnsignedRoundingMode_HPP +#define temporal_rs_UnsignedRoundingMode_HPP + +#include "UnsignedRoundingMode.d.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include "../diplomat_runtime.hpp" + + +namespace temporal_rs { +namespace capi { + extern "C" { + + + } // extern "C" +} // namespace capi +} // namespace + +inline temporal_rs::capi::UnsignedRoundingMode temporal_rs::UnsignedRoundingMode::AsFFI() const { + return static_cast(value); +} + +inline temporal_rs::UnsignedRoundingMode temporal_rs::UnsignedRoundingMode::FromFFI(temporal_rs::capi::UnsignedRoundingMode c_enum) { + switch (c_enum) { + case temporal_rs::capi::UnsignedRoundingMode_Infinity: + case temporal_rs::capi::UnsignedRoundingMode_Zero: + case temporal_rs::capi::UnsignedRoundingMode_HalfInfinity: + case temporal_rs::capi::UnsignedRoundingMode_HalfZero: + case temporal_rs::capi::UnsignedRoundingMode_HalfEven: + return static_cast(c_enum); + default: + abort(); + } +} +#endif // temporal_rs_UnsignedRoundingMode_HPP diff --git a/temporal_capi/src/calendar.rs b/temporal_capi/src/calendar.rs index bcbeabbf5..014e6e7c7 100644 --- a/temporal_capi/src/calendar.rs +++ b/temporal_capi/src/calendar.rs @@ -5,7 +5,7 @@ pub mod ffi { use crate::duration::ffi::Duration; use crate::error::ffi::TemporalError; use crate::iso::ffi::IsoDate; - use crate::options::ffi::{ArithmeticOverflow, TemporalUnit}; + use crate::options::ffi::{ArithmeticOverflow, Unit}; use crate::plain_date::ffi::{PartialDate, PlainDate}; use crate::plain_month_day::ffi::PlainMonthDay; use crate::plain_year_month::ffi::PlainYearMonth; @@ -109,7 +109,7 @@ pub mod ffi { &self, one: IsoDate, two: IsoDate, - largest_unit: TemporalUnit, + largest_unit: Unit, ) -> Result, TemporalError> { self.0 .date_until(&one.into(), &two.into(), largest_unit.into()) diff --git a/temporal_capi/src/options.rs b/temporal_capi/src/options.rs index 2798670af..b89bf5794 100644 --- a/temporal_capi/src/options.rs +++ b/temporal_capi/src/options.rs @@ -55,8 +55,8 @@ pub mod ffi { Reject, } - #[diplomat::enum_convert(options::TemporalRoundingMode)] - pub enum TemporalRoundingMode { + #[diplomat::enum_convert(options::RoundingMode)] + pub enum RoundingMode { Ceil, Floor, Expand, @@ -68,8 +68,8 @@ pub mod ffi { HalfEven, } - #[diplomat::enum_convert(options::TemporalUnit)] - pub enum TemporalUnit { + #[diplomat::enum_convert(options::Unit)] + pub enum Unit { Auto = 0, Nanosecond = 1, Microsecond = 2, @@ -83,8 +83,8 @@ pub mod ffi { Year = 10, } - #[diplomat::enum_convert(options::TemporalUnsignedRoundingMode)] - pub enum TemporalUnsignedRoundingMode { + #[diplomat::enum_convert(options::UnsignedRoundingMode)] + pub enum UnsignedRoundingMode { Infinity, Zero, HalfInfinity, @@ -100,22 +100,22 @@ pub mod ffi { } pub struct RoundingOptions { - pub largest_unit: DiplomatOption, - pub smallest_unit: DiplomatOption, - pub rounding_mode: DiplomatOption, + pub largest_unit: DiplomatOption, + pub smallest_unit: DiplomatOption, + pub rounding_mode: DiplomatOption, pub increment: DiplomatOption, } pub struct ToStringRoundingOptions { pub precision: Precision, - pub smallest_unit: DiplomatOption, - pub rounding_mode: DiplomatOption, + pub smallest_unit: DiplomatOption, + pub rounding_mode: DiplomatOption, } pub struct DifferenceSettings { - pub largest_unit: DiplomatOption, - pub smallest_unit: DiplomatOption, - pub rounding_mode: DiplomatOption, + pub largest_unit: DiplomatOption, + pub smallest_unit: DiplomatOption, + pub rounding_mode: DiplomatOption, pub increment: DiplomatOption, } } diff --git a/temporal_capi/src/plain_time.rs b/temporal_capi/src/plain_time.rs index a20a63b41..934307ca0 100644 --- a/temporal_capi/src/plain_time.rs +++ b/temporal_capi/src/plain_time.rs @@ -6,8 +6,7 @@ pub mod ffi { use crate::duration::ffi::{Duration, TimeDuration}; use crate::error::ffi::TemporalError; use crate::options::ffi::{ - ArithmeticOverflow, DifferenceSettings, TemporalRoundingMode, TemporalUnit, - ToStringRoundingOptions, + ArithmeticOverflow, DifferenceSettings, RoundingMode, ToStringRoundingOptions, Unit, }; use diplomat_runtime::{DiplomatOption, DiplomatWrite}; use std::fmt::Write; @@ -147,9 +146,9 @@ pub mod ffi { } pub fn round( &self, - smallest_unit: TemporalUnit, + smallest_unit: Unit, rounding_increment: Option, - rounding_mode: Option, + rounding_mode: Option, ) -> Result, TemporalError> { self.0 .round(