Skip to content

Commit 2f5c0e9

Browse files
Remove JapaneseExtended from DateTimeFormatter::try_new (#6366)
Fixes #4889
1 parent 7e287f1 commit 2f5c0e9

File tree

7 files changed

+121
-128
lines changed

7 files changed

+121
-128
lines changed

components/datetime/src/external_loaders.rs

+12-25
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use icu_decimal::options::DecimalFormatterOptions;
88
use icu_decimal::{DecimalFormatter, DecimalFormatterPreferences};
99
use icu_provider::prelude::*;
1010

11-
use crate::scaffold::{AnyCalendarForFormatting, AnyCalendarForFormattingKind};
11+
use crate::scaffold::{FormattableAnyCalendar, FormattableAnyCalendarKind};
1212

1313
/// Trait for loading a DecimalFormatter.
1414
///
@@ -24,11 +24,8 @@ pub(crate) trait DecimalFormatterLoader {
2424
/// Trait for loading an AnyCalendar.
2525
///
2626
/// Implemented on the provider-specific loader types in this module.
27-
pub(crate) trait AnyCalendarLoader {
28-
fn load(
29-
&self,
30-
kind: AnyCalendarForFormattingKind,
31-
) -> Result<AnyCalendarForFormatting, DataError>;
27+
pub(crate) trait FormattableAnyCalendarLoader {
28+
fn load(&self, kind: FormattableAnyCalendarKind) -> Result<FormattableAnyCalendar, DataError>;
3229
}
3330

3431
/// Loader for types from other crates using compiled data.
@@ -48,13 +45,10 @@ impl DecimalFormatterLoader for ExternalLoaderCompiledData {
4845
}
4946

5047
#[cfg(feature = "compiled_data")]
51-
impl AnyCalendarLoader for ExternalLoaderCompiledData {
48+
impl FormattableAnyCalendarLoader for ExternalLoaderCompiledData {
5249
#[inline]
53-
fn load(
54-
&self,
55-
kind: AnyCalendarForFormattingKind,
56-
) -> Result<AnyCalendarForFormatting, DataError> {
57-
AnyCalendarForFormatting::try_new(kind)
50+
fn load(&self, kind: FormattableAnyCalendarKind) -> Result<FormattableAnyCalendar, DataError> {
51+
FormattableAnyCalendar::try_new(kind)
5852
}
5953
}
6054

@@ -78,16 +72,13 @@ where
7872
}
7973

8074
#[cfg(feature = "serde")]
81-
impl<P> AnyCalendarLoader for ExternalLoaderBuffer<'_, P>
75+
impl<P> FormattableAnyCalendarLoader for ExternalLoaderBuffer<'_, P>
8276
where
8377
P: ?Sized + BufferProvider,
8478
{
8579
#[inline]
86-
fn load(
87-
&self,
88-
kind: AnyCalendarForFormattingKind,
89-
) -> Result<AnyCalendarForFormatting, DataError> {
90-
AnyCalendarForFormatting::try_new_with_buffer_provider(self.0, kind)
80+
fn load(&self, kind: FormattableAnyCalendarKind) -> Result<FormattableAnyCalendar, DataError> {
81+
FormattableAnyCalendar::try_new_with_buffer_provider(self.0, kind)
9182
}
9283
}
9384

@@ -110,21 +101,17 @@ where
110101
}
111102
}
112103

113-
impl<P> AnyCalendarLoader for ExternalLoaderUnstable<'_, P>
104+
impl<P> FormattableAnyCalendarLoader for ExternalLoaderUnstable<'_, P>
114105
where
115106
P: DataProvider<icu_calendar::provider::CalendarJapaneseModernV1>
116-
+ DataProvider<icu_calendar::provider::CalendarJapaneseExtendedV1>
117107
+ DataProvider<icu_calendar::provider::CalendarChineseV1>
118108
+ DataProvider<icu_calendar::provider::CalendarDangiV1>
119109
+ DataProvider<icu_calendar::provider::CalendarHijriObservationalMeccaV1>
120110
+ DataProvider<icu_calendar::provider::CalendarHijriUmmalquraV1>
121111
+ ?Sized,
122112
{
123113
#[inline]
124-
fn load(
125-
&self,
126-
kind: AnyCalendarForFormattingKind,
127-
) -> Result<AnyCalendarForFormatting, DataError> {
128-
AnyCalendarForFormatting::try_new_unstable(self.0, kind)
114+
fn load(&self, kind: FormattableAnyCalendarKind) -> Result<FormattableAnyCalendar, DataError> {
115+
FormattableAnyCalendar::try_new_unstable(self.0, kind)
129116
}
130117
}

components/datetime/src/neo.rs

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

55
//! High-level entrypoints for Neo DateTime Formatter
66
7-
use crate::error::{DateTimeFormatterLoadError, UnsupportedCalendarError};
7+
use crate::error::DateTimeFormatterLoadError;
88
use crate::fieldsets::builder::FieldSetBuilder;
99
use crate::fieldsets::enums::CompositeFieldSet;
1010
use crate::format::datetime::try_write_pattern_items;
@@ -153,7 +153,8 @@ where
153153
/// Creates a new [`FixedCalendarDateTimeFormatter`] from compiled data with
154154
/// datetime components specified at build time.
155155
///
156-
/// This ignores the `calendar_kind` preference and instead uses the static calendar type.
156+
/// This ignores the `calendar_kind` preference and instead uses the static calendar type,
157+
/// and supports calendars that are not expressible as preferences, such as [`JapaneseExtended`](icu_calendar::cal::JapaneseExtended).
157158
///
158159
/// Use this constructor for optimal data size and memory use
159160
/// if you know the required datetime components at build time.
@@ -165,7 +166,7 @@ where
165166
/// Basic usage:
166167
///
167168
/// ```
168-
/// use icu::calendar::Gregorian;
169+
/// use icu::calendar::cal::JapaneseExtended;
169170
/// use icu::datetime::fieldsets::YMD;
170171
/// use icu::datetime::input::Date;
171172
/// use icu::datetime::FixedCalendarDateTimeFormatter;
@@ -179,8 +180,8 @@ where
179180
/// .unwrap();
180181
///
181182
/// assert_writeable_eq!(
182-
/// formatter.format(&Date::try_new_gregorian(2023, 12, 20).unwrap()),
183-
/// "20 de diciembre de 2023"
183+
/// formatter.format(&Date::try_new_iso(2023, 12, 20).unwrap().to_calendar(JapaneseExtended::new())),
184+
/// "20 de diciembre de 5 Reiwa"
184185
/// );
185186
/// ```
186187
#[cfg(feature = "compiled_data")]
@@ -457,7 +458,7 @@ size_test!(
457458
pub struct DateTimeFormatter<FSet: DateTimeNamesMarker> {
458459
selection: DateTimeZonePatternSelectionData,
459460
pub(crate) names: RawDateTimeNames<FSet>,
460-
pub(crate) calendar: UntaggedAnyCalendarForFormatting,
461+
pub(crate) calendar: UntaggedFormattableAnyCalendar,
461462
}
462463

463464
impl<FSet: DateTimeMarkers> DateTimeFormatter<FSet>
@@ -562,33 +563,31 @@ where
562563
) -> Result<Self, DateTimeFormatterLoadError>
563564
where
564565
P: ?Sized + AllAnyCalendarFormattingDataMarkers<FSet>,
565-
L: DecimalFormatterLoader + AnyCalendarLoader,
566+
L: DecimalFormatterLoader + FormattableAnyCalendarLoader,
566567
{
567-
let kind = AnyCalendarForFormattingKind::from_preferences(prefs);
568-
let calendar = AnyCalendarLoader::load(loader, kind)?;
568+
let kind = FormattableAnyCalendarKind::from_preferences(prefs);
569+
let calendar = FormattableAnyCalendarLoader::load(loader, kind)?;
569570
let names = RawDateTimeNames::new_without_number_formatting();
570571
Self::try_new_internal_with_calendar_and_names(
571572
provider, provider, loader, prefs, field_set, calendar, names,
572573
)
573574
.map_err(|e| e.0)
574575
}
575576

576-
// calendar.kind() is one of Buddhist, Chinese, Coptic, Dangi, Ethiopian, EthiopianAmeteAlem, Gregorian, Hebrew, Indian,
577-
// HijriCivil, HijriObservationalMecca, HijriTabular, HijriUmmAlQura, Japanese, JapaneseExtended, Persian, Roc
578577
#[allow(clippy::result_large_err)] // returning ownership of an argument to the caller
579578
pub(crate) fn try_new_internal_with_calendar_and_names<P0, P1, L>(
580579
provider_p: &P0,
581580
provider: &P1,
582581
loader: &L,
583582
prefs: DateTimeFormatterPreferences,
584583
field_set: CompositeFieldSet,
585-
calendar: AnyCalendarForFormatting,
584+
calendar: FormattableAnyCalendar,
586585
mut names: RawDateTimeNames<FSet>,
587586
) -> Result<
588587
Self,
589588
(
590589
DateTimeFormatterLoadError,
591-
(AnyCalendarForFormatting, RawDateTimeNames<FSet>),
590+
(FormattableAnyCalendar, RawDateTimeNames<FSet>),
592591
),
593592
>
594593
where
@@ -597,7 +596,7 @@ where
597596
L: DecimalFormatterLoader,
598597
{
599598
let selection = DateTimeZonePatternSelectionData::try_new_with_skeleton(
600-
&AnyCalendarProvider::<<FSet::D as DateDataMarkers>::Skel, _>::new(
599+
&FormattableAnyCalendarNamesLoader::<<FSet::D as DateDataMarkers>::Skel, _>::new(
601600
provider_p,
602601
calendar.kind(),
603602
),
@@ -611,11 +610,11 @@ where
611610
Err(e) => return Err((DateTimeFormatterLoadError::Data(e), (calendar, names))),
612611
};
613612
let result = names.load_for_pattern(
614-
&AnyCalendarProvider::<<FSet::D as DateDataMarkers>::Year, _>::new(
613+
&FormattableAnyCalendarNamesLoader::<<FSet::D as DateDataMarkers>::Year, _>::new(
615614
provider,
616615
calendar.kind(),
617616
),
618-
&AnyCalendarProvider::<<FSet::D as DateDataMarkers>::Month, _>::new(
617+
&FormattableAnyCalendarNamesLoader::<<FSet::D as DateDataMarkers>::Month, _>::new(
619618
provider,
620619
calendar.kind(),
621620
),
@@ -879,29 +878,21 @@ impl<C: CldrCalendar, FSet: DateTimeMarkers> FixedCalendarDateTimeFormatter<C, F
879878
/// YMD::long(),
880879
/// )
881880
/// .unwrap()
882-
/// .try_into_formatter(Hebrew::new())
883-
/// .expect("Hebrew is supported in DateTimeFormatter");
881+
/// .into_formatter(Hebrew::new());
884882
///
885883
/// let date = Date::try_new_iso(2024, 10, 14).unwrap();
886884
///
887885
/// assert_writeable_eq!(formatter.format(&date), "12 Tishri 5785");
888886
/// ```
889-
pub fn try_into_formatter(
890-
self,
891-
calendar: C,
892-
) -> Result<DateTimeFormatter<FSet>, UnsupportedCalendarError>
887+
pub fn into_formatter(self, calendar: C) -> DateTimeFormatter<FSet>
893888
where
894-
C: IntoAnyCalendar,
889+
C: IntoFormattableAnyCalendar,
895890
{
896-
let any_calendar = calendar.to_any();
897-
let kind = any_calendar.kind();
898-
let calendar = AnyCalendarForFormatting::try_from_any_calendar(any_calendar)
899-
.ok_or(UnsupportedCalendarError { kind })?;
900-
Ok(DateTimeFormatter {
891+
DateTimeFormatter {
901892
selection: self.selection,
902893
names: self.names,
903-
calendar: calendar.into_untagged(),
904-
})
894+
calendar: FormattableAnyCalendar::from_calendar(calendar).into_untagged(),
895+
}
905896
}
906897

907898
/// Maps a [`FixedCalendarDateTimeFormatter`] of a specific `FSet` to a more general `FSet`.

components/datetime/src/pattern/names.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ pub struct FixedCalendarDateTimeNames<C, FSet: DateTimeNamesMarker = CompositeDa
591591
#[derive(Debug, Clone)]
592592
pub struct DateTimeNames<FSet: DateTimeNamesMarker> {
593593
inner: FixedCalendarDateTimeNames<(), FSet>,
594-
calendar: AnyCalendarForFormatting,
594+
calendar: FormattableAnyCalendar,
595595
}
596596

597597
pub(crate) struct RawDateTimeNames<FSet: DateTimeNamesMarker> {
@@ -996,13 +996,12 @@ impl<FSet: DateTimeNamesMarker> DateTimeNames<FSet> {
996996
calendar: AnyCalendar,
997997
) -> Result<Self, UnsupportedCalendarError> {
998998
let kind = calendar.kind();
999-
match AnyCalendarForFormatting::try_from_any_calendar(calendar) {
1000-
Some(calendar) => Ok(Self {
1001-
inner: FixedCalendarDateTimeNames::new_without_number_formatting(prefs),
1002-
calendar,
1003-
}),
1004-
None => Err(UnsupportedCalendarError { kind }),
1005-
}
999+
let calendar = FormattableAnyCalendar::try_from_any_calendar(calendar)
1000+
.ok_or(UnsupportedCalendarError { kind })?;
1001+
Ok(Self {
1002+
inner: FixedCalendarDateTimeNames::new_without_number_formatting(prefs),
1003+
calendar,
1004+
})
10061005
}
10071006

10081007
/// Creates an instance with the names and calendar loaded in a [`DateTimeFormatter`].
@@ -1058,7 +1057,7 @@ impl<FSet: DateTimeNamesMarker> DateTimeNames<FSet> {
10581057

10591058
fn from_parts(
10601059
prefs: DateTimeFormatterPreferences,
1061-
parts: (AnyCalendarForFormatting, RawDateTimeNames<FSet>),
1060+
parts: (FormattableAnyCalendar, RawDateTimeNames<FSet>),
10621061
) -> Self {
10631062
Self {
10641063
inner: FixedCalendarDateTimeNames {

0 commit comments

Comments
 (0)