summaryrefslogtreecommitdiff
path: root/src/cfg/config.rs
blob: ee4e3b759e27066f75384b9e7d502a5036b98842 (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
use std::fs;
use std::env;
use serde::{Deserialize, Serialize};
use toml;

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Loglevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error
}

#[derive(Deserialize, Debug, Serialize)]
pub struct Config {
    repo:     Repo,
    log:      Log,
    paths:    Paths,
}

#[derive(Deserialize, Debug, Serialize)]
pub struct Log {
    logfile: String,
    loglevel: Loglevel,
}

#[derive(Deserialize, Debug, Serialize)]
pub struct Repo {
    #[serde(rename = "repo_url")] 
    repo_url: String,
    #[serde(rename = "auto_update")] 
    auto_update: bool,
}

#[derive(Deserialize, Debug, Serialize)]
pub struct Paths {
    #[serde(rename = "cachedir")] 
    cachedir: String,
    #[serde(rename = "builddir")] 
    builddir: String,
}

impl Config {
    pub fn parse() -> Result<Config, toml::de::Error> {
        let contents = fs::read_to_string("/etc/mesk.toml").unwrap();
        let result: Config = toml::from_str(&contents)?;
        Ok(result)
    }


    pub fn default() -> Result<String, toml::ser::Error> {
        let default: Config = Config {
            repo: Repo {
                repo_url: format!("https://mesk.anthrill.i2p/repo/{}/",
                                       std::env::consts::ARCH), 
                auto_update: true,
            },
            log: Log {
                logfile: String::from("/var/log/mesk.log"),
                loglevel: Loglevel::Info,
            },
            paths: Paths {
                cachedir: String::from("/var/cache/mesk"),
                builddir: String::from("/var/lib/mesk"),
            },
        };

        let toml_str = toml::to_string(&default)?;
        Ok(toml_str)
    }
}