mirror of
https://github.com/italicsjenga/muda.git
synced 2025-02-23 23:27:44 +11:00
fix: parse one letter string to valid accelerator (#28)
* fix: parse one letter string to valid accelerator * clippy
This commit is contained in:
parent
0b0ec147bc
commit
0173987ed5
2 changed files with 65 additions and 51 deletions
5
.changes/one-letter-accel.md
Normal file
5
.changes/one-letter-accel.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"muda": "patch"
|
||||
---
|
||||
|
||||
Fix parsing one letter string as valid accelerator without modifiers.
|
|
@ -77,6 +77,29 @@ fn parse_accelerator(accelerator_string: &str) -> crate::Result<Accelerator> {
|
|||
let mut mods = Modifiers::empty();
|
||||
let mut key = Code::Unidentified;
|
||||
|
||||
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),
|
||||
}
|
||||
} 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() {
|
||||
|
@ -117,21 +140,7 @@ fn parse_accelerator(accelerator_string: &str) -> crate::Result<Accelerator> {
|
|||
mods.set(Modifiers::CONTROL, true);
|
||||
}
|
||||
_ => {
|
||||
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
|
||||
)));
|
||||
key = parse_key(token.as_str())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue