Skip to content

Commit

Permalink
fix: parse one letter string to valid accelerator (#28)
Browse files Browse the repository at this point in the history
* fix: parse one letter string to valid accelerator

* clippy
  • Loading branch information
amrbashir committed Dec 19, 2022
1 parent 0b0ec14 commit 0173987
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changes/one-letter-accel.md
@@ -0,0 +1,5 @@
---
"muda": "patch"
---

Fix parsing one letter string as valid accelerator without modifiers.
113 changes: 61 additions & 52 deletions src/accelerator.rs
Expand Up @@ -77,61 +77,70 @@ fn parse_accelerator(accelerator_string: &str) -> crate::Result<Accelerator> {
let mut mods = Modifiers::empty();
let mut key = Code::Unidentified;

for raw in accelerator_string.split('+') {
let token = raw.trim().to_string();
if token.is_empty() {
return Err(crate::Error::AcceleratorParseError(
"Unexpected empty token while parsing accelerator".into(),
));
}

if key != Code::Unidentified {
// at this point we already parsed the modifiers and found a main key but
// 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
)));
}

match token.to_uppercase().as_str() {
"OPTION" | "ALT" => {
mods.set(Modifiers::ALT, true);
let mut split = accelerator_string.split('+');
let len = split.clone().count();
let parse_key = |token: &str| -> crate::Result<Code> {
if let Ok(code) = Code::from_str(token) {
match code {
Code::Unidentified => Err(crate::Error::AcceleratorParseError(format!(
"Couldn't identify \"{}\" as a valid `Code`",
token
))),
_ => Ok(code),
}
"CONTROL" | "CTRL" => {
mods.set(Modifiers::CONTROL, true);
}
"COMMAND" | "CMD" | "SUPER" => {
mods.set(Modifiers::META, true);
}
"SHIFT" => {
mods.set(Modifiers::SHIFT, true);
} else {
Err(crate::Error::AcceleratorParseError(format!(
"Couldn't identify \"{}\" as a valid `Code`",
token
)))
}
};

if len == 1 {
let token = split.next().unwrap();
key = parse_key(token)?;
} else {
for raw in accelerator_string.split('+') {
let token = raw.trim().to_string();
if token.is_empty() {
return Err(crate::Error::AcceleratorParseError(
"Unexpected empty token while parsing accelerator".into(),
));
}
"COMMANDORCONTROL" | "COMMANDORCTRL" | "CMDORCTRL" | "CMDORCONTROL" => {
#[cfg(target_os = "macos")]
mods.set(Modifiers::META, true);
#[cfg(not(target_os = "macos"))]
mods.set(Modifiers::CONTROL, true);

if key != Code::Unidentified {
// at this point we already parsed the modifiers and found a main key but
// 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
)));
}
_ => {
if let Ok(code) = Code::from_str(token.as_str()) {
match code {
Code::Unidentified => {
return Err(crate::Error::AcceleratorParseError(format!(
"Couldn't identify \"{}\" as a valid `Code`",
token
)))
}
_ => key = code,
}
} else {
return Err(crate::Error::AcceleratorParseError(format!(
"Couldn't identify \"{}\" as a valid `Code`",
token
)));

match token.to_uppercase().as_str() {
"OPTION" | "ALT" => {
mods.set(Modifiers::ALT, true);
}
"CONTROL" | "CTRL" => {
mods.set(Modifiers::CONTROL, true);
}
"COMMAND" | "CMD" | "SUPER" => {
mods.set(Modifiers::META, true);
}
"SHIFT" => {
mods.set(Modifiers::SHIFT, true);
}
"COMMANDORCONTROL" | "COMMANDORCTRL" | "CMDORCTRL" | "CMDORCONTROL" => {
#[cfg(target_os = "macos")]
mods.set(Modifiers::META, true);
#[cfg(not(target_os = "macos"))]
mods.set(Modifiers::CONTROL, true);
}
_ => {
key = parse_key(token.as_str())?;
}
}
}
Expand Down

0 comments on commit 0173987

Please sign in to comment.