diff options
| author | Teddy Wing | 2018-10-20 16:46:46 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-10-20 16:46:46 +0200 |
| commit | 2e79762f80e55c9f0897fafc0f5499060343217f (patch) | |
| tree | f58631214d1f2a9abddb9bd612a1736ccd6b19c3 | |
| parent | 56e02991a4577b99dbb5d1b73802b768bdaadd1b (diff) | |
| download | dome-key-map-2e79762f80e55c9f0897fafc0f5499060343217f.tar.bz2 | |
read_config_file(): Update to get the config from `config.toml`
Use XDG handling from `state_load_map_group()` to do basically the same
thing here for reading the config from a `config.toml` file.
Use 'error-chain' to handle the `Result`s more easily. No more nested
pattern matching, we'll just have to unwrap one `Result` and write the
error to stderr in an FFI function.
| -rw-r--r-- | src/config.rs | 19 | ||||
| -rw-r--r-- | src/errors.rs | 7 | ||||
| -rw-r--r-- | src/lib.rs | 4 |
3 files changed, 26 insertions, 4 deletions
diff --git a/src/config.rs b/src/config.rs index 9962ba0..1e75575 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,10 @@ +use std::fs; + use getopts::Options; use toml; +use xdg; + +use errors::*; type Milliseconds = u16; @@ -61,9 +66,15 @@ pub fn parse_args<'a>(args: &[String], config: &'a mut Config) -> &'a mut Config config } -// TODO: Get config file from .config/dome-key/config.toml -pub fn read_config_file() -> Config { - let config = toml::from_str("").unwrap(); +pub fn read_config_file() -> Result<Config> { + let xdg_dirs = xdg::BaseDirectories::with_prefix("dome-key")?; + let config_file = xdg_dirs.find_config_file("config.toml") + .chain_err(|| "config home path contains invalid unicode")?; + let config_str = fs::read_to_string(config_file) + .chain_err(|| "failed to read config file")?; - config + let config = toml::from_str(&config_str) + .chain_err(|| "failed to parse config file")?; + + Ok(config) } diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..beafc9f --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,7 @@ +use xdg; + +error_chain! { + foreign_links { + Xdg(xdg::BaseDirectoriesError); + } +} @@ -6,6 +6,9 @@ extern crate cocoa; #[macro_use] extern crate combine; extern crate core_graphics; + +#[macro_use] +extern crate error_chain; extern crate foreign_types; extern crate getopts; extern crate libc; @@ -25,6 +28,7 @@ extern crate xdg; mod autopilot_internal; mod cocoa_bridge; mod config; +mod errors; mod key_code; mod parser; |
