diff options
| author | Teddy Wing | 2018-08-09 02:15:15 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-08-09 02:15:15 +0200 |
| commit | 7b0c36e446db0f0c98ef9b0af06f1b8e65c9b0e2 (patch) | |
| tree | c79dffae1ec4afc0ae638a26631895c8fadb96c7 | |
| parent | 93341ab2b08a85da33d28782e7ffe2f4d8c855d3 (diff) | |
| download | dome-key-map-7b0c36e446db0f0c98ef9b0af06f1b8e65c9b0e2.tar.bz2 | |
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!
| -rw-r--r-- | src/lib.rs | 42 |
1 files changed, 40 insertions, 2 deletions
@@ -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<HeadphoneButton>; type Action = String; + +#[derive(Debug, PartialEq)] pub enum MapKind { Map, Command, @@ -25,10 +31,42 @@ pub struct DKMapGroup { modes: HashMap<Trigger, MapCollection>, } + +fn map_kind<I>() -> impl Parser<Input = I, Output = MapKind> +where + I: Stream<Item = char>, + I::Error: ParseError<I::Item, I::Range, I::Position>, +{ + // 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)); } } |
