Debian Codegraph @

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