refactor(dictgen): Make room for different maps

This commit is contained in:
Ed Page 2024-12-30 16:26:21 -06:00
parent d4767767b2
commit 92c9aa17f4

View file

@ -19,6 +19,19 @@ impl MapGen<'_> {
let mut smallest = usize::MAX; let mut smallest = usize::MAX;
let mut largest = usize::MIN; let mut largest = usize::MIN;
for (key, _) in data.iter() {
smallest = std::cmp::min(smallest, key.len());
largest = std::cmp::max(largest, key.len());
}
if largest == 0 {
smallest = 0;
}
writeln!(
file,
"pub static {name}: dictgen::Map<{key_type}, {value_type}> = dictgen::Map {{"
)?;
let mut builder = phf_codegen::Map::new(); let mut builder = phf_codegen::Map::new();
let data = data let data = data
.iter() .iter()
@ -34,20 +47,11 @@ impl MapGen<'_> {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for (key, value) in data.iter() { for (key, value) in data.iter() {
smallest = std::cmp::min(smallest, key.len());
largest = std::cmp::max(largest, key.len());
builder.entry(key, value.as_str()); builder.entry(key, value.as_str());
} }
let builder = builder.build(); let builder = builder.build();
if largest == 0 {
smallest = 0;
}
writeln!(
file,
"pub static {name}: dictgen::Map<{key_type}, {value_type}> = dictgen::Map {{"
)?;
writeln!(file, " map: {builder},")?; writeln!(file, " map: {builder},")?;
writeln!(file, " range: {smallest}..={largest},")?; writeln!(file, " range: {smallest}..={largest},")?;
writeln!(file, "}};")?; writeln!(file, "}};")?;