-
Notifications
You must be signed in to change notification settings - Fork 119
/
codegen.rs
46 lines (43 loc) · 1.37 KB
/
codegen.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
#[test]
fn codegen() {
let mut content = vec![];
const DICT: &[u8] = include_bytes!("../assets/words.csv");
generate(&mut content, "WORD", DICT);
let content = String::from_utf8(content).unwrap();
let content = codegenrs::rustfmt(&content, None).unwrap();
snapbox::assert_data_eq!(content, snapbox::file!["../src/word_codegen.rs"].raw());
}
fn generate<W: std::io::Write>(file: &mut W, prefix: &str, dict: &[u8]) {
writeln!(
file,
"// This file is @generated by {}",
file!().replace('\\', "/")
)
.unwrap();
writeln!(file, "#![allow(clippy::unreadable_literal)]",).unwrap();
writeln!(file, "#![allow(unreachable_pub)]",).unwrap();
writeln!(file).unwrap();
let records: Vec<_> = csv::ReaderBuilder::new()
.has_headers(false)
.flexible(true)
.from_reader(dict)
.records()
.map(|r| r.unwrap())
.collect();
dictgen::generate_trie(
file,
prefix,
"&'static [&'static str]",
records.iter().map(|record| {
let mut record_fields = record.iter();
let key = record_fields.next().unwrap();
let value = format!(
"&[{}]",
itertools::join(record_fields.map(|field| format!(r#""{field}""#)), ", ")
);
(key, value)
}),
64,
)
.unwrap();
}