summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 1933a70c68f6686c56b16d42b7c1b6db42f7ac8c (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
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
use crate::*;

use highlight::*;

use std::collections::HashMap;


pub struct Config {
    pub config: HashMap<String, String>,
    pub last_modified: Option<SystemTime>,
    pub languages: HashMap<String, usize>,
    pub highlighters: Vec<Highlighter>,
    pub root_redirects: Vec<String>,
}

impl Config {
    pub fn new() -> Self {
        Self {
            config: HashMap::new(),
            last_modified: None,
            languages: HashMap::new(),
            highlighters: Vec::new(),
            root_redirects: Vec::new(),
        }
    }

    pub fn get(&self, key: &str) -> String {
        match self.config.get(key) {
            Some(value) => value.to_owned(),
            None => String::new(),
        }
    }

    pub fn parse_file(&mut self, content: &str, last_modified: Option<SystemTime>) {
        if self.last_modified.is_none() || self.last_modified > last_modified {
            self.last_modified = last_modified;
        }

        let mut key = None;
        let mut value = String::new();
        macro_rules! bank_value {
            () => {
                value = value.trim().to_string();
                if let Some(key) = key {
                    if key == "highlighters" {
                        self.parse_highlighters(&value);
                    }
                    self.config.insert(key, std::mem::take(&mut value));
                }
            };
        }
        for line in content.lines() {
            if line.starts_with(" ") || line.trim().is_empty() {
                value.push_str(line.trim());
                value.push('\n');
            } else {
                bank_value!();
                key = Some(line.trim().to_lowercase().to_string());
            }
        }
        bank_value!();
    }

    fn parse_highlighters(&mut self, value: &str) {
        let mut languages = Vec::new();
        let mut source = String::new();
        macro_rules! bank_sources {
            () => {
                if !languages.is_empty() {
                    let i = self.highlighters.len();
                    for language in std::mem::take(&mut languages) {
                        self.languages.insert(language, i);
                    }
                    let highlighter = Highlighter::from_str(&std::mem::take(&mut source));
                    self.highlighters.push(highlighter);
                }
            };
        }
        for line in value.lines() {
            if let Some(line) = line.trim().strip_prefix('[') {
                if let Some(line) = line.strip_suffix(']') {
                    bank_sources!();
                    languages = line.split('/').map(|s| s.trim().to_string()).collect();
                    continue;
                }
            }
            source.push_str(line);
            source.push('\n');
        }
        bank_sources!();
    }
}