From f276e2f66b389a96f7d0981feefb79f6c8257d25 Mon Sep 17 00:00:00 2001 From: Mario Krehl Date: Sun, 10 Dec 2017 18:41:57 +0100 Subject: Document and put some newlines for readability in config.rs --- src/config.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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 { + 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 { + 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 = 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 = 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, all_aliases : Vec, @@ -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 { + let conf_deser = ConfigDeser::load(filename)?; + let mut l_items : Vec = 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 = 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 }) } -- cgit v1.2.3