aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2018-09-01 17:50:13 +0200
committerTeddy Wing2018-09-01 17:50:13 +0200
commita432dd2824499959635ac9a7cabec25a31dddb14 (patch)
tree31074fd3d5e073eac63cf0956189202124056676 /src
parentc7e5b43191651716df737aea0baeaf4e75f75a33 (diff)
downloaddome-key-map-a432dd2824499959635ac9a7cabec25a31dddb14.tar.bz2
KeyActionResult: Make a pseudo-builder for the struct (WIP)
Make a builder for a new `KeyActionResult`, but without a new "builder" type (seems like I should be able to get away without one in this simple case). I had make `parser::Trigger` public because at first I was using them in `KeyActionResult` for the `in_mode` field. Oh yeah, there's a new `in_mode` field on `KeyActionResult`. We'll be using that to store and pass back the current mode to our FFI caller. This field can be used to call `c_run_key_action()` with that mode scope. Well, the idea is here, but I'm getting move errors: error[E0507]: cannot move out of borrowed content --> src/cocoa_bridge.rs:79:9 | 79 | *self | ^^^^^ cannot move out of borrowed content error[E0596]: cannot borrow immutable borrowed content as mutable --> src/cocoa_bridge.rs:162:30 | 162 | *KeyActionResult::new(MapKind::Map) | ______________________________^ 163 | | .with_action(&map.action) | |_________________________________________________________^ cannot borrow as mutable error[E0507]: cannot move out of borrowed content --> src/cocoa_bridge.rs:162:29 | 162 | / *KeyActionResult::new(MapKind::Map) 163 | | .with_action(&map.action) 164 | | .in_mode(trigger) | |_________________________________________________^ cannot move out of borrowed content Looks like I'll need to change the `KeyActionResult` impl code to pass by value instead of reference. Committing what I have now, though, because it's been a while and it's become kind of a mess.
Diffstat (limited to 'src')
-rw-r--r--src/cocoa_bridge.rs69
-rw-r--r--src/parser.rs2
2 files changed, 58 insertions, 13 deletions
diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs
index b4cc755..22fcc58 100644
--- a/src/cocoa_bridge.rs
+++ b/src/cocoa_bridge.rs
@@ -7,6 +7,7 @@ use std::slice;
use libc::{c_char, size_t};
use {HeadphoneButton, MapGroup, MapKind};
+use parser;
#[repr(C)]
struct renameMeMapGroup {
@@ -48,9 +49,35 @@ pub struct Trigger {
}
#[repr(C)]
-pub struct KeyActionResult {
+pub struct KeyActionResult<'a> {
pub action: Option<CString>,
pub kind: MapKind,
+ pub in_mode: Option<&'a [HeadphoneButton]>,
+}
+
+impl<'a> KeyActionResult<'a> {
+ fn new(kind: MapKind) -> Self {
+ KeyActionResult {
+ action: None,
+ kind: kind,
+ in_mode: None,
+ }
+ }
+
+ fn with_action(&mut self, action: &str) -> &Self {
+ let action = CString::new(action.clone()).unwrap();
+ self.action = Some(action);
+ self
+ }
+
+ fn in_mode(&mut self, mode: &'a [HeadphoneButton]) -> &Self {
+ self.in_mode = Some(mode);
+ self
+ }
+
+ fn build(&self) -> Self {
+ *self
+ }
}
#[repr(C)]
@@ -111,10 +138,10 @@ pub extern "C" fn c_run_key_action(
}
#[no_mangle]
-pub extern "C" fn run_key_action_for_mode(
- trigger: &[HeadphoneButton],
+pub extern "C" fn run_key_action_for_mode<'a>(
+ trigger: &'a [HeadphoneButton],
in_mode: Option<&[HeadphoneButton]>
-) -> Option<KeyActionResult> {
+) -> Option<KeyActionResult<'a>> {
let sample_maps = "map <up> k
map <down> j
map <play><down> works!
@@ -131,8 +158,17 @@ map <play><down> works!
if let Some(map) = mode.get(trigger) {
return match map.kind {
MapKind::Map => {
+ Some(
+ *KeyActionResult::new(MapKind::Map)
+ .with_action(&map.action)
+ .in_mode(trigger)
+ )
},
MapKind::Command => {
+ Some(
+ *KeyActionResult::new(MapKind::Command)
+ .in_mode(trigger)
+ )
},
}
}
@@ -147,16 +183,25 @@ map <play><down> works!
// let action = CStr::from_bytes_with_nul(x).unwrap();
let action = CString::new(map.action.clone()).unwrap();
- Some(KeyActionResult {
- action: Some(action),
- kind: MapKind::Map,
- })
+ Some(
+ KeyActionResult::new(MapKind::Map)
+ .with_action(&map.action)
+ .build()
+ )
+ // Some(KeyActionResult {
+ // action: Some(action),
+ // kind: MapKind::Map,
+ // })
},
MapKind::Command => {
- Some(KeyActionResult {
- action: None,
- kind: MapKind::Command,
- })
+ Some(
+ KeyActionResult::new(MapKind::Command)
+ .build()
+ )
+ // Some(KeyActionResult {
+ // action: None,
+ // kind: MapKind::Command,
+ // })
},
}
}
diff --git a/src/parser.rs b/src/parser.rs
index 1d9aa56..4a7fec8 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -20,7 +20,7 @@ pub enum HeadphoneButton {
Up,
Down,
}
-type Trigger = Vec<HeadphoneButton>;
+pub type Trigger = Vec<HeadphoneButton>;
type Action = String;
#[repr(C)]