#[cfg(feature = "codegen")] pub fn generate_map<'d, W: std::io::Write, V: std::fmt::Display>( file: &mut W, name: &str, value_type: &str, data: impl Iterator, ) -> Result<(), std::io::Error> { let mut data: Vec<_> = data.collect(); data.sort_unstable_by_key(|v| unicase::UniCase::new(v.0)); let mut smallest = usize::MAX; let mut largest = usize::MIN; writeln!( file, "pub static {name}: dictgen::DictTable<{value_type}> = dictgen::DictTable {{" )?; writeln!(file, " keys: &[")?; for (key, _value) in data.iter() { smallest = std::cmp::min(smallest, key.len()); largest = std::cmp::max(largest, key.len()); let key = if key.is_ascii() { format!("dictgen::InsensitiveStr::Ascii({key:?})") } else { format!("dictgen::InsensitiveStr::Unicode({key:?})") }; writeln!(file, " {key},")?; } if largest == 0 { smallest = 0; } writeln!(file, " ],")?; writeln!(file, " values: &[")?; for (_key, value) in data.iter() { writeln!(file, " {value},")?; } writeln!(file, " ],")?; writeln!(file, " range: {smallest}..={largest},")?; writeln!(file, "}};")?; Ok(()) } pub struct DictMap { pub map: phf::Map, V>, pub range: std::ops::RangeInclusive, } impl DictMap { pub fn find(&self, word: &'_ unicase::UniCase<&str>) -> Option<&V> { if self.range.contains(&word.len()) { self.map.get(&(*word).into()) } else { None } } pub fn iter(&self) -> impl Iterator, &V)> + '_ { self.map.entries().map(|(k, v)| (k.convert(), v)) } } impl phf_shared::PhfHash for crate::InsensitiveStr<'_> { #[inline] fn phf_hash(&self, state: &mut H) { core::hash::Hash::hash(self, state); } } impl phf_shared::FmtConst for crate::InsensitiveStr<'_> { fn fmt_const(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { crate::InsensitiveStr::Ascii(_) => f.write_str("dictgen::InsensitiveStr::Ascii(")?, crate::InsensitiveStr::Unicode(_) => { f.write_str("dictgen::InsensitiveStr::Unicode(")?; } } self.into_inner().fmt_const(f)?; f.write_str(")") } } impl<'b, 'a: 'b> phf_shared::PhfBorrow> for crate::InsensitiveStr<'a> { fn borrow(&self) -> &crate::InsensitiveStr<'b> { self } }