Debian Codegraph @

third_party/rust/icu_locid/examples/syntatically_canonicalize_locales.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 // A sample application which takes a comma separated list of locales,
6 // makes them syntatically canonical and serializes the list back into a comma separated list.
7
8 icu_benchmark_macros::static_setup!();
9
10 use std::env;
11
12 use icu_locid::Locale;
13
14 const DEFAULT_INPUT: &str = "sr-cyrL-rS, es-mx, und-arab-u-ca-Buddhist";
15
16 fn syntatically_canonicalize_locales(input: &str) -> String {
17 // Split input string and canonicalize each locale identifier.
18 let canonical_locales: Vec<String> = input
19 .split(',')
20 .filter_map(|s| Locale::canonicalize(s.trim()).ok())
21 .collect();
22
23 canonical_locales.join(", ")
24 }
25
26 fn main() {
27 icu_benchmark_macros::main_setup!();
28 let args: Vec<String> = env::args().collect();
29
30 let input = if let Some(input) = args.get(1) {
31 input.as_str()
32 } else {
33 DEFAULT_INPUT
34 };
35 let _output = syntatically_canonicalize_locales(input);
36
37 #[cfg(debug_assertions)]
38 println!("\nInput: {input}\nOutput: {_output}");
39 }
40
41 #[cfg(test)]
42 mod tests {
43 use super::*;
44
45 const DEFAULT_OUTPUT: &str = "sr-Cyrl-RS, es-MX, und-Arab-u-ca-buddhist";
46
47 #[test]
48 fn ensure_default_output() {
49 assert_eq!(
50 syntatically_canonicalize_locales(DEFAULT_INPUT),
51 DEFAULT_OUTPUT
52 );
53 }
54 }