Debian Codegraph @

debian/rust-vendor/icu_datetime/src/provider/names.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 //! Data structs and markers for datetime names.
6
7 use crate::provider::day_periods::DayPeriodRules;
8 use crate::size_test_macro::size_test;
9 use alloc::borrow::Cow;
10 use icu_pattern::SinglePlaceholderPattern;
11 use icu_provider::marker::ErasedMarker;
12 use icu_provider::prelude::*;
13 use icu_time::Hour;
14 #[cfg(feature = "serde")]
15 use potential_utf::PotentialUtf8;
16 use zerovec::VarZeroVec;
17 use zerovec::ule::vartuple::VarTupleULE;
18 #[cfg(feature = "serde")]
19 use zerovec::{VarZeroCow, VarZeroSlice, ule::tuplevar::Tuple2VarULE};
20
21 icu_provider::data_marker!(
22 /// `DatetimeNamesYearBuddhistV1`
23 ///
24 /// This uses a data marker attribute for length. The value is simply the number of
25 /// characters in the equivalent CLDR field syntax name.
26 /// For example, `GGG`/`UUU` both correspond to `3`.
27 ///
28 /// The full list is:
29 /// - 3 is "abbreviated"
30 /// - 4 is "narrow"
31 /// - 5 is "wide"
32 DatetimeNamesYearBuddhistV1,
33 YearNames<'static>,
34 #[cfg(feature = "datagen")]
35 attributes_domain = "datetime_year_length"
36 );
37 icu_provider::data_marker!(
38 /// `DatetimeNamesYearChineseV1`
39 ///
40 /// This uses a data marker attribute for length. The value is simply the number of
41 /// characters in the equivalent CLDR field syntax name.
42 /// For example, `GGG`/`UUU` both correspond to `3`.
43 ///
44 /// The full list is:
45 /// - 3 is "abbreviated"
46 /// - 4 is "narrow"
47 /// - 5 is "wide"
48 DatetimeNamesYearChineseV1,
49 YearNames<'static>,
50 #[cfg(feature = "datagen")]
51 attributes_domain = "datetime_year_length"
52 );
53 icu_provider::data_marker!(
54 /// `DatetimeNamesYearCopticV1`
55 ///
56 /// This uses a data marker attribute for length. The value is simply the number of
57 /// characters in the equivalent CLDR field syntax name.
58 /// For example, `GGG`/`UUU` both correspond to `3`.
59 ///
60 /// The full list is:
61 /// - 3 is "abbreviated"
62 /// - 4 is "narrow"
63 /// - 5 is "wide"
64 DatetimeNamesYearCopticV1,
65 YearNames<'static>,
66 #[cfg(feature = "datagen")]
67 attributes_domain = "datetime_year_length"
68 );
69 icu_provider::data_marker!(
70 /// `DatetimeNamesYearDangiV1`
71 ///
72 /// This uses a data marker attribute for length. The value is simply the number of
73 /// characters in the equivalent CLDR field syntax name.
74 /// For example, `GGG`/`UUU` both correspond to `3`.
75 ///
76 /// The full list is:
77 /// - 3 is "abbreviated"
78 /// - 4 is "narrow"
79 /// - 5 is "wide"
80 DatetimeNamesYearDangiV1,
81 YearNames<'static>,
82 #[cfg(feature = "datagen")]
83 attributes_domain = "datetime_year_length"
84 );
85 icu_provider::data_marker!(
86 /// `DatetimeNamesYearEthiopianV1`
87 ///
88 /// This uses a data marker attribute for length. The value is simply the number of
89 /// characters in the equivalent CLDR field syntax name.
90 /// For example, `GGG`/`UUU` both correspond to `3`.
91 ///
92 /// The full list is:
93 /// - 3 is "abbreviated"
94 /// - 4 is "narrow"
95 /// - 5 is "wide"
96 DatetimeNamesYearEthiopianV1,
97 YearNames<'static>,
98 #[cfg(feature = "datagen")]
99 attributes_domain = "datetime_year_length"
100 );
101 icu_provider::data_marker!(
102 /// `DatetimeNamesYearGregorianV1`
103 ///
104 /// This uses a data marker attribute for length. The value is simply the number of
105 /// characters in the equivalent CLDR field syntax name.
106 /// For example, `GGG`/`UUU` both correspond to `3`.
107 ///
108 /// The full list is:
109 /// - 3 is "abbreviated"
110 /// - 4 is "narrow"
111 /// - 5 is "wide"
112 DatetimeNamesYearGregorianV1,
113 YearNames<'static>,
114 #[cfg(feature = "datagen")]
115 attributes_domain = "datetime_year_length"
116 );
117 icu_provider::data_marker!(
118 /// `DatetimeNamesYearHebrewV1`
119 ///
120 /// This uses a data marker attribute for length. The value is simply the number of
121 /// characters in the equivalent CLDR field syntax name.
122 /// For example, `GGG`/`UUU` both correspond to `3`.
123 ///
124 /// The full list is:
125 /// - 3 is "abbreviated"
126 /// - 4 is "narrow"
127 /// - 5 is "wide"
128 DatetimeNamesYearHebrewV1,
129 YearNames<'static>,
130 #[cfg(feature = "datagen")]
131 attributes_domain = "datetime_year_length"
132 );
133 icu_provider::data_marker!(
134 /// `DatetimeNamesYearIndianV1`
135 ///
136 /// This uses a data marker attribute for length. The value is simply the number of
137 /// characters in the equivalent CLDR field syntax name.
138 /// For example, `GGG`/`UUU` both correspond to `3`.
139 ///
140 /// The full list is:
141 /// - 3 is "abbreviated"
142 /// - 4 is "narrow"
143 /// - 5 is "wide"
144 DatetimeNamesYearIndianV1,
145 YearNames<'static>,
146 #[cfg(feature = "datagen")]
147 attributes_domain = "datetime_year_length"
148 );
149 icu_provider::data_marker!(
150 /// `DatetimeNamesYearHijriV1`
151 ///
152 /// This uses a data marker attribute for length. The value is simply the number of
153 /// characters in the equivalent CLDR field syntax name.
154 /// For example, `GGG`/`UUU` both correspond to `3`.
155 ///
156 /// The full list is:
157 /// - 3 is "abbreviated"
158 /// - 4 is "narrow"
159 /// - 5 is "wide"
160 DatetimeNamesYearHijriV1,
161 YearNames<'static>,
162 #[cfg(feature = "datagen")]
163 attributes_domain = "datetime_year_length"
164 );
165 icu_provider::data_marker!(
166 /// `DatetimeNamesYearJapaneseV1`
167 ///
168 /// This uses a data marker attribute for length. The value is simply the number of
169 /// characters in the equivalent CLDR field syntax name.
170 /// For example, `GGG`/`UUU` both correspond to `3`.
171 ///
172 /// The full list is:
173 /// - 3 is "abbreviated"
174 /// - 4 is "narrow"
175 /// - 5 is "wide"
176 DatetimeNamesYearJapaneseV1,
177 YearNames<'static>,
178 #[cfg(feature = "datagen")]
179 attributes_domain = "datetime_year_length"
180 );
181 icu_provider::data_marker!(
182 /// `DatetimeNamesYearPersianV1`
183 ///
184 /// This uses a data marker attribute for length. The value is simply the number of
185 /// characters in the equivalent CLDR field syntax name.
186 /// For example, `GGG`/`UUU` both correspond to `3`.
187 ///
188 /// The full list is:
189 /// - 3 is "abbreviated"
190 /// - 4 is "narrow"
191 /// - 5 is "wide"
192 DatetimeNamesYearPersianV1,
193 YearNames<'static>,
194 #[cfg(feature = "datagen")]
195 attributes_domain = "datetime_year_length"
196 );
197 icu_provider::data_marker!(
198 /// `DatetimeNamesYearRocV1`
199 ///
200 /// This uses a data marker attribute for length. The value is simply the number of
201 /// characters in the equivalent CLDR field syntax name.
202 /// For example, `GGG`/`UUU` both correspond to `3`.
203 ///
204 /// The full list is:
205 /// - 3 is "abbreviated"
206 /// - 4 is "narrow"
207 /// - 5 is "wide"
208 DatetimeNamesYearRocV1,
209 YearNames<'static>,
210 #[cfg(feature = "datagen")]
211 attributes_domain = "datetime_year_length"
212 );
213
214 icu_provider::data_marker!(
215 /// `DatetimeNamesMonthBuddhistV1`
216 ///
217 /// This uses a data marker attribute for length. The value is simply the number of
218 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
219 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
220 ///
221 /// The full list is:
222 /// - 3 is "abbreviated"
223 /// - 4 is "narrow"
224 /// - 5 is "wide"
225 DatetimeNamesMonthBuddhistV1,
226 MonthNames<'static>,
227 #[cfg(feature = "datagen")]
228 attributes_domain = "datetime_month_length"
229 );
230 icu_provider::data_marker!(
231 /// `DatetimeNamesMonthChineseV1`
232 ///
233 /// This uses a data marker attribute for length. The value is simply the number of
234 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
235 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
236 ///
237 /// The full list is:
238 /// - 1 is "numeric" (only used for months with leap months, like this one)
239 /// - 3 is "abbreviated"
240 /// - 4 is "narrow"
241 /// - 5 is "wide"
242 DatetimeNamesMonthChineseV1,
243 MonthNames<'static>,
244 #[cfg(feature = "datagen")]
245 attributes_domain = "datetime_month_length"
246 );
247 icu_provider::data_marker!(
248 /// `DatetimeNamesMonthCopticV1`
249 ///
250 /// This uses a data marker attribute for length. The value is simply the number of
251 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
252 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
253 ///
254 /// The full list is:
255 /// - 3 is "abbreviated"
256 /// - 4 is "narrow"
257 /// - 5 is "wide"
258 DatetimeNamesMonthCopticV1,
259 MonthNames<'static>,
260 #[cfg(feature = "datagen")]
261 attributes_domain = "datetime_month_length"
262 );
263 icu_provider::data_marker!(
264 /// `DatetimeNamesMonthDangiV1`
265 ///
266 /// This uses a data marker attribute for length. The value is simply the number of
267 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
268 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
269 ///
270 /// The full list is:
271 /// - 1 is "numeric" (only used for months with leap months, like this one)
272 /// - 3 is "abbreviated"
273 /// - 4 is "narrow"
274 /// - 5 is "wide"
275 DatetimeNamesMonthDangiV1,
276 MonthNames<'static>,
277 #[cfg(feature = "datagen")]
278 attributes_domain = "datetime_month_length"
279 );
280 icu_provider::data_marker!(
281 /// `DatetimeNamesMonthEthiopianV1`
282 ///
283 /// This uses a data marker attribute for length. The value is simply the number of
284 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
285 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
286 ///
287 /// The full list is:
288 /// - 3 is "abbreviated"
289 /// - 4 is "narrow"
290 /// - 5 is "wide"
291 DatetimeNamesMonthEthiopianV1,
292 MonthNames<'static>,
293 #[cfg(feature = "datagen")]
294 attributes_domain = "datetime_month_length"
295 );
296 icu_provider::data_marker!(
297 /// `DatetimeNamesMonthGregorianV1`
298 ///
299 /// This uses a data marker attribute for length. The value is simply the number of
300 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
301 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
302 ///
303 /// The full list is:
304 /// - 3 is "abbreviated"
305 /// - 4 is "narrow"
306 /// - 5 is "wide"
307 DatetimeNamesMonthGregorianV1,
308 MonthNames<'static>,
309 #[cfg(feature = "datagen")]
310 attributes_domain = "datetime_month_length"
311 );
312 icu_provider::data_marker!(
313 /// `DatetimeNamesMonthHebrewV1`
314 ///
315 /// This uses a data marker attribute for length. The value is simply the number of
316 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
317 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
318 ///
319 /// The full list is:
320 /// - 1 is "numeric" (only used for months with leap months, like this one)
321 /// - 3 is "abbreviated"
322 /// - 4 is "narrow"
323 /// - 5 is "wide"
324 DatetimeNamesMonthHebrewV1,
325 MonthNames<'static>,
326 #[cfg(feature = "datagen")]
327 attributes_domain = "datetime_month_length"
328 );
329 icu_provider::data_marker!(
330 /// `DatetimeNamesMonthIndianV1`
331 ///
332 /// This uses a data marker attribute for length. The value is simply the number of
333 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
334 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
335 ///
336 /// The full list is:
337 /// - 3 is "abbreviated"
338 /// - 4 is "narrow"
339 /// - 5 is "wide"
340 DatetimeNamesMonthIndianV1,
341 MonthNames<'static>,
342 #[cfg(feature = "datagen")]
343 attributes_domain = "datetime_month_length"
344 );
345 icu_provider::data_marker!(
346 /// `DatetimeNamesMonthHijriV1`
347 ///
348 /// This uses a data marker attribute for length. The value is simply the number of
349 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
350 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
351 ///
352 /// The full list is:
353 /// - 3 is "abbreviated"
354 /// - 4 is "narrow"
355 /// - 5 is "wide"
356 DatetimeNamesMonthHijriV1,
357 MonthNames<'static>,
358 #[cfg(feature = "datagen")]
359 attributes_domain = "datetime_month_length"
360 );
361 icu_provider::data_marker!(
362 /// `DatetimeNamesMonthJapaneseV1`
363 ///
364 /// This uses a data marker attribute for length. The value is simply the number of
365 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
366 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
367 ///
368 /// The full list is:
369 /// - 3 is "abbreviated"
370 /// - 4 is "narrow"
371 /// - 5 is "wide"
372 DatetimeNamesMonthJapaneseV1,
373 MonthNames<'static>,
374 #[cfg(feature = "datagen")]
375 attributes_domain = "datetime_month_length"
376 );
377 icu_provider::data_marker!(
378 /// `DatetimeNamesMonthPersianV1`
379 ///
380 /// This uses a data marker attribute for length. The value is simply the number of
381 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
382 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
383 ///
384 /// The full list is:
385 /// - 3 is "abbreviated"
386 /// - 4 is "narrow"
387 /// - 5 is "wide"
388 DatetimeNamesMonthPersianV1,
389 MonthNames<'static>,
390 #[cfg(feature = "datagen")]
391 attributes_domain = "datetime_month_length"
392 );
393 icu_provider::data_marker!(
394 /// `DatetimeNamesMonthRocV1`
395 ///
396 /// This uses a data marker attribute for length. The value is simply the number of
397 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
398 /// For example, `MMM` corresponds to `3`, and `LLL` to `3s`.
399 ///
400 /// The full list is:
401 /// - 3 is "abbreviated"
402 /// - 4 is "narrow"
403 /// - 5 is "wide"
404 DatetimeNamesMonthRocV1,
405 MonthNames<'static>,
406 #[cfg(feature = "datagen")]
407 attributes_domain = "datetime_month_length"
408 );
409
410 icu_provider::data_marker!(
411 /// `DatetimeNamesWeekdayV1`
412 ///
413 /// This uses a data marker attribute for length. The value is simply the number of
414 /// characters in the equivalent CLDR field syntax name, plus "s" for "standalone" contexts.
415 /// For example, `EEE` corresponds to `3`, and `ccc` to `3s`.
416 ///
417 /// The full list is:
418 /// - 3 is "abbreviated"
419 /// - 4 is "narrow"
420 /// - 5 is "wide"
421 /// - 6 is "short"
422 DatetimeNamesWeekdayV1,
423 WeekdayNames<'static>,
424 );
425 icu_provider::data_marker!(
426 /// `DatetimeNamesDayperiodV1`
427 ///
428 /// This uses a data marker attribute for length. The value is simply the number of
429 /// characters in the equivalent CLDR field syntax name. For example, `aaa`/`bbb`/`BBB`
430 /// all correspond to `3`.
431 ///
432 /// The full list is:
433 /// - 3 is "abbreviated"
434 /// - 4 is "narrow"
435 /// - 5 is "wide"
436 DatetimeNamesDayperiodV1,
437 DayPeriodNames<'static>,
438 );
439
440 size_test!(YearNames, year_names_v1_size, 32);
441
442 /// Names used for representing the year.
443 ///
444 #[doc = year_names_v1_size!()]
445 ///
446 /// <div class="stab unstable">
447 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
448 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
449 /// to be stable, their Rust representation might not be. Use with caution.
450 /// </div>
451 #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
452 #[cfg_attr(feature = "datagen", derive(databake::Bake))]
453 #[cfg_attr(feature = "datagen", databake(path = icu_datetime::provider::names))]
454 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
455 #[yoke(prove_covariance_manually)]
456 pub enum YearNames<'data> {
457 /// This calendar has a small, fixed set of eras.
458 ///
459 /// See [`era_index`](icu_calendar::types::EraYear::era_index) for how this is keyed.
460 FixedEras(#[cfg_attr(feature = "serde", serde(borrow))] VarZeroVec<'data, str>),
461 /// This calendar has a variable set of eras with numeric years, this stores the era names mapped from
462 /// era code to the name.
463 #[cfg(feature = "serde")]
464 VariableEras(#[cfg_attr(feature = "serde", serde(borrow))] YearNamesMap<'data>),
465 /// This calendar is cyclic (Chinese, Dangi), so it uses cyclic year names without any eras
466 Cyclic(#[cfg_attr(feature = "serde", serde(borrow))] VarZeroVec<'data, str>),
467 }
468
469 #[cfg(feature = "serde")]
470 impl serde::Serialize for YearNames<'_> {
471 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
472 where
473 S: serde::Serializer,
474 {
475 use alloc::vec::Vec;
476
477 #[derive(serde::Serialize)]
478 enum Raw<'a> {
479 FixedEras(&'a VarZeroVec<'a, str>),
480 VariableEras(&'a YearNamesMap<'a>),
481 Cyclic(&'a VarZeroVec<'a, str>),
482 }
483
484 let x: YearNamesMap;
485
486 match self {
487 // Japanese eras are now generated as `FixedEras`, but we want to keep serializing
488 // them as VariableEras. It's the only calendar with 7 eras.
489 Self::FixedEras(e) if e.len() == 7 => {
490 let mut kvs = [
491 PotentialUtf8::from_str("bce"),
492 PotentialUtf8::from_str("ce"),
493 PotentialUtf8::from_str("meiji"),
494 PotentialUtf8::from_str("taisho"),
495 PotentialUtf8::from_str("showa"),
496 PotentialUtf8::from_str("heisei"),
497 PotentialUtf8::from_str("reiwa"),
498 ]
499 .into_iter()
500 .zip(e.iter())
501 .collect::<Vec<_>>();
502 kvs.sort_unstable();
503 let (ks, vs) = kvs.into_iter().unzip::<_, _, Vec<_>, Vec<_>>();
504 x = VarZeroCow::from_encodeable(&(ks, vs));
505 Raw::VariableEras(&x)
506 }
507 Self::FixedEras(e) => Raw::FixedEras(e),
508 Self::VariableEras(e) => Raw::VariableEras(e),
509 Self::Cyclic(c) => Raw::Cyclic(c),
510 }
511 .serialize(serializer)
512 }
513 }
514
515 icu_provider::data_struct!(
516 YearNames<'_>,
517 #[cfg(feature = "datagen")]
518 );
519
520 #[cfg(feature = "serde")]
521 type YearNamesMap<'data> =
522 VarZeroCow<'data, Tuple2VarULE<VarZeroSlice<PotentialUtf8>, VarZeroSlice<str>>>;
523
524 #[cfg(feature = "serde")]
525 pub(crate) fn get_year_name_from_map<'a>(
526 map: &'a YearNamesMap<'_>,
527 year: &PotentialUtf8,
528 ) -> Option<&'a str> {
529 let idx = map.a().binary_search_by(|x| x.cmp(year)).ok()?;
530 map.b().get(idx)
531 }
532
533 size_test!(MonthNames, month_names_v1_size, 32);
534
535 /// Names used for representing the month.
536 ///
537 #[doc = month_names_v1_size!()]
538 ///
539 /// <div class="stab unstable">
540 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
541 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
542 /// to be stable, their Rust representation might not be. Use with caution.
543 /// </div>
544 #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
545 #[cfg_attr(feature = "datagen", derive(databake::Bake))]
546 #[cfg_attr(feature = "datagen", databake(path = icu_datetime::provider::names))]
547 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
548 #[yoke(prove_covariance_manually)]
549 pub enum MonthNames<'data> {
550 /// Month codes M01, M02, M03, .. (can allow for M13 onwards)
551 ///
552 /// Found for solar and pure lunar calendars
553 Linear(#[cfg_attr(feature = "serde", serde(borrow))] VarZeroVec<'data, str>),
554
555 #[cfg(feature = "serde")]
556 /// Month codes M01, M02, M03, .. M01L, M02L, ...
557 ///
558 /// Empty entries for non-present month codes. Will have an equal number of leap and non-leap
559 /// entries.
560 ///
561 /// Found for lunisolar and lunisidereal calendars
562 ///
563 /// Not used anymore, but kept around for serde stabililty.
564 LeapLinear(#[cfg_attr(feature = "serde", serde(borrow))] VarZeroVec<'data, str>),
565
566 /// This represents the formatting to apply to numeric values to produce the corresponding
567 /// leap month symbol.
568 ///
569 /// For numeric formatting only, on calendars with leap months
570 LeapNumeric(
571 #[cfg_attr(
572 feature = "serde",
573 serde(
574 borrow,
575 deserialize_with = "icu_pattern::deserialize_borrowed_cow::<icu_pattern::SinglePlaceholder, _>"
576 )
577 )]
578 Cow<'data, SinglePlaceholderPattern>,
579 ),
580
581 /// This represents the formatting to apply to numeric values to produce the corresponding
582 /// leap month symbol.
583 ///
584 /// The VZV contains two elements, the pattern for leap months and the pattern for base months. The
585 /// associated `i8` is the offset to apply to the month number before interpolating it into the pattern.
586 ///
587 /// For numeric formatting only, on calendars with leap months.
588 LeapNumericWithBase(
589 #[cfg_attr(feature = "serde", serde(borrow))]
590 VarZeroVec<'data, VarTupleULE<i8, SinglePlaceholderPattern>>,
591 ),
592
593 /// Numeric only
594 Numeric,
595
596 /// This represents the formatting to apply to calendars with leap months.
597 /// The last two elements are patterns:
598 /// * N-2: `SinglePlaceholderPattern` for leap months
599 /// * N-1: `SinglePlaceholderPattern` for leap base months
600 LeapPattern(VarZeroVec<'data, str>),
601 }
602
603 // Stability, don't want to serialize ::LeapPattern
604 #[cfg(feature = "serde")]
605 impl serde::Serialize for MonthNames<'_> {
606 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
607 where
608 S: serde::Serializer,
609 {
610 #[derive(serde::Serialize)]
611 enum Raw<'a> {
612 Linear(&'a VarZeroVec<'a, str>),
613 LeapLinear(&'a VarZeroVec<'a, str>),
614 LeapNumeric(&'a Cow<'a, SinglePlaceholderPattern>),
615 LeapNumericWithBase(&'a VarZeroVec<'a, VarTupleULE<i8, SinglePlaceholderPattern>>),
616 Numeric,
617 }
618
619 let z;
620
621 match self {
622 Self::Numeric => Raw::Numeric,
623 Self::Linear(l) => Raw::Linear(l),
624 Self::LeapLinear(l) => Raw::LeapLinear(l),
625 Self::LeapNumeric(l) => Raw::LeapNumeric(l),
626 Self::LeapNumericWithBase(l) => Raw::LeapNumericWithBase(l),
627 Self::LeapPattern(l) => {
628 use alloc::string::String;
629 use alloc::vec::Vec;
630 use zerovec::vecs::VarZeroVecOwned;
631
632 let leap_pattern = l.get(l.len() - 2).unwrap_or_default();
633 let leap_base_pattern = l.get(l.len() - 1).unwrap_or_default();
634
635 let normal_names = l.iter().take(l.len() - 2);
636
637 let r = if leap_pattern.starts_with('\0') {
638 // The leap pattern is not actually a pattern (no placeholder) - this means it's Hebrew
639 normal_names
640 .map(String::from)
641 .chain([
642 String::new(),
643 String::new(),
644 String::new(),
645 String::new(),
646 SinglePlaceholderPattern::from_ref_store(leap_pattern)
647 .unwrap_or(SinglePlaceholderPattern::PASS_THROUGH)
648 .interpolate_to_string([l.get(5).unwrap_or_default()]),
649 SinglePlaceholderPattern::from_ref_store(leap_base_pattern)
650 .unwrap_or(SinglePlaceholderPattern::PASS_THROUGH)
651 .interpolate_to_string([l.get(5).unwrap_or_default()]),
652 String::new(),
653 String::new(),
654 String::new(),
655 String::new(),
656 String::new(),
657 String::new(),
658 ])
659 .collect()
660 } else {
661 normal_names
662 .clone()
663 .map(String::from)
664 .chain(normal_names.map(|m| {
665 SinglePlaceholderPattern::from_ref_store(leap_pattern)
666 .unwrap_or(SinglePlaceholderPattern::PASS_THROUGH)
667 .interpolate_to_string([m])
668 }))
669 .collect::<Vec<_>>()
670 };
671
672 #[allow(clippy::unwrap_used)] // small enough
673 {
674 z = VarZeroVecOwned::try_from_elements(&r).unwrap().into();
675 }
676 Raw::LeapLinear(&z)
677 }
678 }
679 .serialize(serializer)
680 }
681 }
682
683 icu_provider::data_struct!(
684 MonthNames<'_>,
685 #[cfg(feature = "datagen")]
686 );
687
688 size_test!(WeekdayNames, linear_names_v1_size, 24);
689
690 /// Names used for representing the weekday.
691 ///
692 #[doc = linear_names_v1_size!()]
693 ///
694 /// <div class="stab unstable">
695 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
696 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
697 /// to be stable, their Rust representation might not be. Use with caution.
698 /// </div>
699 #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
700 #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
701 #[cfg_attr(feature = "datagen", databake(path = icu_datetime::provider::names))]
702 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
703 #[yoke(prove_covariance_manually)]
704 pub struct WeekdayNames<'data> {
705 /// Element 0 is Sunday.
706 #[cfg_attr(feature = "serde", serde(borrow))]
707 pub names: VarZeroVec<'data, str>,
708 }
709
710 icu_provider::data_struct!(
711 WeekdayNames<'_>,
712 #[cfg(feature = "datagen")]
713 );
714
715 impl WeekdayNames<'_> {
716 /// Creates a new [`WeekdayNames`] from an iterator of weekday names.
717 #[cfg(feature = "datagen")]
718 pub fn new<'a>(names: impl Iterator<Item = (icu_calendar::types::Weekday, &'a str)>) -> Self {
719 let mut v = [""; 7];
720 for (day, name) in names {
721 use icu_calendar::types::Weekday::*;
722 *match day {
723 Sunday => &mut v[0],
724 Monday => &mut v[1],
725 Tuesday => &mut v[2],
726 Wednesday => &mut v[3],
727 Thursday => &mut v[4],
728 Friday => &mut v[5],
729 Saturday => &mut v[6],
730 } = name;
731 }
732 Self { names: (&v).into() }
733 }
734
735 /// Returns the name for a weekday, if possible.
736 pub fn get(&self, day: icu_calendar::types::Weekday) -> Option<&str> {
737 use icu_calendar::types::Weekday::*;
738 self.names.get(match day {
739 Sunday => 0,
740 Monday => 1,
741 Tuesday => 2,
742 Wednesday => 3,
743 Thursday => 4,
744 Friday => 5,
745 Saturday => 6,
746 })
747 }
748 }
749
750 size_test!(DayPeriodNames, day_period_names_v1_size, 24);
751
752 /// Names used for representing the day period.
753 ///
754 #[doc = day_period_names_v1_size!()]
755 ///
756 /// <div class="stab unstable">
757 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
758 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
759 /// to be stable, their Rust representation might not be. Use with caution.
760 /// </div>
761 #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
762 #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
763 #[cfg_attr(feature = "datagen", databake(path = icu_datetime::provider::names))]
764 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
765 #[yoke(prove_covariance_manually)]
766 pub struct DayPeriodNames<'data> {
767 /// The elements are in order: AM, PM, (noon), (midnight), where the latter two are optional.
768 /// In the case noon is missing but midnight is present, the noon value can be the empty string. This is unlikely.
769 /// If the locale has flexible day periods, the day period rules are encoded as the first 4 bytes of the 5th string,
770 /// and the remainder of the 5th string and any remaining strings contain the flexible period names. Noon and
771 /// midnight might be empty in this case.
772 #[cfg_attr(feature = "serde", serde(borrow))]
773 pub names: VarZeroVec<'data, str>,
774 }
775
776 icu_provider::data_struct!(
777 DayPeriodNames<'_>,
778 #[cfg(feature = "datagen")]
779 );
780
781 impl DayPeriodNames<'_> {
782 /// Gets the 'am' name.
783 pub(crate) fn am(&self) -> Option<&str> {
784 self.names.get(0)
785 }
786 /// Gets the 'pm' name.
787 pub(crate) fn pm(&self) -> Option<&str> {
788 self.names.get(1)
789 }
790 /// Gets the 'noon' name.
791 pub(crate) fn noon(&self) -> Option<&str> {
792 self.names.get(2).filter(|s| !s.is_empty())
793 }
794 /// Gets the 'midnight' name.
795 pub(crate) fn midnight(&self) -> Option<&str> {
796 self.names.get(3).filter(|s| !s.is_empty())
797 }
798 /// Gets the name for a flexible day period.
799 pub(crate) fn flexible_day_period(&self, hour: Hour) -> Option<&str> {
800 let (rules, first_name) = self.names.get(4)?.split_at_checked(4)?;
801 let offset = DayPeriodRules::decode_from_str(rules)?.name_offset(hour);
802 if offset == 0 {
803 Some(first_name)
804 } else {
805 self.names.get(4 + offset)
806 }
807 }
808 /// Gets the day period rules, if present.
809 pub fn day_period_rules(&self) -> Option<DayPeriodRules> {
810 let (rules, _) = self.names.get(4)?.split_at_checked(4)?;
811 DayPeriodRules::decode_from_str(rules)
812 }
813 }
814
815 /// Calendar-agnostic year name data marker
816 pub type YearNamesV1 = ErasedMarker<YearNames<'static>>;
817
818 /// Calendar-agnostic month name data marker
819 pub type MonthNamesV1 = ErasedMarker<MonthNames<'static>>;
820
821 /// Re-export of weekday names marker for more consistency
822 pub use DatetimeNamesWeekdayV1 as WeekdayNamesV1;
823
824 /// Re-export of day period names marker for more consistency
825 pub use DatetimeNamesDayperiodV1 as DayPeriodNamesV1;
826
827 #[test]
828 fn test_dayperiod_names() {
829 let names_zh = DataProvider::<DayPeriodNamesV1>::load(
830 &crate::provider::Baked,
831 DataRequest {
832 id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
833 DataMarkerAttributes::from_str_or_panic("5"),
834 &icu::locale::data_locale!("zh"),
835 ),
836 ..Default::default()
837 },
838 )
839 .unwrap()
840 .payload;
841
842 // Simplified Chinese (zh) does not map non-B skeletons to B in CLDR, so it only has standard names
843 assert_eq!(names_zh.get().am(), Some("上午"));
844 assert_eq!(names_zh.get().pm(), Some("δΈ‹εˆ"));
845 assert_eq!(
846 names_zh.get().flexible_day_period(12u8.try_into().unwrap()),
847 None
848 );
849
850 let names_zh_hant = DataProvider::<DayPeriodNamesV1>::load(
851 &crate::provider::Baked,
852 DataRequest {
853 id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
854 DataMarkerAttributes::from_str_or_panic("5"),
855 &icu::locale::data_locale!("zh-Hant"),
856 ),
857 ..Default::default()
858 },
859 )
860 .unwrap()
861 .payload;
862
863 // Traditional Chinese (zh-Hant) maps non-B skeletons to B, so it has flexible names
864 assert_eq!(names_zh_hant.get().am(), Some("上午"));
865 assert_eq!(names_zh_hant.get().pm(), Some("δΈ‹εˆ"));
866 assert_eq!(
867 names_zh_hant
868 .get()
869 .flexible_day_period(19u8.try_into().unwrap()),
870 Some("ζ™šδΈŠ")
871 );
872 }