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]
fn codegen() {
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 = 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!(
file,
"// This file is @generated by {}",
@ -23,13 +22,13 @@ fn generate<W: std::io::Write>(file: &mut W) {
let records: Vec<_> = csv::ReaderBuilder::new()
.has_headers(false)
.flexible(true)
.from_reader(DICT)
.from_reader(dict)
.records()
.map(|r| r.unwrap())
.collect();
dictgen::generate_trie(
file,
"WORD",
prefix,
"&'static [&'static str]",
records.iter().map(|record| {
let mut record_fields = record.iter();