2022-08-01 15:45:58 -04:00
|
|
|
const DICT: &[u8] = include_bytes!("../assets/words.csv");
|
2019-10-05 09:30:30 -04:00
|
|
|
|
2022-08-01 15:45:58 -04:00
|
|
|
#[test]
|
|
|
|
fn codegen() {
|
|
|
|
let mut content = vec![];
|
|
|
|
generate(&mut content);
|
|
|
|
|
|
|
|
let content = String::from_utf8(content).unwrap();
|
|
|
|
let content = codegenrs::rustfmt(&content, None).unwrap();
|
|
|
|
snapbox::assert_eq_path("./src/dict_codegen.rs", content);
|
|
|
|
}
|
2019-10-28 12:09:29 -04:00
|
|
|
|
|
|
|
fn generate<W: std::io::Write>(file: &mut W) {
|
2022-08-01 15:45:58 -04:00
|
|
|
writeln!(file, "// This file is @generated by {}", file!()).unwrap();
|
2019-10-29 09:53:32 -04:00
|
|
|
writeln!(file, "#![allow(clippy::unreadable_literal)]",).unwrap();
|
2019-10-05 09:30:30 -04:00
|
|
|
writeln!(file).unwrap();
|
|
|
|
|
2020-08-15 22:03:00 -04:00
|
|
|
let records: Vec<_> = csv::ReaderBuilder::new()
|
|
|
|
.has_headers(false)
|
2021-05-15 20:06:04 -04:00
|
|
|
.flexible(true)
|
2020-08-15 22:03:00 -04:00
|
|
|
.from_reader(DICT)
|
2019-10-05 09:30:30 -04:00
|
|
|
.records()
|
|
|
|
.map(|r| r.unwrap())
|
|
|
|
.collect();
|
2021-07-01 09:27:45 -04:00
|
|
|
dictgen::generate_trie(
|
2021-04-30 22:16:04 -04:00
|
|
|
file,
|
2021-07-01 09:27:45 -04:00
|
|
|
"WORD",
|
|
|
|
"&'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,
|
2021-04-30 22:16:04 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2019-10-05 09:30:30 -04:00
|
|
|
}
|