-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
60 lines (57 loc) · 2.06 KB
/
Copy pathmain.rs
File metadata and controls
60 lines (57 loc) · 2.06 KB
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
extern crate phf_codegen;
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
fn generate_by_dir(dir: &Path) -> io::Result<Vec<String>> {
let mut codes = Vec::new();
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
continue;
}
let code = path.file_stem().unwrap().to_string_lossy();
codes.push(code.to_string());
let code = code.replace("-", "_");
let bytes = fs::read(path).unwrap();
let content = String::from_utf8_lossy(&bytes);
print!(
"static DIVISIONS_{}: phf::Map<&'static str, &'static str> = ",
code
);
let mut map = phf_codegen::Map::new();
for line in content.lines().skip(1) {
let parts: Vec<&str> = line.split('\t').collect();
let code = parts[2];
let division = parts[3];
map.entry(code, &format!("\"{}\"", division));
}
print!("{}", map.build());
println!(";\n");
}
Ok(codes)
}
fn main() {
let data_dir = env::args().skip(1).next().expect("No data dir provided");
println!("// Do not edit, this file is auto-generated.");
println!("#![allow(non_upper_case_globals)]\n");
println!("use phf;\n");
let path: PathBuf = data_dir.into();
let mca_codes =
generate_by_dir(path.join("mca").as_path()).expect("generate from mca failed");
let contrib_codes =
generate_by_dir(path.join("contrib").as_path()).expect("generate from contrib failed");
print!("pub static DIVISIONS: phf::Map<&'static str, &'static phf::Map<&'static str, &'static str>> = ");
let mut map = phf_codegen::Map::new();
for code in &mca_codes {
let code_name = code.replace("-", "_");
map.entry(&code[..], &format!("&DIVISIONS_{}", code_name));
}
for code in &contrib_codes {
let code_name = code.replace("-", "_");
map.entry(&code[..], &format!("&DIVISIONS_{}", code_name));
}
print!("{}", map.build());
print!(";\n");
}