fix: parse one letter string to valid accelerator (#28)

* fix: parse one letter string to valid accelerator

* clippy
This commit is contained in:
Amr Bashir 2022-12-20 00:55:57 +02:00 committed by GitHub
parent 0b0ec147bc
commit 0173987ed5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 51 deletions

View file

@ -0,0 +1,5 @@
---
"muda": "patch"
---
Fix parsing one letter string as valid accelerator without modifiers.

View file

@ -77,61 +77,70 @@ fn parse_accelerator(accelerator_string: &str) -> crate::Result<Accelerator> {
let mut mods = Modifiers::empty(); let mut mods = Modifiers::empty();
let mut key = Code::Unidentified; let mut key = Code::Unidentified;
for raw in accelerator_string.split('+') { let mut split = accelerator_string.split('+');
let token = raw.trim().to_string(); let len = split.clone().count();
if token.is_empty() { let parse_key = |token: &str| -> crate::Result<Code> {
return Err(crate::Error::AcceleratorParseError( if let Ok(code) = Code::from_str(token) {
"Unexpected empty token while parsing accelerator".into(), match code {
)); Code::Unidentified => Err(crate::Error::AcceleratorParseError(format!(
"Couldn't identify \"{}\" as a valid `Code`",
token
))),
_ => Ok(code),
}
} else {
Err(crate::Error::AcceleratorParseError(format!(
"Couldn't identify \"{}\" as a valid `Code`",
token
)))
} }
};
if key != Code::Unidentified { if len == 1 {
// at this point we already parsed the modifiers and found a main key but let token = split.next().unwrap();
// the function received more then one main key or it is not in the right order key = parse_key(token)?;
// examples: } else {
// 1. "Ctrl+Shift+C+A" => only one main key should be allowd. for raw in accelerator_string.split('+') {
// 2. "Ctrl+C+Shift" => wrong order let token = raw.trim().to_string();
return Err(crate::Error::AcceleratorParseError(format!( if token.is_empty() {
"Unexpected accelerator string format: \"{}\"", return Err(crate::Error::AcceleratorParseError(
accelerator_string "Unexpected empty token while parsing accelerator".into(),
))); ));
} }
match token.to_uppercase().as_str() { if key != Code::Unidentified {
"OPTION" | "ALT" => { // at this point we already parsed the modifiers and found a main key but
mods.set(Modifiers::ALT, true); // the function received more then one main key or it is not in the right order
// examples:
// 1. "Ctrl+Shift+C+A" => only one main key should be allowd.
// 2. "Ctrl+C+Shift" => wrong order
return Err(crate::Error::AcceleratorParseError(format!(
"Unexpected accelerator string format: \"{}\"",
accelerator_string
)));
} }
"CONTROL" | "CTRL" => {
mods.set(Modifiers::CONTROL, true); match token.to_uppercase().as_str() {
} "OPTION" | "ALT" => {
"COMMAND" | "CMD" | "SUPER" => { mods.set(Modifiers::ALT, true);
mods.set(Modifiers::META, true); }
} "CONTROL" | "CTRL" => {
"SHIFT" => { mods.set(Modifiers::CONTROL, true);
mods.set(Modifiers::SHIFT, true); }
} "COMMAND" | "CMD" | "SUPER" => {
"COMMANDORCONTROL" | "COMMANDORCTRL" | "CMDORCTRL" | "CMDORCONTROL" => { mods.set(Modifiers::META, true);
#[cfg(target_os = "macos")] }
mods.set(Modifiers::META, true); "SHIFT" => {
#[cfg(not(target_os = "macos"))] mods.set(Modifiers::SHIFT, true);
mods.set(Modifiers::CONTROL, true); }
} "COMMANDORCONTROL" | "COMMANDORCTRL" | "CMDORCTRL" | "CMDORCONTROL" => {
_ => { #[cfg(target_os = "macos")]
if let Ok(code) = Code::from_str(token.as_str()) { mods.set(Modifiers::META, true);
match code { #[cfg(not(target_os = "macos"))]
Code::Unidentified => { mods.set(Modifiers::CONTROL, true);
return Err(crate::Error::AcceleratorParseError(format!( }
"Couldn't identify \"{}\" as a valid `Code`", _ => {
token key = parse_key(token.as_str())?;
)))
}
_ => key = code,
}
} else {
return Err(crate::Error::AcceleratorParseError(format!(
"Couldn't identify \"{}\" as a valid `Code`",
token
)));
} }
} }
} }