Skip to content

Commit

Permalink
DateTime Options migration (#5811)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Nov 14, 2024
1 parent ef5fda1 commit dd9bad0
Show file tree
Hide file tree
Showing 42 changed files with 577 additions and 1,217 deletions.
9 changes: 4 additions & 5 deletions components/datetime/benches/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_datetime::{neo_skeleton, options::components};
use icu_datetime::{options, provider::skeleton::components};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -25,7 +25,8 @@ pub struct TestOptions {
pub length: Option<TestOptionsLength>,
pub components: Option<TestComponentsBag>,
pub semantic: Option<icu_datetime::fieldset::dynamic::CompositeFieldSet>,
pub preferences: Option<icu_datetime::options::preferences::Bag>,
#[serde(rename = "hourCycle")]
pub hour_cycle: Option<TestHourCycle>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -58,11 +59,9 @@ pub struct TestComponentsBag {
pub hour: Option<components::Numeric>,
pub minute: Option<components::Numeric>,
pub second: Option<components::Numeric>,
pub fractional_second: Option<neo_skeleton::FractionalSecondDigits>,
pub fractional_second: Option<options::FractionalSecondDigits>,

pub time_zone_name: Option<components::TimeZoneName>,

pub hour_cycle: Option<TestHourCycle>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
36 changes: 18 additions & 18 deletions components/datetime/benches/fixtures/tests/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
"length": "medium",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h12" }
"hourCycle": "h12"
}
},
{
Expand All @@ -270,7 +270,7 @@
"length": "medium",
"timePrecision": "secondPlus"
},
"preferences": { "hourCycle": "h12" }
"hourCycle": "h12"
}
},
{
Expand All @@ -286,7 +286,7 @@
"length": "medium",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h23" }
"hourCycle": "h23"
}
},
{
Expand All @@ -303,7 +303,7 @@
"length": "medium",
"timePrecision": "secondPlus"
},
"preferences": { "hourCycle": "h23" }
"hourCycle": "h23"
}
},
{
Expand All @@ -317,7 +317,7 @@
"length": "medium",
"timePrecision": "hourExact"
},
"preferences": { "hourCycle": "h12" }
"hourCycle": "h12"
}
},
{
Expand All @@ -332,7 +332,7 @@
"length": "medium",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h12" }
"hourCycle": "h12"
}
},
{
Expand All @@ -348,7 +348,7 @@
"length": "medium",
"timePrecision": "secondPlus"
},
"preferences": { "hourCycle": "h12" }
"hourCycle": "h12"
}
},
{
Expand All @@ -362,7 +362,7 @@
"length": "medium",
"timePrecision": "hourExact"
},
"preferences": { "hourCycle": "h23" }
"hourCycle": "h23"
}
},
{
Expand All @@ -377,7 +377,7 @@
"length": "medium",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h23" }
"hourCycle": "h23"
}
},
{
Expand All @@ -393,7 +393,7 @@
"length": "medium",
"timePrecision": "secondPlus"
},
"preferences": { "hourCycle": "h23" }
"hourCycle": "h23"
}
},
{
Expand Down Expand Up @@ -454,7 +454,7 @@
"length": "medium",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h12" }
"hourCycle": "h12"
}
},
{
Expand All @@ -474,7 +474,7 @@
"length": "long",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h12" }
"hourCycle": "h12"
}
},
{
Expand All @@ -489,7 +489,7 @@
"length": "short",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h11" }
"hourCycle": "h11"
}
},
{
Expand All @@ -504,7 +504,7 @@
"length": "short",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h24" }
"hourCycle": "h24"
}
},
{
Expand All @@ -519,7 +519,7 @@
"length": "short",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h11" }
"hourCycle": "h11"
}
},
{
Expand All @@ -534,7 +534,7 @@
"length": "short",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h12" }
"hourCycle": "h12"
}
},
{
Expand All @@ -549,7 +549,7 @@
"length": "short",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h23" }
"hourCycle": "h23"
}
},
{
Expand All @@ -564,7 +564,7 @@
"length": "short",
"timePrecision": "minuteExact"
},
"preferences": { "hourCycle": "h24" }
"hourCycle": "h24"
}
}
],
Expand Down
5 changes: 2 additions & 3 deletions components/datetime/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#[cfg(feature = "serde")]
use crate::neo_serde::*;
use crate::options::preferences::HourCycle;
use crate::raw::neo::RawNeoOptions;
use crate::scaffold::GetField;
use crate::{fields, fieldset, NeoSkeletonLength};
Expand Down Expand Up @@ -415,9 +414,9 @@ impl_attrs! {
impl TimeFieldSet {
pub(crate) const fn id_str_for_hour_cycle(
self,
hour_cycle: Option<HourCycle>,
hour_cycle: Option<fields::Hour>,
) -> &'static DataMarkerAttributes {
use HourCycle::*;
use fields::Hour::*;
match hour_cycle {
None => Self::ATTR_T,
Some(H11 | H12) => Self::ATTR_T12,
Expand Down
15 changes: 14 additions & 1 deletion components/datetime/src/fields/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

#[cfg(feature = "datagen")]
use crate::fields::FieldLength;
use crate::neo_skeleton::FractionalSecondDigits;
use crate::options::FractionalSecondDigits;
use core::{cmp::Ordering, convert::TryFrom};
use displaydoc::Display;
use icu_locale_core::preferences::extensions::unicode::keywords::HourCycle;
use icu_provider::prelude::*;
use zerovec::ule::{AsULE, UleError, ULE};

Expand Down Expand Up @@ -593,6 +594,18 @@ field_type!(
HourULE
);

impl Hour {
pub(crate) fn from_hour_cycle(hour_cycle: HourCycle) -> Self {
match hour_cycle {
HourCycle::H11 => Self::H11,
HourCycle::H12 => Self::H12,
HourCycle::H23 => Self::H23,
HourCycle::H24 => Self::H24,
_ => unreachable!(),
}
}
}

// NOTE: 'S' FractionalSecond is represented via DecimalSecond,
// so it is not included in the Second enum.

Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/fieldset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
dynamic::*,
fields,
format::neo::*,
neo_skeleton::*,
options::*,
provider::{neo::*, time_zones::tz, *},
raw::neo::RawNeoOptions,
scaffold::*,
Expand Down
3 changes: 1 addition & 2 deletions components/datetime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ mod neo_marker;
pub mod neo_pattern;
#[cfg(feature = "serde")]
mod neo_serde;
pub mod neo_skeleton;
pub mod options;
pub mod provider;
pub(crate) mod raw;
Expand All @@ -113,4 +112,4 @@ pub use format::neo::{FormattedDateTimePattern, PatternLoadError, TypedDateTimeN
pub use neo::DateTimeFormatter;
pub use neo::FixedCalendarDateTimeFormatter;
pub use neo::FormattedNeoDateTime;
pub use neo_skeleton::NeoSkeletonLength;
pub use options::NeoSkeletonLength;
12 changes: 7 additions & 5 deletions components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::format::datetime::try_write_pattern_items;
use crate::format::neo::*;
use crate::input::ExtractedInput;
use crate::neo_pattern::DateTimePattern;
use crate::options::preferences::HourCycle;
use crate::raw::neo::*;
use crate::scaffold::*;
use crate::scaffold::{
Expand All @@ -24,6 +23,7 @@ use core::fmt;
use core::marker::PhantomData;
use icu_calendar::any_calendar::IntoAnyCalendar;
use icu_calendar::AnyCalendar;
use icu_locale_core::preferences::extensions::unicode::keywords::HourCycle;
use icu_provider::prelude::*;
use writeable::TryWriteable;

Expand Down Expand Up @@ -243,12 +243,13 @@ where
P: ?Sized + AllFixedCalendarFormattingDataMarkers<C, FSet>,
L: FixedDecimalFormatterLoader,
{
// TODO: Remove this when NeoOptions is gone
// TODO: Fix this when we have DateTimePreferences
let prefs = RawPreferences {
hour_cycle: locale
.get_unicode_ext(&icu_locale_core::extensions::unicode::key!("hc"))
.as_ref()
.and_then(HourCycle::from_locale_value),
.and_then(|v| HourCycle::try_from(v).ok())
.map(crate::fields::Hour::from_hour_cycle),
};
// END TODO

Expand Down Expand Up @@ -473,12 +474,13 @@ where
P: ?Sized + AllAnyCalendarFormattingDataMarkers<FSet>,
L: FixedDecimalFormatterLoader + AnyCalendarLoader,
{
// TODO: Remove this when NeoOptions is gone
// TODO: Fix this when we have DateTimePreferences
let prefs = RawPreferences {
hour_cycle: locale
.get_unicode_ext(&icu_locale_core::extensions::unicode::key!("hc"))
.as_ref()
.and_then(HourCycle::from_locale_value),
.and_then(|v| HourCycle::try_from(v).ok())
.map(crate::fields::Hour::from_hour_cycle),
};
// END TODO

Expand Down
12 changes: 6 additions & 6 deletions components/datetime/src/neo_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! use icu::calendar::Date;
//! use icu::calendar::Gregorian;
//! use icu::datetime::fieldset::YMD;
//! use icu::datetime::neo_skeleton::Alignment;
//! use icu::datetime::options::Alignment;
//! use icu::datetime::FixedCalendarDateTimeFormatter;
//! use icu::locale::locale;
//! use writeable::assert_try_writeable_eq;
Expand Down Expand Up @@ -56,7 +56,7 @@
//! use icu::calendar::Date;
//! use icu::calendar::Gregorian;
//! use icu::datetime::fieldset::YMD;
//! use icu::datetime::neo_skeleton::YearStyle;
//! use icu::datetime::options::YearStyle;
//! use icu::datetime::FixedCalendarDateTimeFormatter;
//! use icu::locale::locale;
//! use writeable::assert_try_writeable_eq;
Expand Down Expand Up @@ -233,8 +233,8 @@
//! ```
//! use icu::calendar::Time;
//! use icu::datetime::fieldset::T;
//! use icu::datetime::neo_skeleton::FractionalSecondDigits;
//! use icu::datetime::neo_skeleton::TimePrecision;
//! use icu::datetime::options::FractionalSecondDigits;
//! use icu::datetime::options::TimePrecision;
//! use icu::datetime::FixedCalendarDateTimeFormatter;
//! use icu::locale::locale;
//! use writeable::assert_try_writeable_eq;
Expand Down Expand Up @@ -291,8 +291,8 @@
//! use icu::calendar::Gregorian;
//! use icu::calendar::Time;
//! use icu::datetime::fieldset::T;
//! use icu::datetime::neo_skeleton::FractionalSecondDigits;
//! use icu::datetime::neo_skeleton::TimePrecision;
//! use icu::datetime::options::FractionalSecondDigits;
//! use icu::datetime::options::TimePrecision;
//! use icu::datetime::FixedCalendarDateTimeFormatter;
//! use icu::locale::locale;
//! use writeable::assert_try_writeable_eq;
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/neo_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ size_test!(DateTimePattern, date_time_pattern_size, 32);
/// use icu::calendar::Gregorian;
/// use icu::datetime::fieldset::YMD;
/// use icu::datetime::neo_pattern::DateTimePattern;
/// use icu::datetime::options::components;
/// use icu::datetime::provider::skeleton::components;
/// use icu::datetime::FixedCalendarDateTimeFormatter;
/// use icu::locale::locale;
/// use writeable::assert_writeable_eq;
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/neo_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! Serde definitions for semantic skeleta
use crate::{dynamic::*, fieldset, neo_skeleton::*, raw::neo::RawNeoOptions};
use crate::{dynamic::*, fieldset, options::*, raw::neo::RawNeoOptions};
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

Expand Down
Loading

0 comments on commit dd9bad0

Please sign in to comment.