Skip to content

Commit 02153c6

Browse files
authored
Add field set options constructor (#5556)
1 parent 622a992 commit 02153c6

File tree

4 files changed

+623
-370
lines changed

4 files changed

+623
-370
lines changed

components/datetime/src/neo.rs

Lines changed: 120 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ pub struct NeoOptions<R: DateTimeMarkers> {
174174
pub fractional_second_digits: R::FractionalSecondDigitsOption,
175175
}
176176

177+
impl<R> From<NeoOptions<R>> for RawNeoOptions
178+
where
179+
R: DateTimeMarkers,
180+
{
181+
fn from(options: NeoOptions<R>) -> Self {
182+
Self {
183+
length: options.length.into(),
184+
alignment: options.alignment.into(),
185+
era_display: options.era_display.into(),
186+
fractional_second_digits: options.fractional_second_digits.into(),
187+
}
188+
}
189+
}
190+
177191
impl<R> From<NeoSkeletonLength> for NeoOptions<R>
178192
where
179193
R: DateTimeMarkers,
@@ -214,6 +228,28 @@ where
214228
}
215229
}
216230

231+
impl RawNeoOptions {
232+
#[cfg(feature = "compiled_data")]
233+
pub(crate) fn from_field_set<FSet>(field_set: FSet) -> Self
234+
where
235+
FSet: DateTimeMarkers,
236+
FSet: NeoGetField<FSet::LengthOption>,
237+
FSet: NeoGetField<FSet::AlignmentOption>,
238+
FSet: NeoGetField<FSet::EraDisplayOption>,
239+
FSet: NeoGetField<FSet::FractionalSecondDigitsOption>,
240+
{
241+
Self {
242+
length: NeoGetField::<FSet::LengthOption>::get_field(&field_set).into(),
243+
alignment: NeoGetField::<FSet::AlignmentOption>::get_field(&field_set).into(),
244+
era_display: NeoGetField::<FSet::EraDisplayOption>::get_field(&field_set).into(),
245+
fractional_second_digits: NeoGetField::<FSet::FractionalSecondDigitsOption>::get_field(
246+
&field_set,
247+
)
248+
.into(),
249+
}
250+
}
251+
}
252+
217253
size_test!(TypedNeoFormatter<icu_calendar::Gregorian, crate::neo_marker::NeoYearMonthDayMarker>, typed_neo_year_month_day_formatter_size, 504);
218254

219255
/// [`TypedNeoFormatter`] is a formatter capable of formatting dates and/or times from
@@ -229,6 +265,78 @@ pub struct TypedNeoFormatter<C: CldrCalendar, R: DateTimeNamesMarker> {
229265
_calendar: PhantomData<C>,
230266
}
231267

268+
impl<C: CldrCalendar, FSet: DateTimeMarkers + HasConstComponents> TypedNeoFormatter<C, FSet>
269+
where
270+
FSet::D: TypedDateDataMarkers<C>,
271+
FSet::T: TimeMarkers,
272+
FSet::Z: ZoneMarkers,
273+
FSet: NeoGetField<FSet::LengthOption>,
274+
FSet: NeoGetField<FSet::AlignmentOption>,
275+
FSet: NeoGetField<FSet::EraDisplayOption>,
276+
FSet: NeoGetField<FSet::FractionalSecondDigitsOption>,
277+
{
278+
/// Creates a new [`TypedNeoFormatter`] from compiled data with
279+
/// datetime components specified at build time.
280+
///
281+
/// Use this constructor for optimal data size and memory use
282+
/// if you know the required datetime components at build time.
283+
/// If you do not know the datetime components until runtime,
284+
/// use a `with_components` constructor.
285+
///
286+
/// # Examples
287+
///
288+
/// Basic usage:
289+
///
290+
/// ```
291+
/// use icu::calendar::Date;
292+
/// use icu::calendar::Gregorian;
293+
/// use icu::datetime::neo::TypedNeoFormatter;
294+
/// use icu::datetime::neo_marker::NeoYearMonthDayMarker;
295+
/// use icu::datetime::neo_skeleton::NeoSkeletonLength;
296+
/// use icu::locale::locale;
297+
/// use writeable::assert_try_writeable_eq;
298+
///
299+
/// let formatter =
300+
/// TypedNeoFormatter::try_new2(
301+
/// &locale!("es-MX").into(),
302+
/// NeoYearMonthDayMarker::with_length(NeoSkeletonLength::Long),
303+
/// )
304+
/// .unwrap();
305+
///
306+
/// assert_try_writeable_eq!(
307+
/// formatter.format(&Date::try_new_gregorian_date(2023, 12, 20).unwrap()),
308+
/// "20 de diciembre de 2023"
309+
/// );
310+
/// ```
311+
#[cfg(feature = "compiled_data")]
312+
pub fn try_new2(locale: &DataLocale, options: FSet) -> Result<Self, LoadError>
313+
where
314+
crate::provider::Baked: Sized
315+
// Date formatting markers
316+
+ DataProvider<<FSet::D as TypedDateDataMarkers<C>>::YearNamesV1Marker>
317+
+ DataProvider<<FSet::D as TypedDateDataMarkers<C>>::MonthNamesV1Marker>
318+
+ DataProvider<<FSet::D as TypedDateDataMarkers<C>>::DateSkeletonPatternsV1Marker>
319+
+ DataProvider<<FSet::D as TypedDateDataMarkers<C>>::WeekdayNamesV1Marker>
320+
+ DataProvider<<FSet::T as TimeMarkers>::DayPeriodNamesV1Marker>
321+
+ DataProvider<<FSet::T as TimeMarkers>::TimeSkeletonPatternsV1Marker>
322+
+ DataProvider<<FSet::Z as ZoneMarkers>::EssentialsV1Marker>
323+
+ DataProvider<<FSet::Z as ZoneMarkers>::ExemplarCitiesV1Marker>
324+
+ DataProvider<<FSet::Z as ZoneMarkers>::GenericLongV1Marker>
325+
+ DataProvider<<FSet::Z as ZoneMarkers>::GenericShortV1Marker>
326+
+ DataProvider<<FSet::Z as ZoneMarkers>::SpecificLongV1Marker>
327+
+ DataProvider<<FSet::Z as ZoneMarkers>::SpecificShortV1Marker>
328+
+ DataProvider<FSet::GluePatternV1Marker>,
329+
{
330+
Self::try_new_internal(
331+
&crate::provider::Baked,
332+
&ExternalLoaderCompiledData,
333+
locale,
334+
FSet::COMPONENTS,
335+
RawNeoOptions::from_field_set(options),
336+
)
337+
}
338+
}
339+
232340
impl<C: CldrCalendar, R: DateTimeMarkers + HasConstComponents> TypedNeoFormatter<C, R>
233341
where
234342
R::D: TypedDateDataMarkers<C>,
@@ -292,7 +400,7 @@ where
292400
&ExternalLoaderCompiledData,
293401
locale,
294402
R::COMPONENTS,
295-
options,
403+
options.into(),
296404
)
297405
}
298406

@@ -337,7 +445,7 @@ where
337445
&ExternalLoaderUnstable(provider),
338446
locale,
339447
R::COMPONENTS,
340-
options,
448+
options.into(),
341449
)
342450
}
343451
}
@@ -459,7 +567,7 @@ where
459567
&ExternalLoaderCompiledData,
460568
locale,
461569
components.into(),
462-
options,
570+
options.into(),
463571
)
464572
}
465573

@@ -505,7 +613,7 @@ where
505613
&ExternalLoaderUnstable(provider),
506614
locale,
507615
components.into(),
508-
options,
616+
options.into(),
509617
)
510618
}
511619
}
@@ -521,7 +629,7 @@ where
521629
loader: &L,
522630
locale: &DataLocale,
523631
components: NeoComponents,
524-
options: NeoOptions<R>,
632+
options: RawNeoOptions,
525633
) -> Result<Self, LoadError>
526634
where
527635
P: ?Sized
@@ -550,11 +658,8 @@ where
550658
&<R::T as TimeMarkers>::TimeSkeletonPatternsV1Marker::bind(provider),
551659
&R::GluePatternV1Marker::bind(provider),
552660
locale,
553-
options.length.into(),
554661
components,
555-
options.alignment.into(),
556-
options.era_display.into(),
557-
options.fractional_second_digits.into(),
662+
options,
558663
hour_cycle,
559664
)
560665
.map_err(LoadError::Data)?;
@@ -790,7 +895,7 @@ where
790895
&ExternalLoaderCompiledData,
791896
locale,
792897
R::COMPONENTS,
793-
options,
898+
options.into(),
794899
)
795900
}
796901

@@ -890,7 +995,7 @@ where
890995
&ExternalLoaderUnstable(provider),
891996
locale,
892997
R::COMPONENTS,
893-
options,
998+
options.into(),
894999
)
8951000
}
8961001
}
@@ -1055,7 +1160,7 @@ where
10551160
&ExternalLoaderCompiledData,
10561161
locale,
10571162
components.into(),
1058-
options,
1163+
options.into(),
10591164
)
10601165
}
10611166

@@ -1156,7 +1261,7 @@ where
11561261
&ExternalLoaderUnstable(provider),
11571262
locale,
11581263
components.into(),
1159-
options,
1264+
options.into(),
11601265
)
11611266
}
11621267
}
@@ -1172,7 +1277,7 @@ where
11721277
loader: &L,
11731278
locale: &DataLocale,
11741279
components: NeoComponents,
1175-
options: NeoOptions<R>,
1280+
options: RawNeoOptions,
11761281
) -> Result<Self, LoadError>
11771282
where
11781283
P: ?Sized
@@ -1251,11 +1356,8 @@ where
12511356
&<R::T as TimeMarkers>::TimeSkeletonPatternsV1Marker::bind(provider),
12521357
&R::GluePatternV1Marker::bind(provider),
12531358
locale,
1254-
options.length.into(),
12551359
components,
1256-
options.alignment.into(),
1257-
options.era_display.into(),
1258-
options.fractional_second_digits.into(),
1360+
options,
12591361
hour_cycle,
12601362
)
12611363
.map_err(LoadError::Data)?;

0 commit comments

Comments
 (0)