aboutsummaryrefslogtreecommitdiff
path: root/src/section.rs
blob: 48f237d5ca16bea073a8a47f9f9938181a2dc805 (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
use super::regex::Regex;
use std::collections::HashMap;

#[derive(Debug)]
pub struct Section {
    pub name: String,
    pub re: Regex,
    pub keys: Vec<String>,
    pub data: HashMap<String, String>,
}

impl Section {
    pub fn new(name: &str, re: &str, groups: Vec<&'static str>) -> Self {
        Section {
            name: name.into(),
            re: Regex::new(re).unwrap(),
            keys: groups.into_iter().map(|s| s.into()).collect(),
            data: HashMap::new(),
        }
    }
}

pub fn process(d: &str, s: &mut HashMap<String, Section>) {
    for (_, section) in s {
        for m in section.re.captures_iter(d) {
            assert!(m.len() >= section.keys.len() + 1);

            let mut idx = 0;
            for key in &section.keys {
                section.data.insert(key.clone(), m.at(idx + 1).unwrap().into());
                idx += 1;
            }
        }
    }
}