diff options
| author | Teddy Wing | 2018-08-20 04:36:47 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2018-08-20 04:36:47 +0200 | 
| commit | b226320a9ba7e6e8e14687b59677b5e475ca015f (patch) | |
| tree | 332c8c070d736305293159ae0ed784a9fa442c02 | |
| parent | a528adb596d6e7ff2fedcee075356b49f29cf8ea (diff) | |
| download | dome-key-map-b226320a9ba7e6e8e14687b59677b5e475ca015f.tar.bz2 | |
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.
| -rw-r--r-- | src/lib.rs | 81 | 
1 files changed, 80 insertions, 1 deletions
| @@ -50,7 +50,8 @@ struct Mode {      maps: MapCollection,  } -pub struct DKMapGroup { +#[derive(Debug, PartialEq)] +pub struct MapGroup {      maps: MapCollection,      modes: HashMap<Trigger, MapCollection>,  } @@ -176,6 +177,31 @@ where      )  } +fn map_group<I>() -> impl Parser<Input = I, Output = MapGroup> +where +    I: Stream<Item = char>, +    I::Error: ParseError<I::Item, I::Range, I::Position>, +{ +    ( +        many::<Vec<Mode>, _>(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<I>() -> impl Parser<Input = I>  where      I: Stream<Item = char>, @@ -341,4 +367,57 @@ cmd <down> /usr/bin/say 'hello'          assert_eq!(result, Ok(expected));      } + +    #[test] +    fn map_group_parses_a_whole_map_file_string() { +        let text = "map <play> some text + +# The following does nothing +cmd <down> /bin/echo nothing + +mode <down><up> { +    map <play> p +} + +cmd <play> /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<Trigger, MapCollection> = 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)); +    }  } | 
