mirror of
https://github.com/crate-ci/typos.git
synced 2025-01-25 16:09:03 -05:00
feat(dictgen): Generate a naive match
After 23 minutes of compiling, I gave up. The compiler might generate fast `match`s but its not worth the compile time.
This commit is contained in:
parent
b6352341f9
commit
da0e536796
3 changed files with 45 additions and 0 deletions
|
@ -57,6 +57,10 @@ impl<'g> DictGen<'g> {
|
|||
limit: 64,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn r#match(self) -> crate::MatchGen<'g> {
|
||||
crate::MatchGen { gen: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DictGen<'static> {
|
||||
|
|
|
@ -7,6 +7,8 @@ mod gen;
|
|||
mod insensitive;
|
||||
#[cfg(feature = "map")]
|
||||
mod map;
|
||||
#[cfg(feature = "codegen")]
|
||||
mod r#match;
|
||||
mod ordered_map;
|
||||
mod trie;
|
||||
|
||||
|
@ -16,4 +18,6 @@ pub use insensitive::*;
|
|||
#[cfg(feature = "map")]
|
||||
pub use map::*;
|
||||
pub use ordered_map::*;
|
||||
#[cfg(feature = "codegen")]
|
||||
pub use r#match::*;
|
||||
pub use trie::*;
|
||||
|
|
37
crates/dictgen/src/match.rs
Normal file
37
crates/dictgen/src/match.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
#[cfg(feature = "codegen")]
|
||||
pub struct MatchGen<'g> {
|
||||
pub(crate) gen: crate::DictGen<'g>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "codegen")]
|
||||
impl MatchGen<'_> {
|
||||
pub fn write<W: std::io::Write, V: std::fmt::Display>(
|
||||
&self,
|
||||
file: &mut W,
|
||||
data: impl Iterator<Item = (impl AsRef<str>, V)>,
|
||||
) -> Result<(), std::io::Error> {
|
||||
let mut data: Vec<_> = data.collect();
|
||||
data.sort_unstable_by_key(|v| unicase::UniCase::new(v.0.as_ref().to_owned()));
|
||||
|
||||
let name = self.gen.name;
|
||||
let value_type = self.gen.value_type;
|
||||
|
||||
writeln!(file, "pub struct {name};")?;
|
||||
writeln!(file, "impl {name} {{")?;
|
||||
writeln!(
|
||||
file,
|
||||
" pub fn find(&self, word: &&str) -> Option<&'static {value_type}> {{"
|
||||
)?;
|
||||
writeln!(file, " match *word {{")?;
|
||||
for (key, value) in data.iter() {
|
||||
let key = key.as_ref();
|
||||
writeln!(file, " {key:?} => Some(&{value}.as_slice()),")?;
|
||||
}
|
||||
writeln!(file, " _ => None,")?;
|
||||
writeln!(file, " }}")?;
|
||||
writeln!(file, " }}")?;
|
||||
writeln!(file, "}}")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue