New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macros to reduce the syntactic burden of parsing events #874
Comments
Maybe I have the same question, I didn't find a better solution to replace the below code match key_event.code {
KeyCode::Char('A') => some_funtion('A'),
KeyCode::Char('B') => some_funtion('B'),
KeyCode::Char('C') => some_funtion('C'),
KeyCode::Char('D') => some_funtion('D'),
KeyCode::Char('E') => some_funtion('E'),
_ => (),
} Although they all have the same logic, but I need to do the same thing for they all |
you can match on variable no?
|
For the macros i am not sure. Surely it removes redundancy, but the thing is that they are quite hard to understand on how to use them. |
Yes, I think this is correct. After comment on this issue, I found such usage in other project. LOL |
Problem:
Parsing crossterm events can be incredibly wordy. Of course, this can be improved with splitting out the parsing of different groups of events into functions, but the depth of nesting and length of the declarations to, e.g, match all
KeyCode
events and then match on specificKeyCode::Char(_)
events can get deeply messy.Potential solution
Macros could be defined that return match conditions scoped to work on
Event
.I implemented a basic proof of concept macro for generating the match conditions and it's definitely feasible:
I ran it through some basic tests and it worked fine, although it has many flaws in usability which I can elaborate on if wanted (I'm pretty bad at macros).
I understand that this might be out of scope for this project, but I thought it could make an interesting improvement to code readability.
The text was updated successfully, but these errors were encountered: