Debian Codegraph @

debian/missing-sources/icu_locale_core/tests/fixtures/mod.rs

raw
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 use std::collections::HashMap;
6 use std::convert::{TryFrom, TryInto};
7
8 use icu_locale_core::extensions::private;
9 use icu_locale_core::extensions::transform;
10 use icu_locale_core::extensions::unicode;
11 use icu_locale_core::extensions::Extensions;
12 use icu_locale_core::{subtags, LanguageIdentifier, Locale, ParseError};
13 use serde::Deserialize;
14
15 #[derive(Debug, Deserialize, Clone)]
16 pub struct LocaleIdentifier {
17 #[serde(rename = "type")]
18 pub field_type: String,
19 pub identifier: String,
20 }
21
22 #[derive(Debug, Deserialize, Clone)]
23 pub struct LocaleExtensionUnicode {
24 #[serde(default)]
25 keywords: HashMap<String, Option<String>>,
26 #[serde(default)]
27 attributes: Vec<String>,
28 }
29
30 #[derive(Debug, Deserialize, Clone)]
31 pub struct LocaleExtensionTransform {
32 tlang: Option<String>,
33 #[serde(default)]
34 tfields: HashMap<String, Option<String>>,
35 }
36
37 #[derive(Debug, Deserialize, Clone)]
38 pub struct LocaleExtensions {
39 unicode: Option<LocaleExtensionUnicode>,
40 transform: Option<LocaleExtensionTransform>,
41 #[serde(default)]
42 private: Vec<String>,
43 _other: Option<String>,
44 }
45
46 impl TryFrom<LocaleExtensions> for Extensions {
47 type Error = ParseError;
48
49 fn try_from(input: LocaleExtensions) -> Result<Self, Self::Error> {
50 let mut ext = Extensions::default();
51 if let Some(unicode) = input.unicode {
52 ext.unicode.keywords = unicode
53 .keywords
54 .iter()
55 .map(|(k, v)| {
56 (
57 unicode::Key::try_from_str(k).expect("Parsing key failed."),
58 v.as_ref().map_or(
59 unicode::Value::try_from_str("").expect("Failed to parse Value"),
60 |v| unicode::Value::try_from_str(v).expect("Parsing type failed."),
61 ),
62 )
63 })
64 .collect();
65 let v: Vec<unicode::Attribute> = unicode
66 .attributes
67 .iter()
68 .map(|v| unicode::Attribute::try_from_str(v).expect("Parsing attribute failed."))
69 .collect();
70 ext.unicode.attributes = unicode::Attributes::from_vec_unchecked(v);
71 }
72 if let Some(transform) = input.transform {
73 ext.transform.fields = transform
74 .tfields
75 .iter()
76 .map(|(k, v)| {
77 (
78 transform::Key::try_from_str(k).expect("Parsing key failed."),
79 v.as_ref()
80 .map(|v| {
81 transform::Value::try_from_str(v).expect("Parsing value failed.")
82 })
83 .expect("Value cannot be empty."),
84 )
85 })
86 .collect();
87
88 if let Some(tlang) = transform.tlang {
89 ext.transform.lang = Some(tlang.parse().expect("Failed to parse tlang."));
90 }
91 }
92 let v: Vec<private::Subtag> = input
93 .private
94 .iter()
95 .map(|v| private::Subtag::try_from_str(v).expect("Failed to add field."))
96 .collect();
97 ext.private = private::Private::from_vec_unchecked(v);
98 Ok(ext)
99 }
100 }
101
102 #[derive(Debug, Deserialize, Clone)]
103 pub struct LocaleSubtags {
104 #[serde(rename = "type")]
105 pub field_type: String,
106 pub language: Option<String>,
107 pub script: Option<String>,
108 pub region: Option<String>,
109 #[serde(default)]
110 pub variants: Vec<String>,
111 pub extensions: Option<LocaleExtensions>,
112 }
113
114 #[derive(Debug, Deserialize, Clone)]
115 pub struct LocaleError {
116 pub error: String,
117 pub text: String,
118 }
119
120 #[derive(Debug, Deserialize, Clone)]
121 #[serde(untagged)]
122 #[expect(clippy::large_enum_variant)] // test code
123 pub enum LocaleInfo {
124 String(String),
125 Error(LocaleError),
126 Identifier(LocaleIdentifier),
127 Object(LocaleSubtags),
128 }
129
130 impl TryFrom<LocaleInfo> for LanguageIdentifier {
131 type Error = ParseError;
132
133 fn try_from(input: LocaleInfo) -> Result<Self, Self::Error> {
134 match input {
135 LocaleInfo::String(s) => s.parse(),
136 LocaleInfo::Error(e) => Err(e.into()),
137 LocaleInfo::Identifier(ident) => ident.try_into(),
138 LocaleInfo::Object(o) => o.try_into(),
139 }
140 }
141 }
142
143 impl TryFrom<LocaleInfo> for Locale {
144 type Error = ParseError;
145
146 fn try_from(input: LocaleInfo) -> Result<Self, Self::Error> {
147 match input {
148 LocaleInfo::String(s) => s.parse(),
149 LocaleInfo::Error(e) => Err(e.into()),
150 LocaleInfo::Identifier(ident) => ident.try_into(),
151 LocaleInfo::Object(o) => o.try_into(),
152 }
153 }
154 }
155
156 impl TryFrom<LocaleIdentifier> for LanguageIdentifier {
157 type Error = ParseError;
158
159 fn try_from(input: LocaleIdentifier) -> Result<Self, Self::Error> {
160 LanguageIdentifier::try_from_locale_bytes(input.identifier.as_bytes())
161 }
162 }
163
164 impl TryFrom<LocaleIdentifier> for Locale {
165 type Error = ParseError;
166
167 fn try_from(input: LocaleIdentifier) -> Result<Self, Self::Error> {
168 Locale::try_from_str(&input.identifier)
169 }
170 }
171
172 impl TryFrom<LocaleSubtags> for LanguageIdentifier {
173 type Error = ParseError;
174
175 fn try_from(subtags: LocaleSubtags) -> Result<Self, Self::Error> {
176 let language = if let Some(lang) = subtags.language {
177 lang.parse().expect("Failed to parse language subtag")
178 } else {
179 subtags::Language::UNKNOWN
180 };
181 let script = subtags
182 .script
183 .map(|s| s.parse().expect("Failed to parse script subtag."));
184 let region = subtags
185 .region
186 .map(|s| s.parse().expect("Failed to parse region subtag."));
187 let variants = subtags
188 .variants
189 .iter()
190 .map(|v| v.parse().expect("Failed to parse variant subtag."))
191 .collect::<Vec<_>>();
192 Ok(LanguageIdentifier {
193 language,
194 script,
195 region,
196 variants: subtags::Variants::from_vec_unchecked(variants),
197 })
198 }
199 }
200
201 impl TryFrom<LocaleSubtags> for Locale {
202 type Error = ParseError;
203
204 fn try_from(subtags: LocaleSubtags) -> Result<Self, Self::Error> {
205 let language = if let Some(lang) = subtags.language {
206 lang.parse().expect("Failed to parse language subtag")
207 } else {
208 subtags::Language::UNKNOWN
209 };
210 let script = subtags
211 .script
212 .map(|s| s.parse().expect("Failed to parse script subtag."));
213 let region = subtags
214 .region
215 .map(|s| s.parse().expect("Failed to parse region subtag."));
216 let variants = subtags
217 .variants
218 .iter()
219 .map(|v| v.parse().expect("Failed to parse variant subtag."))
220 .collect::<Vec<_>>();
221 let extensions = if let Some(e) = subtags.extensions {
222 e.try_into().expect("Failed to parse extensions.")
223 } else {
224 Extensions::default()
225 };
226 Ok(Locale {
227 id: LanguageIdentifier {
228 language,
229 script,
230 region,
231 variants: subtags::Variants::from_vec_unchecked(variants),
232 },
233 extensions,
234 })
235 }
236 }
237
238 impl From<LocaleError> for ParseError {
239 fn from(e: LocaleError) -> Self {
240 match e.error.as_str() {
241 "InvalidLanguage" => ParseError::InvalidLanguage,
242 "InvalidSubtag" => ParseError::InvalidSubtag,
243 "InvalidExtension" => ParseError::InvalidExtension,
244 "DuplicatedExtension" => ParseError::DuplicatedExtension,
245 _ => unreachable!("Unknown error name"),
246 }
247 }
248 }
249
250 #[derive(Debug, Deserialize)]
251 pub struct LocaleTest {
252 pub input: LocaleInfo,
253 pub output: LocaleInfo,
254 }