aboutsummaryrefslogtreecommitdiff
path: root/src/load_config.rs
blob: 763b6357bc0a8fb03bfe2b076cea3c24d46a77eb (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::env;
use std::{fs::File, io::Error, io::Read};
use yaml_rust::{yaml, YamlLoader};

pub struct Time {
    pub format: String,
    pub icon: String,
}

pub struct Memory {
    pub icon: String,
    pub enabled: bool,
}

pub struct Disk {
    pub icon: String,
    pub enabled: bool,
}

pub struct Volume {
    pub icon: String,
    pub enabled: bool,
}

pub struct Settings {
    pub seperator: String,
    pub time: Time,
    pub memory: Memory,
    pub disk: Disk,
    pub volume: Volume,
}

pub fn load() -> Result<Settings, Error> {
    let yml_source = env::var("HOME").unwrap() + "/.config/rsblocks/rsblocks.yml";
    let mut data = String::new();
    let mut file = match File::open(yml_source) {
        Ok(file) => file,
        Err(_) => {
            println!("~/.config/rsblocks/rsblocks.yml file not found, loading defaults!");
            return Ok(load_defaults());
        }
    };
    file.read_to_string(&mut data)?;

    let yml_content = &YamlLoader::load_from_str(&data).unwrap()[0];
    let settings = gen_settings(yml_content);
    Ok(settings)
}

fn gen_settings(doc: &yaml::Yaml) -> Settings {
    // setting icons
    let seperator = get_or_set_string(doc, "general", "seperator", "|");
    let time_icon = get_or_set_string(doc, "time", "icon", "");
    let time_format = get_or_set_string(doc, "time", "format", "%T");
    let mem_icon = get_or_set_string(doc, "memory", "icon", "");
    let disk_icon = get_or_set_string(doc, "disk", "icon", "");
    let volume_icon = get_or_set_string(doc, "volume", "icon", "");

    // setting enable status, everything false by default
    let disk_enabled = get_or_set_bool(doc, "disk", "enable");
    let memory_enabled = get_or_set_bool(doc, "memory", "enable");
    let volume_enabled = get_or_set_bool(doc, "volume", "enable");

    Settings {
        seperator,
        time: Time {
            format: time_format,
            icon: time_icon,
        },
        memory: Memory {
            icon: mem_icon,
            enabled: memory_enabled,
        },
        disk: Disk {
            icon: disk_icon,
            enabled: disk_enabled,
        },
        volume: Volume {
            icon: volume_icon,
            enabled: volume_enabled,
        },
    }
}

// getting the bool value from rsblocks.yml file or set it false if it does not exist
fn get_or_set_bool(doc: &yaml::Yaml, parent: &str, child: &str) -> bool {
    let val: bool;
    if doc[parent][child].is_badvalue() {
        val = false;
    } else {
        val = doc[parent][child].as_bool().unwrap()
    }
    val
}

// getting the value from the rsblocks.yml file or set the default in the last parameter
fn get_or_set_string(doc: &yaml::Yaml, parent: &str, child: &str, default_val: &str) -> String {
    let val: String;
    if doc[parent][child].is_badvalue() {
        val = String::from(default_val)
    } else {
        val = String::from(doc[parent][child].as_str().unwrap());
    }

    val
}

fn load_defaults() -> Settings {
    Settings {
        seperator: String::from("|"),
        time: Time {
            format: String::from("%T"),
            icon: String::from(""),
        },
        memory: Memory {
            icon: String::from(""),
            enabled: false,
        },
        disk: Disk {
            icon: String::from(""),
            enabled: false,
        },
        volume: Volume {
            icon: String::from(""),
            enabled: false,
        },
    }
}