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
use anyhow::{anyhow, Context as ErrContext, Result};
use config::Config;
use serde::{Deserialize, Serialize};
use std::{env, path::PathBuf};

use super::config::GlobalConfig;

pub trait Context<'a, Cfg>: Send
where
    Cfg: Serialize + Deserialize<'a> + Default,
{
    fn config_file_name(&self) -> String {
        "Beaker.toml".to_string()
    }

    fn config_file_path(&self) -> Result<PathBuf> {
        let curr = env::current_dir()?;
        let it = curr.ancestors();

        for p in it {
            let p = p.join(self.config_file_name());
            if p.exists() {
                return Ok(p);
            }
        }

        Err(anyhow!(
            "Config file `{}` not found in all the ancestor paths",
            self.config_file_name()
        ))
    }

    fn root(&self) -> Result<PathBuf> {
        self.config_file_path()?
            .parent()
            .map(|p| p.to_path_buf())
            .ok_or_else(|| anyhow!("Already at root dir"))
    }

    fn config(&self) -> Result<Cfg> {
        let conf = Config::builder().add_source(Config::try_from(&Cfg::default())?);
        let conf = match self.config_file_path() {
            Ok(path) => conf.add_source(config::File::from(path)),
            _ => conf,
        };
        conf.build()?
            .try_deserialize::<Cfg>()
            .with_context(|| "Unable to deserialize configuration.")
    }

    fn global_config(&self) -> Result<GlobalConfig> {
        let conf = Config::builder().add_source(Config::try_from(&GlobalConfig::default())?);
        let conf = match self.config_file_path() {
            Ok(path) => conf.add_source(config::File::from(path)),
            _ => conf,
        };
        conf.build()?
            .try_deserialize::<GlobalConfig>()
            .with_context(|| "Unable to deserialize configuration.")
    }
}