aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-10-19 04:11:54 +0200
committerTeddy Wing2018-10-19 04:11:54 +0200
commit086ff7801a73464b06a9629ff5dc0df84a5e8e52 (patch)
tree0c304a6ef2ce3faae50572e7e655a86f4f169e41
parentedba0cc4c81cc91db470fb9604edb6f283c16b88 (diff)
downloaddome-key-map-086ff7801a73464b06a9629ff5dc0df84a5e8e52.tar.bz2
config: Add a function to read a config file (WIP)
A new function that will read a TOML config file. Doesn't currently access the filesystem. This commit is mostly supporting code to allow that to work. * Add the 'toml' crate * `parse_args()`: Take a mutable `Config` and return that config instead of creating a new one. The recipe will now be to read the config from a file first and then pass that config to this function to be updated. * Modify `c_parse_args()` to take a `*Config` and mirror the new method signature of `parse_args()`
-rw-r--r--Cargo.toml1
-rw-r--r--dome_key_map.h2
-rw-r--r--src/cocoa_bridge.rs10
-rw-r--r--src/config.rs13
-rw-r--r--src/lib.rs1
5 files changed, 21 insertions, 6 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 45b5883..17a080b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,6 +15,7 @@ objc = "0.2.5"
serde = "1.0.58"
serde_derive = "1.0.58"
stderrlog = "0.4.1"
+toml = "0.4.8"
xdg = "2.1.0"
[build-dependencies]
diff --git a/dome_key_map.h b/dome_key_map.h
index 2a73d8f..224cc21 100644
--- a/dome_key_map.h
+++ b/dome_key_map.h
@@ -36,7 +36,7 @@ typedef struct {
size_t length;
} Trigger;
-Config *c_parse_args(const char *const *args, size_t length);
+Config *c_parse_args(const char *const *args, size_t length, Config *config_ptr);
void c_run_key_action(State *state, Trigger trigger, const Trigger *mode);
diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs
index 37654ea..ba7b432 100644
--- a/src/cocoa_bridge.rs
+++ b/src/cocoa_bridge.rs
@@ -302,6 +302,7 @@ fn run_action(map_action: &MapAction) {
pub extern "C" fn c_parse_args(
args: *const *const c_char,
length: size_t,
+ config_ptr: *mut Config
) -> *mut Config {
let args = unsafe {
assert!(!args.is_null());
@@ -320,9 +321,14 @@ pub extern "C" fn c_parse_args(
.collect::<Vec<String>>()
};
- let config = config::parse_args(&args);
+ let config = unsafe {
+ assert!(!config_ptr.is_null());
- Box::into_raw(Box::new(config))
+ &mut *config_ptr
+ };
+ let config = config::parse_args(&args, config);
+
+ config_ptr
}
#[no_mangle]
diff --git a/src/config.rs b/src/config.rs
index 22f37b6..9962ba0 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,4 +1,5 @@
use getopts::Options;
+use toml;
type Milliseconds = u16;
@@ -11,6 +12,7 @@ struct Args {
#[repr(C)]
#[derive(Deserialize)]
+#[serde(default)]
pub struct Config {
#[serde(skip)]
args: Args,
@@ -31,7 +33,7 @@ fn print_usage(opts: Options) {
print!("{}", opts.usage(&brief));
}
-pub fn parse_args(args: &[String]) -> Config {
+pub fn parse_args<'a>(args: &[String], config: &'a mut Config) -> &'a mut Config {
let mut opts = Options::new();
opts.optflag("d", "daemon", "run the daemon in the current shell");
@@ -43,8 +45,6 @@ pub fn parse_args(args: &[String]) -> Config {
Err(e) => panic!(e),
};
- let mut config = Config::default();
-
if matches.opt_present("h") {
print_usage(opts);
return config;
@@ -60,3 +60,10 @@ pub fn parse_args(args: &[String]) -> Config {
config
}
+
+// TODO: Get config file from .config/dome-key/config.toml
+pub fn read_config_file() -> Config {
+ let config = toml::from_str("").unwrap();
+
+ config
+}
diff --git a/src/lib.rs b/src/lib.rs
index 300cc66..d5af695 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,6 +19,7 @@ extern crate objc;
#[macro_use]
extern crate serde_derive;
extern crate stderrlog;
+extern crate toml;
extern crate xdg;
mod autopilot_internal;