refactor(dict): Make codegen more specific

This commit is contained in:
Ed Page 2023-09-25 12:13:53 -05:00
parent 4c91f16ae1
commit b798780a13
3 changed files with 8 additions and 9 deletions

View file

@ -1,3 +1,3 @@
mod dict_codegen; mod word_codegen;
pub use crate::dict_codegen::WORD_TRIE; pub use crate::word_codegen::WORD_TRIE;

View file

@ -1,16 +1,15 @@
const DICT: &[u8] = include_bytes!("../assets/words.csv");
#[test] #[test]
fn codegen() { fn codegen() {
let mut content = vec![]; let mut content = vec![];
generate(&mut content); const DICT: &[u8] = include_bytes!("../assets/words.csv");
generate(&mut content, "WORD", DICT);
let content = String::from_utf8(content).unwrap(); let content = String::from_utf8(content).unwrap();
let content = codegenrs::rustfmt(&content, None).unwrap(); let content = codegenrs::rustfmt(&content, None).unwrap();
snapbox::assert_eq_path("./src/dict_codegen.rs", content); snapbox::assert_eq_path("./src/word_codegen.rs", content);
} }
fn generate<W: std::io::Write>(file: &mut W) { fn generate<W: std::io::Write>(file: &mut W, prefix: &str, dict: &[u8]) {
writeln!( writeln!(
file, file,
"// This file is @generated by {}", "// This file is @generated by {}",
@ -23,13 +22,13 @@ fn generate<W: std::io::Write>(file: &mut W) {
let records: Vec<_> = csv::ReaderBuilder::new() let records: Vec<_> = csv::ReaderBuilder::new()
.has_headers(false) .has_headers(false)
.flexible(true) .flexible(true)
.from_reader(DICT) .from_reader(dict)
.records() .records()
.map(|r| r.unwrap()) .map(|r| r.unwrap())
.collect(); .collect();
dictgen::generate_trie( dictgen::generate_trie(
file, file,
"WORD", prefix,
"&'static [&'static str]", "&'static [&'static str]",
records.iter().map(|record| { records.iter().map(|record| {
let mut record_fields = record.iter(); let mut record_fields = record.iter();