diff options
author | Mario Krehl <mario-krehl@gmx.de> | 2017-12-09 21:11:26 +0100 |
---|---|---|
committer | Mario Krehl <mario-krehl@gmx.de> | 2017-12-09 21:11:26 +0100 |
commit | 4bc2caadbfbb831dbaa7faf291f5e00fe1182583 (patch) | |
tree | 082e90369215f4345003f3ba7271134d7f000a96 /src/config.rs | |
parent | f31d9b16c91afd4bedc9a9dfe856e019561ed4f9 (diff) |
Tests with Regexin main; refactoring of config handling code
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 92 |
1 files changed, 81 insertions, 11 deletions
diff --git a/src/config.rs b/src/config.rs index 5d1885c..635be32 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,24 +5,43 @@ extern crate toml; use std::fs::File; use std::io::Read; +use regex::Regex; use error::*; -/// Main Config struct -#[derive(Debug, Deserialize)] -pub struct Config { - item : Vec<Item>, -} +//------------------------------------// +// structs for deserialization // +//------------------------------------// -/// Holds data for one antikoerper Log-File -#[derive(Debug, Deserialize)] -struct Item { +/// Holds data for one antikoerper Log-File. +/// Used for deserialization only +#[derive(Clone, Debug, Deserialize)] +pub struct LogItemDeser { file : String, regex : String, alias : String, } -impl Config { - pub fn load(filename : String) -> Result<Config> { +impl LogItemDeser { + pub fn file(&self) -> String { + self.file.clone() + } + pub fn regex(&self) -> Result<Regex> { + debug!("trying to parse regex `{}`", self.regex); + Regex::new(self.regex.as_str()).chain_err(|| format!("failed to parse regex `{}`", self.regex)) + } + pub fn alias(&self) -> String { + self.alias.clone() + } +} + +/// Used for deserialization only +#[derive(Debug, Deserialize)] +pub struct ConfigDeser { + item : Vec<LogItemDeser>, +} + +impl ConfigDeser { + pub fn load(filename : String) -> Result<ConfigDeser> { debug!("configuration file name: '{}'", filename); let mut file = File::open(filename.clone()) .chain_err(|| "configuration file could not be opened")?; @@ -33,10 +52,61 @@ impl Config { debug!("configuration file successfully read"); match toml::from_str(content.as_str()) { Ok(config) => { - debug!("configuration file successfully parsed"); + info!("successfully parsed configuration file"); Ok(config) }, _ => Err(ErrorKind::ConfigParseError(filename).into()), } } + + pub fn get_items(&self) -> Vec<LogItemDeser> { + self.item.clone() + } +} + +//------------------------------------// +// struct to access data later on // +//------------------------------------// + +pub struct LogItem { + file : String, + regex : Regex, + aliases : Vec<String>, +} + +impl LogItem { + fn from_log_item_deser(lid : LogItemDeser) -> Result<LogItem> { + debug!("trying to parse regex `{}`", lid.regex); + let l_regex = Regex::new(lid.regex.as_str()) + .chain_err(|| format!("regex not parseable: '{}'", lid.regex))?; + let cnames : Vec<String> = l_regex + .capture_names() + .skip(2) + .filter_map(|n| n) + .map(|n| String::from(n)) + .collect(); + debug!("capture names: {:?}", cnames); + let mut als : Vec<String> = Vec::new(); + for name in cnames { + let mut temp = String::from(lid.alias.as_str()); + temp.push('.'); + temp.push_str(name.as_str()); + als.push(temp); + } + debug!("aliases: {:?}", als); + + Ok(LogItem { file : lid.file, regex : l_regex, aliases : als }) + } } + +pub struct Config { + items : Vec<LogItem>, + all_aliases : Vec<String>, +} + +impl Config { + pub fn load(filename : String) -> Result<Self> { + Err(ErrorKind::ConfigParseError(String::from("meh")).into()) + } +} + |