Debian Codegraph @

debian/rust-vendor/icu_datetime/src/provider/semantic_skeletons.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 semantic skeletons.
6
7 use crate::provider::packed_pattern::PackedPatterns;
8 use crate::provider::pattern::runtime;
9 use crate::size_test_macro::size_test;
10 use icu_provider::prelude::*;
11
12 /// Helpers involving the data marker attributes used for date names.
13 ///
14 /// <div class="stab unstable">
15 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
16 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
17 /// to be stable, their Rust representation might not be. Use with caution.
18 /// </div>
19 #[allow(missing_docs)]
20 pub mod marker_attrs {
21 use icu_provider::DataMarkerAttributes;
22
23 pub const NUMERIC: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("1");
24 pub const ABBR: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("3");
25 pub const NARROW: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("4");
26 pub const WIDE: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("5");
27 pub const SHORT: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("6");
28 pub const ABBR_STANDALONE: &DataMarkerAttributes =
29 DataMarkerAttributes::from_str_or_panic("3s");
30 pub const NARROW_STANDALONE: &DataMarkerAttributes =
31 DataMarkerAttributes::from_str_or_panic("4s");
32 pub const WIDE_STANDALONE: &DataMarkerAttributes =
33 DataMarkerAttributes::from_str_or_panic("5s");
34 pub const SHORT_STANDALONE: &DataMarkerAttributes =
35 DataMarkerAttributes::from_str_or_panic("6s");
36
37 pub const PATTERN_LONG: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("l");
38 pub const PATTERN_MEDIUM: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("m");
39 pub const PATTERN_SHORT: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("s");
40
41 // TODO: The 12-hour and 24-hour DataMarkerAttributes can probably be deleted
42
43 pub const PATTERN_LONG12: &DataMarkerAttributes =
44 DataMarkerAttributes::from_str_or_panic("l12");
45 pub const PATTERN_MEDIUM12: &DataMarkerAttributes =
46 DataMarkerAttributes::from_str_or_panic("m12");
47 pub const PATTERN_SHORT12: &DataMarkerAttributes =
48 DataMarkerAttributes::from_str_or_panic("s12");
49
50 pub const PATTERN_LONG24: &DataMarkerAttributes =
51 DataMarkerAttributes::from_str_or_panic("l24");
52 pub const PATTERN_MEDIUM24: &DataMarkerAttributes =
53 DataMarkerAttributes::from_str_or_panic("m24");
54 pub const PATTERN_SHORT24: &DataMarkerAttributes =
55 DataMarkerAttributes::from_str_or_panic("s24");
56
57 pub const PATTERN_LONG_DT: &DataMarkerAttributes =
58 DataMarkerAttributes::from_str_or_panic("ldt");
59 pub const PATTERN_MEDIUM_DT: &DataMarkerAttributes =
60 DataMarkerAttributes::from_str_or_panic("mdt");
61 pub const PATTERN_SHORT_DT: &DataMarkerAttributes =
62 DataMarkerAttributes::from_str_or_panic("sdt");
63
64 pub const PATTERN_LONG_DZ: &DataMarkerAttributes =
65 DataMarkerAttributes::from_str_or_panic("ldz");
66 pub const PATTERN_MEDIUM_DZ: &DataMarkerAttributes =
67 DataMarkerAttributes::from_str_or_panic("mdz");
68 pub const PATTERN_SHORT_DZ: &DataMarkerAttributes =
69 DataMarkerAttributes::from_str_or_panic("sdz");
70
71 pub const PATTERN_LONG_TZ: &DataMarkerAttributes =
72 DataMarkerAttributes::from_str_or_panic("ltz");
73 pub const PATTERN_MEDIUM_TZ: &DataMarkerAttributes =
74 DataMarkerAttributes::from_str_or_panic("mtz");
75 pub const PATTERN_SHORT_TZ: &DataMarkerAttributes =
76 DataMarkerAttributes::from_str_or_panic("stz");
77
78 pub const PATTERN_LONG_DTZ: &DataMarkerAttributes =
79 DataMarkerAttributes::from_str_or_panic("ldtz");
80 pub const PATTERN_MEDIUM_DTZ: &DataMarkerAttributes =
81 DataMarkerAttributes::from_str_or_panic("mdtz");
82 pub const PATTERN_SHORT_DTZ: &DataMarkerAttributes =
83 DataMarkerAttributes::from_str_or_panic("sdtz");
84
85 pub const NUMERIC_STR: &str = NUMERIC.as_str();
86 pub const ABBR_STR: &str = ABBR.as_str();
87 pub const NARROW_STR: &str = NARROW.as_str();
88 pub const WIDE_STR: &str = WIDE.as_str();
89 pub const SHORT_STR: &str = SHORT.as_str();
90 pub const ABBR_STANDALONE_STR: &str = ABBR_STANDALONE.as_str();
91 pub const NARROW_STANDALONE_STR: &str = NARROW_STANDALONE.as_str();
92 pub const WIDE_STANDALONE_STR: &str = WIDE_STANDALONE.as_str();
93 pub const SHORT_STANDALONE_STR: &str = SHORT_STANDALONE.as_str();
94
95 pub const PATTERN_LONG_STR: &str = PATTERN_LONG.as_str();
96 pub const PATTERN_MEDIUM_STR: &str = PATTERN_MEDIUM.as_str();
97 pub const PATTERN_SHORT_STR: &str = PATTERN_SHORT.as_str();
98
99 pub const PATTERN_LONG12_STR: &str = PATTERN_LONG12.as_str();
100 pub const PATTERN_MEDIUM12_STR: &str = PATTERN_MEDIUM12.as_str();
101 pub const PATTERN_SHORT12_STR: &str = PATTERN_SHORT12.as_str();
102
103 pub const PATTERN_LONG24_STR: &str = PATTERN_LONG24.as_str();
104 pub const PATTERN_MEDIUM24_STR: &str = PATTERN_MEDIUM24.as_str();
105 pub const PATTERN_SHORT24_STR: &str = PATTERN_SHORT24.as_str();
106
107 pub const PATTERN_LONG_DT_STR: &str = PATTERN_LONG_DT.as_str();
108 pub const PATTERN_MEDIUM_DT_STR: &str = PATTERN_MEDIUM_DT.as_str();
109 pub const PATTERN_SHORT_DT_STR: &str = PATTERN_SHORT_DT.as_str();
110
111 pub const PATTERN_LONG_DZ_STR: &str = PATTERN_LONG_DZ.as_str();
112 pub const PATTERN_MEDIUM_DZ_STR: &str = PATTERN_MEDIUM_DZ.as_str();
113 pub const PATTERN_SHORT_DZ_STR: &str = PATTERN_SHORT_DZ.as_str();
114
115 pub const PATTERN_LONG_TZ_STR: &str = PATTERN_LONG_TZ.as_str();
116 pub const PATTERN_MEDIUM_TZ_STR: &str = PATTERN_MEDIUM_TZ.as_str();
117 pub const PATTERN_SHORT_TZ_STR: &str = PATTERN_SHORT_TZ.as_str();
118
119 pub const PATTERN_LONG_DTZ_STR: &str = PATTERN_LONG_DTZ.as_str();
120 pub const PATTERN_MEDIUM_DTZ_STR: &str = PATTERN_MEDIUM_DTZ.as_str();
121 pub const PATTERN_SHORT_DTZ_STR: &str = PATTERN_SHORT_DTZ.as_str();
122
123 /// Field lengths supported in data marker attribute.
124 ///
125 /// For a stable version of this enum, use one of the length enums in [`pattern`].
126 ///
127 /// <div class="stab unstable">
128 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
129 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
130 /// to be stable, their Rust representation might not be. Use with caution.
131 /// </div>
132 ///
133 /// [`pattern`]: crate::pattern
134 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
135 #[allow(clippy::exhaustive_enums)] // documented as unstable
136 pub enum Length {
137 Abbr,
138 Narrow,
139 Wide,
140 Short,
141 Numeric,
142 }
143
144 /// Pattern lengths supported in data marker attributes.
145 ///
146 /// For a stable version of this enum, use [`Length`].
147 ///
148 /// <div class="stab unstable">
149 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
150 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
151 /// to be stable, their Rust representation might not be. Use with caution.
152 /// </div>
153 ///
154 /// [`Length`]: crate::options::Length
155 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
156 pub enum PatternLength {
157 Long,
158 Medium,
159 Short,
160 }
161
162 /// Field contexts supported in data marker attributes.
163 ///
164 /// For a stable version of this enum, use one of the specific field symbol enums in [`fields`].
165 ///
166 /// <div class="stab unstable">
167 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
168 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
169 /// to be stable, their Rust representation might not be. Use with caution.
170 /// </div>
171 ///
172 /// [`fields`]: crate::provider::fields
173 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
174 #[allow(clippy::exhaustive_enums)] // documented as unstable
175 pub enum Context {
176 Format,
177 Standalone,
178 }
179
180 /// Date, time, and time zone combinations supported in data marker attributes.
181 ///
182 /// <div class="stab unstable">
183 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
184 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
185 /// to be stable, their Rust representation might not be. Use with caution.
186 /// </div>
187 ///
188 /// [`fields`]: crate::provider::fields
189 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
190 #[allow(clippy::exhaustive_enums)] // documented as unstable
191 pub enum GlueType {
192 DateTime,
193 DateZone,
194 TimeZone,
195 DateTimeZone,
196 }
197
198 /// Parses a name data marker attribute to enum values.
199 ///
200 /// <div class="stab unstable">
201 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
202 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
203 /// to be stable, their Rust representation might not be. Use with caution.
204 /// </div>
205 pub fn name_marker_attr_info(marker_attr: &DataMarkerAttributes) -> Option<(Context, Length)> {
206 use {Context::*, Length::*};
207 match &**marker_attr {
208 NUMERIC_STR => Some((Format, Numeric)),
209 ABBR_STR => Some((Format, Abbr)),
210 NARROW_STR => Some((Format, Narrow)),
211 WIDE_STR => Some((Format, Wide)),
212 SHORT_STR => Some((Format, Short)),
213 ABBR_STANDALONE_STR => Some((Standalone, Abbr)),
214 NARROW_STANDALONE_STR => Some((Standalone, Narrow)),
215 WIDE_STANDALONE_STR => Some((Standalone, Wide)),
216 SHORT_STANDALONE_STR => Some((Standalone, Short)),
217 _ => None,
218 }
219 }
220
221 /// Parses a pattern data marker attribute to enum values.
222 ///
223 /// <div class="stab unstable">
224 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
225 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
226 /// to be stable, their Rust representation might not be. Use with caution.
227 /// </div>
228 pub fn pattern_marker_attr_info_for_glue(
229 marker_attr: &DataMarkerAttributes,
230 ) -> Option<(PatternLength, GlueType)> {
231 use {GlueType::*, PatternLength::*};
232 match &**marker_attr {
233 PATTERN_LONG_DT_STR => Some((Long, DateTime)),
234 PATTERN_MEDIUM_DT_STR => Some((Medium, DateTime)),
235 PATTERN_SHORT_DT_STR => Some((Short, DateTime)),
236
237 PATTERN_LONG_DZ_STR => Some((Long, DateZone)),
238 PATTERN_MEDIUM_DZ_STR => Some((Medium, DateZone)),
239 PATTERN_SHORT_DZ_STR => Some((Short, DateZone)),
240
241 PATTERN_LONG_TZ_STR => Some((Long, TimeZone)),
242 PATTERN_MEDIUM_TZ_STR => Some((Medium, TimeZone)),
243 PATTERN_SHORT_TZ_STR => Some((Short, TimeZone)),
244
245 PATTERN_LONG_DTZ_STR => Some((Long, DateTimeZone)),
246 PATTERN_MEDIUM_DTZ_STR => Some((Medium, DateTimeZone)),
247 PATTERN_SHORT_DTZ_STR => Some((Short, DateTimeZone)),
248
249 _ => None,
250 }
251 }
252
253 /// Creates a name data marker attribute from the enum values.
254 ///
255 /// <div class="stab unstable">
256 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
257 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
258 /// to be stable, their Rust representation might not be. Use with caution.
259 /// </div>
260 pub fn name_attr_for(context: Context, length: Length) -> &'static DataMarkerAttributes {
261 use {Context::*, Length::*};
262 match (context, length) {
263 (Format, Numeric) => NUMERIC,
264 (Format, Abbr) => ABBR,
265 (Format, Narrow) => NARROW,
266 (Format, Wide) => WIDE,
267 (Format, Short) => SHORT,
268 (Standalone, Numeric) => NUMERIC,
269 (Standalone, Abbr) => ABBR_STANDALONE,
270 (Standalone, Narrow) => NARROW_STANDALONE,
271 (Standalone, Wide) => WIDE_STANDALONE,
272 (Standalone, Short) => SHORT_STANDALONE,
273 }
274 }
275
276 pub fn pattern_marker_attr_for_glue(
277 length: PatternLength,
278 glue_type: GlueType,
279 ) -> &'static DataMarkerAttributes {
280 use {GlueType::*, PatternLength::*};
281 match (length, glue_type) {
282 (Long, DateTime) => PATTERN_LONG_DT,
283 (Medium, DateTime) => PATTERN_MEDIUM_DT,
284 (Short, DateTime) => PATTERN_SHORT_DT,
285
286 (Long, DateZone) => PATTERN_LONG_DZ,
287 (Medium, DateZone) => PATTERN_MEDIUM_DZ,
288 (Short, DateZone) => PATTERN_SHORT_DZ,
289
290 (Long, TimeZone) => PATTERN_LONG_TZ,
291 (Medium, TimeZone) => PATTERN_MEDIUM_TZ,
292 (Short, TimeZone) => PATTERN_SHORT_TZ,
293
294 (Long, DateTimeZone) => PATTERN_LONG_DTZ,
295 (Medium, DateTimeZone) => PATTERN_MEDIUM_DTZ,
296 (Short, DateTimeZone) => PATTERN_SHORT_DTZ,
297 }
298 }
299 }
300
301 // TODO: We may need to support plural forms here. Something like
302 // pub enum NeoPatternPlurals<'data> {
303 // SingleDate(runtime::Pattern<'data>),
304 // WeekPlurals(ZeroMap<'data, PluralCategory, runtime::PatternULE>),
305 // }
306
307 size_test!(GluePattern, glue_pattern_v1_size, 24);
308
309 /// The default per-length patterns used for combining dates, times, and timezones into formatted strings.
310 #[doc = glue_pattern_v1_size!()]
311 ///
312 /// <div class="stab unstable">
313 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
314 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
315 /// to be stable, their Rust representation might not be. Use with caution.
316 /// </div>
317 #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
318 #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
319 #[cfg_attr(feature = "datagen", databake(path = icu_datetime::provider::semantic_skeletons))]
320 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
321 #[yoke(prove_covariance_manually)]
322 pub struct GluePattern<'data> {
323 /// The pattern
324 #[cfg_attr(feature = "serde", serde(borrow))]
325 pub pattern: runtime::GenericPattern<'data>,
326 }
327
328 icu_provider::data_struct!(
329 GluePattern<'_>,
330 #[cfg(feature = "datagen")]
331 );
332
333 icu_provider::data_marker!(
334 /// `DatetimePatternsGlueV1`
335 DatetimePatternsGlueV1,
336 GluePattern<'static>
337 );
338
339 icu_provider::data_marker!(
340 /// `DatetimePatternsDateBuddhistV1`
341 DatetimePatternsDateBuddhistV1,
342 PackedPatterns<'static>
343 );
344 icu_provider::data_marker!(
345 /// `DatetimePatternsDateChineseV1`
346 DatetimePatternsDateChineseV1,
347 PackedPatterns<'static>
348 );
349 icu_provider::data_marker!(
350 /// `DatetimePatternsDateCopticV1`
351 DatetimePatternsDateCopticV1,
352 PackedPatterns<'static>
353 );
354 icu_provider::data_marker!(
355 /// `DatetimePatternsDateDangiV1`
356 DatetimePatternsDateDangiV1,
357 PackedPatterns<'static>
358 );
359 icu_provider::data_marker!(
360 /// `DatetimePatternsDateEthiopianV1`
361 DatetimePatternsDateEthiopianV1,
362 PackedPatterns<'static>
363 );
364 icu_provider::data_marker!(
365 /// `DatetimePatternsDateGregorianV1`
366 DatetimePatternsDateGregorianV1,
367 PackedPatterns<'static>
368 );
369 icu_provider::data_marker!(
370 /// `DatetimePatternsDateHebrewV1`
371 DatetimePatternsDateHebrewV1,
372 PackedPatterns<'static>
373 );
374 icu_provider::data_marker!(
375 /// `DatetimePatternsDateIndianV1`
376 DatetimePatternsDateIndianV1,
377 PackedPatterns<'static>
378 );
379 icu_provider::data_marker!(
380 /// `DatetimePatternsDateHijriV1`
381 DatetimePatternsDateHijriV1,
382 PackedPatterns<'static>
383 );
384 icu_provider::data_marker!(
385 /// `DatetimePatternsDateJapaneseV1`
386 DatetimePatternsDateJapaneseV1,
387 PackedPatterns<'static>
388 );
389 icu_provider::data_marker!(
390 /// `DatetimePatternsDatePersianV1`
391 DatetimePatternsDatePersianV1,
392 PackedPatterns<'static>
393 );
394 icu_provider::data_marker!(
395 /// `DatetimePatternsDateRocV1`
396 DatetimePatternsDateRocV1,
397 PackedPatterns<'static>
398 );
399 icu_provider::data_marker!(
400 /// `DatetimePatternsTimeV1`
401 DatetimePatternsTimeV1,
402 PackedPatterns<'static>
403 );