Skip to main content

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(#[automatically_derived]
impl ::core::fmt::Debug for LocaleFallbackPriority {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                LocaleFallbackPriority::Language => "Language",
                LocaleFallbackPriority::Script => "Script",
                LocaleFallbackPriority::Region => "Region",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for LocaleFallbackPriority {
    #[inline]
    fn eq(&self, other: &LocaleFallbackPriority) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for LocaleFallbackPriority {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::marker::Copy for LocaleFallbackPriority { }Copy, #[automatically_derived]
impl ::core::clone::Clone for LocaleFallbackPriority {
    #[inline]
    fn clone(&self) -> LocaleFallbackPriority { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialOrd for LocaleFallbackPriority {
    #[inline]
    fn partial_cmp(&self, other: &LocaleFallbackPriority)
        -> ::core::option::Option<::core::cmp::Ordering> {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
    }
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for LocaleFallbackPriority {
    #[inline]
    fn cmp(&self, other: &LocaleFallbackPriority) -> ::core::cmp::Ordering {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr)
    }
}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    ///
29    /// This should be used for [data that is region-specific](https://github.com/unicode-org/cldr/blob/main/common/supplemental/rgScope.xml).
30    Region,
31}
32
33impl LocaleFallbackPriority {
34    /// Const-friendly version of [`Default::default`].
35    pub const fn default() -> Self {
36        Self::Language
37    }
38}
39
40impl Default for LocaleFallbackPriority {
41    fn default() -> Self {
42        Self::default()
43    }
44}
45
46/// Configuration settings for a particular fallback operation.
47#[derive(#[automatically_derived]
impl ::core::fmt::Debug for LocaleFallbackConfig {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "LocaleFallbackConfig", "priority", &&self.priority)
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for LocaleFallbackConfig {
    #[inline]
    fn clone(&self) -> LocaleFallbackConfig {
        let _: ::core::clone::AssertParamIsClone<LocaleFallbackPriority>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for LocaleFallbackConfig {
    #[inline]
    fn eq(&self, other: &LocaleFallbackConfig) -> bool {
        self.priority == other.priority
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for LocaleFallbackConfig {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<LocaleFallbackPriority>;
    }
}Eq, #[automatically_derived]
impl ::core::marker::Copy for LocaleFallbackConfig { }Copy)]
48#[non_exhaustive]
49pub struct LocaleFallbackConfig {
50    /// Strategy for choosing which subtags to drop during locale fallback.
51    ///
52    /// # Examples
53    ///
54    /// Retain the language and script subtags until the final step:
55    ///
56    /// ```
57    /// use icu::locale::fallback::LocaleFallbackConfig;
58    /// use icu::locale::fallback::LocaleFallbackPriority;
59    /// use icu::locale::locale;
60    /// use icu::locale::LocaleFallbacker;
61    ///
62    /// // Set up the fallback iterator.
63    /// let fallbacker = LocaleFallbacker::new();
64    /// let mut config = LocaleFallbackConfig::default();
65    /// config.priority = LocaleFallbackPriority::Language;
66    /// let mut fallback_iterator = fallbacker
67    ///     .for_config(config)
68    ///     .fallback_for(locale!("ca-ES-valencia").into());
69    ///
70    /// // Run the algorithm and check the results.
71    /// assert_eq!(fallback_iterator.get(), &locale!("ca-ES-valencia").into());
72    /// fallback_iterator.step();
73    /// assert_eq!(fallback_iterator.get(), &locale!("ca-ES").into());
74    /// fallback_iterator.step();
75    /// assert_eq!(fallback_iterator.get(), &locale!("ca-valencia").into());
76    /// fallback_iterator.step();
77    /// assert_eq!(fallback_iterator.get(), &locale!("ca").into());
78    /// fallback_iterator.step();
79    /// assert_eq!(fallback_iterator.get(), &locale!("und").into());
80    /// ```
81    ///
82    /// Retain the region subtag until the final step:
83    ///
84    /// ```
85    /// use icu::locale::fallback::LocaleFallbackConfig;
86    /// use icu::locale::fallback::LocaleFallbackPriority;
87    /// use icu::locale::locale;
88    /// use icu::locale::LocaleFallbacker;
89    ///
90    /// // Set up the fallback iterator.
91    /// let fallbacker = LocaleFallbacker::new();
92    /// let mut config = LocaleFallbackConfig::default();
93    /// config.priority = LocaleFallbackPriority::Region;
94    /// let mut fallback_iterator = fallbacker
95    ///     .for_config(config)
96    ///     .fallback_for(locale!("ca-ES-valencia").into());
97    ///
98    /// // Run the algorithm and check the results.
99    /// assert_eq!(fallback_iterator.get(), &locale!("ca-ES-valencia").into());
100    /// fallback_iterator.step();
101    /// assert_eq!(fallback_iterator.get(), &locale!("ca-ES").into());
102    /// fallback_iterator.step();
103    /// assert_eq!(fallback_iterator.get(), &locale!("und-ES-valencia").into());
104    /// fallback_iterator.step();
105    /// assert_eq!(fallback_iterator.get(), &locale!("und-ES").into());
106    /// fallback_iterator.step();
107    /// assert_eq!(fallback_iterator.get(), &locale!("und").into());
108    /// ```
109    pub priority: LocaleFallbackPriority,
110}
111
112impl LocaleFallbackConfig {
113    /// Const version of [`Default::default`].
114    pub const fn default() -> Self {
115        Self {
116            priority: LocaleFallbackPriority::default(),
117        }
118    }
119}
120
121impl Default for LocaleFallbackConfig {
122    fn default() -> Self {
123        Self::default()
124    }
125}