diff options
| author | Teddy Wing | 2018-08-09 02:31:11 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-08-09 02:31:11 +0200 |
| commit | 38101cf31564884103000c33d224b4b5381c52c6 (patch) | |
| tree | a5e58a8122df3a37e5b8646561a33aba59eb9c5c | |
| parent | 1f24497e4c63f99dd1ae8c6e17fc647bf7564658 (diff) | |
| download | dome-key-map-38101cf31564884103000c33d224b4b5381c52c6.tar.bz2 | |
Parse headphone buttons (`<play>`, `<up>`, `<down>`)
Parses a headphone button into the `HeadphoneButton` enum.
| -rw-r--r-- | src/lib.rs | 26 |
1 files changed, 26 insertions, 0 deletions
@@ -1,3 +1,4 @@ +#[macro_use] extern crate combine; use std::collections::HashMap; @@ -6,6 +7,7 @@ use combine::*; use combine::parser::choice::or; use combine::parser::char::string; +#[derive(Debug, PartialEq)] pub enum HeadphoneButton { Play, Up, @@ -43,6 +45,22 @@ where ) } +fn headphone_button<I>() -> impl Parser<Input = I, Output = HeadphoneButton> +where + I: Stream<Item = char>, + I::Error: ParseError<I::Item, I::Range, I::Position>, +{ + between( + token('<'), + token('>'), + choice!( + string("play").map(|_| HeadphoneButton::Play), + string("up").map(|_| HeadphoneButton::Up), + string("down").map(|_| HeadphoneButton::Down) + ), + ) +} + #[cfg(test)] mod tests { @@ -63,4 +81,12 @@ mod tests { assert_eq!(result, Ok(MapKind::Command)); } + + #[test] + fn headphone_button_parses_play() { + let text = "<play>"; + let result = headphone_button().parse(text).map(|t| t.0); + + assert_eq!(result, Ok(HeadphoneButton::Play)); + } } |
