summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMario Krehl <mario-krehl@gmx.de>2017-12-10 18:41:57 +0100
committerMario Krehl <mario-krehl@gmx.de>2017-12-10 18:41:57 +0100
commitf276e2f66b389a96f7d0981feefb79f6c8257d25 (patch)
treee087e596a6ec31583df435cd27428eec1226e91a
parentc84f290db91ed1d18cecd2709782f2bc539bb90e (diff)
Document and put some newlines for readability in config.rs
-rw-r--r--src/config.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
index c004493..ef0e330 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -28,15 +28,21 @@ pub struct ConfigDeser {
}
impl ConfigDeser {
+
+ /// Tries to open, read and parse a toml-file
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")?;
debug!("configuration file successfully opened");
+
let mut content = String::new();
file.read_to_string(&mut content)
.chain_err(|| "configuration file could not be read")?;
debug!("configuration file successfully read");
+
match toml::from_str(content.as_str()) {
Ok(config) => {
info!("successfully parsed configuration file");
@@ -55,6 +61,9 @@ impl ConfigDeser {
// struct to access data later on //
//------------------------------------//
+/// The deserialized Item would nearly always require some operation on its
+/// contents to use it, so we do those operations beforehand and only access
+/// the useful data from main().
pub struct LogItem {
file : String,
regex : Regex,
@@ -65,10 +74,13 @@ pub struct LogItem {
impl LogItem {
+ /// Transforms a LogItemDeser into a more immediately usable 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))?;
+
// first capture is the whole match and nameless
// second capture is always the timestamp
let cnames : Vec<String> = l_regex
@@ -78,6 +90,10 @@ impl LogItem {
.map(|n| String::from(n))
.collect();
debug!("capture names: {:?}", cnames);
+
+ // The metric seen by grafana will be `alias.capturegroup_name`
+ // One Regex may contain multiple named capture groups, so a vector
+ // with all names is prepared here.
let mut als : Vec<String> = Vec::new();
for name in cnames.clone() {
let mut temp = String::from(lid.alias.as_str());
@@ -119,6 +135,7 @@ impl LogItem {
}
}
+/// Containts more immediately usable data
pub struct Config {
items : Vec<LogItem>,
all_aliases : Vec<String>,
@@ -126,18 +143,25 @@ pub struct Config {
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)?;
+
let mut l_items : Vec<LogItem> = Vec::new();
for lid in conf_deser.get_items() {
l_items.push(LogItem::from_log_item_deser((*lid).clone())?);
}
+
+ // combines all aliases into one Vec for the /search endpoint
let mut all_als : Vec<String> = Vec::new();
for li in &l_items {
for als in li.aliases() {
all_als.push((*als).clone());
}
}
+
Ok(Config { items: l_items, all_aliases : all_als })
}