From 84fd84cc65f69b23b7e87f5dbbe687caef88f16b Mon Sep 17 00:00:00 2001 From: EdenQwQ Date: Thu, 3 Feb 2022 15:54:17 +0800 Subject: [PATCH] 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.