icu_provider_source/currency/
compact.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::cldr_serde;
use crate::IterableDataProviderCached;
use crate::SourceDataProvider;

use std::collections::BTreeMap;
use std::collections::HashSet;

use icu::experimental::dimension::provider::currency_compact::*;
use icu::plurals::PluralCategory;
use icu_provider::prelude::*;
use icu_provider::DataProvider;

impl DataProvider<ShortCurrencyCompactV1Marker> for SourceDataProvider {
    fn load(
        &self,
        req: DataRequest,
    ) -> Result<DataResponse<ShortCurrencyCompactV1Marker>, DataError> {
        let numbers_resource: &cldr_serde::numbers::Resource = self
            .cldr()?
            .numbers()
            .read_and_parse(req.id.locale, "numbers.json")?;

        let default_system = numbers_resource
            .main
            .value
            .numbers
            .default_numbering_system
            .as_str();

        let currency_patterns = &numbers_resource
            .main
            .value
            .numbers
            .numsys_data
            .currency_patterns;

        let compact_patterns = match currency_patterns
            .get(default_system)
            .and_then(|patterns| patterns.compact_short.as_ref())
            .map(|short_compact| &short_compact.standard.patterns)
        {
            Some(patterns) => patterns,
            None => {
                return Ok(DataResponse {
                    metadata: Default::default(),
                    payload: DataPayload::from_owned(ShortCurrencyCompactV1 {
                        compact_patterns: Default::default(),
                    }),
                })
            }
        };

        /// Try to parse a compact count from a string.
        fn try_parse_count_from_str(value: &str) -> Result<CompactCount, DataError> {
            let (count_str, is_alpha_next) = match value.split_once("-alt-alphaNextToNumber") {
                Some((count, _)) => (count, true),
                None => (value, false),
            };

            let count = match count_str {
                "zero" => PluralCategory::Zero,
                "one" => PluralCategory::One,
                "two" => PluralCategory::Two,
                "few" => PluralCategory::Few,
                "many" => PluralCategory::Many,
                "other" => PluralCategory::Other,
                _ => return Err(DataErrorKind::IdentifierNotFound.into_error()),
            };

            Ok(if is_alpha_next {
                CompactCount::AlphaNextToNumber(count)
            } else {
                CompactCount::Standard(count)
            })
        }

        let mut result = BTreeMap::new();

        for pattern in compact_patterns {
            let lg10 = pattern.magnitude.chars().filter(|&c| c == '0').count() as i8;

            if lg10 + 1 != pattern.magnitude.len() as i8 {
                return Err(DataErrorKind::IdentifierNotFound
                    .into_error()
                    .with_debug_context("the number of zeros must be one less than the number of digits in the compact decimal count"));
            }

            let count = try_parse_count_from_str(pattern.count.as_str())?;

            result.insert((lg10, count), pattern.pattern.as_str());
        }

        // Deduplicate against `::Other`
        let compact_patterns = result
            .iter()
            .filter(|(&(lg10, count), pattern)| {
                let (CompactCount::AlphaNextToNumber(p) | CompactCount::Standard(p)) = count;
                p == PluralCategory::Other
                    || result.get(&(
                        lg10,
                        match count {
                            CompactCount::AlphaNextToNumber(_) => {
                                CompactCount::AlphaNextToNumber(PluralCategory::Other)
                            }
                            CompactCount::Standard(_) => {
                                CompactCount::Standard(PluralCategory::Other)
                            }
                        },
                    )) != Some(pattern)
            })
            .map(|(k, v)| (*k, *v))
            .collect();

        Ok(DataResponse {
            metadata: Default::default(),
            payload: DataPayload::from_owned(ShortCurrencyCompactV1 { compact_patterns }),
        })
    }
}

impl IterableDataProviderCached<ShortCurrencyCompactV1Marker> for SourceDataProvider {
    fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
        Ok(self
            .cldr()?
            .numbers()
            .list_locales()?
            .map(DataIdentifierCow::from_locale)
            .collect())
    }
}

#[test]
fn test_basic() {
    use icu::experimental::dimension::provider::currency_compact::*;
    use icu::locale::langid;

    let provider = SourceDataProvider::new_testing();
    let en: DataResponse<ShortCurrencyCompactV1Marker> = provider
        .load(DataRequest {
            id: DataIdentifierBorrowed::for_locale(&langid!("en").into()),
            ..Default::default()
        })
        .unwrap();

    let en_patterns = &en.payload.get().to_owned().compact_patterns;

    assert_eq!(
        en_patterns.get(&(3, CompactCount::Standard(PluralCategory::One))),
        None
    );
    assert_eq!(
        en_patterns.get(&(3, CompactCount::AlphaNextToNumber(PluralCategory::One))),
        None
    );

    assert_eq!(
        en_patterns.get(&(3, CompactCount::Standard(PluralCategory::Other))),
        Some("¤0K")
    );
    assert_eq!(
        en_patterns.get(&(3, CompactCount::AlphaNextToNumber(PluralCategory::Other))),
        Some("¤ 0K")
    );

    let ja: DataResponse<ShortCurrencyCompactV1Marker> = provider
        .load(DataRequest {
            id: DataIdentifierBorrowed::for_locale(&langid!("ja").into()),
            ..Default::default()
        })
        .unwrap();

    let ja_patterns = &ja.payload.get().to_owned().compact_patterns;

    assert_eq!(
        ja_patterns.get(&(4, CompactCount::Standard(PluralCategory::Other))),
        Some("¤0万")
    );
    assert_eq!(
        ja_patterns.get(&(4, CompactCount::AlphaNextToNumber(PluralCategory::Other))),
        Some("¤\u{a0}0万")
    );
}