From 4e90fd77eb0ef56a39c2af6fae068fb91782fd0d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 20 Oct 2018 17:40:20 +0200 Subject: 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. --- src/config.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src') 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 { +pub fn get_config() -> Result { 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) } -- cgit v1.2.3