From bae5bdae98491075b9fe8af9a0a92d825f9bca23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Thu, 25 Aug 2022 11:29:37 +0200 Subject: [PATCH] roezio/config: implement Config.new MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/config.rs | 13 ++++++++----- src/error.rs | 5 +++++ 2 files changed, 13 insertions(+), 5 deletions(-) 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()) + } } } }