From 0b3d81c50925d8f0d7e1ea642ce41d3672a74809 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 1 Nov 2018 07:26:33 +0100 Subject: parser: Restore `Map` struct In 13e90c090923a209e5e26fb3e609d5d12f737f53 I decided to make `Map` a type alias to a tuple containing the key-value pair used by `MapCollection`. Here I've decided that I prefer the type as it was before. Sure, it was more of an intermediary type, but it did have a name that was used consistently. We do have to leave in our custom `HashMap` building, so perhaps this isn't the ideal solution, but I'm going with it, at least for now. --- src/parser.rs | 115 ++++++++++++++++++++++++---------------------------------- 1 file changed, 47 insertions(+), 68 deletions(-) (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs index 8df629d..ea1bcc5 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -155,13 +155,12 @@ impl MapAction { } } -// #[derive(Debug, PartialEq)] -// struct Map { -// trigger: Trigger, -// action: Action, -// kind: MapKind, -// } -type Map = (Trigger, MapAction); +#[derive(Debug, PartialEq)] +struct Map { + trigger: Trigger, + action: Action, + kind: MapKind, +} type MapCollection = HashMap; @@ -554,18 +553,11 @@ where whitespace_separator(), action() ).map(|(kind, _, trigger, _, action)| - ( - trigger, - MapAction { - action: action, - kind: kind, - }, - ) - // Map { - // trigger: trigger, - // action: action, - // kind: kind, - // } + Map { + trigger: trigger, + action: action, + kind: kind, + } ) } @@ -574,7 +566,22 @@ where I: Stream, I::Error: ParseError, { - many1(map().skip(blank())) + many1::, _>(map().skip(blank())) + .map(|collection| { + let mut maps = HashMap::new(); + + for map in collection { + maps.insert( + map.trigger, + MapAction { + action: map.action, + kind: map.kind, + } + ); + } + + maps + }) } fn map_collection() -> impl Parser @@ -586,21 +593,6 @@ where blank(), maps(), ).map(|(_, collection)| collection) - // .map(|(_, collection)| { - // let mut maps = HashMap::new(); - // - // for map in collection { - // maps.insert( - // map.trigger, - // MapAction { - // action: map.action, - // kind: map.kind, - // } - // ); - // } - // - // maps - // }) } fn mode() -> impl Parser @@ -652,24 +644,17 @@ where ).map(|_| MapGroup::default()), definitions() .map(|definitions| { - // MapGroup { - // maps: , - // modes: , - // } - let mut map_group = MapGroup::default(); for definition in definitions { match definition { Definition::Map(map) => { map_group.maps.insert( - // map.trigger, - // MapAction { - // action: map.action, - // kind: map.kind, - // } - map.0, - map.1, + map.trigger, + MapAction { + action: map.action, + kind: map.kind, + } ); }, Definition::Mode(mode) => { @@ -1101,13 +1086,11 @@ mod tests { fn map_parses_map_line() { let text = "map test "; - let expected = ( - vec![HeadphoneButton::Play, HeadphoneButton::Down], - MapAction { - action: Action::String("test".to_owned()), - kind: MapKind::Map, - } - ); + let expected = Map { + trigger: vec![HeadphoneButton::Play, HeadphoneButton::Down], + action: Action::String("test".to_owned()), + kind: MapKind::Map, + }; let result = map().parse(text).map(|t| t.0); assert_eq!(result, Ok(expected)); @@ -1277,24 +1260,20 @@ map k trigger: vec![HeadphoneButton::Up], maps: mode_up_maps, }), - Definition::Map(( - vec![HeadphoneButton::Play], - MapAction { - action: Action::String("m".to_owned()), - kind: MapKind::Map, - }) - ), + Definition::Map(Map { + trigger: vec![HeadphoneButton::Play], + action: Action::String("m".to_owned()), + kind: MapKind::Map, + }), Definition::Mode(Mode { trigger: vec![HeadphoneButton::Down, HeadphoneButton::Up], maps: mode_down_up_maps, }), - Definition::Map(( - vec![HeadphoneButton::Down], - MapAction { - action: Action::String("k".to_owned()), - kind: MapKind::Map, - }) - ), + Definition::Map(Map { + trigger: vec![HeadphoneButton::Down], + action: Action::String("k".to_owned()), + kind: MapKind::Map, + }), ]; assert_eq!(result, Ok(expected)); -- cgit v1.2.3