Skip to content

Commit

Permalink
fix: fix panic when multiple modifier keys and no main key (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmJSD authored Aug 13, 2024
1 parent 86e68f7 commit c750004
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/panic-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"global-hotkey": patch
---

Fix a panic when parsing `HotKey` from a string and return an error instead, if the hotkey string consists of only modifiers and doesn't contain a key.
11 changes: 10 additions & 1 deletion src/hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ fn parse_hotkey(hotkey: &str) -> Result<HotKey, HotKeyParseError> {
}
}

Ok(HotKey::new(Some(mods), key.unwrap()))
Ok(HotKey::new(
Some(mods),
key.ok_or_else(|| HotKeyParseError::InvalidFormat(hotkey.to_string()))?,
))
}

fn parse_key(key: &str) -> Result<Code, HotKeyParseError> {
Expand Down Expand Up @@ -448,6 +451,12 @@ fn test_parse_hotkey() {
id: 0,
}
);

// Ensure that if it is just multiple modifiers, we do not panic.
// This would be a regression if this happened.
if HotKey::from_str("Shift+Ctrl").is_ok() {
panic!("This is not a valid hotkey");
}
}

#[test]
Expand Down

0 comments on commit c750004

Please sign in to comment.