This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.rs
54 lines (44 loc) · 1.46 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
use serde::Deserialize;
use std::collections::{BTreeMap, HashSet};
use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
static JSON: &str = include_str!("data/mime.json");
#[derive(Debug, Deserialize)]
struct Record {
#[serde(default)]
extensions: Vec<String>,
}
fn main() {
let json: BTreeMap<String, Record> = serde_json::from_str(JSON).unwrap();
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
let mut used_exts = HashSet::new();
let mut ext_by_type = phf_codegen::Map::new();
let mut type_by_ext = phf_codegen::Map::new();
for (mime_type, record) in json.iter() {
let mime_type_value = format!("\"{}\"", mime_type);
let exts = &record.extensions;
for ext in exts {
if used_exts.insert(ext) {
type_by_ext.entry(ext, &mime_type_value);
}
}
let exts: Vec<_> = exts.iter().map(|ext| format!("\"{}\"", ext)).collect();
let exts = format!("&[{}]", exts.join(", "));
ext_by_type.entry(mime_type, &exts);
}
writeln!(
&mut file,
"static EXT_BY_TYPE: phf::Map<&'static str, &[&'static str]> = \n{};\n",
ext_by_type.build()
)
.unwrap();
writeln!(
&mut file,
"static TYPE_BY_EXT: phf::Map<&'static str, &'static str> = \n{};\n",
type_by_ext.build()
)
.unwrap();
}