From ef99a47aaa32902979bb793a7c2e179a4289a5ec Mon Sep 17 00:00:00 2001 From: Astrid Gealer Date: Fri, 9 Aug 2024 16:06:59 +0200 Subject: [PATCH 1/4] fix: Fix the application panicing when multiple modifier keys and no main key is set --- src/hotkey.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/hotkey.rs b/src/hotkey.rs index 2525987..fe1d2d6 100644 --- a/src/hotkey.rs +++ b/src/hotkey.rs @@ -229,7 +229,16 @@ fn parse_hotkey(hotkey: &str) -> Result { } } - Ok(HotKey::new(Some(mods), key.unwrap())) + Ok(HotKey::new( + Some(mods), + match key { + Some(k) => k, + None => { + // This would mean the hotkey is more than one modifier with no key. + return Err(HotKeyParseError::InvalidFormat(hotkey.to_string())); + } + }, + )) } fn parse_key(key: &str) -> Result { @@ -448,6 +457,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 let Ok(_) = HotKey::from_str("Shift+Ctrl") { + panic!("This is not a valid hotkey"); + } } #[test] From c476e23e834f16a664261fea94531e880d470859 Mon Sep 17 00:00:00 2001 From: Astrid Gealer Date: Fri, 9 Aug 2024 20:09:28 +0200 Subject: [PATCH 2/4] style: fix linting error --- src/hotkey.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hotkey.rs b/src/hotkey.rs index fe1d2d6..4ed396d 100644 --- a/src/hotkey.rs +++ b/src/hotkey.rs @@ -460,7 +460,7 @@ fn test_parse_hotkey() { // Ensure that if it is just multiple modifiers, we do not panic. // This would be a regression if this happened. - if let Ok(_) = HotKey::from_str("Shift+Ctrl") { + if HotKey::from_str("Shift+Ctrl").is_ok() { panic!("This is not a valid hotkey"); } } From 381e32c7f6dffc6be76742316b358c2ab3dac8ff Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 13 Aug 2024 20:14:35 +0300 Subject: [PATCH 3/4] ok_or_else --- src/hotkey.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/hotkey.rs b/src/hotkey.rs index 4ed396d..57d665e 100644 --- a/src/hotkey.rs +++ b/src/hotkey.rs @@ -231,13 +231,7 @@ fn parse_hotkey(hotkey: &str) -> Result { Ok(HotKey::new( Some(mods), - match key { - Some(k) => k, - None => { - // This would mean the hotkey is more than one modifier with no key. - return Err(HotKeyParseError::InvalidFormat(hotkey.to_string())); - } - }, + key.ok_or_else(|| HotKeyParseError::InvalidFormat(hotkey.to_string()))?, )) } From cd185cf82ad657107ae335d36f88650909eb793c Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 13 Aug 2024 20:29:09 +0300 Subject: [PATCH 4/4] change file --- .changes/panic-key.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/panic-key.md 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