From 92c9aa17f4e815a892647cecef4c2d7f7ca574d0 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 30 Dec 2024 16:26:21 -0600 Subject: [PATCH] refactor(dictgen): Make room for different maps --- crates/dictgen/src/map.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/dictgen/src/map.rs b/crates/dictgen/src/map.rs index 711ce65..453a6e1 100644 --- a/crates/dictgen/src/map.rs +++ b/crates/dictgen/src/map.rs @@ -19,6 +19,19 @@ impl MapGen<'_> { let mut smallest = usize::MAX; 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 data = data .iter() @@ -34,20 +47,11 @@ impl MapGen<'_> { }) .collect::>(); 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()); } 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, " range: {smallest}..={largest},")?; writeln!(file, "}};")?;