aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-10-20 17:40:20 +0200
committerTeddy Wing2018-10-20 17:40:20 +0200
commit4e90fd77eb0ef56a39c2af6fae068fb91782fd0d (patch)
treef17e39491f8d5aed317b79dbce067e29aff1f65d
parent2e79762f80e55c9f0897fafc0f5499060343217f (diff)
downloaddome-key-map-4e90fd77eb0ef56a39c2af6fae068fb91782fd0d.tar.bz2
read_config_file(): Return a default config if no file found
The only config option we have is `timeout`, which has a default value. Any future config options, it seems, should have default values also. We shouldn't force users to create a config file, as the prior code would do. They should be able to rely on the default, and create a `config.toml` only if they want to change it. If no config file is found, we should create a default `Config`. Rename the method because now it still works without reading the config file.
-rw-r--r--src/config.rs19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/config.rs b/src/config.rs
index 1e75575..750b47a 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -66,15 +66,18 @@ pub fn parse_args<'a>(args: &[String], config: &'a mut Config) -> &'a mut Config
config
}
-pub fn read_config_file() -> Result<Config> {
+pub fn get_config() -> 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")?;
-
- let config = toml::from_str(&config_str)
- .chain_err(|| "failed to parse config file")?;
+ let config = match xdg_dirs.find_config_file("config.toml") {
+ Some(config_file) => {
+ let config_str = fs::read_to_string(config_file)
+ .chain_err(|| "failed to read config file")?;
+
+ toml::from_str(&config_str)
+ .chain_err(|| "failed to parse config file")?
+ },
+ None => Config::default(),
+ };
Ok(config)
}