-
-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathbuild.rs
More file actions
40 lines (33 loc) · 1.32 KB
/
build.rs
File metadata and controls
40 lines (33 loc) · 1.32 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
use entities::ENTITIES;
use std::io::Write;
use std::{env, path::PathBuf};
fn main() {
let out_dir: PathBuf = env::var("OUT_DIR").unwrap().parse().unwrap();
// entity::lookup is handed just the inner entity name, like "amp" for
// "&"; we only match those with a trailing ";".
//
// entities::ENTITIES includes many both with and without a trailing ";".
// Exclude those without, and then write to source only the name, without
// the leading or trailing "&" or ";".
let translated_entities = ENTITIES
.iter()
.filter(|e| e.entity.starts_with('&') && e.entity.ends_with(';'))
.map(|e| (&e.entity[1..e.entity.len() - 1], e.characters))
.collect::<Vec<_>>();
// Generate a perfect hash map for O(1) lookup
let out = std::fs::File::create(out_dir.join("entitydata.rs")).unwrap();
let mut bw = std::io::BufWriter::new(out);
writeln!(bw, "mod entitydata {{").unwrap();
writeln!(bw).unwrap();
write!(
bw,
" pub static ENTITY_MAP: phf::Map<&'static str, &'static str> = "
)
.unwrap();
let mut map = phf_codegen::Map::new();
for (entity, characters) in &translated_entities {
map.entry(*entity, format!("{:?}", characters));
}
writeln!(bw, "{};", map.build()).unwrap();
writeln!(bw, "}}").unwrap();
}