forked from johnstonskj/rust-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
199 lines (167 loc) · 5.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use codes_common::build::csv::{open_csv_file, process_tsv_input};
use codes_common::build::{
default_finalize_for, default_init, make_default_renderer, process, Data as DataTrait, DataRow,
};
use codes_common::{insert_field, insert_optional_field};
use csv::StringRecord;
use std::collections::BTreeMap;
use tera::{Map, Value};
#[derive(Debug, Default)]
struct Data {
rows: BTreeMap<String, Map<String, Value>>,
macros: Map<String, Value>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
process(
default_init,
|data: Data| process_tsv_input(data, "iso-639-1.tsv", process_part1_row),
default_finalize_for,
make_default_renderer("part_1._rs", "part_1.rs"),
)?;
process(
default_init,
|data| {
process_tsv_input(data, "iso-639-3.tsv", process_part3_row)
.and_then(process_part3_macro_csv)
},
finalize_part3,
make_default_renderer("part_3._rs", "part_3.rs"),
)?;
process(
default_init,
|data: Data| process_tsv_input(data, "iso-639-5.tsv", process_part5_row),
default_finalize_for,
make_default_renderer("part_5._rs", "part_5.rs"),
)?;
Ok(())
}
fn process_part1_row(
record: StringRecord,
row: &mut DataRow,
) -> Result<String, Box<dyn std::error::Error>> {
let id = record.get(1).unwrap().to_string();
// ID field
insert_field!(id.clone() => row, "code");
// Required fields
let names = record.get(2).unwrap();
insert_field!(
if names.contains('|') {
let names = names.split('|');
names
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join("; ")
} else {
names.to_string()
} => row, "label"
);
Ok(id)
}
fn process_part3_row(
record: StringRecord,
row: &mut DataRow,
) -> Result<String, Box<dyn std::error::Error>> {
let id = record.get(0).unwrap().to_string();
// ID field
insert_field!(id.clone() => row, "code");
// Required fields
let part_1_code = record.get(3).unwrap();
if part_1_code != "sh" {
// Serbo-Croatian - Code element for 639-1 has been deprecated
insert_field!(part_1_code => row, "part_1_code");
}
insert_field!(
match record.get(4).unwrap() {
"C" => "Collection",
"D" => "Dialect",
"I" => "Individual",
"M" => "Macro",
"R" => "Reserved",
"S" => "Special",
_ => unreachable!(),
}
=> row, "scope"
);
insert_field!(
match record.get(5).unwrap() {
"A" => "Ancient",
"C" => "Constructed",
"E" => "Extinct",
"H" => "Historic",
"L" => "Living",
"S" => "Special",
_ => unreachable!(),
}
=> row, "language_type"
);
insert_field!(record, 6 => row, "ref_name");
// Optional fields
insert_optional_field!(record, 7 => row, "comment");
Ok(id)
}
fn process_part3_macro_csv(mut data: Data) -> Result<Data, Box<dyn std::error::Error>> {
let mut rdr = open_csv_file("iso-639-3-macro-languages.tsv", Some(b'\t'))?;
for result in rdr.records() {
let record = result?;
if record.get(2).unwrap() == "A" {
let macro_code = record.get(0).unwrap();
let individual_code = Value::String(record.get(1).unwrap().to_string());
if let Some(Value::Array(macro_langs)) = data.macros.get_mut(macro_code) {
macro_langs.push(individual_code)
} else {
data.macros
.insert(macro_code.to_string(), Value::Array(vec![individual_code]));
}
}
}
Ok(data)
}
fn finalize_part3(data: Data) -> std::result::Result<tera::Context, Box<dyn std::error::Error>> {
let macros = data.macros.clone();
let mut ctx = default_finalize_for(data)?;
ctx.insert("macro_langs", ¯os);
Ok(ctx)
}
fn process_part5_row(
record: StringRecord,
row: &mut DataRow,
) -> Result<String, Box<dyn std::error::Error>> {
let id = record.get(1).unwrap().to_string();
// ID field
insert_field!(id.clone() => row, "code");
// Required fields
insert_field!(record, 2 => row, "label");
Ok(id)
}
impl DataTrait for Data {
fn new(_: &'static str) -> Self
where
Self: Sized,
{
Self {
rows: Default::default(),
macros: Default::default(),
}
}
fn new_with_inner(_: &'static str, _: &'static str) -> Self
where
Self: Sized,
{
unreachable!()
}
fn type_name(&self) -> &'static str {
"LanguageCode"
}
fn inner_type_name(&self) -> Option<&'static str> {
None
}
fn rows(&self) -> &std::collections::BTreeMap<String, Map<String, Value>> {
&self.rows
}
fn rows_mut(&mut self) -> &mut std::collections::BTreeMap<String, Map<String, Value>> {
&mut self.rows
}
fn into_rows(self) -> std::collections::BTreeMap<String, Map<String, Value>> {
self.rows
}
}