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
use ini::Ini;
use std::collections::HashMap;
use std::fs;
use std::str::FromStr;

pub struct EditorConfig {
    pub config: HashMap<String, HashMap<String, String>>,
}

impl EditorConfig {
    pub fn from_file(file_path: &str) -> Result<Self, String> {
        let contents: String = fs::read_to_string(file_path).map_err(|e| e.to_string())?;
        EditorConfig::from_str(&contents)
    }
    pub fn get_property(&self, section: &str, property: &str) -> Option<&String> {
        self.config
            .get(section)
            .and_then(|props| props.get(property))
    }
}

impl FromStr for EditorConfig {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parsed = Ini::load_from_str(s).map_err(|e| e.to_string())?;
        let mut config = HashMap::new();
        for (section, properties) in parsed.iter() {
            let section_name = section.as_ref().map_or("*".to_string(), |s| s.to_string());
            let section_props = properties
                .iter()
                .map(|(key, value)| (key.to_string(), value.to_string()))
                .collect();
            config.insert(section_name, section_props);
        }

        Ok(EditorConfig { config })
    }
}