From b226320a9ba7e6e8e14687b59677b5e475ca015f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 20 Aug 2018 04:36:47 +0200 Subject: Try to parse a `MapGroup` No dice. Getting this error: thread 'tests::map_group_parses_a_whole_map_file_string' panicked at 'assertion failed: `(left == right)` left: `Err(Errors { position: PointerOffset(4376730950), errors: [Unexpected(Token('a')), Expected(Borrowed("mode"))] })`, right: `Ok(MapGroup { maps: {[Down]: MapAction { action: "/bin/echo nothing", kind: Command }, [Play]: MapAction { action: "/usr/bin/say hello", kind: Command }}, modes: {[Down, Up]: {[Play]: MapAction { action: "p", kind: Map }}} })`', src/lib.rs:421:9 I need a way to parse modes and maps in any order. Here the code is trying to parse first modes, then maps. --- src/lib.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index aebf43b..7cb2444 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,8 @@ struct Mode { maps: MapCollection, } -pub struct DKMapGroup { +#[derive(Debug, PartialEq)] +pub struct MapGroup { maps: MapCollection, modes: HashMap, } @@ -176,6 +177,31 @@ where ) } +fn map_group() -> impl Parser +where + I: Stream, + I::Error: ParseError, +{ + ( + many::, _>(mode()), + map_collection(), + ).map(|(modes, maps)| { + let mut modes_hash = HashMap::new(); + + for mode in modes { + modes_hash.insert( + mode.trigger, + mode.maps, + ); + } + + MapGroup { + maps: maps, + modes: modes_hash, + } + }) +} + fn comment() -> impl Parser where I: Stream, @@ -341,4 +367,57 @@ cmd /usr/bin/say 'hello' assert_eq!(result, Ok(expected)); } + + #[test] + fn map_group_parses_a_whole_map_file_string() { + let text = "map some text + +# The following does nothing +cmd /bin/echo nothing + +mode { + map p +} + +cmd /usr/bin/say hello"; + let result = map_group().easy_parse(text).map(|t| t.0); + + let mut maps: MapCollection = HashMap::new(); + let mut modes: HashMap = HashMap::new(); + let mut mode_maps: MapCollection = HashMap::new(); + + maps.insert( + vec![HeadphoneButton::Down], + MapAction { + action: "/bin/echo nothing".to_owned(), + kind: MapKind::Command, + }, + ); + maps.insert( + vec![HeadphoneButton::Play], + MapAction { + action: "/usr/bin/say hello".to_owned(), + kind: MapKind::Command, + }, + ); + + mode_maps.insert( + vec![HeadphoneButton::Play], + MapAction { + action: "p".to_owned(), + kind: MapKind::Map, + }, + ); + modes.insert( + vec![HeadphoneButton::Down, HeadphoneButton::Up], + mode_maps, + ); + + let expected = MapGroup { + maps: maps, + modes: modes, + }; + + assert_eq!(result, Ok(expected)); + } } -- cgit v1.2.3