Skip to content

Commit dd9bad0

Browse files
authored
DateTime Options migration (#5811)
#1317
1 parent ef5fda1 commit dd9bad0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+577
-1217
lines changed

components/datetime/benches/fixtures/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5-
use icu_datetime::{neo_skeleton, options::components};
5+
use icu_datetime::{options, provider::skeleton::components};
66
use serde::{Deserialize, Serialize};
77

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

3132
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -58,11 +59,9 @@ pub struct TestComponentsBag {
5859
pub hour: Option<components::Numeric>,
5960
pub minute: Option<components::Numeric>,
6061
pub second: Option<components::Numeric>,
61-
pub fractional_second: Option<neo_skeleton::FractionalSecondDigits>,
62+
pub fractional_second: Option<options::FractionalSecondDigits>,
6263

6364
pub time_zone_name: Option<components::TimeZoneName>,
64-
65-
pub hour_cycle: Option<TestHourCycle>,
6665
}
6766

6867
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]

components/datetime/benches/fixtures/tests/components.json

+18-18
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
"length": "medium",
254254
"timePrecision": "minuteExact"
255255
},
256-
"preferences": { "hourCycle": "h12" }
256+
"hourCycle": "h12"
257257
}
258258
},
259259
{
@@ -270,7 +270,7 @@
270270
"length": "medium",
271271
"timePrecision": "secondPlus"
272272
},
273-
"preferences": { "hourCycle": "h12" }
273+
"hourCycle": "h12"
274274
}
275275
},
276276
{
@@ -286,7 +286,7 @@
286286
"length": "medium",
287287
"timePrecision": "minuteExact"
288288
},
289-
"preferences": { "hourCycle": "h23" }
289+
"hourCycle": "h23"
290290
}
291291
},
292292
{
@@ -303,7 +303,7 @@
303303
"length": "medium",
304304
"timePrecision": "secondPlus"
305305
},
306-
"preferences": { "hourCycle": "h23" }
306+
"hourCycle": "h23"
307307
}
308308
},
309309
{
@@ -317,7 +317,7 @@
317317
"length": "medium",
318318
"timePrecision": "hourExact"
319319
},
320-
"preferences": { "hourCycle": "h12" }
320+
"hourCycle": "h12"
321321
}
322322
},
323323
{
@@ -332,7 +332,7 @@
332332
"length": "medium",
333333
"timePrecision": "minuteExact"
334334
},
335-
"preferences": { "hourCycle": "h12" }
335+
"hourCycle": "h12"
336336
}
337337
},
338338
{
@@ -348,7 +348,7 @@
348348
"length": "medium",
349349
"timePrecision": "secondPlus"
350350
},
351-
"preferences": { "hourCycle": "h12" }
351+
"hourCycle": "h12"
352352
}
353353
},
354354
{
@@ -362,7 +362,7 @@
362362
"length": "medium",
363363
"timePrecision": "hourExact"
364364
},
365-
"preferences": { "hourCycle": "h23" }
365+
"hourCycle": "h23"
366366
}
367367
},
368368
{
@@ -377,7 +377,7 @@
377377
"length": "medium",
378378
"timePrecision": "minuteExact"
379379
},
380-
"preferences": { "hourCycle": "h23" }
380+
"hourCycle": "h23"
381381
}
382382
},
383383
{
@@ -393,7 +393,7 @@
393393
"length": "medium",
394394
"timePrecision": "secondPlus"
395395
},
396-
"preferences": { "hourCycle": "h23" }
396+
"hourCycle": "h23"
397397
}
398398
},
399399
{
@@ -454,7 +454,7 @@
454454
"length": "medium",
455455
"timePrecision": "minuteExact"
456456
},
457-
"preferences": { "hourCycle": "h12" }
457+
"hourCycle": "h12"
458458
}
459459
},
460460
{
@@ -474,7 +474,7 @@
474474
"length": "long",
475475
"timePrecision": "minuteExact"
476476
},
477-
"preferences": { "hourCycle": "h12" }
477+
"hourCycle": "h12"
478478
}
479479
},
480480
{
@@ -489,7 +489,7 @@
489489
"length": "short",
490490
"timePrecision": "minuteExact"
491491
},
492-
"preferences": { "hourCycle": "h11" }
492+
"hourCycle": "h11"
493493
}
494494
},
495495
{
@@ -504,7 +504,7 @@
504504
"length": "short",
505505
"timePrecision": "minuteExact"
506506
},
507-
"preferences": { "hourCycle": "h24" }
507+
"hourCycle": "h24"
508508
}
509509
},
510510
{
@@ -519,7 +519,7 @@
519519
"length": "short",
520520
"timePrecision": "minuteExact"
521521
},
522-
"preferences": { "hourCycle": "h11" }
522+
"hourCycle": "h11"
523523
}
524524
},
525525
{
@@ -534,7 +534,7 @@
534534
"length": "short",
535535
"timePrecision": "minuteExact"
536536
},
537-
"preferences": { "hourCycle": "h12" }
537+
"hourCycle": "h12"
538538
}
539539
},
540540
{
@@ -549,7 +549,7 @@
549549
"length": "short",
550550
"timePrecision": "minuteExact"
551551
},
552-
"preferences": { "hourCycle": "h23" }
552+
"hourCycle": "h23"
553553
}
554554
},
555555
{
@@ -564,7 +564,7 @@
564564
"length": "short",
565565
"timePrecision": "minuteExact"
566566
},
567-
"preferences": { "hourCycle": "h24" }
567+
"hourCycle": "h24"
568568
}
569569
}
570570
],

components/datetime/src/dynamic.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#[cfg(feature = "serde")]
66
use crate::neo_serde::*;
7-
use crate::options::preferences::HourCycle;
87
use crate::raw::neo::RawNeoOptions;
98
use crate::scaffold::GetField;
109
use crate::{fields, fieldset, NeoSkeletonLength};
@@ -415,9 +414,9 @@ impl_attrs! {
415414
impl TimeFieldSet {
416415
pub(crate) const fn id_str_for_hour_cycle(
417416
self,
418-
hour_cycle: Option<HourCycle>,
417+
hour_cycle: Option<fields::Hour>,
419418
) -> &'static DataMarkerAttributes {
420-
use HourCycle::*;
419+
use fields::Hour::*;
421420
match hour_cycle {
422421
None => Self::ATTR_T,
423422
Some(H11 | H12) => Self::ATTR_T12,

components/datetime/src/fields/symbols.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
#[cfg(feature = "datagen")]
66
use crate::fields::FieldLength;
7-
use crate::neo_skeleton::FractionalSecondDigits;
7+
use crate::options::FractionalSecondDigits;
88
use core::{cmp::Ordering, convert::TryFrom};
99
use displaydoc::Display;
10+
use icu_locale_core::preferences::extensions::unicode::keywords::HourCycle;
1011
use icu_provider::prelude::*;
1112
use zerovec::ule::{AsULE, UleError, ULE};
1213

@@ -593,6 +594,18 @@ field_type!(
593594
HourULE
594595
);
595596

597+
impl Hour {
598+
pub(crate) fn from_hour_cycle(hour_cycle: HourCycle) -> Self {
599+
match hour_cycle {
600+
HourCycle::H11 => Self::H11,
601+
HourCycle::H12 => Self::H12,
602+
HourCycle::H23 => Self::H23,
603+
HourCycle::H24 => Self::H24,
604+
_ => unreachable!(),
605+
}
606+
}
607+
}
608+
596609
// NOTE: 'S' FractionalSecond is represented via DecimalSecond,
597610
// so it is not included in the Second enum.
598611

components/datetime/src/fieldset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
dynamic::*,
1111
fields,
1212
format::neo::*,
13-
neo_skeleton::*,
13+
options::*,
1414
provider::{neo::*, time_zones::tz, *},
1515
raw::neo::RawNeoOptions,
1616
scaffold::*,

components/datetime/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ mod neo_marker;
100100
pub mod neo_pattern;
101101
#[cfg(feature = "serde")]
102102
mod neo_serde;
103-
pub mod neo_skeleton;
104103
pub mod options;
105104
pub mod provider;
106105
pub(crate) mod raw;
@@ -113,4 +112,4 @@ pub use format::neo::{FormattedDateTimePattern, PatternLoadError, TypedDateTimeN
113112
pub use neo::DateTimeFormatter;
114113
pub use neo::FixedCalendarDateTimeFormatter;
115114
pub use neo::FormattedNeoDateTime;
116-
pub use neo_skeleton::NeoSkeletonLength;
115+
pub use options::NeoSkeletonLength;

components/datetime/src/neo.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::format::datetime::try_write_pattern_items;
1010
use crate::format::neo::*;
1111
use crate::input::ExtractedInput;
1212
use crate::neo_pattern::DateTimePattern;
13-
use crate::options::preferences::HourCycle;
1413
use crate::raw::neo::*;
1514
use crate::scaffold::*;
1615
use crate::scaffold::{
@@ -24,6 +23,7 @@ use core::fmt;
2423
use core::marker::PhantomData;
2524
use icu_calendar::any_calendar::IntoAnyCalendar;
2625
use icu_calendar::AnyCalendar;
26+
use icu_locale_core::preferences::extensions::unicode::keywords::HourCycle;
2727
use icu_provider::prelude::*;
2828
use writeable::TryWriteable;
2929

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

@@ -473,12 +474,13 @@ where
473474
P: ?Sized + AllAnyCalendarFormattingDataMarkers<FSet>,
474475
L: FixedDecimalFormatterLoader + AnyCalendarLoader,
475476
{
476-
// TODO: Remove this when NeoOptions is gone
477+
// TODO: Fix this when we have DateTimePreferences
477478
let prefs = RawPreferences {
478479
hour_cycle: locale
479480
.get_unicode_ext(&icu_locale_core::extensions::unicode::key!("hc"))
480481
.as_ref()
481-
.and_then(HourCycle::from_locale_value),
482+
.and_then(|v| HourCycle::try_from(v).ok())
483+
.map(crate::fields::Hour::from_hour_cycle),
482484
};
483485
// END TODO
484486

components/datetime/src/neo_marker.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! use icu::calendar::Date;
1717
//! use icu::calendar::Gregorian;
1818
//! use icu::datetime::fieldset::YMD;
19-
//! use icu::datetime::neo_skeleton::Alignment;
19+
//! use icu::datetime::options::Alignment;
2020
//! use icu::datetime::FixedCalendarDateTimeFormatter;
2121
//! use icu::locale::locale;
2222
//! use writeable::assert_try_writeable_eq;
@@ -56,7 +56,7 @@
5656
//! use icu::calendar::Date;
5757
//! use icu::calendar::Gregorian;
5858
//! use icu::datetime::fieldset::YMD;
59-
//! use icu::datetime::neo_skeleton::YearStyle;
59+
//! use icu::datetime::options::YearStyle;
6060
//! use icu::datetime::FixedCalendarDateTimeFormatter;
6161
//! use icu::locale::locale;
6262
//! use writeable::assert_try_writeable_eq;
@@ -233,8 +233,8 @@
233233
//! ```
234234
//! use icu::calendar::Time;
235235
//! use icu::datetime::fieldset::T;
236-
//! use icu::datetime::neo_skeleton::FractionalSecondDigits;
237-
//! use icu::datetime::neo_skeleton::TimePrecision;
236+
//! use icu::datetime::options::FractionalSecondDigits;
237+
//! use icu::datetime::options::TimePrecision;
238238
//! use icu::datetime::FixedCalendarDateTimeFormatter;
239239
//! use icu::locale::locale;
240240
//! use writeable::assert_try_writeable_eq;
@@ -291,8 +291,8 @@
291291
//! use icu::calendar::Gregorian;
292292
//! use icu::calendar::Time;
293293
//! use icu::datetime::fieldset::T;
294-
//! use icu::datetime::neo_skeleton::FractionalSecondDigits;
295-
//! use icu::datetime::neo_skeleton::TimePrecision;
294+
//! use icu::datetime::options::FractionalSecondDigits;
295+
//! use icu::datetime::options::TimePrecision;
296296
//! use icu::datetime::FixedCalendarDateTimeFormatter;
297297
//! use icu::locale::locale;
298298
//! use writeable::assert_try_writeable_eq;

components/datetime/src/neo_pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ size_test!(DateTimePattern, date_time_pattern_size, 32);
4242
/// use icu::calendar::Gregorian;
4343
/// use icu::datetime::fieldset::YMD;
4444
/// use icu::datetime::neo_pattern::DateTimePattern;
45-
/// use icu::datetime::options::components;
45+
/// use icu::datetime::provider::skeleton::components;
4646
/// use icu::datetime::FixedCalendarDateTimeFormatter;
4747
/// use icu::locale::locale;
4848
/// use writeable::assert_writeable_eq;

components/datetime/src/neo_serde.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//! Serde definitions for semantic skeleta
66
7-
use crate::{dynamic::*, fieldset, neo_skeleton::*, raw::neo::RawNeoOptions};
7+
use crate::{dynamic::*, fieldset, options::*, raw::neo::RawNeoOptions};
88
use alloc::vec::Vec;
99
use serde::{Deserialize, Serialize};
1010

0 commit comments

Comments
 (0)