From a528adb596d6e7ff2fedcee075356b49f29cf8ea Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 19 Aug 2018 02:27:37 +0200 Subject: Parse modes Add a new `Mode` type to parse into, similar to the `Map` type we made recently. Parse a mode definition, which contains a `MapCollection`. Modes cannot contain other modes, so we don't bother worrying about recursive parsers. --- src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index df490e5..aebf43b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,12 @@ struct Map { type MapCollection = HashMap; +#[derive(Debug, PartialEq)] +struct Mode { + trigger: Trigger, + maps: MapCollection, +} + pub struct DKMapGroup { maps: MapCollection, modes: HashMap, @@ -149,6 +155,27 @@ where }) } +fn mode() -> impl Parser +where + I: Stream, + I::Error: ParseError, +{ + ( + string("mode"), + whitespace_separator(), + trigger(), + whitespace_separator(), + token('{'), + map_collection(), + token('}'), // Verify that this is parsed on its own line, not inside a map + ).map(|(_, _, trigger, _, _, collection, _)| + Mode { + trigger: trigger, + maps: collection, + } + ) +} + fn comment() -> impl Parser where I: Stream, @@ -283,4 +310,35 @@ cmd /usr/bin/say 'hello' assert_eq!(result, Ok(expected)); } + + #[test] + fn mode_parses_a_mode() { + let text = "mode { + cmd echo hello + map insert {} + }"; + let result = mode().parse(text).map(|t| t.0); + + let mut expected = Mode { + trigger: vec![HeadphoneButton::Down, HeadphoneButton::Up], + maps: HashMap::new(), + }; + + expected.maps.insert( + vec![HeadphoneButton::Up, HeadphoneButton::Play], + MapAction { + action: "echo hello".to_owned(), + kind: MapKind::Command, + }, + ); + expected.maps.insert( + vec![HeadphoneButton::Down], + MapAction { + action: "insert {}".to_owned(), + kind: MapKind::Map, + }, + ); + + assert_eq!(result, Ok(expected)); + } } -- cgit v1.2.3