diff options
| author | Teddy Wing | 2018-09-29 14:00:02 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-09-29 14:00:02 +0200 |
| commit | b02e7366c3c4b9edb5afa0d012952fad369b66a9 (patch) | |
| tree | f20872432c265b07603ce03900eb8ae26f1ee749 | |
| parent | a6f5f275e781bfa443e69b74a6c776eb6d970ded (diff) | |
| download | dome-key-map-b02e7366c3c4b9edb5afa0d012952fad369b66a9.tar.bz2 | |
Change `parser::Action` to an enum
It will now be a different type of `Vec` depending on whether it
represents a string map or a command. We'll have new parsers to parse
the action definition in a more fine-grained way.
The `String` variant is just there for temporary backward compatibility
while I figure out parsing for the other two.
Still need to update the tests for this change.
| -rw-r--r-- | src/cocoa_bridge.rs | 34 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/parser.rs | 47 |
3 files changed, 65 insertions, 18 deletions
diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index 8718e58..265c052 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -11,7 +11,7 @@ use libc::{c_char, size_t}; use stderrlog; use xdg; -use {HeadphoneButton, MapGroup, MapKind}; +use {Action, HeadphoneButton, MapGroup, MapKind}; #[repr(C)] struct renameMeMapGroup { @@ -290,13 +290,17 @@ mode <play><up> { if let Some(map) = mode.get(trigger) { return match map.kind { MapKind::Map => { - type_string(&map.action, &[], 0.0, 0.0); - - Some( - KeyActionResult::new(ActionKind::Map) - .with_action(&map.action) - .in_mode(trigger) - ) + if let Action::String(s) = &map.action { + type_string(s, &[], 0.0, 0.0); + + Some( + KeyActionResult::new(ActionKind::Map) + .with_action(s) + .in_mode(trigger) + ) + } else { + None + } }, MapKind::Command => { Some( @@ -314,12 +318,16 @@ mode <play><up> { if let Some(map) = map { return match map.kind { MapKind::Map => { - type_string(&map.action, &[], 0.0, 0.0); + if let Action::String(s) = &map.action { + type_string(s, &[], 0.0, 0.0); - Some( - KeyActionResult::new(ActionKind::Map) - .with_action(&map.action) - ) + Some( + KeyActionResult::new(ActionKind::Map) + .with_action(s) + ) + } else { + None + } }, MapKind::Command => { Some( @@ -13,6 +13,6 @@ extern crate xdg; mod cocoa_bridge; mod parser; -use parser::{HeadphoneButton, MapGroup, MapKind}; +use parser::{Action, HeadphoneButton, MapGroup, MapKind}; pub use cocoa_bridge::*; diff --git a/src/parser.rs b/src/parser.rs index 57abbbb..6ac52f1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use autopilot; use autopilot::key::{Flag, KeyCodeConvertible}; use combine::*; use combine::easy::Errors as CombineErrors; @@ -22,11 +23,48 @@ pub enum HeadphoneButton { Down, } type Trigger = Vec<HeadphoneButton>; -type Action = String; +// type Action = String; -enum Action2<'a, T: 'a + KeyCodeConvertible> { - Map(&'a [(T, &'a [Flag])]), - Command(&'a [&'a str]), +// enum Action2<'a, T: 'a + KeyCodeConvertible> { +// Map(&'a [(T, &'a [Flag])]), +// Command(&'a [&'a str]), +// } + +#[derive(Debug)] +struct Character(autopilot::key::Character); + +impl PartialEq for Character { + fn eq(&self, other: &Character) -> bool { + (self.0).0 == (other.0).0 + } +} + +#[derive(Debug)] +struct KeyCode(autopilot::key::Code); + +impl PartialEq for KeyCode { + fn eq(&self, other: &KeyCode) -> bool { + (self.0).0 == (other.0).0 + } +} + +#[derive(Debug, PartialEq)] +enum KeyboardKey { + Character(Character), + KeyCode(KeyCode), +} + +#[derive(Debug, PartialEq)] +struct KeyboardKeyWithModifiers { + key: KeyboardKey, + flags: Vec<Flag>, +} + +#[derive(Debug, PartialEq)] +pub enum Action { + String(String), + Map(Vec<KeyboardKeyWithModifiers>), + Command(Vec<String>), } #[repr(C)] @@ -123,6 +161,7 @@ where I::Error: ParseError<I::Item, I::Range, I::Position>, { take_until(newline()) + .map(|action| Action::String(action)) } fn whitespace_separator<I>() -> impl Parser<Input = I> |
