summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMario Krehl <mario-krehl@gmx.de>2017-12-09 21:11:26 +0100
committerMario Krehl <mario-krehl@gmx.de>2017-12-09 21:11:26 +0100
commit4bc2caadbfbb831dbaa7faf291f5e00fe1182583 (patch)
tree082e90369215f4345003f3ba7271134d7f000a96 /src
parentf31d9b16c91afd4bedc9a9dfe856e019561ed4f9 (diff)
Tests with Regexin main; refactoring of config handling code
Diffstat (limited to 'src')
-rw-r--r--src/config.rs92
-rw-r--r--src/error.rs4
-rw-r--r--src/main.rs45
3 files changed, 124 insertions, 17 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())
+ }
+}
+
diff --git a/src/error.rs b/src/error.rs
index 6258c10..af5af98 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -5,6 +5,10 @@ error_chain! {
description("configuration file could not be parsed"),
display("configuration file could not be parsed: '{}'", filename),
}
+ RegexParseError(regex: String) {
+ description("regex could not be parsed"),
+ display("regex not parsable: '{}'", regex),
+ }
}
}
diff --git a/src/main.rs b/src/main.rs
index 4a4e53d..1d4abe2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,18 +5,20 @@ extern crate clap;
extern crate error_chain;
#[macro_use]
extern crate log;
+extern crate regex;
#[macro_use]
extern crate serde_derive;
extern crate simplelog;
+use std::process::exit;
+use std::fs::File;
+
use clap::{App, Arg};
use simplelog::{SimpleLogger, LogLevelFilter, Config as LogConfig};
-use std::process::exit;
-
mod error;
mod config;
-use config::Config;
+use config::ConfigDeser;
fn main() {
let matches = App::new("aklog-server")
@@ -47,12 +49,43 @@ 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 ConfigDeser::load(String::from(config_file)) {
Ok(c) => c,
- Err(_) => {
- error!("Failed to open/parse configuration file: '{}'", config_file);
+ Err(e) => {
+ error!("{}", e);
exit(1);
},
};
+ let items = config.get_items();
+ let aliases : Vec<_> = items.clone().into_iter()
+ .map(|it| it.alias())
+ .collect();
+ let first_item = items.first().unwrap();
+
+ let mut file = File::open(first_item.file()).unwrap();
+ use std::io::BufReader;
+ use std::io::BufRead;
+ let mut bufreader = BufReader::new(file);
+ let mut line = String::new();
+ let cregex = match first_item.regex() {
+ Ok(r) => {
+ info!("regex parsed successfully");
+ r
+ },
+ Err(_) => exit(2),
+ };
+ let mut capturename_iter = cregex.capture_names().skip(1);
+ while let Some(Some(name)) = capturename_iter.next() {
+ println!("Named Capture: {}", name);
+ }
+ let mut line_iter = bufreader.lines();
+ while let Some(Ok(line)) = line_iter.next() {
+ if cregex.is_match(line.as_str()) {
+ println!("{}", line);
+ }
+ else {
+ println!("did not match");
+ }
+ }
}