diff options
| author | Teddy Wing | 2018-09-29 01:04:53 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-09-29 01:04:53 +0200 |
| commit | 16cd3895f7b111544927d71904aab912d9abbf59 (patch) | |
| tree | 7232dae8de98221eb549d4b33a01eaad27fa01d2 /src | |
| parent | 829f257d4c0064209102083dc9d692834e42a8c1 (diff) | |
| download | dome-key-map-16cd3895f7b111544927d71904aab912d9abbf59.tar.bz2 | |
Try to propagate KeyCodeConvertible from Action to everywhere
Such a pain. As soon as I clear one set of compilation errors, another
set crops up. The last one was like the following:
error[E0277]: the trait bound `K: std::default::Default` is not satisfied
--> src/cocoa_bridge.rs:117:1
|
117 | / pub extern "C" fn state_new<K>() -> *mut State<K>
118 | | where K: KeyCodeConvertible {
119 | | Box::into_raw(Box::new(State::default()))
120 | | }
| |_^ the trait `std::default::Default` is not implemented for `K`
|
= help: consider adding a `where K: std::default::Default` bound
note: required by `cocoa_bridge::State`
--> src/cocoa_bridge.rs:100:1
|
100 | / pub struct State<K: KeyCodeConvertible>
101 | | where K: Default {
102 | | in_mode: Option<Vec<HeadphoneButton>>,
103 | | map_group: Option<MapGroup<K>>,
104 | | }
| |_^
error[E0277]: the trait bound `K: std::default::Default` is not satisfied
I'm done with this. Just going to make a darn enum of 'autopilot's
`Character` and `KeyCode` structs so I don't have to deal with this
mess.
Diffstat (limited to 'src')
| -rw-r--r-- | src/cocoa_bridge.rs | 30 | ||||
| -rw-r--r-- | src/parser.rs | 78 |
2 files changed, 69 insertions, 39 deletions
diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index 8718e58..a4a072f 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -4,7 +4,7 @@ use std::mem; use std::ptr; use std::slice; -use autopilot::key::type_string; +use autopilot::key::{KeyCodeConvertible, type_string}; // use cocoa::base::nil; // use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary}; use libc::{c_char, size_t}; @@ -97,9 +97,10 @@ pub struct CKeyActionResult { } #[derive(Default)] -pub struct State { +pub struct State<K: KeyCodeConvertible> +where K: Default { in_mode: Option<Vec<HeadphoneButton>>, - map_group: Option<MapGroup>, + map_group: Option<MapGroup<K>>, } #[no_mangle] @@ -113,18 +114,21 @@ pub extern "C" fn logger_init() { } #[no_mangle] -pub extern "C" fn state_new() -> *mut State { +pub extern "C" fn state_new<K>() -> *mut State<K> +where K: KeyCodeConvertible { Box::into_raw(Box::new(State::default())) } #[no_mangle] -pub extern "C" fn state_free(ptr: *mut State) { +pub extern "C" fn state_free<K>(ptr: *mut State<K>) +where K: KeyCodeConvertible { if ptr.is_null() { return } unsafe { Box::from_raw(ptr); } } #[no_mangle] -pub extern "C" fn state_load_map_group(ptr: *mut State) { +pub extern "C" fn state_load_map_group<K>(ptr: *mut State<K>) +where K: KeyCodeConvertible { match xdg::BaseDirectories::with_prefix("dome-key") { Ok(xdg_dirs) => { match xdg_dirs.find_config_file("mappings.dkmap") { @@ -162,11 +166,12 @@ pub extern "C" fn state_load_map_group(ptr: *mut State) { } #[no_mangle] -pub extern "C" fn c_run_key_action( - state: *mut State, +pub extern "C" fn c_run_key_action<K>( + state: *mut State<K>, trigger: Trigger, mode: *const Trigger, -) -> *const CKeyActionResult { +) -> *const CKeyActionResult +where K: KeyCodeConvertible { let trigger = unsafe { assert!(!trigger.buttons.is_null()); @@ -258,11 +263,12 @@ pub extern "C" fn c_run_key_action( } #[no_mangle] -pub extern "C" fn run_key_action_for_mode<'a>( - state: &mut State, +pub extern "C" fn run_key_action_for_mode<'a, K>( + state: &mut State<K>, trigger: &'a [HeadphoneButton], in_mode: Option<&[HeadphoneButton]> -) -> Option<KeyActionResult<'a>> { +) -> Option<KeyActionResult<'a>> +where K: KeyCodeConvertible { let sample_maps = "map <up> k map <down> j map <play><down> works! diff --git a/src/parser.rs b/src/parser.rs index 57abbbb..7fcea71 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -22,11 +22,28 @@ pub enum HeadphoneButton { Down, } type Trigger = Vec<HeadphoneButton>; -type Action = String; - -enum Action2<'a, T: 'a + KeyCodeConvertible> { - Map(&'a [(T, &'a [Flag])]), - Command(&'a [&'a str]), +// type Action = String; + +// enum Action<'a, T: 'a + KeyCodeConvertible> { +// String(String), +// Map(&'a [(T, &'a [Flag])]), +// Command(&'a [&'a str]), +// } + +// struct KeyboardKey { +// key: , +// flags: &[Flags], +// } + +// enum Action<T: KeyCodeConvertible> { +// String(String), +// Map(Vec<(T, Vec<Flag>)>), +// Command(Vec<String>), +// } +enum Action<K: KeyCodeConvertible> { + String(String), + Map(Vec<(K, Vec<Flag>)>), + Command(Vec<String>), } #[repr(C)] @@ -37,42 +54,42 @@ pub enum MapKind { } #[derive(Debug, PartialEq)] -pub struct MapAction { - pub action: Action, +pub struct MapAction<K: KeyCodeConvertible> { + pub action: Action<K>, pub kind: MapKind, } #[derive(Debug, PartialEq)] -struct Map { +struct Map<K: KeyCodeConvertible> { trigger: Trigger, - action: Action, + action: Action<K>, kind: MapKind, } -type MapCollection = HashMap<Trigger, MapAction>; +type MapCollection<K: KeyCodeConvertible> = HashMap<Trigger, MapAction<K>>; #[derive(Debug, PartialEq)] -struct Mode { +struct Mode<K: KeyCodeConvertible> { trigger: Trigger, - maps: MapCollection, + maps: MapCollection<K>, } #[derive(Debug, PartialEq)] -pub struct MapGroup { - pub maps: MapCollection, - pub modes: HashMap<Trigger, MapCollection>, +pub struct MapGroup<K: KeyCodeConvertible> { + pub maps: MapCollection<K>, + pub modes: HashMap<Trigger, MapCollection<K>>, } #[derive(Debug, PartialEq)] -enum Definition { - Map(Map), - Mode(Mode), +enum Definition<K: KeyCodeConvertible> { + Map(Map<K>), + Mode(Mode<K>), } -impl MapGroup { +impl<K: KeyCodeConvertible> MapGroup<K> { pub fn parse( mappings: &str - ) -> Result<MapGroup, CombineErrors<char, &str, SourcePosition>> { + ) -> Result<MapGroup<K>, CombineErrors<char, &str, SourcePosition>> { let input = State::new(mappings); map_group().easy_parse(input).map(|t| t.0) } @@ -117,12 +134,14 @@ where many1(headphone_button()) } -fn action<I>() -> impl Parser<Input = I, Output = Action> +fn action<I, K>() -> impl Parser<Input = I, Output = Action<K>> where I: Stream<Item = char>, I::Error: ParseError<I::Item, I::Range, I::Position>, + K: KeyCodeConvertible, { take_until(newline()) + .map(|action| Action::String(action)) } fn whitespace_separator<I>() -> impl Parser<Input = I> @@ -133,10 +152,11 @@ where skip_many1(space().or(tab())) } -fn map<I>() -> impl Parser<Input = I, Output = Map> +fn map<I, K>() -> impl Parser<Input = I, Output = Map<K>> where I: Stream<Item = char>, I::Error: ParseError<I::Item, I::Range, I::Position>, + K: KeyCodeConvertible, { ( map_kind(), @@ -153,14 +173,15 @@ where ) } -fn map_collection<I>() -> impl Parser<Input = I, Output = MapCollection> +fn map_collection<I, K>() -> impl Parser<Input = I, Output = MapCollection<K>> where I: Stream<Item = char>, I::Error: ParseError<I::Item, I::Range, I::Position>, + K: KeyCodeConvertible, { ( blank(), - many::<Vec<Map>, _>(map().skip(blank())), + many::<Vec<Map<K>>, _>(map().skip(blank())), ).map(|(_, collection)| { let mut maps = HashMap::new(); @@ -178,10 +199,11 @@ where }) } -fn mode<I>() -> impl Parser<Input = I, Output = Mode> +fn mode<I, K>() -> impl Parser<Input = I, Output = Mode<K>> where I: Stream<Item = char>, I::Error: ParseError<I::Item, I::Range, I::Position>, + K: KeyCodeConvertible, { ( string("mode"), @@ -199,10 +221,11 @@ where ) } -fn definitions<I>() -> impl Parser<Input = I, Output = Vec<Definition>> +fn definitions<I, K>() -> impl Parser<Input = I, Output = Vec<Definition<K>>> where I: Stream<Item = char>, I::Error: ParseError<I::Item, I::Range, I::Position>, + K: KeyCodeConvertible, { ( blank(), @@ -215,10 +238,11 @@ where ).map(|(_, definitions)| definitions) } -fn map_group<I>() -> impl Parser<Input = I, Output = MapGroup> +fn map_group<I, K>() -> impl Parser<Input = I, Output = MapGroup<K>> where I: Stream<Item = char>, I::Error: ParseError<I::Item, I::Range, I::Position>, + K: KeyCodeConvertible, { definitions() .map(|definitions| { |
