diff --git a/src/config.rs b/src/config.rs index 0c5bf7b1..d3135b58 100644 --- a/src/config.rs +++ b/src/config.rs @@ -92,15 +92,18 @@ impl<'a> Config<'a> { filename: P, defaults: HashMap<&'a str, HashMap<&'a str, ConfigValue>>, default_section: Option<&'a str>, - ) -> Self { - // TODO: read ini file. + ) -> Result { + let filename: PathBuf = filename.into(); + let mut ini = Ini::new(); + ini.load(filename.clone()) + .map_err(|_| Error::UnableToOpenConfigFile(filename.clone()))?; - Config { - filename: filename.into(), + Ok(Config { + filename, defaults, default_section: default_section.unwrap_or("default"), ini: Ini::new(), - } + }) } /// Sets a key/value pair in memory and updates the config file. diff --git a/src/error.rs b/src/error.rs index bc16f1bc..a9baf6fe 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,6 +18,7 @@ use crate::config::ConfigValue; use std::error::Error as StdError; use std::fmt; use std::io; +use std::path::PathBuf; #[derive(Debug)] pub(crate) enum Error { @@ -25,6 +26,7 @@ pub(crate) enum Error { UnableToCreateConfigDir, InvalidConfigValueType(ConfigValue), InvalidValueType(String), + UnableToOpenConfigFile(PathBuf), } impl fmt::Display for Error { @@ -34,6 +36,9 @@ impl fmt::Display for Error { Error::UnableToCreateConfigDir => write!(f, "Unable to create config dir"), Error::InvalidConfigValueType(err) => write!(f, "Invalid ConfigValue type: {}", err), Error::InvalidValueType(err) => write!(f, "Invalid value type: {}", err), + Error::UnableToOpenConfigFile(err) => { + write!(f, "Unable to open config file: {}", err.display()) + } } } }