-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.rs
238 lines (215 loc) · 8.23 KB
/
gen.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! This script generates Rust modules that provide the Twemoji SVG and PNG assets as part of the
//! `twemoji-assets` crate.
//!
//! The script generates the following modules:
//!
//! - `twemoji_assets::svg::codes`: provides the Twemoji SVG assets accessed by Unicode codepoints.
//! - `twemoji_assets::svg::names`: provides the Twemoji SVG assets accessed by human-readable names.
//! - `twemoji_assets::png::codes`: provides the Twemoji PNG assets accessed by Unicode codepoints.
//! - `twemoji_assets::png::names`: provides the Twemoji PNG assets accessed by human-readable names.
//!
//! The assets are embedded in the modules using macros that are generated by this script.
//! The script requires access to two JSON files, `emojibase.data.json` and `emojibase.shortcodes.json`,
//! that are downloaded by the `cargo make` command when building the crate.
//!
//! To run this script, navigate to the root directory of the crate and run the following command:
//!
//! ```shell
//! rust-script gen.rs
//! ```
//!
//! Note that this script is only meant to be run by maintainers of the crate when the Twemoji
//! or Emojibase assets are updated.
//!
//! ```cargo
//! [dependencies]
//! serde = { version = "1", features = ["derive"] }
//! serde_json = "1"
//! ```
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::path::Path;
const EMOJIBASE_DATA: &str = include_str!(concat!(
env!("RUST_SCRIPT_BASE_PATH"),
"/emojibase.data.json"
));
const EMOJIBASE_SHORTCODES: &str = include_str!(concat!(
env!("RUST_SCRIPT_BASE_PATH"),
"/emojibase.shortcodes.json"
));
const INDENT: &str = " ";
/// Struct for deserializing the `emojibase.data.json` file.
#[derive(Debug, Deserialize)]
struct EmojibaseData {
label: String,
hexcode: String,
}
/// Type alias for deserializing the `emojibase.shortcodes.json` file.
type Shortcodes = Vec<String>;
fn main() -> Result<(), Box<dyn Error>> {
println!("Deserializing emojibase data...");
let emojibase_data: Vec<EmojibaseData> = serde_json::from_str(EMOJIBASE_DATA)?;
println!("Deserializing emojibase shortcodes...");
let emojibase_shortcodes: HashMap<String, Shortcodes> = {
let values: HashMap<String, Value> = serde_json::from_str(EMOJIBASE_SHORTCODES)?;
let mut shortcodes: HashMap<String, Shortcodes> = HashMap::new();
for (k, v) in values {
match v {
Value::String(s) => shortcodes.insert(k, Vec::from([s])),
Value::Array(v) => shortcodes.insert(
k,
v.into_iter()
.map(|v| {
serde_json::from_value::<String>(v)
.expect("shortcodes array values are always strings")
})
.collect(),
),
_ => panic!("shortcodes value neither string nor array"),
};
}
shortcodes
};
println!("Reading SVG files...");
let svg_path = Path::new(concat!(env!("RUST_SCRIPT_BASE_PATH"), "/assets/svg"));
let svg_files = fs::read_dir(svg_path)?;
let svg_files: Vec<String> = {
let mut files = Vec::new();
for file in svg_files {
let file = file?;
files.push(file.file_name().to_string_lossy().into())
}
files
};
let mut svg_codes_mod = include_str!(concat!(
env!("RUST_SCRIPT_BASE_PATH"),
"/templates/svg_codes_mod.template.rs"
))
.to_owned() + "\n";
let mut svg_match_emoji = String::new();
let mut svg_shortcodes_mod = include_str!(concat!(
env!("RUST_SCRIPT_BASE_PATH"),
"/templates/svg_names_mod.template.rs"
))
.to_owned() + "\n";
let mut svg_match_shortcode = String::new();
let mut svg_match_emoji_macro = String::new();
let mut svg_match_emoji_from_name_macro = String::new();
for file in svg_files {
let emojibase_name = file.split(".svg").next().unwrap().to_uppercase();
let emoji: String = emojibase_name
.split('-')
.map(|d| u32::from_str_radix(d, 16).unwrap())
.map(|n| char::from_u32(n).unwrap())
.collect();
let label = emojibase_data
.iter()
.find(|data| data.hexcode.as_str() == &emojibase_name)
.map(|data| data.label.as_str())
.unwrap_or("")
.to_string();
let mut emoji_split = emoji.chars();
let emoji_matcher = format!(
"({:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?})",
emoji_split.next(),
emoji_split.next(),
emoji_split.next(),
emoji_split.next(),
emoji_split.next(),
emoji_split.next(),
emoji_split.next(),
emoji_split.next(),
emoji_split.next(),
emoji_split.next()
);
let ident = format!("U_{}", emojibase_name.replace('-', "_"));
svg_codes_mod += &format!("svg_code!({ident}, \"{emoji}\", {label:?}, {file:?});\n",);
svg_match_emoji += &format!("{INDENT}({emoji_matcher}, {ident}),\n");
svg_match_emoji_macro +=
&format!("{INDENT}(\"{emoji}\") => {{ &twemoji_assets::svg::codes::{ident} }};\n");
if let Some(shortcodes) = emojibase_shortcodes.get(&emojibase_name) {
for shortcode in shortcodes {
let name_ident = sanitize_ident(shortcode);
svg_shortcodes_mod += &format!(
"svg_name!({name_ident}, \"{emoji}\", {label:?}, {ident}, {file:?});\n"
);
let char_count = shortcode.chars().count();
let name_matcher = format!("({char_count}, {shortcode:?})");
svg_match_shortcode += &format!("{INDENT}({name_matcher}, {name_ident}),\n");
svg_match_emoji_from_name_macro += &format!(
"{INDENT}({shortcode:?}) => {{ &twemoji_assets::svg::codes::{ident} }};\n"
);
}
}
}
svg_codes_mod += "\n";
svg_codes_mod += &format!(
include_str!(concat!(
env!("RUST_SCRIPT_BASE_PATH"),
"/templates/svg_match_emoji.template.rs"
)),
format!("{INDENT}{}", svg_match_emoji.trim())
);
svg_shortcodes_mod += "\n";
svg_shortcodes_mod += &format!(
include_str!(concat!(
env!("RUST_SCRIPT_BASE_PATH"),
"/templates/svg_match_name.template.rs"
)),
format!("{INDENT}{}", svg_match_shortcode.trim())
);
svg_codes_mod += "\n";
svg_codes_mod += &format!(
include_str!(concat!(
env!("RUST_SCRIPT_BASE_PATH"),
"/templates/svg_twemoji_asset.template.rs"
)),
format!("{INDENT}{}", svg_match_emoji_macro.trim())
);
svg_shortcodes_mod += "\n";
svg_shortcodes_mod += &format!(
include_str!(concat!(
env!("RUST_SCRIPT_BASE_PATH"),
"/templates/svg_twemoji_asset_from_name.template.rs"
)),
format!("{INDENT}{}", svg_match_emoji_from_name_macro.trim())
);
println!("Generating SVG code module...");
fs::write(
Path::new(concat!(env!("RUST_SCRIPT_BASE_PATH"), "/src/svg/codes.rs")),
&svg_codes_mod,
)?;
println!("Generating SVG names module...");
fs::write(
Path::new(concat!(env!("RUST_SCRIPT_BASE_PATH"), "/src/svg/names.rs")),
&svg_shortcodes_mod,
)?;
println!("Generating PNG code module...");
fs::write(
Path::new(concat!(env!("RUST_SCRIPT_BASE_PATH"), "/src/png/codes.rs")),
svg_codes_mod
.replace("svg", "png")
.replace("Svg", "Png")
.replace("SVG", "PNG"),
)?;
println!("Generating PNG names module...");
fs::write(
Path::new(concat!(env!("RUST_SCRIPT_BASE_PATH"), "/src/png/names.rs")),
svg_shortcodes_mod
.replace("svg", "png")
.replace("Svg", "Png")
.replace("SVG", "PNG"),
)?;
println!("Done!");
Ok(())
}
fn sanitize_ident(ident: &str) -> String {
let as_ident = ident.to_uppercase().replace('-', "_").replace('+', "");
match as_ident.chars().next() {
Some('A'..='Z') => as_ident,
_ => format!("X_{as_ident}"),
}
}