From ae48205ed1549678835574f962c7d2c62704d147 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 16 Aug 2018 13:23:40 +0200 Subject: Parse single map definitions Parse a map definition line into a `Map`. Got so stuck on this error: error[E0277]: the trait bound `(): combine::Parser` is not satisfied --> src/lib.rs:107:16 | 107 | fn map() -> impl Parser | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `combine::Parser` is not implemented for `()` | = note: the return type of a function must have a statically known size error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. Turns out the problem was that I had put a semicolon at the end of the expression in `map()`. Geez. Took hours to finally see that. --- src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 79941d1..5dbf477 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,7 @@ pub struct MapAction { pub kind: MapKind, } +#[derive(Debug, PartialEq)] struct Map { trigger: Trigger, action: Action, @@ -103,6 +104,26 @@ where skip_many1(space().or(tab())) } +fn map() -> impl Parser +where + I: Stream, + I::Error: ParseError, +{ + ( + map_kind(), + whitespace_separator(), + trigger(), + whitespace_separator(), + action() + ).map(|(kind, _, trigger, _, action)| + Map { + trigger: trigger, + action: action, + kind: kind, + } + ) +} + fn map_collection() -> impl Parser where I: Stream, @@ -195,6 +216,20 @@ mod tests { assert_eq!(result, Ok(expected)); } + #[test] + fn map_parses_map_line() { + let text = "map test +"; + let expected = Map { + trigger: vec![HeadphoneButton::Play, HeadphoneButton::Down], + action: "test".to_owned(), + kind: MapKind::Map, + }; + let result = map().parse(text).map(|t| t.0); + + assert_eq!(result, Ok(expected)); + } + #[test] fn map_collection_parses_maps() { let text = " -- cgit v1.2.3