aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 86ab51aba7bc2a058640d4dbc680610db0ee8fe6 (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
use std::fs::File;
use std::io::Read;
use dirs;
use toml;

use errors::ConfigError;

const CONFIG_NAME: &str = "almanac.toml";

#[derive(Deserialize)]
pub struct Config {
    pub cals: Vec<String>,
    pub period: String,
}

impl Config {
    pub fn new() -> Config {
        Config {
            cals: vec![],
            period: "".to_string(),
        }
    }

    pub fn parse() -> Result<Config, ConfigError> {
        let config_path = match dirs::config_dir() {
            Some(path) => path,
            None => return Err(ConfigError::MissingPath),
        }.join(CONFIG_NAME);

        let mut file = File::open(&config_path)?;
        let mut toml_str = String::new();
        file.read_to_string(&mut toml_str)?;

        Ok(toml::from_str(&toml_str)?)
    }
}