-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
182 lines (150 loc) · 5.25 KB
/
build.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
//! Build script that generates the i18n module.
use std::{env, fmt, fs};
use std::collections::HashMap;
use std::path::Path;
const LANG: &str = "crate::lang::Lang";
//const LANG_VARIANTS: &[&str] = &["en", "de"];
const REQUEST_STATE: &str = "crate::state::RequestState";
//------------ main ----------------------------------------------------------
fn main() {
let mut target = String::new();
terms(&mut target);
enums(&mut target);
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("i18n.rs");
fs::write(&dest_path, &target).unwrap();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=i18n/terms.yaml");
println!("cargo:rerun-if-changed=i18n/enums.yaml");
}
//------------ terms ---------------------------------------------------------
type Terms = HashMap<String, HashMap<String, String>>;
#[derive(Default)]
struct TermsModule {
mods: HashMap<String, Box<Self>>,
funcs: HashMap<String, HashMap<String, String>>,
}
fn terms(target: &mut String) {
let input = serde_yaml::from_str::<Terms>(
&fs::read_to_string("i18n/terms.yaml").unwrap()
).unwrap();
let mut output = TermsModule::default();
for (ident, content) in input {
let mut ident = ident.split("::").collect::<Vec<_>>();
let func = ident.pop().unwrap();
let mut module = &mut output;
let mut ident = ident.into_iter();
while let Some(word) = ident.next() {
module = module.mods.entry(word.into()).or_insert_with(|| {
TermsModule::default().into()
})
}
module.funcs.insert(func.into(), content);
}
writeln!(target, "pub mod term {{");
term_module(&output, target);
writeln!(target, "}}");
}
fn term_module(module: &TermsModule, target: &mut String) {
for (name, module) in &module.mods {
writeln!(target, "pub mod {} {{", name);
term_module(module, target);
writeln!(target, "}}");
}
for (name, content) in &module.funcs {
writeln!(target,
"pub fn {}(\
state: &{}\
) -> &'static str {{\
match state.lang() {{",
name, REQUEST_STATE
);
for (lang, term) in content {
writeln!(target,
"{}::{}{} => \"{}\",",
LANG, &lang[0..1].to_uppercase(), &lang[1..], term
);
}
writeln!(target, " }}\n}}");
}
}
//------------ enums ---------------------------------------------------------
type Enums = HashMap<String, Enum>;
#[derive(serde::Deserialize)]
struct Enum {
#[serde(rename = "enum")]
enum_path: String,
variants: HashMap<String, HashMap<String, String>>,
}
#[derive(Default)]
struct EnumsModule {
mods: HashMap<String, Box<Self>>,
funcs: HashMap<String, Enum>,
}
fn enums(target: &mut String) {
let input = serde_yaml::from_str::<Enums>(
&fs::read_to_string("i18n/enums.yaml").unwrap()
).unwrap();
let mut output = EnumsModule::default();
for (ident, content) in input {
let mut ident = ident.split("::").collect::<Vec<_>>();
let func = ident.pop().unwrap();
let mut module = &mut output;
let mut ident = ident.into_iter();
while let Some(word) = ident.next() {
module = module.mods.entry(word.into()).or_insert_with(|| {
EnumsModule::default().into()
})
}
module.funcs.insert(func.into(), content);
}
writeln!(target, "pub mod enums {{");
enums_module(&output, target);
writeln!(target, "}}");
}
fn enums_module(module: &EnumsModule, target: &mut String) {
for (name, module) in &module.mods {
writeln!(target, "pub mod {} {{", name);
enums_module(module, target);
writeln!(target, "}}");
}
for (name, content) in &module.funcs {
writeln!(target,
"pub fn {}(\
value: {},\
state: &{},\
) -> &'static str {{\
match value {{",
name, content.enum_path, REQUEST_STATE,
);
for (variant, value) in &content.variants {
writeln!(target,
" {}::{} => {{", content.enum_path, variant
);
writeln!(target, " match state.lang() {{");
for (lang, term) in value {
writeln!(target,
" {}::{}{} => \"{}\",",
LANG, &lang[0..1].to_uppercase(), &lang[1..], term
);
}
writeln!(target, " }}");
writeln!(target, " }}");
}
writeln!(target, " }}\n}}");
}
}
//------------ WriteOrPanic --------------------------------------------------
/// A target for writing formatted data into without error.
///
/// This provides a method `write_fmt` for use with the `write!` macro and
/// friends that does not return a result. Rather, it panics if an error
/// occurs.
pub trait WriteOrPanic {
fn write_fmt(&mut self, args: fmt::Arguments);
}
impl WriteOrPanic for String {
fn write_fmt(&mut self, args: fmt::Arguments) {
std::fmt::Write::write_fmt(self, args).expect("formatting failed");
}
}