aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-08-09 02:15:15 +0200
committerTeddy Wing2018-08-09 02:15:15 +0200
commit7b0c36e446db0f0c98ef9b0af06f1b8e65c9b0e2 (patch)
treec79dffae1ec4afc0ae638a26631895c8fadb96c7
parent93341ab2b08a85da33d28782e7ffe2f4d8c855d3 (diff)
downloaddome-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.rs42
1 files changed, 40 insertions, 2 deletions
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<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));
}
}