summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-01-02 15:48:54 +0100
committerMatthias Beyer <mail@beyermatthias.de>2021-01-02 15:48:54 +0100
commit90e70a65caf968a51cd39da7a100fe451699212b (patch)
tree97daaa8715215c261672711f9d882d776b4c6a67
parentb530bd482b611b368acdf137b18f6642e8b25631 (diff)
Cleanup config reading code
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--src/config.rs33
-rw-r--r--src/error.rs5
-rw-r--r--src/main.rs3
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<ConfigDeser> {
+ pub fn load(path: PathBuf) -> Result<ConfigDeser> {
+ 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<LogItemDeser> {
@@ -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<Self> {
-
- let conf_deser = ConfigDeser::load(filename)?;
+ pub fn load(path: PathBuf) -> Result<Self> {
+ let conf_deser = ConfigDeser::load(path)?;
let mut l_items : Vec<LogItem> = 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);