Debian Codegraph @

debian/rust-vendor/icu_locale/src/provider.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 // Provider structs must be stable
6 #![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]
7
8 //! 🚧 \[Unstable\] Data provider struct definitions for this ICU4X component.
9 //!
10 //! <div class="stab unstable">
11 //! 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
12 //! including in SemVer minor releases. While the serde representation of data structs is guaranteed
13 //! to be stable, their Rust representation might not be. Use with caution.
14 //! </div>
15 //!
16 //! Read more about data providers: [`icu_provider`]
17
18 #[cfg(feature = "unstable")]
19 /// Data provider structs for locale display names.
20 pub mod names;
21
22 #[cfg(feature = "compiled_data")]
23 #[derive(Debug)]
24 /// Baked data
25 ///
26 /// <div class="stab unstable">
27 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
28 /// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
29 /// guaranteed to match with this version's `*_unstable` providers. Use with caution.
30 /// </div>
31 pub struct Baked;
32
33 #[cfg(feature = "compiled_data")]
34 #[allow(unused_imports)]
35 const _: () = {
36 use icu_locale_data::*;
37 pub mod icu {
38 pub use crate as locale;
39 pub use icu_collections as collections;
40 }
41 make_provider!(Baked);
42 impl_locale_aliases_v1!(Baked);
43 impl_locale_likely_subtags_extended_v1!(Baked);
44 impl_locale_likely_subtags_script_region_v1!(Baked);
45 impl_locale_script_direction_v1!(Baked);
46
47 impl_locale_exemplar_characters_auxiliary_v1!(Baked);
48 impl_locale_exemplar_characters_index_v1!(Baked);
49 impl_locale_exemplar_characters_main_v1!(Baked);
50 impl_locale_exemplar_characters_numbers_v1!(Baked);
51 impl_locale_exemplar_characters_punctuation_v1!(Baked);
52
53 #[cfg(feature = "unstable")]
54 impl_locale_names_region_medium_tiny_v1!(Baked);
55 #[cfg(feature = "unstable")]
56 impl_locale_names_region_medium_light_v1!(Baked);
57 #[cfg(feature = "unstable")]
58 impl_locale_names_region_short_tiny_v1!(Baked);
59 #[cfg(feature = "unstable")]
60 impl_locale_names_region_short_light_v1!(Baked);
61 #[cfg(feature = "unstable")]
62 impl_locale_names_language_medium_tiny_v1!(Baked);
63 #[cfg(feature = "unstable")]
64 impl_locale_names_language_medium_light_v1!(Baked);
65 #[cfg(feature = "unstable")]
66 impl_locale_names_language_medium_heavy_v1!(Baked);
67 #[cfg(feature = "unstable")]
68 impl_locale_names_language_short_light_v1!(Baked);
69 #[cfg(feature = "unstable")]
70 impl_locale_names_language_short_heavy_v1!(Baked);
71 #[cfg(feature = "unstable")]
72 impl_locale_names_language_long_light_v1!(Baked);
73 #[cfg(feature = "unstable")]
74 impl_locale_names_language_long_heavy_v1!(Baked);
75 #[cfg(feature = "unstable")]
76 impl_locale_names_language_menu_medium_light_v1!(Baked);
77 #[cfg(feature = "unstable")]
78 impl_locale_names_language_menu_medium_heavy_v1!(Baked);
79 #[cfg(feature = "unstable")]
80 impl_locale_names_script_medium_tiny_v1!(Baked);
81 #[cfg(feature = "unstable")]
82 impl_locale_names_script_medium_light_v1!(Baked);
83 #[cfg(feature = "unstable")]
84 impl_locale_names_script_medium_heavy_v1!(Baked);
85 #[cfg(feature = "unstable")]
86 impl_locale_names_script_short_heavy_v1!(Baked);
87 #[cfg(feature = "unstable")]
88 impl_locale_names_variant_medium_heavy_v1!(Baked);
89 #[cfg(feature = "unstable")]
90 impl_locale_names_essentials_v1!(Baked);
91 };
92
93 icu_provider::data_marker!(
94 /// Marker for locale alias data.
95 LocaleAliasesV1,
96 "locale/aliases/v1",
97 Aliases<'static>,
98 is_singleton = true
99 );
100 icu_provider::data_marker!(
101 /// Marker for data for likely subtags for scripts and regions.
102 LocaleLikelySubtagsScriptRegionV1,
103 "locale/likely/subtags/script/region/v1",
104 LikelySubtagsForScriptRegion<'static>,
105 is_singleton = true
106 );
107 icu_provider::data_marker!(
108 /// Marker for extended data for likely subtags.
109 LocaleLikelySubtagsExtendedV1,
110 "locale/likely/subtags/extended/v1",
111 LikelySubtagsExtended<'static>,
112 is_singleton = true
113 );
114
115 icu_provider::data_marker!(
116 /// Marker for script direction data.
117 LocaleScriptDirectionV1,
118 "locale/script/direction/v1",
119 ScriptDirection<'static>,
120 is_singleton = true
121 );
122
123 icu_provider::data_marker!(
124 /// Marker for auxiliary exemplar characters data.
125 LocaleExemplarCharactersAuxiliaryV1,
126 "locale/exemplar/characters/auxiliary/v1",
127 ExemplarCharactersData<'static>,
128 );
129 icu_provider::data_marker!(
130 /// Marker for index exemplar characters data.
131 LocaleExemplarCharactersIndexV1,
132 "locale/exemplar/characters/index/v1",
133 ExemplarCharactersData<'static>,
134 );
135 icu_provider::data_marker!(
136 /// Marker for main exemplar characters data.
137 LocaleExemplarCharactersMainV1,
138 "locale/exemplar/characters/main/v1",
139 ExemplarCharactersData<'static>,
140 );
141 icu_provider::data_marker!(
142 /// Marker for numbers exemplar characters data.
143 LocaleExemplarCharactersNumbersV1,
144 "locale/exemplar/characters/numbers/v1",
145 ExemplarCharactersData<'static>,
146 );
147 icu_provider::data_marker!(
148 /// Marker for punctuation exemplar characters data.
149 LocaleExemplarCharactersPunctuationV1,
150 "locale/exemplar/characters/punctuation/v1",
151 ExemplarCharactersData<'static>,
152 );
153
154 #[cfg(feature = "datagen")]
155 /// The latest minimum set of markers required by this component.
156 pub const MARKERS: &[DataMarkerInfo] = &[
157 LocaleAliasesV1::INFO,
158 LocaleExemplarCharactersAuxiliaryV1::INFO,
159 LocaleExemplarCharactersIndexV1::INFO,
160 LocaleExemplarCharactersMainV1::INFO,
161 LocaleExemplarCharactersNumbersV1::INFO,
162 LocaleExemplarCharactersPunctuationV1::INFO,
163 LocaleLikelySubtagsExtendedV1::INFO,
164 LocaleLikelySubtagsScriptRegionV1::INFO,
165 LocaleScriptDirectionV1::INFO,
166 #[cfg(feature = "unstable")]
167 names::LocaleNamesRegionMediumTinyV1::INFO,
168 #[cfg(feature = "unstable")]
169 names::LocaleNamesRegionMediumLightV1::INFO,
170 #[cfg(feature = "unstable")]
171 names::LocaleNamesRegionShortTinyV1::INFO,
172 #[cfg(feature = "unstable")]
173 names::LocaleNamesRegionShortLightV1::INFO,
174 #[cfg(feature = "unstable")]
175 names::LocaleNamesLanguageMediumTinyV1::INFO,
176 #[cfg(feature = "unstable")]
177 names::LocaleNamesLanguageMediumLightV1::INFO,
178 #[cfg(feature = "unstable")]
179 names::LocaleNamesLanguageMediumHeavyV1::INFO,
180 #[cfg(feature = "unstable")]
181 names::LocaleNamesLanguageShortLightV1::INFO,
182 #[cfg(feature = "unstable")]
183 names::LocaleNamesLanguageShortHeavyV1::INFO,
184 #[cfg(feature = "unstable")]
185 names::LocaleNamesLanguageLongLightV1::INFO,
186 #[cfg(feature = "unstable")]
187 names::LocaleNamesLanguageLongHeavyV1::INFO,
188 #[cfg(feature = "unstable")]
189 names::LocaleNamesLanguageMenuMediumLightV1::INFO,
190 #[cfg(feature = "unstable")]
191 names::LocaleNamesLanguageMenuMediumHeavyV1::INFO,
192 #[cfg(feature = "unstable")]
193 names::LocaleNamesScriptMediumTinyV1::INFO,
194 #[cfg(feature = "unstable")]
195 names::LocaleNamesScriptMediumLightV1::INFO,
196 #[cfg(feature = "unstable")]
197 names::LocaleNamesScriptMediumHeavyV1::INFO,
198 #[cfg(feature = "unstable")]
199 names::LocaleNamesScriptShortHeavyV1::INFO,
200 #[cfg(feature = "unstable")]
201 names::LocaleNamesVariantMediumHeavyV1::INFO,
202 #[cfg(feature = "unstable")]
203 names::LocaleNamesEssentialsV1::INFO,
204 ];
205
206 #[cfg(doc)]
207 use crate::LanguageIdentifier;
208 use alloc::borrow::Cow;
209 use icu_collections::codepointinvliststringlist::CodePointInversionListAndStringList;
210 use icu_locale_core::subtags::{Language, Region, Script, Variant};
211 use icu_provider::prelude::*;
212 use tinystr::{TinyAsciiStr, UnvalidatedTinyAsciiStr};
213 use zerovec::{VarZeroVec, ZeroMap, ZeroSlice, ZeroVec};
214
215 // We use raw TinyAsciiStrs for map keys, as we then don't have to
216 // validate them as subtags on deserialization. Map lookup can be
217 // done even if they are not valid tags (an invalid key will just
218 // become inaccessible).
219 type UnvalidatedLanguage = UnvalidatedTinyAsciiStr<3>;
220 type UnvalidatedScript = UnvalidatedTinyAsciiStr<4>;
221 type UnvalidatedRegion = UnvalidatedTinyAsciiStr<3>;
222 type UnvalidatedVariant = UnvalidatedTinyAsciiStr<8>;
223 type UnvalidatedSubdivision = UnvalidatedTinyAsciiStr<7>;
224 type SemivalidatedSubdivision = TinyAsciiStr<7>;
225
226 // LanguageIdentifier doesn't have an AsULE implementation, so we have
227 // to store strs and parse when needed.
228 type UnvalidatedLanguageIdentifier = str;
229 type UnvalidatedLanguageIdentifierPair = StrStrPairVarULE;
230 type UnvalidatedLanguageVariantsPair = LanguageStrStrPairVarULE;
231
232 #[zerovec::make_varule(StrStrPairVarULE)]
233 #[zerovec::derive(Debug)]
234 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
235 #[cfg_attr(
236 feature = "serde",
237 derive(serde::Deserialize),
238 zerovec::derive(Deserialize)
239 )]
240 #[cfg_attr(
241 feature = "datagen",
242 derive(serde::Serialize, databake::Bake),
243 zerovec::derive(Serialize),
244 databake(path = icu_locale::provider),
245 )]
246 /// A pair of strings with a [`EncodeAsVarULE`](zerovec::ule::EncodeAsVarULE) implementation.
247 ///
248 /// <div class="stab unstable">
249 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
250 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
251 /// to be stable, their Rust representation might not be. Use with caution.
252 /// </div>
253 pub struct StrStrPair<'a>(
254 #[cfg_attr(feature = "serde", serde(borrow))] pub Cow<'a, str>,
255 #[cfg_attr(feature = "serde", serde(borrow))] pub Cow<'a, str>,
256 );
257
258 #[zerovec::make_varule(LanguageStrStrPairVarULE)]
259 #[zerovec::derive(Debug)]
260 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
261 #[cfg_attr(
262 feature = "serde",
263 derive(serde::Deserialize),
264 zerovec::derive(Deserialize)
265 )]
266 #[cfg_attr(
267 feature = "datagen",
268 derive(serde::Serialize, databake::Bake),
269 zerovec::derive(Serialize),
270 databake(path = icu_locale::provider),
271 )]
272 /// A triplet of strings with a [`EncodeAsVarULE`](zerovec::ule::EncodeAsVarULE) implementation.
273 pub struct LanguageStrStrPair<'a>(
274 pub Language,
275 #[cfg_attr(feature = "serde", serde(borrow))] pub Cow<'a, str>,
276 #[cfg_attr(feature = "serde", serde(borrow))] pub Cow<'a, str>,
277 );
278
279 #[derive(PartialEq, Clone, Default, yoke::Yokeable, zerofrom::ZeroFrom)]
280 #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
281 #[cfg_attr(feature = "datagen", databake(path = icu_locale::provider))]
282 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
283 #[yoke(prove_covariance_manually)]
284 /// This alias data is used for locale canonicalization.
285 ///
286 /// Each field defines a
287 /// mapping from an old identifier to a new identifier, based upon the rules in
288 /// from <https://unicode.org/reports/tr35/#LocaleId_Canonicalization>. The data
289 /// is stored in sorted order, allowing for binary search to identify rules to
290 /// apply. It is broken down into smaller vectors based upon some characteristic
291 /// of the data, to help avoid unnecessary searches. For example, the `sgn_region`
292 /// field contains aliases for sign language and region, so that it is not
293 /// necessary to search the data unless the input is a sign language.
294 ///
295 /// The algorithm in tr35 is not guaranteed to terminate on data other than what
296 /// is currently in CLDR. For this reason, it is not a good idea to attempt to add
297 /// or modify aliases for use in this structure.
298 ///
299 /// <div class="stab unstable">
300 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
301 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
302 /// to be stable, their Rust representation might not be. Use with caution.
303 /// </div>
304 // TODO: Use validated types as value types
305 // Notice: improves the alignment of `language_variants` speeding up canonicalization by upon
306 // to 40%. See https://github.com/unicode-org/icu4x/pull/2935 for details.
307 #[derive(Debug)]
308 pub struct Aliases<'data> {
309 /// `[language, variant(-variant)*] -> [langid]`
310 /// This is not a map as it's searched linearly according to the canonicalization rules.
311 #[cfg_attr(feature = "serde", serde(borrow))]
312 pub language_variants: VarZeroVec<'data, UnvalidatedLanguageVariantsPair>,
313 /// `sgn-[region] -> [language]`
314 #[cfg_attr(feature = "serde", serde(borrow))]
315 pub sgn_region: ZeroMap<'data, UnvalidatedRegion, Language>,
316 /// `[language{2}] -> [langid]`
317 #[cfg_attr(feature = "serde", serde(borrow))]
318 pub language_len2: ZeroMap<'data, UnvalidatedTinyAsciiStr<2>, UnvalidatedLanguageIdentifier>,
319 /// `[language{3}] -> [langid]`
320 #[cfg_attr(feature = "serde", serde(borrow))]
321 pub language_len3: ZeroMap<'data, UnvalidatedLanguage, UnvalidatedLanguageIdentifier>,
322 /// `[langid] -> [langid]`
323 /// This is not a map as it's searched linearly according to the canonicalization rules.
324 #[cfg_attr(feature = "serde", serde(borrow))]
325 pub language: VarZeroVec<'data, UnvalidatedLanguageIdentifierPair>,
326
327 /// `[script] -> [script]`
328 #[cfg_attr(feature = "serde", serde(borrow))]
329 pub script: ZeroMap<'data, UnvalidatedScript, Script>,
330
331 /// `[region{2}] -> [region]`
332 #[cfg_attr(feature = "serde", serde(borrow))]
333 pub region_alpha: ZeroMap<'data, UnvalidatedTinyAsciiStr<2>, Region>,
334 /// `[region{3}] -> [region]`
335 #[cfg_attr(feature = "serde", serde(borrow))]
336 pub region_num: ZeroMap<'data, UnvalidatedRegion, Region>,
337
338 /// `[region] -> [region]+`
339 #[cfg_attr(feature = "serde", serde(borrow))]
340 pub complex_region: ZeroMap<'data, UnvalidatedRegion, ZeroSlice<Region>>,
341
342 /// `[variant] -> [variant]`
343 #[cfg_attr(feature = "serde", serde(borrow))]
344 pub variant: ZeroMap<'data, UnvalidatedVariant, Variant>,
345
346 /// `[value{7}] -> [value{7}]`
347 #[cfg_attr(feature = "serde", serde(borrow))]
348 pub subdivision: ZeroMap<'data, UnvalidatedSubdivision, SemivalidatedSubdivision>,
349 }
350
351 icu_provider::data_struct!(
352 Aliases<'_>,
353 #[cfg(feature = "datagen")]
354 );
355
356 #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
357 #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
358 #[cfg_attr(feature = "datagen", databake(path = icu_locale::provider))]
359 /// This likely subtags data is used for the minimize and maximize operations.
360 ///
361 /// Each field defines a mapping from an old identifier to a new identifier,
362 /// based upon the rules in
363 /// <https://www.unicode.org/reports/tr35/#Likely_Subtags>.
364 ///
365 /// The data is stored is broken down into smaller vectors based upon the rules
366 /// defined for the likely subtags maximize algorithm.
367 ///
368 /// For efficiency, only the relevant part of the [`LanguageIdentifier`] is stored
369 /// for searching and replacing. E.g., the `script_region` field is used to store
370 /// rules for [`LanguageIdentifier`]s that contain a script and a region, but not a
371 /// language.
372 ///
373 /// This struct contains mappings when the input does not contain a language subtag.
374 /// Also see [`LikelySubtagsForLanguage`](icu_locale_fallback::provider::LikelySubtagsForLanguage).
375 ///
376 /// <div class="stab unstable">
377 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
378 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
379 /// to be stable, their Rust representation might not be. Use with caution.
380 /// </div>
381 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
382 #[yoke(prove_covariance_manually)]
383 pub struct LikelySubtagsForScriptRegion<'data> {
384 /// Script and region.
385 #[cfg_attr(feature = "serde", serde(borrow))]
386 pub script_region: ZeroMap<'data, (UnvalidatedScript, UnvalidatedRegion), Language>,
387 /// Just script.
388 #[cfg_attr(feature = "serde", serde(borrow))]
389 pub script: ZeroMap<'data, UnvalidatedScript, (Language, Region)>,
390 /// Just region.
391 #[cfg_attr(feature = "serde", serde(borrow))]
392 pub region: ZeroMap<'data, UnvalidatedRegion, (Language, Script)>,
393 }
394
395 icu_provider::data_struct!(
396 LikelySubtagsForScriptRegion<'_>,
397 #[cfg(feature = "datagen")]
398 );
399
400 #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
401 #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
402 #[cfg_attr(feature = "datagen", databake(path = icu_locale::provider))]
403 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
404 /// This likely subtags data is used for full coverage of locales, including ones that
405 /// don't otherwise have data in the Common Locale Data Repository (CLDR).
406 ///
407 /// <div class="stab unstable">
408 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
409 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
410 /// to be stable, their Rust representation might not be. Use with caution.
411 /// </div>
412 #[yoke(prove_covariance_manually)]
413 pub struct LikelySubtagsExtended<'data> {
414 /// Language and script.
415 #[cfg_attr(feature = "serde", serde(borrow))]
416 pub language_script: ZeroMap<'data, (UnvalidatedLanguage, UnvalidatedScript), Region>,
417 /// Language and region.
418 #[cfg_attr(feature = "serde", serde(borrow))]
419 pub language_region: ZeroMap<'data, (UnvalidatedLanguage, UnvalidatedRegion), Script>,
420 /// Just language.
421 #[cfg_attr(feature = "serde", serde(borrow))]
422 pub language: ZeroMap<'data, UnvalidatedLanguage, (Script, Region)>,
423 /// Script and region.
424 #[cfg_attr(feature = "serde", serde(borrow))]
425 pub script_region: ZeroMap<'data, (UnvalidatedScript, UnvalidatedRegion), Language>,
426 /// Just script.
427 #[cfg_attr(feature = "serde", serde(borrow))]
428 pub script: ZeroMap<'data, UnvalidatedScript, (Language, Region)>,
429 /// Just region.
430 #[cfg_attr(feature = "serde", serde(borrow))]
431 pub region: ZeroMap<'data, UnvalidatedRegion, (Language, Script)>,
432 }
433
434 icu_provider::data_struct!(
435 LikelySubtagsExtended<'_>,
436 #[cfg(feature = "datagen")]
437 );
438
439 #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
440 #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
441 #[cfg_attr(feature = "datagen", databake(path = icu_locale::provider))]
442 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
443 /// This directionality data is used to determine the script directionality of a locale.
444 ///
445 /// <div class="stab unstable">
446 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
447 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
448 /// to be stable, their Rust representation might not be. Use with caution.
449 /// </div>
450 #[yoke(prove_covariance_manually)]
451 pub struct ScriptDirection<'data> {
452 /// Scripts in right-to-left direction.
453 #[cfg_attr(feature = "serde", serde(borrow))]
454 pub rtl: ZeroVec<'data, UnvalidatedScript>,
455 /// Scripts in left-to-right direction.
456 #[cfg_attr(feature = "serde", serde(borrow))]
457 pub ltr: ZeroVec<'data, UnvalidatedScript>,
458 }
459
460 icu_provider::data_struct!(
461 ScriptDirection<'_>,
462 #[cfg(feature = "datagen")]
463 );
464
465 /// A set of characters and strings which share a particular property value.
466 ///
467 /// <div class="stab unstable">
468 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
469 /// including in SemVer minor releases. While the serde representation of data structs is guaranteed
470 /// to be stable, their Rust representation might not be. Use with caution.
471 /// </div>
472 #[derive(Debug, Eq, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
473 #[cfg_attr(
474 feature = "datagen",
475 derive(serde::Serialize, databake::Bake),
476 databake(path = icu_locale::provider),
477 )]
478 #[cfg_attr(feature = "serde", derive(serde::Deserialize))]
479 pub struct ExemplarCharactersData<'data>(
480 #[cfg_attr(feature = "serde", serde(borrow))] pub CodePointInversionListAndStringList<'data>,
481 );
482
483 icu_provider::data_struct!(
484 ExemplarCharactersData<'_>,
485 #[cfg(feature = "datagen")]
486 );