diff options
| author | Teddy Wing | 2018-11-01 04:07:38 +0100 |
|---|---|---|
| committer | Teddy Wing | 2018-11-01 04:20:39 +0100 |
| commit | 13e90c090923a209e5e26fb3e609d5d12f737f53 (patch) | |
| tree | f0338374e7bfe9f7f70dde86fda067b19215c728 | |
| parent | b69f46ced56fb199c8cdb58742f8babb5d953988 (diff) | |
| download | dome-key-map-13e90c090923a209e5e26fb3e609d5d12f737f53.tar.bz2 | |
parser: Change `Map` to a tuple type alias
Investigating why I'm not getting error messages in my parsed result.
The layer where I stop getting them is when parsing to a
`MapCollection`.
Split the list of `map()` parser out from `map_collection()` to get more
information. Turns out that the new `maps()` parser is now where parse
errors get discarded.
In an attempt to get the errors to appear, I tried to replicate the
structure of Combine's INI example parser:
https://github.com/Marwes/combine/blob/921202a018000041c9d3e8b12b7c1a53d0252f67/examples/ini.rs
That program makes a `property()` parser which outputs a 2-tuple of
`(String, String)`, and a `properties()` parser which outputs a
`HashMap<String, String>`, using the values in the tuple to construct
the HashMap.
Unfortunately, this change didn't solve the problem of the non-bubbling
error messages. Unsure about whether or not to keep this change.
| -rw-r--r-- | src/parser.rs | 149 |
1 files changed, 102 insertions, 47 deletions
diff --git a/src/parser.rs b/src/parser.rs index 8dde762..7afce9d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -155,12 +155,13 @@ impl MapAction { } } -#[derive(Debug, PartialEq)] -struct Map { - trigger: Trigger, - action: Action, - kind: MapKind, -} +// #[derive(Debug, PartialEq)] +// struct Map { +// trigger: Trigger, +// action: Action, +// kind: MapKind, +// } +type Map = (Trigger, MapAction); type MapCollection = HashMap<Trigger, MapAction>; @@ -553,14 +554,29 @@ where whitespace_separator(), action() ).map(|(kind, _, trigger, _, action)| - Map { - trigger: trigger, - action: action, - kind: kind, - } + ( + trigger, + MapAction { + action: action, + kind: kind, + }, + ) + // Map { + // trigger: trigger, + // action: action, + // kind: kind, + // } ) } +fn maps<I>() -> impl Parser<Input = I, Output = MapCollection> +where + I: Stream<Item = char>, + I::Error: ParseError<I::Item, I::Range, I::Position>, +{ + many(map().skip(blank())) +} + fn map_collection<I>() -> impl Parser<Input = I, Output = MapCollection> where I: Stream<Item = char>, @@ -568,22 +584,23 @@ where { ( blank(), - many::<Vec<Map>, _>(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 - }) + 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<I>() -> impl Parser<Input = I, Output = Mode> @@ -630,17 +647,24 @@ where { 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.trigger, + // MapAction { + // action: map.action, + // kind: map.kind, + // } + map.0, + map.1, ); }, Definition::Mode(mode) => { @@ -1071,17 +1095,44 @@ mod tests { fn map_parses_map_line() { let text = "map <play><down> test "; - let expected = Map { - trigger: vec![HeadphoneButton::Play, HeadphoneButton::Down], - action: Action::String("test".to_owned()), - kind: MapKind::Map, - }; + let expected = ( + vec![HeadphoneButton::Play, HeadphoneButton::Down], + MapAction { + action: Action::String("test".to_owned()), + kind: MapKind::Map, + } + ); let result = map().parse(text).map(|t| t.0); assert_eq!(result, Ok(expected)); } #[test] + fn maps_parses_multiple_maps() { + let text = "map <play><down> test +cmd <down> echo test +"; + let mut expected = HashMap::new(); + expected.insert( + vec![HeadphoneButton::Play, HeadphoneButton::Down], + MapAction { + action: Action::String("test".to_owned()), + kind: MapKind::Map, + } + ); + expected.insert( + vec![HeadphoneButton::Down], + MapAction { + action: Action::String("echo test".to_owned()), + kind: MapKind::Command, + } + ); + let result = maps().easy_parse(text).map(|t| t.0); + + assert_eq!(result, Ok(expected)); + } + + #[test] fn map_collection_parses_maps() { let text = " # Test comment @@ -1191,20 +1242,24 @@ map <down> k trigger: vec![HeadphoneButton::Up], maps: mode_up_maps, }), - Definition::Map(Map { - trigger: vec![HeadphoneButton::Play], - action: Action::String("m".to_owned()), - kind: MapKind::Map, - }), + Definition::Map(( + vec![HeadphoneButton::Play], + MapAction { + 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(Map { - trigger: vec![HeadphoneButton::Down], - action: Action::String("k".to_owned()), - kind: MapKind::Map, - }), + Definition::Map(( + vec![HeadphoneButton::Down], + MapAction { + action: Action::String("k".to_owned()), + kind: MapKind::Map, + }) + ), ]; assert_eq!(result, Ok(expected)); |
