Skip to content

Commit 98e9aa8

Browse files
authored
Move DateTimePattern into pattern module (#5834)
#1317 Also removes `NeoNeverMarker` and fixes #5689
1 parent bea1735 commit 98e9aa8

26 files changed

+1273
-1309
lines changed

components/datetime/src/combo.rs

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

55
use core::marker::PhantomData;
66

7-
use crate::{format::neo::*, provider::neo::*, scaffold::*};
7+
use crate::{provider::neo::*, scaffold::*};
88

99
/// Struct for combining date/time fields with zone fields.
1010
///

components/datetime/src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use icu_calendar::{
1010
};
1111

1212
#[cfg(doc)]
13-
use crate::TypedDateTimeNames;
13+
use crate::pattern::TypedDateTimeNames;
1414
#[cfg(doc)]
1515
use icu_calendar::types::YearInfo;
1616
#[cfg(doc)]

components/datetime/src/fields/components.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
provider::pattern::{runtime::Pattern, PatternItem},
3636
};
3737

38-
use crate::neo_pattern::DateTimePattern;
38+
use crate::pattern::DateTimePattern;
3939
use icu_locale_core::preferences::extensions::unicode::keywords::HourCycle;
4040
#[cfg(feature = "serde")]
4141
use serde::{Deserialize, Serialize};

components/datetime/src/fieldset.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub use crate::combo::Combo;
3535

3636
use crate::{
3737
fields,
38-
format::neo::*,
3938
options::*,
4039
provider::{neo::*, time_zones::tz, *},
4140
raw::neo::RawNeoOptions,
@@ -454,8 +453,8 @@ macro_rules! impl_date_or_calendar_period_marker {
454453
}
455454
impl DateTimeMarkers for $type {
456455
type D = Self;
457-
type T = NeoNeverMarker;
458-
type Z = NeoNeverMarker;
456+
type T = ();
457+
type Z = ();
459458
type GluePatternV1Marker = datetime_marker_helper!(@glue,);
460459
}
461460
};
@@ -614,7 +613,7 @@ macro_rules! impl_date_marker {
614613
impl DateTimeMarkers for $type_time {
615614
type D = Self;
616615
type T = Self;
617-
type Z = NeoNeverMarker;
616+
type Z = ();
618617
type GluePatternV1Marker = datetime_marker_helper!(@glue, yes);
619618
}
620619
impl_composite!($type_time, DateTime, DateAndTimeFieldSet);
@@ -757,9 +756,9 @@ macro_rules! impl_time_marker {
757756
type NanoSecondInput = datetime_marker_helper!(@input/nanosecond, $($nanosecond_yes)?);
758757
}
759758
impl DateTimeMarkers for $type {
760-
type D = NeoNeverMarker;
759+
type D = ();
761760
type T = Self;
762-
type Z = NeoNeverMarker;
761+
type Z = ();
763762
type GluePatternV1Marker = datetime_marker_helper!(@glue,);
764763
}
765764
impl_composite!($type, Time, TimeFieldSet);
@@ -882,8 +881,8 @@ macro_rules! impl_zone_marker {
882881
type MetazonePeriodV1Marker = datetime_marker_helper!(@data/zone/metazone_periods, $($metazone_periods_yes)?);
883882
}
884883
impl DateTimeMarkers for $type {
885-
type D = NeoNeverMarker;
886-
type T = NeoNeverMarker;
884+
type D = ();
885+
type T = ();
887886
type Z = Self;
888887
type GluePatternV1Marker = datetime_marker_helper!(@glue,);
889888
}

components/datetime/src/format/datetime.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
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 super::neo::RawDateTimeNamesBorrowed;
65
use super::time_zone::{FormatTimeZone, FormatTimeZoneError, Iso8601Format, TimeZoneFormatterUnit};
7-
use super::{
8-
GetNameForDayPeriodError, GetNameForMonthError, GetNameForWeekdayError,
9-
GetSymbolForCyclicYearError, GetSymbolForEraError, MonthPlaceholderValue,
10-
};
116
use crate::error::DateTimeWriteError;
127
use crate::fields::{self, FieldLength, FieldSymbol, Second, Year};
138
use crate::input::ExtractedInput;
9+
use crate::pattern::*;
1410
use crate::provider::pattern::runtime::PatternMetadata;
1511
use crate::provider::pattern::PatternItem;
1612

components/datetime/src/format/mod.rs

-32
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,4 @@
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

55
pub(crate) mod datetime;
6-
pub(crate) mod neo;
76
pub(crate) mod time_zone;
8-
9-
use icu_pattern::SinglePlaceholderPattern;
10-
11-
pub(crate) enum GetNameForMonthError {
12-
Invalid,
13-
NotLoaded,
14-
}
15-
pub(crate) enum GetNameForWeekdayError {
16-
NotLoaded,
17-
}
18-
19-
pub(crate) enum GetSymbolForEraError {
20-
Invalid,
21-
NotLoaded,
22-
}
23-
24-
pub(crate) enum GetSymbolForCyclicYearError {
25-
Invalid { max: usize },
26-
NotLoaded,
27-
}
28-
29-
pub(crate) enum GetNameForDayPeriodError {
30-
NotLoaded,
31-
}
32-
33-
/// Internal enum to represent the kinds of month symbols for interpolation
34-
pub(crate) enum MonthPlaceholderValue<'a> {
35-
PlainString(&'a str),
36-
Numeric,
37-
NumericPattern(&'a SinglePlaceholderPattern),
38-
}

components/datetime/src/format/time_zone.rs

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

55
//! A formatter specifically for the time zone.
66
7-
use super::neo::TimeZoneDataPayloadsBorrowed;
7+
use crate::pattern::TimeZoneDataPayloadsBorrowed;
88
use crate::provider::time_zones::MetazoneId;
99
use crate::{fields::FieldLength, input::ExtractedInput};
1010
use core::fmt;

components/datetime/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ pub mod neo_pattern;
9999
#[cfg(all(feature = "experimental", feature = "serde"))]
100100
mod neo_serde;
101101
pub mod options;
102+
pub mod pattern;
102103
pub mod provider;
103104
pub(crate) mod raw;
104105
pub mod scaffold;
105106
pub(crate) mod size_test_macro;
106107

107108
pub use error::{DateTimeWriteError, MismatchedCalendarError};
108-
pub use format::neo::{FormattedDateTimePattern, PatternLoadError, TypedDateTimeNames};
109109

110110
pub use neo::DateTimeFormatter;
111111
pub use neo::FixedCalendarDateTimeFormatter;

components/datetime/src/neo.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
use crate::external_loaders::*;
88
use crate::fieldset::dynamic::CompositeFieldSet;
99
use crate::format::datetime::try_write_pattern_items;
10-
use crate::format::neo::*;
1110
use crate::input::ExtractedInput;
12-
use crate::neo_pattern::DateTimePattern;
11+
use crate::pattern::*;
1312
use crate::raw::neo::*;
1413
use crate::scaffold::*;
1514
use crate::scaffold::{

components/datetime/src/neo_pattern.rs

-131
Original file line numberDiff line numberDiff line change
@@ -3,134 +3,3 @@
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

55
//! Temporary module for neo datetime patterns
6-
7-
use core::str::FromStr;
8-
9-
use writeable::{impl_display_with_writeable, Writeable};
10-
11-
use crate::provider::pattern::{runtime, PatternError, PatternItem};
12-
use crate::size_test_macro::size_test;
13-
14-
size_test!(DateTimePattern, date_time_pattern_size, 32);
15-
16-
/// A pattern for formatting a datetime in a calendar.
17-
///
18-
/// [`DateTimePattern`] forgoes most internationalization functionality of the datetime crate.
19-
/// It assumes that the pattern is already localized for the customer's locale. Most clients
20-
/// should use [`DateTimeFormatter`] instead of directly formatting with patterns.
21-
///
22-
/// There are two ways to make one of these:
23-
///
24-
/// 1. From a custom pattern string: [`DateTimePattern::try_from_pattern_str`]
25-
/// 2. From a formatted datetime: [`FormattedNeoDateTime::pattern`]
26-
///
27-
/// Things you can do with one of these:
28-
///
29-
/// 1. Use it to directly format a datetime via [`TypedDateTimeNames`]
30-
/// 2. Convert it to a string pattern via [`Writeable`]
31-
/// 3. Get the resolved components
32-
///
33-
#[doc = date_time_pattern_size!()]
34-
///
35-
/// # Examples
36-
///
37-
/// Create a pattern from a custom string and compare it to one from data,
38-
/// then check the resolved components:
39-
///
40-
/// ```
41-
/// use icu::calendar::Date;
42-
/// use icu::calendar::Gregorian;
43-
/// use icu::datetime::fieldset::YMD;
44-
/// use icu::datetime::neo_pattern::DateTimePattern;
45-
/// use icu::datetime::fields::components;
46-
/// use icu::datetime::FixedCalendarDateTimeFormatter;
47-
/// use icu::locale::locale;
48-
/// use writeable::assert_writeable_eq;
49-
///
50-
/// // Create the pattern from a string:
51-
/// let pattern_str = "d MMM y";
52-
/// let custom_pattern =
53-
/// DateTimePattern::try_from_pattern_str(pattern_str).unwrap();
54-
/// assert_writeable_eq!(custom_pattern, pattern_str);
55-
///
56-
/// // Load data that resolves to the same pattern:
57-
/// let data_pattern = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
58-
/// &locale!("es-MX").into(),
59-
/// YMD::medium(),
60-
/// )
61-
/// .unwrap()
62-
/// // The pattern can depend on the datetime being formatted.
63-
/// .format(&Date::try_new_iso(2024, 1, 1).unwrap().to_calendar(Gregorian))
64-
/// .pattern();
65-
/// assert_writeable_eq!(data_pattern, pattern_str);
66-
/// assert_eq!(custom_pattern, data_pattern);
67-
///
68-
/// // Check the resolved components:
69-
/// let mut expected_components_bag = components::Bag::default();
70-
/// expected_components_bag.year = Some(components::Year::Numeric);
71-
/// expected_components_bag.month = Some(components::Month::Short);
72-
/// expected_components_bag.day = Some(components::Day::NumericDayOfMonth);
73-
/// let actual_components_bag = components::Bag::from(&data_pattern);
74-
/// assert_eq!(actual_components_bag, expected_components_bag);
75-
/// ```
76-
///
77-
/// [`DateTimeFormatter`]: crate::DateTimeFormatter
78-
/// [`FormattedNeoDateTime::pattern`]: crate::FormattedNeoDateTime::pattern
79-
/// [`TypedDateTimeNames`]: crate::TypedDateTimeNames
80-
#[derive(Debug)]
81-
pub struct DateTimePattern {
82-
pattern: runtime::Pattern<'static>,
83-
}
84-
85-
impl DateTimePattern {
86-
/// Creates a [`DateTimePattern`] from a pattern string.
87-
///
88-
/// For more details on the syntax, see UTS 35:
89-
/// <https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns>
90-
pub fn try_from_pattern_str(pattern_str: &str) -> Result<Self, PatternError> {
91-
let pattern = runtime::Pattern::from_str(pattern_str)?;
92-
Ok(Self { pattern })
93-
}
94-
95-
#[doc(hidden)] // TODO(#4467): Internal API
96-
pub fn from_runtime_pattern(pattern: runtime::Pattern<'static>) -> Self {
97-
Self { pattern }
98-
}
99-
100-
pub(crate) fn iter_items(&self) -> impl Iterator<Item = PatternItem> + '_ {
101-
self.pattern.items.iter()
102-
}
103-
104-
pub(crate) fn as_borrowed(&self) -> DateTimePatternBorrowed {
105-
DateTimePatternBorrowed(&self.pattern)
106-
}
107-
}
108-
109-
impl FromStr for DateTimePattern {
110-
type Err = PatternError;
111-
fn from_str(s: &str) -> Result<Self, Self::Err> {
112-
Self::try_from_pattern_str(s)
113-
}
114-
}
115-
116-
// Check equality on the borrowed variant since it flattens the difference
117-
// between the three `Single` pattern variants, which is not public-facing
118-
impl PartialEq for DateTimePattern {
119-
fn eq(&self, other: &Self) -> bool {
120-
self.as_borrowed().eq(&other.as_borrowed())
121-
}
122-
}
123-
124-
impl Eq for DateTimePattern {}
125-
126-
impl Writeable for DateTimePattern {
127-
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
128-
self.pattern.write_to(sink)
129-
}
130-
}
131-
132-
impl_display_with_writeable!(DateTimePattern);
133-
134-
// Not clear if this needs to be public. For now, make it private.
135-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
136-
pub(crate) struct DateTimePatternBorrowed<'a>(pub(crate) &'a runtime::Pattern<'a>);

0 commit comments

Comments
 (0)