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
use merge_yaml_hash::MergeYamlHash;
use yaml_rust::yaml::Hash;
use yaml_rust::yaml::Yaml;

pub struct Config(Hash);

pub fn load() -> Config {
    let mut hash = MergeYamlHash::new();

    hash.merge(include_str!("config/default.yml"));
    // hash.merge("cherry:\n  sweet: 1\n  tart: 2");

    Config(hash.data)
}

impl Config {
    pub fn get_array(&self, cop: &str, key: &str) -> Vec<String> {
        let cop_config = &self.0[&Yaml::String(cop.to_string())];
        let mut output: Vec<String> = vec![];

        match &cop_config[key] {
            Yaml::Array(array) => {
                for item in array {
                    if let Yaml::String(string) = item {
                        output.push(string.clone());
                    } else {
                        panic!("item has to be string");
                    }
                }
            }
            Yaml::BadValue => {
                return self.get_array("AllCops", key);
            }
            _ => (),
        }

        output
    }

    pub fn get_string(&self, cop: &str, key: &str) -> String {
        let cop_config = &self.0[&Yaml::String(cop.to_string())];

        if let Yaml::String(value) = &cop_config[key] {
            return value.clone();
        } else {
            panic!("Enabled field not found");
        }
    }

    pub fn is_enabled(&self, cop: &str) -> bool {
        let cop_config = &self.0[&Yaml::String(cop.to_string())];
        if let Yaml::Boolean(enabled) = &cop_config["Enabled"] {
            return *enabled;
        } else {
            panic!("Enabled field not found");
        }
    }
}

#[cfg(test)]
mod tests {
    use super::load;

    #[test]
    fn test_get_array() {
        let config = load();

        assert_eq!(
            config.get_array("Bundler/DuplicatedGem", "Include"),
            vec![
                String::from("**/*.gemfile"),
                String::from("**/Gemfile"),
                String::from("**/gems.rb")
            ]
        );
    }

    #[test]
    fn test_is_enabled() {
        let config = load();

        assert!(config.is_enabled("Bundler/DuplicatedGem"));
        assert!(!config.is_enabled("Bundler/GemComment"));
    }
}