refactor: reduce one line of indent in parsing

Checking if the current line is a command can be refactored
into a guard clause.
main
Angelo Fallaria 2 years ago
parent ff635cc3d9
commit 981d4c0387

@ -113,6 +113,14 @@ fn parse_contents(contents: String) -> Result<Vec<Hotkey>, Error> {
("0", evdev::Key::KEY_0), ("0", evdev::Key::KEY_0),
]); ]);
let mod_to_mod_enum: HashMap<&str, Modifier> = HashMap::from([
("ctrl", Modifier::Control),
("control", Modifier::Control),
("super", Modifier::Super),
("alt", Modifier::Alt),
("shift", Modifier::Shift),
]);
let lines: Vec<&str> = contents.split('\n').collect(); let lines: Vec<&str> = contents.split('\n').collect();
let mut hotkeys: Vec<Hotkey> = Vec::new(); let mut hotkeys: Vec<Hotkey> = Vec::new();
@ -135,7 +143,10 @@ fn parse_contents(contents: String) -> Result<Vec<Hotkey>, Error> {
// in a file are of course counted from 1 // in a file are of course counted from 1
let real_line_no: u32 = (i + 1).try_into().unwrap(); let real_line_no: u32 = (i + 1).try_into().unwrap();
if key_to_evdev_key.contains_key(lines[i].trim()) { if !key_to_evdev_key.contains_key(lines[i].trim()) {
return Err(Error::InvalidConfig(ParseError::UnknownSymbol(real_line_no)));
}
// Error if keybind line is at the very last line // Error if keybind line is at the very last line
// ( It's impossible for there to be a command ) // ( It's impossible for there to be a command )
if i >= lines.len() - 1 { if i >= lines.len() - 1 {
@ -182,9 +193,6 @@ fn parse_contents(contents: String) -> Result<Vec<Hotkey>, Error> {
// Skip trying to parse the next line (command) // Skip trying to parse the next line (command)
// because we already dealt with it // because we already dealt with it
lines_to_skip += 1; lines_to_skip += 1;
} else {
return Err(Error::InvalidConfig(ParseError::UnknownSymbol(real_line_no)));
}
} }
Ok(hotkeys) Ok(hotkeys)
@ -435,6 +443,7 @@ shift + k +
ParseError::UnknownSymbol(2)) ParseError::UnknownSymbol(2))
} }
#[test]
fn test_common_modifiers() -> std::io::Result<()> { fn test_common_modifiers() -> std::io::Result<()> {
let contents = " let contents = "
shift + k shift + k
@ -620,7 +629,6 @@ WE WISH YOU A MERRY RUSTMAS
} }
#[test] #[test]
#[ignore]
fn test_real_config_snippet() -> std::io::Result<()> { fn test_real_config_snippet() -> std::io::Result<()> {
let contents = " let contents = "
# reloads sxhkd configuration: # reloads sxhkd configuration:

Loading…
Cancel
Save