summaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 9441973ecb1fcc780ab3f0307fb3761902567d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Module for parsing/accessing the configuration

extern crate log;
extern crate toml;

use std::path::PathBuf;
use regex::Regex;
use error::*;

//------------------------------------//
//  structs for deserialization       //
//------------------------------------//

/// Holds data for one Log-File.
/// Used for deserialization only
#[derive(Clone, Debug, Deserialize)]
pub struct LogItemDeser {
    file : String,
    regex : String,
    alias : String,
}

/// Used for deserialization only
#[derive(Debug, Deserialize)]
pub struct ConfigDeser {
    item : Vec<LogItemDeser>,
}

impl ConfigDeser {

    /// Tries to open, read and parse a toml-file
    pub fn load(path: PathBuf) -> Result<ConfigDeser> {
        debug!("configuration file name: '{}'", path.display());

        let s = std::fs::read_to_string(&path)
            .chain_err(|| "configuration file could not be read")?;

        toml::from_str(&s)
            .map(|obj| {
                info!("successfully parsed configuration file");
                debug!("Config = {:?}", obj);
                obj
            })
            .map_err(|_| ErrorKind::ConfigParseError(path).into())
    }

    fn get_items(&self) -> &Vec<LogItemDeser> {
        &self.item
    }
}

//------------------------------------//
//  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,
    alias : String,
    capture_names : Vec<String>,
    aliases : Vec<String>,
}

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
            .capture_names()
            .skip(2)
            .filter_map(|n| n)
            .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.iter() {
            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,
                alias: lid.alias,
                capture_names : cnames,
                aliases : als
            }
        )
    }

    pub fn file(&self) -> &String {
        &self.file
    }

    pub fn regex(&self) -> &Regex {
        &self.regex
    }

    pub fn alias(&self) -> &String {
        &self.alias
    }

    pub fn capture_names(&self) -> &Vec<String> {
        &self.capture_names
    }

    pub fn aliases(&self) -> &Vec<String> {
        &self.aliases
    }
}

/// Contains more immediately usable data
pub struct Config {
    items : Vec<LogItem>,
    all_aliases : Vec<String>,
}

impl Config {

    /// Lets serde do the deserialization, and transforms the given data
    /// for later access
    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() {
            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 })
    }

    pub fn items(&self) -> &Vec<LogItem> {
        &self.items
    }

    pub fn all_aliases(&self) -> &Vec<String> {
        &self.all_aliases
    }
}