summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 13dd0e4ba62697e2bf3a79a38329253709e2163c (plain) (blame)
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
use config::{File, Map};
use serde::{Deserialize, Serialize};
use tracing::{debug, trace};

#[derive(Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
    pub root: String,
    pub user: Vec<String>,
    pub directories: Vec<String>,
    pub custom_directories: Map<String, String>,
    pub device: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            root: "/mnt/backup".to_string(),
            user: vec![],
            directories: vec![],
            custom_directories: Map::new(),
            device: gethostname::gethostname()
                .into_string()
                .expect("invalid hostname string"),
        }
    }
}

impl Config {
    pub fn load() -> Result<Self, config::ConfigError> {
        debug!("load config");
        let config = config::Config::builder()
            .add_source(File::with_name("config.toml").required(false))
            .add_source(config::Environment::with_prefix("FXBAUP").separator("_"))
            .build()?;

        let cfg = config.try_deserialize();
        trace!(?cfg, "loaded config");

        cfg
    }
}