icu_provider/
fallback.rs

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//! Options to define fallback behaviour.
6//!
7//! These options are consumed by the `LocaleFallbacker` in the `icu_locales` crate
8//! (or the `icu::locales` module), but are defined here because they are used by `DataMarkerInfo`.
9
10/// Hint for which subtag to prioritize during fallback.
11///
12/// For example, `"en-US"` might fall back to either `"en"` or `"und-US"` depending
13/// on this enum.
14#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
15#[non_exhaustive]
16pub enum LocaleFallbackPriority {
17    /// Prioritize the language. This is the default behavior.
18    ///
19    /// For example, `"en-US"` should go to `"en"` and then `"und"`.
20    Language,
21    /// Prioritize the script.
22    ///
23    /// For example, `"en-US"` should go to `"en"` and then `"und-Latn"` and then `"und"`.
24    Script,
25    /// Prioritize the region.
26    ///
27    /// For example, `"en-US"` should go to `"und-US"` and then `"und"`.
28    Region,
29}
30
31impl LocaleFallbackPriority {
32    /// Const-friendly version of [`Default::default`].
33    pub const fn default() -> Self {
34        Self::Language
35    }
36}
37
38impl Default for LocaleFallbackPriority {
39    fn default() -> Self {
40        Self::default()
41    }
42}
43
44/// Configuration settings for a particular fallback operation.
45#[derive(Debug, Clone, PartialEq, Eq, Copy)]
46#[non_exhaustive]
47pub struct LocaleFallbackConfig {
48    /// Strategy for choosing which subtags to drop during locale fallback.
49    ///
50    /// # Examples
51    ///
52    /// Retain the language and script subtags until the final step:
53    ///
54    /// ```
55    /// use icu::locale::fallback::LocaleFallbackConfig;
56    /// use icu::locale::fallback::LocaleFallbackPriority;
57    /// use icu::locale::locale;
58    /// use icu::locale::LocaleFallbacker;
59    ///
60    /// // Set up the fallback iterator.
61    /// let fallbacker = LocaleFallbacker::new();
62    /// let mut config = LocaleFallbackConfig::default();
63    /// config.priority = LocaleFallbackPriority::Language;
64    /// let mut fallback_iterator = fallbacker
65    ///     .for_config(config)
66    ///     .fallback_for(locale!("ca-ES-valencia").into());
67    ///
68    /// // Run the algorithm and check the results.
69    /// assert_eq!(fallback_iterator.get(), &locale!("ca-ES-valencia").into());
70    /// fallback_iterator.step();
71    /// assert_eq!(fallback_iterator.get(), &locale!("ca-ES").into());
72    /// fallback_iterator.step();
73    /// assert_eq!(fallback_iterator.get(), &locale!("ca-valencia").into());
74    /// fallback_iterator.step();
75    /// assert_eq!(fallback_iterator.get(), &locale!("ca").into());
76    /// fallback_iterator.step();
77    /// assert_eq!(fallback_iterator.get(), &locale!("und").into());
78    /// ```
79    ///
80    /// Retain the region subtag until the final step:
81    ///
82    /// ```
83    /// use icu::locale::fallback::LocaleFallbackConfig;
84    /// use icu::locale::fallback::LocaleFallbackPriority;
85    /// use icu::locale::locale;
86    /// use icu::locale::LocaleFallbacker;
87    ///
88    /// // Set up the fallback iterator.
89    /// let fallbacker = LocaleFallbacker::new();
90    /// let mut config = LocaleFallbackConfig::default();
91    /// config.priority = LocaleFallbackPriority::Region;
92    /// let mut fallback_iterator = fallbacker
93    ///     .for_config(config)
94    ///     .fallback_for(locale!("ca-ES-valencia").into());
95    ///
96    /// // Run the algorithm and check the results.
97    /// assert_eq!(fallback_iterator.get(), &locale!("ca-ES-valencia").into());
98    /// fallback_iterator.step();
99    /// assert_eq!(fallback_iterator.get(), &locale!("ca-ES").into());
100    /// fallback_iterator.step();
101    /// assert_eq!(fallback_iterator.get(), &locale!("und-ES-valencia").into());
102    /// fallback_iterator.step();
103    /// assert_eq!(fallback_iterator.get(), &locale!("und-ES").into());
104    /// fallback_iterator.step();
105    /// assert_eq!(fallback_iterator.get(), &locale!("und").into());
106    /// ```
107    pub priority: LocaleFallbackPriority,
108}
109
110impl LocaleFallbackConfig {
111    /// Const version of [`Default::default`].
112    pub const fn default() -> Self {
113        Self {
114            priority: LocaleFallbackPriority::default(),
115        }
116    }
117}
118
119impl Default for LocaleFallbackConfig {
120    fn default() -> Self {
121        Self::default()
122    }
123}