-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
81 lines (70 loc) · 2.25 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
// https://doc.rust-lang.org/cargo/reference/build-scripts.html
//
// rls doesn't seem to work here, so `cargo watch -w build.rs -x check`
#[macro_use]
extern crate quote;
use proc_macro2::TokenStream;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
generate_emojis_module()?;
Ok(())
}
fn generate_emojis_module() -> std::io::Result<()> {
println!("cargo:rerun-if-changed=emojis.json");
#[derive(Deserialize)]
struct Emoji {
keywords: Vec<String>,
char: String,
fitzpatrick_scale: bool,
category: String,
}
let json_file = File::open("emojis.json")?;
let mut file = File::create("src/emojis.rs")?;
// Parse emojis and return a _sorted_ vector of pairs
// (sorted so that we don't keep dirtying the generated file)
let emojis = {
let m: HashMap<String, Emoji> =
serde_json::from_reader(json_file).expect("error reading emojis.json");
let mut v: Vec<(String, Emoji)> = m.into_iter().collect();
v.sort_by(|a, b| a.0.cmp(&b.0));
v
};
let mut inserts = TokenStream::new();
for (k, v) in emojis {
let keywords_iter = v.keywords.iter();
let keywords_field = quote! {
&[#(#keywords_iter),*]
};
let char_field = &v.char;
let fitzpatrick_scale_field = v.fitzpatrick_scale;
let category_field = &v.category;
inserts.extend(quote! {
m.insert(#k, Emoji {
keywords: #keywords_field,
char: #char_field,
fitzpatrick_scale: #fitzpatrick_scale_field,
category: #category_field,
});
})
}
let tokens = quote! {
pub struct Emoji {
pub keywords: &'static [&'static str],
pub char: &'static str,
pub fitzpatrick_scale: bool,
pub category: &'static str,
}
lazy_static! {
pub static ref EMOJIS: std::collections::HashMap<&'static str, Emoji> = {
let mut m = std::collections::HashMap::new();
#inserts
m
};
}
};
file.write(tokens.to_string().as_bytes())?;
Ok(())
}