-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add FromStr trait for Key #4
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Can you bump the minimum supported Rust version so Travis does not fail?
src/key.rs
Outdated
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
use Key::*; | ||
match s { | ||
ch if ch.chars().take(2).count() == 1 => Ok(Character(ch.to_string())), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A key string can also consist of a base character and multiple combining characters.
https://w3c.github.io/uievents-key/#keys-unicode
Not sure if it is worth supporting.
Additionally Control Characters should be excluded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could add the exact check if string is a key string, but I'm not sure how do you feel with adding new dependency (I would use https://docs.rs/unic-ucd-category/0.9.0/unic_ucd_category/enum.GeneralCategory.html )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the dependency would be quite big for such a small feature.
How about checking that:
- there are no control characters in the string (
char::is_control
) - No characters besides the first are in the ASCII range (
char::is_ascii
)
This prevents common mistakes like mistyped keynames (e.g. Ennter
) from being recognized as characters while allowing all characters specified. If the need arises we can add the dependency later.
Released as v0.5.0 |
Extended the
convert.py
script, so it implement traitFromStr
for Key. Thanks to that, one can now parse strings from e.g.js_sys::KeyboardEvent::key()
and have the enum value.