Debian Codegraph @

debian/rust-vendor/unic-langid-impl/src/bin/generate_layout.rs

raw
1 use serde_json::Value;
2 use std::collections::HashMap;
3 use std::collections::HashSet;
4 use std::fs;
5 use unic_langid_impl::subtags::Script;
6 use unic_langid_impl::CharacterDirection;
7 use unic_langid_impl::LanguageIdentifier;
8
9 fn langid_to_direction_map(path: &str) -> HashMap<LanguageIdentifier, CharacterDirection> {
10 let mut result = HashMap::new();
11 for entry in fs::read_dir(path).unwrap() {
12 let entry = entry.unwrap();
13 let mut path = entry.path();
14 path.push("layout.json");
15 let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
16 let v: Value = serde_json::from_str(&contents).unwrap();
17
18 let langid_key = v["main"].as_object().unwrap().keys().next().unwrap();
19
20 if langid_key == "root" {
21 continue;
22 }
23 let langid: LanguageIdentifier = langid_key.parse().unwrap();
24
25 let character_order = match v["main"][langid_key]["layout"]["orientation"]["characterOrder"]
26 .as_str()
27 .unwrap()
28 {
29 "right-to-left" => CharacterDirection::RTL,
30 "left-to-right" => CharacterDirection::LTR,
31 "top-to-bottom" => CharacterDirection::TTB,
32 _ => unimplemented!("Encountered unknown directionality!"),
33 };
34 result.insert(langid, character_order);
35 }
36 result
37 }
38
39 fn check_all_script_variants(
40 map: &HashMap<LanguageIdentifier, CharacterDirection>,
41 exp_dir: CharacterDirection,
42 script: Option<Script>,
43 ) -> bool {
44 for (langid, dir) in map.iter() {
45 if let Some(reference_script) = script {
46 if let Some(s) = langid.script {
47 if reference_script == s && *dir != exp_dir {
48 return false;
49 }
50 }
51 }
52 }
53 true
54 }
55
56 fn main() {
57 let path = "./data/cldr-misc-full/main/";
58 let map = langid_to_direction_map(path);
59
60 let mut scripts_ltr = HashSet::new();
61 let mut scripts_rtl = HashSet::new();
62 let mut scripts_ttb = HashSet::new();
63 let mut langs_rtl = HashSet::new();
64
65 for (langid, dir) in map.iter() {
66 let script = langid.script;
67
68 let scripts = match dir {
69 CharacterDirection::LTR => &mut scripts_ltr,
70 CharacterDirection::RTL => {
71 langs_rtl.insert(langid.language);
72 &mut scripts_rtl
73 }
74 CharacterDirection::TTB => &mut scripts_ttb,
75 };
76
77 if let Some(script) = script {
78 if scripts.contains(&script) {
79 continue;
80 }
81 assert!(
82 check_all_script_variants(&map, *dir, Some(script)),
83 "We didn't expect a script with two directionalities!"
84 );
85 scripts.insert(script);
86 continue;
87 }
88 }
89
90 let mut scripts_ltr: Vec<String> = scripts_ltr
91 .into_iter()
92 .map(|s| {
93 let v: u32 = s.into();
94 v.to_string()
95 })
96 .collect();
97 scripts_ltr.sort();
98 let mut scripts_rtl: Vec<String> = scripts_rtl
99 .into_iter()
100 .map(|s| {
101 let v: u32 = s.into();
102 v.to_string()
103 })
104 .collect();
105 scripts_rtl.sort();
106 let mut scripts_ttb: Vec<String> = scripts_ttb
107 .into_iter()
108 .map(|s| {
109 let v: u32 = s.into();
110 v.to_string()
111 })
112 .collect();
113 scripts_ttb.sort();
114 let mut langs_rtl: Vec<String> = langs_rtl
115 .into_iter()
116 .map(|s| {
117 let v: Option<u64> = s.into();
118 let v: u64 = v.expect("Expected language to not be undefined.");
119 v.to_string()
120 })
121 .collect();
122 langs_rtl.sort();
123
124 println!(
125 "pub const SCRIPTS_CHARACTER_DIRECTION_LTR: [u32; {}] = [{}];",
126 scripts_ltr.len(),
127 scripts_ltr.join(", ")
128 );
129
130 println!(
131 "pub const SCRIPTS_CHARACTER_DIRECTION_RTL: [u32; {}] = [{}];",
132 scripts_rtl.len(),
133 scripts_rtl.join(", ")
134 );
135
136 println!(
137 "pub const SCRIPTS_CHARACTER_DIRECTION_TTB: [u32; {}] = [{}];",
138 scripts_ttb.len(),
139 scripts_ttb.join(", ")
140 );
141
142 println!(
143 "pub const LANGS_CHARACTER_DIRECTION_RTL: [u64; {}] = [{}];",
144 langs_rtl.len(),
145 langs_rtl.join(", ")
146 );
147 }