-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} | ||
} |