icu_provider_source/transforms/
mod.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// 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 super::cldr_serde::transforms;
use super::CldrCache;
use crate::SourceDataProvider;
use icu::experimental::transliterate::provider::*;
use icu::experimental::transliterate::RuleCollection;
use icu::locale::Locale;
use icu_provider::prelude::*;
use itertools::Itertools;
use std::collections::HashSet;
use std::sync::Mutex;

impl CldrCache {
    pub(crate) fn transforms(&self) -> Result<&Mutex<RuleCollection>, DataError> {
        self.transforms
            .get_or_init(|| {
                fn invent_bcp47(transform: &str) -> Locale {
                    let transform = transform.to_ascii_lowercase();
                    let r = ["und", "x"]
                        .into_iter()
                        .chain(
                            transform
                                .split('-')
                                .map(|t| &t[..core::cmp::min(8, t.len())]),
                        )
                        .join("-")
                        .parse()
                        .unwrap();
                    r
                }

                let mut provider = RuleCollection::default();

                for transform in self.serde_cache.list("cldr-transforms/transforms/")? {
                    let Some(transform) = transform.strip_suffix(".json") else {
                        continue;
                    };

                    if transform == "und-Ethi-t-und-latn-m0-beta_metsehaf-geminate" {
                        // References an unknown transliterator
                        continue;
                    }

                    if transform == "byn-Ethi-t-byn-latn-m0-xaleget" {
                        // Doesn't parse (backreference error)
                        continue;
                    }

                    if transform == "Thai-Latin" {
                        // References an unknown transliterator (Any-BreakInternal)
                        continue;
                    }

                    let metadata = self
                        .serde_cache
                        .read_and_parse_json::<transforms::Resource>(&format!(
                            "cldr-transforms/transforms/{transform}.json"
                        ))?;
                    let source = self
                        .serde_cache
                        .root
                        .read_to_string(&format!(
                            "cldr-transforms/transforms/{}",
                            metadata.rules_file
                        ))?
                        // Declares a sequence of Unicode sets instead of a Unicode set
                        .replace(
                            "$initialPunct = [:Ps:][:Pi:];",
                            "$initialPunct = [[:Ps:][:Pi:]];",
                        )
                        // I'm not sure why this errors
                        .replace("ə̃ {ə̃}+ → ə̃;", "")
                        // This does not escape the $, so the = is interpreted as a variable name
                        .replace(r#""$="#, r#""\$="#)
                        // Any-ASCII does not exist and should probably the Latin-ASCII
                        .replace("Any-ASCII", "Latin-ASCII")
                        // Non-canonical property names
                        .replace("block=", "Block=")
                        .replace("script=", "Script=")
                        .replace("case-ignorable:", "Case_Ignorable:")
                        .replace("cased:", "Cased:")
                        .replace("ideographic:", "Ideographic:")
                        // Non-canonical property values
                        .replace("ccc=above", "ccc=Above")
                        .replace("ccc=below", "ccc=Below")
                        .replace("UppercaseLetter:", "Uppercase_Letter:")
                        .replace("nonspacing mark:", "Nonspacing_Mark:")
                        .replace("letter:", "Letter:")
                        .replace("ARABIC:", "Arabic:")
                        .replace("arabic:", "Arabic:")
                        .replace("bengali:", "Bengali:")
                        .replace("greek:", "Greek:")
                        .replace("han:", "Han:")
                        .replace("latin:", "Latin:")
                        .replace("thaana:", "Thaana:")
                        .replace("thai:", "Thai:");

                    if matches!(
                        metadata.direction,
                        transforms::Direction::Forward | transforms::Direction::Both
                    ) {
                        let bcp47_alias =
                            if let Some(bcp47_aliases) = metadata.alias_bcp47.as_ref() {
                                Locale::try_from_str(bcp47_aliases.split(' ').next().unwrap())
                                    .map_err(|_| {
                                        DataError::custom("invalid locale")
                                            .with_display_context(bcp47_aliases)
                                    })?
                            } else {
                                invent_bcp47(&format!("{transform}-rev"))
                            };

                        provider.register_source(
                            &bcp47_alias,
                            source.clone(),
                            [metadata.alias.as_deref().unwrap_or(
                                match metadata.variant.as_deref() {
                                    None => format!("{}-{}", metadata.source, metadata.target),
                                    Some(v) => {
                                        format!("{}-{}/{v}", metadata.source, metadata.target)
                                    }
                                }
                                .as_str(),
                            )]
                            .into_iter()
                            .chain(
                                metadata
                                    .alias_bcp47
                                    .as_deref()
                                    .unwrap_or_default()
                                    .split(' ')
                                    .skip(1),
                            ),
                            false,
                            metadata.visibility == transforms::Visibility::External,
                        );
                    }

                    if matches!(
                        metadata.direction,
                        transforms::Direction::Backward | transforms::Direction::Both
                    ) {
                        let bcp47_alias =
                            if let Some(bcp47_aliases) = metadata.backward_alias_bcp47.as_ref() {
                                Locale::try_from_str(bcp47_aliases.split(' ').next().unwrap())
                                    .map_err(|_| {
                                        DataError::custom("invalid locale")
                                            .with_display_context(bcp47_aliases)
                                    })?
                            } else {
                                invent_bcp47(&format!("{transform}-rev"))
                            };
                        provider.register_source(
                            &bcp47_alias,
                            source,
                            [metadata.backward_alias.as_deref().unwrap_or(
                                match metadata.variant.as_deref() {
                                    None => format!("{}-{}", metadata.target, metadata.source),
                                    Some(v) => {
                                        format!("{}-{}/{v}", metadata.target, metadata.source)
                                    }
                                }
                                .as_str(),
                            )]
                            .into_iter()
                            .chain(
                                metadata
                                    .backward_alias_bcp47
                                    .as_deref()
                                    .unwrap_or_default()
                                    .split(' ')
                                    .skip(1),
                            ),
                            true,
                            metadata.visibility == transforms::Visibility::External,
                        );
                    }
                }
                Ok(Mutex::new(provider))
            })
            .as_ref()
            .map_err(|&e| e)
    }
}

impl DataProvider<TransliteratorRulesV1Marker> for SourceDataProvider {
    fn load(
        &self,
        req: DataRequest,
    ) -> Result<DataResponse<TransliteratorRulesV1Marker>, DataError> {
        self.check_req::<TransliteratorRulesV1Marker>(req)?;
        self.cldr()?
            .transforms()?
            .lock()
            .expect("poison")
            .as_provider_unstable(self, self)?
            .load(req)
    }
}

impl crate::IterableDataProviderCached<TransliteratorRulesV1Marker> for SourceDataProvider {
    fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
        Ok(self
            .cldr()?
            .transforms()?
            .lock()
            .expect("poison")
            .as_provider_unstable(self, self)?
            .iter_ids()?
            .into_iter()
            .map(|id| id.as_borrowed().into_owned())
            .collect())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_de_ascii_forward() {
        let provider = SourceDataProvider::new_testing();

        let _data: DataPayload<TransliteratorRulesV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierCow::from_marker_attributes(
                    DataMarkerAttributes::from_str_or_panic("de-t-de-d0-ascii"),
                )
                .as_borrowed(),
                ..Default::default()
            })
            .unwrap()
            .payload;
    }

    #[test]
    fn test_latin_ascii_backward() {
        let provider = SourceDataProvider::new_testing();

        let _data: DataPayload<TransliteratorRulesV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierCow::from_marker_attributes(
                    DataMarkerAttributes::from_str_or_panic("und-latn-t-s0-ascii"),
                )
                .as_borrowed(),
                ..Default::default()
            })
            .unwrap()
            .payload;
    }
}