summaryrefslogtreecommitdiff
path: root/src/template.rs
blob: 67eaceb344917fe8637f7d09e4557ed0aec365a3 (plain) (blame)
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
use fancy_regex::*;


pub struct Template {
    pub tag: String,
    pub subtags: Vec<String>,
    pub expression: Regex,
}

impl Template {
    pub fn from_str(pattern: &str, tag: String) -> Self {
        let pattern = format!("^(?:{pattern})");
        let expression = Regex::new(&pattern).unwrap();

        if let Some((head, tail)) = tag.split_once('(') {
            if let Some(tail) = tail.strip_suffix(')') {
                let tag = head.trim().to_string();
                let subtags = tail.split(',').map(|t| t.trim().to_string()).collect();
                return Self { tag, subtags, expression };
            }
        }
        let subtags = Vec::new();
        return Self { tag, subtags, expression };
    }
}