From 90e70a65caf968a51cd39da7a100fe451699212b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 2 Jan 2021 15:48:54 +0100 Subject: Cleanup config reading code Signed-off-by: Matthias Beyer --- src/config.rs | 33 ++++++++++++--------------------- src/error.rs | 5 +++-- src/main.rs | 3 ++- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/config.rs b/src/config.rs index 9f28158..a786b9e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,8 +3,7 @@ extern crate log; extern crate toml; -use std::fs::File; -use std::io::Read; +use std::path::PathBuf; use regex::Regex; use error::*; @@ -30,26 +29,19 @@ pub struct ConfigDeser { impl ConfigDeser { /// Tries to open, read and parse a toml-file - pub fn load(filename : String) -> Result { + pub fn load(path: PathBuf) -> Result { + debug!("configuration file name: '{}'", path.display()); - debug!("configuration file name: '{}'", filename); - - let mut file = File::open(filename.clone()) - .chain_err(|| "configuration file could not be opened")?; - debug!("configuration file successfully opened"); - - let mut content = String::new(); - file.read_to_string(&mut content) + let s = std::fs::read_to_string(&path) .chain_err(|| "configuration file could not be read")?; - debug!("configuration file successfully read"); - match toml::from_str(content.as_str()) { - Ok(config) => { + toml::from_str(&s) + .map(|obj| { info!("successfully parsed configuration file"); - Ok(config) - }, - _ => Err(ErrorKind::ConfigParseError(filename).into()), - } + debug!("Config = {:?}", obj); + obj + }) + .map_err(|_| ErrorKind::ConfigParseError(path).into()) } fn get_items(&self) -> &Vec { @@ -145,9 +137,8 @@ impl Config { /// Lets serde do the deserialization, and transforms the given data /// for later access - pub fn load(filename : String) -> Result { - - let conf_deser = ConfigDeser::load(filename)?; + pub fn load(path: PathBuf) -> Result { + let conf_deser = ConfigDeser::load(path)?; let mut l_items : Vec = Vec::new(); for lid in conf_deser.get_items() { diff --git a/src/error.rs b/src/error.rs index e9a25a7..5a8ef70 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,12 +1,13 @@ +use std::path::PathBuf; // Module for errors using error_chain // Might be expanded in the future error_chain! { errors { - ConfigParseError(filename: String) { + ConfigParseError(path: PathBuf) { description("configuration file could not be parsed"), - display("configuration file could not be parsed: '{}'", filename), + display("configuration file could not be parsed: '{}'", path.display()), } } } diff --git a/src/main.rs b/src/main.rs index df40cd3..f03eff4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ extern crate simplelog; use std::collections::HashMap; use std::fs::File; +use std::path::PathBuf; use std::io::{BufReader, BufRead}; use std::process::exit; use std::str::FromStr; @@ -254,7 +255,7 @@ fn main() { debug!("Initialized logger"); let config_file = matches.value_of("config").unwrap(); - let config = match Config::load(String::from(config_file)) { + let config = match Config::load(PathBuf::from(String::from(config_file))) { Ok(c) => c, Err(e) => { error!("{}", e); -- cgit v1.2.3