diff options
Diffstat (limited to 'src/section.rs')
| -rw-r--r-- | src/section.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/section.rs b/src/section.rs new file mode 100644 index 0000000..7e492b1 --- /dev/null +++ b/src/section.rs | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | use super::regex::Regex; | ||
| 2 | use std::collections::HashMap; | ||
| 3 | |||
| 4 | pub struct Section { | ||
| 5 | pub name: String, | ||
| 6 | pub re: Regex, | ||
| 7 | pub keys: Vec<String>, | ||
| 8 | pub data: HashMap<String, String>, | ||
| 9 | } | ||
| 10 | |||
| 11 | impl Section { | ||
| 12 | pub fn new(name: &str, re: &str, groups: Vec<&'static str>) -> Self { | ||
| 13 | Section { | ||
| 14 | name: name.into(), | ||
| 15 | re: Regex::new(re).unwrap(), | ||
| 16 | keys: groups.into_iter().map(|s| s.into()).collect(), | ||
| 17 | data: HashMap::new(), | ||
| 18 | } | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | pub fn process(d: &str, s: &mut HashMap<String, Section>) { | ||
| 23 | for (_, section) in s { | ||
| 24 | for m in section.re.captures_iter(d) { | ||
| 25 | assert!(m.len() >= section.keys.len() + 1); | ||
| 26 | |||
| 27 | let mut idx = 0; | ||
| 28 | for key in §ion.keys { | ||
| 29 | section.data.insert(key.clone(), m.at(idx + 1).unwrap().into()); | ||
| 30 | idx += 1; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
