-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
29 lines (25 loc) · 924 Bytes
/
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
use std::io::prelude::*;
const DICTIONARY: &str = include_str!("dictionary.txt");
fn main() {
let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
let mut f = std::fs::File::create(out_dir.join("dictionary.rs"))
.expect("could not create file in OUT_DIR");
let mut words = Vec::from_iter(DICTIONARY.lines().map(|line| {
let (word, count) = line
.split_once(' ')
.expect("every line is word + space + frequency");
let count: usize = count.parse().expect("every count is a number");
(word, count)
}));
words.sort_unstable_by_key(|&(_, count)| std::cmp::Reverse(count));
writeln!(
f,
"pub const DICTIONARY: [(&str, usize); {}] = [",
words.len()
)
.unwrap();
for (word, count) in words {
writeln!(f, "(\"{}\", {}),", word, count).unwrap();
}
write!(f, "];").unwrap();
}