From 2b81e33446e304aba033198fa04610016c276a81 Mon Sep 17 00:00:00 2001 From: EdenQwQ Date: Thu, 3 Feb 2022 15:54:17 +0800 Subject: [PATCH 1/2] error if command doesn't start with whitespace --- src/config.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4347bd8..a501e65 100644 --- a/src/config.rs +++ b/src/config.rs @@ -134,10 +134,15 @@ fn parse_contents(contents: String) -> Result, Error> { return Err(Error::InvalidConfig(ParseError::MissingCommand(real_line_no))); } - let command = lines[i + 1].trim(); + let command = lines[i + 1]; + + // Error if the command doesn't start with whitespace + if !command.starts_with(' ') && !command.starts_with('\t') { + return Err(Error::InvalidConfig(ParseError::CommandWithoutWhitespace(real_line_no))); + } // Push a new hotkey to the hotkeys vector - hotkeys.push(Hotkey::new(key_presses, String::from(command))); + hotkeys.push(Hotkey::new(key_presses, String::from(command.trim()))); // Skip trying to parse the next line (command) // because we already dealt with it @@ -388,7 +393,9 @@ pesto #[test] fn test_command_without_whitespace() -> std::io::Result<()> { - let contents = "0 + + let contents = " +0 firefox 1 @@ -396,6 +403,8 @@ brave "; let result = parse_contents(contents.to_string()); + assert!(result.is_err()); + let config_error = match result { Ok(_) => panic!( "❌ Commands without whitespaces at the start are invalid. From 42d3c9da71a7bceb3420c76da9e5aced44a9f33c Mon Sep 17 00:00:00 2001 From: EdenQwQ Date: Thu, 3 Feb 2022 16:37:26 +0800 Subject: [PATCH 2/2] return the command line instead --- src/config.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index a501e65..ac72a0d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -138,7 +138,7 @@ fn parse_contents(contents: String) -> Result, Error> { // Error if the command doesn't start with whitespace if !command.starts_with(' ') && !command.starts_with('\t') { - return Err(Error::InvalidConfig(ParseError::CommandWithoutWhitespace(real_line_no))); + return Err(Error::InvalidConfig(ParseError::CommandWithoutWhitespace(real_line_no + 1))); } // Push a new hotkey to the hotkeys vector @@ -394,8 +394,7 @@ pesto #[test] fn test_command_without_whitespace() -> std::io::Result<()> { - let contents = " -0 + let contents = "0 firefox 1 @@ -403,8 +402,6 @@ brave "; let result = parse_contents(contents.to_string()); - assert!(result.is_err()); - let config_error = match result { Ok(_) => panic!( "❌ Commands without whitespaces at the start are invalid.