Coverage Report

Created: 2026-05-16 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/icu_provider-2.2.0/src/fallback.rs
Line
Count
Source
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]
16
pub 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
33
impl LocaleFallbackPriority {
34
    /// Const-friendly version of [`Default::default`].
35
0
    pub const fn default() -> Self {
36
0
        Self::Language
37
0
    }
38
}
39
40
impl Default for LocaleFallbackPriority {
41
0
    fn default() -> Self {
42
0
        Self::default()
43
0
    }
44
}
45
46
/// Configuration settings for a particular fallback operation.
47
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
48
#[non_exhaustive]
49
pub 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
112
impl LocaleFallbackConfig {
113
    /// Const version of [`Default::default`].
114
0
    pub const fn default() -> Self {
115
0
        Self {
116
0
            priority: LocaleFallbackPriority::default(),
117
0
        }
118
0
    }
119
}
120
121
impl Default for LocaleFallbackConfig {
122
0
    fn default() -> Self {
123
0
        Self::default()
124
0
    }
125
}