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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::path::PathBuf;

use crate::env;

#[derive(Debug, Clone, PartialEq)]
pub struct HydroSettings {
    pub root_path: Option<PathBuf>,
    pub settings_file: Option<PathBuf>,
    pub secrets_file: Option<PathBuf>,
    pub env: String,
    pub envvar_prefix: String,
    pub encoding: String,
    pub envvar_nested_sep: String,
}

impl Default for HydroSettings {
    fn default() -> Self {
        let hydro_suffix = "_FOR_HYDRO";
        Self {
            root_path: env::get_var("ROOT_PATH", hydro_suffix),
            settings_file: env::get_var("SETTINGS_FILE", hydro_suffix),
            secrets_file: env::get_var("SECRETS_FILE", hydro_suffix),
            env: env::get_var_default(
                "ENV",
                hydro_suffix,
                "development".into(),
            ),
            envvar_prefix: env::get_var_default(
                "ENVVAR_PREFIX",
                hydro_suffix,
                "HYDRO".into(),
            ),
            encoding: env::get_var_default(
                "ENCODING",
                hydro_suffix,
                "utf-8".into(),
            ),
            envvar_nested_sep: env::get_var_default(
                "ENVVAR_NESTED_SEP",
                hydro_suffix,
                "__".into(),
            ),
        }
    }
}

impl HydroSettings {
    pub fn set_root_path(mut self, p: PathBuf) -> Self {
        self.root_path = Some(p);
        self
    }

    pub fn set_settings_file(mut self, p: PathBuf) -> Self {
        self.settings_file = Some(p);
        self
    }

    pub fn set_secrets_file(mut self, p: PathBuf) -> Self {
        self.secrets_file = Some(p);
        self
    }

    pub fn set_env(mut self, e: String) -> Self {
        self.env = e;
        self
    }

    pub fn set_envvar_prefix(mut self, p: String) -> Self {
        self.envvar_prefix = p;
        self
    }

    pub fn set_encoding(mut self, e: String) -> Self {
        self.encoding = e;
        self
    }

    pub fn set_envvar_nested_sep(mut self, s: String) -> Self {
        self.envvar_nested_sep = s;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env::{remove_var, set_var};

    #[test]
    fn test_default() {
        assert_eq!(
            HydroSettings::default(),
            HydroSettings {
                root_path: None,
                settings_file: None,
                secrets_file: None,
                env: "development".into(),
                envvar_prefix: "HYDRO".into(),
                encoding: "utf-8".into(),
                envvar_nested_sep: "__".into(),
            },
        );
    }

    #[test]
    fn test_default_with_env() {
        set_var("ENCODING_FOR_HYDRO", "latin-1");
        set_var("ROOT_PATH_FOR_HYDRO", "/an/absolute/path");
        assert_eq!(
            HydroSettings::default(),
            HydroSettings {
                root_path: Some("/an/absolute/path".into()),
                settings_file: None,
                secrets_file: None,
                env: "development".into(),
                envvar_prefix: "HYDRO".into(),
                encoding: "latin-1".into(),
                envvar_nested_sep: "__".into(),
            },
        );
        remove_var("ENCODING_FOR_HYDRO");
        remove_var("ROOT_PATH_FOR_HYDRO");
    }

    #[test]
    fn test_one_builder_method() {
        assert_eq!(
            HydroSettings::default()
                .set_root_path(PathBuf::from("~/test/dir")),
            HydroSettings {
                root_path: Some(PathBuf::from("~/test/dir")),
                settings_file: None,
                secrets_file: None,
                env: "development".into(),
                envvar_prefix: "HYDRO".into(),
                encoding: "utf-8".into(),
                envvar_nested_sep: "__".into(),
            },
        );
    }

    #[test]
    fn test_all_builder_methods() {
        assert_eq!(
            HydroSettings::default()
                .set_envvar_prefix("HY_".into())
                .set_encoding("latin-1".into())
                .set_secrets_file(PathBuf::from(".secrets.toml"))
                .set_env("production".into())
                .set_envvar_nested_sep("-".into())
                .set_root_path(PathBuf::from("~/test/dir"))
                .set_settings_file(PathBuf::from("settings.toml")),
            HydroSettings {
                root_path: Some(PathBuf::from("~/test/dir")),
                settings_file: Some(PathBuf::from("settings.toml")),
                secrets_file: Some(PathBuf::from(".secrets.toml")),
                env: "production".into(),
                envvar_prefix: "HY_".into(),
                encoding: "latin-1".into(),
                envvar_nested_sep: "-".into(),
            },
        );
    }
}