From 3edf1126464eb798b6f95789fcce114bad2c05b8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 18 Oct 2018 13:11:59 +0200 Subject: Add a `` pseudo-key This "key" creates an empty action that doesn't do anything. Allows users to clear the default behaviour of a headphone button. --- src/parser.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs index 0cfac2e..7ccaf6b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -72,6 +72,7 @@ pub enum KeyboardKey { Character(Character), KeyCode(KeyCode), NXKey(NXKey), + Nop, } #[derive(Debug, PartialEq)] @@ -107,6 +108,7 @@ impl KeyboardKeyWithModifiers { dkess_press_key(nx, flags); } }, + KeyboardKey::Nop => (), } } } @@ -323,7 +325,7 @@ where between( token('<'), token('>'), - or( + choice(( try(( many(key_modifier()), or( @@ -336,8 +338,9 @@ where action_character().map(|c| KeyboardKey::Character(Character::new(c)) ), - )) - ) + )), + try((value(vec![]), nop())), + )) ).map(|(modifiers, key): (Vec, KeyboardKey)| { KeyboardKeyWithModifiers::new( key, @@ -492,6 +495,15 @@ where ) } +fn nop() -> impl Parser +where + I: Stream, + I::Error: ParseError, +{ + string_case_insensitive("Nop") + .map(|_| KeyboardKey::Nop) +} + fn whitespace_separator() -> impl Parser where I: Stream, @@ -1015,6 +1027,21 @@ mod tests { assert_eq!(result, Ok(expected)); } + #[test] + fn action_parses_map_with_nop() { + let text = ""; + + let expected = Action::Map(vec![ + KeyboardKeyWithModifiers::new( + KeyboardKey::Nop, + vec![], + ), + ]); + let result = action_map().easy_parse(text).map(|t| t.0); + + assert_eq!(result, Ok(expected)); + } + #[test] fn map_parses_map_line() { let text = "map test -- cgit v1.2.3