-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
66 lines (55 loc) · 1.65 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
use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
use csv;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct OwnedRecord {
tag3: String,
tag1: Option<String>,
name: Option<String>,
autonym: Option<String>,
source: Option<String>,
}
impl OwnedRecord {
fn create_as_str(self) -> String {
format!(
"Record {{ tag3: \"{}\", tag1: {}, name: {}, autonym: {}, source: {} }}",
&self.tag3,
option_str(self.tag1),
option_str(self.name),
option_str(self.autonym),
option_str(self.source),
)
}
}
fn option_str(opt: Option<String>) -> String {
match opt {
Some(val) => format!("Some(\"{}\")", val),
None => "None".to_owned(),
}
}
fn main() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("autonyms.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
write!(
&mut file,
"static LANGUAGE_AUTONYMS: phf::Map<&'static str, Record> = "
)
.unwrap();
let autonyms_tsv = include_str!("assets/iso639-autonyms.tsv");
let mut reader = csv::ReaderBuilder::new()
.delimiter(b'\t')
.from_reader(autonyms_tsv.as_bytes());
let mut builder = phf_codegen::Map::new();
reader
.deserialize()
.filter_map(|r: Result<OwnedRecord, csv::Error>| r.ok())
.for_each(|record| {
let key = record.tag1.clone().or(Some(record.tag3.clone())).unwrap();
builder.entry(key, &record.create_as_str());
});
builder.build(&mut file).unwrap();
write!(&mut file, ";\n").unwrap();
}