From 7b0c36e446db0f0c98ef9b0af06f1b8e65c9b0e2 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 9 Aug 2018 02:15:15 +0200 Subject: Parser for `map` and `cmd` keywords This parser reads those tokens and parses them to `MapKind` types. Took a bit of struggling and researching, but it's so cool to see it working now! --- src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index a7850d8..e25ea54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,10 @@ extern crate combine; use std::collections::HashMap; +use combine::*; +use combine::parser::choice::or; +use combine::parser::char::string; + pub enum HeadphoneButton { Play, Up, @@ -9,6 +13,8 @@ pub enum HeadphoneButton { } type Trigger = Vec; type Action = String; + +#[derive(Debug, PartialEq)] pub enum MapKind { Map, Command, @@ -25,10 +31,42 @@ pub struct DKMapGroup { modes: HashMap, } + +fn map_kind() -> impl Parser +where + I: Stream, + I::Error: ParseError, +{ + // tokens( + // |l, r| l.eq_ignore_ascii_case(&r), + // ) + + // satisfy(|s| s == "map" || s == "cmd") + + or( + string("map").map(|_| MapKind::Map), + string("cmd").map(|_| MapKind::Command), + ) +} + + #[cfg(test)] mod tests { + use super::*; + #[test] - fn it_works() { - assert_eq!(2 + 2, 4); + fn map_kind_parses_kind_map() { + let text = "map"; + let result = map_kind().parse(text).map(|t| t.0); + + assert_eq!(result, Ok(MapKind::Map)); + } + + #[test] + fn map_kind_parses_kind_command() { + let text = "cmd"; + let result = map_kind().parse(text).map(|t| t.0); + + assert_eq!(result, Ok(MapKind::Command)); } } -- cgit v1.2.3