diff --git a/.changes/panic-key.md b/.changes/panic-key.md new file mode 100644 index 0000000..c5b389b --- /dev/null +++ b/.changes/panic-key.md @@ -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. \ No newline at end of file diff --git a/src/hotkey.rs b/src/hotkey.rs index 2525987..57d665e 100644 --- a/src/hotkey.rs +++ b/src/hotkey.rs @@ -229,7 +229,10 @@ fn parse_hotkey(hotkey: &str) -> Result { } } - 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 { @@ -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]