support_kit/
sources.rs

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
use convert_case::{Case, Casing};
use figment::{
    providers::{Env, Format, Json, Toml, Yaml},
    Figment, Provider,
};
use std::path::PathBuf;

#[derive(Clone, Debug, bon::Builder)]
pub struct Sources {
    #[builder(default, into)]
    name: crate::ServiceName,
    env: Option<crate::Environment>,
}

impl Sources {
    pub fn with_env(&self, env: crate::Environment) -> Self {
        Self::builder().name(self.name.clone()).env(env).build()
    }

    pub fn prefix(&self) -> Figment {
        let prefix = match self.env {
            Some(env) => format!(
                "{name}__{config_env}__",
                name = self.name.to_string().to_case(Case::UpperSnake),
                config_env = env.to_string().to_case(Case::UpperSnake)
            ),
            None => format!(
                "{name}__",
                name = self.name.to_string().to_case(Case::UpperSnake)
            ),
        };

        Figment::new().merge(Env::prefixed(&prefix).split("__"))
    }

    fn sources(&self) -> figment::Result<Figment> {
        let ext = self.env.map(|env| format!("{env}.")).unwrap_or_default();
        let mut figment = Figment::new();

        for path in canonical_paths() {
            let file = path.with_file_name(&self.name);

            let yaml = file.with_extension(format!("{ext}yaml"));
            if yaml.exists() {
                figment = figment.merge(Yaml::file(yaml))
            }

            let json = file.with_extension(format!("{ext}json"));
            if json.exists() {
                figment = figment.merge(Json::file(json))
            }

            let toml = file.with_extension(format!("{ext}toml"));
            if toml.exists() {
                figment = figment.merge(Toml::file(toml))
            }
        }

        Ok(figment)
    }
}

impl Provider for Sources {
    fn metadata(&self) -> figment::Metadata {
        Default::default()
    }

    fn data(
        &self,
    ) -> Result<figment::value::Map<figment::Profile, figment::value::Dict>, figment::Error> {
        self.sources()?.data()
    }
}

fn canonical_paths() -> Vec<PathBuf> {
    let mut paths = vec![PathBuf::new()];

    paths.extend(dirs::config_dir());
    paths.extend(dirs::home_dir());
    paths.reverse();

    paths
}