1use std::path::PathBuf;
2
3use crate::env;
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct HydroSettings {
7 pub root_path: Option<PathBuf>,
8 pub settings_file: Option<PathBuf>,
9 pub secrets_file: Option<PathBuf>,
10 pub env: String,
11 pub envvar_prefix: String,
12 pub encoding: String,
13 pub envvar_nested_sep: String,
14}
15
16impl Default for HydroSettings {
17 fn default() -> Self {
18 let hydro_suffix = "_FOR_HYDRO";
19 Self {
20 root_path: env::get_var("ROOT_PATH", hydro_suffix),
21 settings_file: env::get_var("SETTINGS_FILE", hydro_suffix),
22 secrets_file: env::get_var("SECRETS_FILE", hydro_suffix),
23 env: env::get_var_default(
24 "ENV",
25 hydro_suffix,
26 "development".into(),
27 ),
28 envvar_prefix: env::get_var_default(
29 "ENVVAR_PREFIX",
30 hydro_suffix,
31 "HYDRO".into(),
32 ),
33 encoding: env::get_var_default(
34 "ENCODING",
35 hydro_suffix,
36 "utf-8".into(),
37 ),
38 envvar_nested_sep: env::get_var_default(
39 "ENVVAR_NESTED_SEP",
40 hydro_suffix,
41 "__".into(),
42 ),
43 }
44 }
45}
46
47impl HydroSettings {
48 pub fn set_root_path(mut self, p: PathBuf) -> Self {
49 self.root_path = Some(p);
50 self
51 }
52
53 pub fn set_settings_file(mut self, p: PathBuf) -> Self {
54 self.settings_file = Some(p);
55 self
56 }
57
58 pub fn set_secrets_file(mut self, p: PathBuf) -> Self {
59 self.secrets_file = Some(p);
60 self
61 }
62
63 pub fn set_env(mut self, e: String) -> Self {
64 self.env = e;
65 self
66 }
67
68 pub fn set_envvar_prefix(mut self, p: String) -> Self {
69 self.envvar_prefix = p;
70 self
71 }
72
73 pub fn set_encoding(mut self, e: String) -> Self {
74 self.encoding = e;
75 self
76 }
77
78 pub fn set_envvar_nested_sep(mut self, s: String) -> Self {
79 self.envvar_nested_sep = s;
80 self
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87 use std::env::{remove_var, set_var};
88
89 #[test]
90 fn test_default() {
91 assert_eq!(
92 HydroSettings::default(),
93 HydroSettings {
94 root_path: None,
95 settings_file: None,
96 secrets_file: None,
97 env: "development".into(),
98 envvar_prefix: "HYDRO".into(),
99 encoding: "utf-8".into(),
100 envvar_nested_sep: "__".into(),
101 },
102 );
103 }
104
105 #[test]
106 fn test_default_with_env() {
107 set_var("ENCODING_FOR_HYDRO", "latin-1");
108 set_var("ROOT_PATH_FOR_HYDRO", "/an/absolute/path");
109 assert_eq!(
110 HydroSettings::default(),
111 HydroSettings {
112 root_path: Some("/an/absolute/path".into()),
113 settings_file: None,
114 secrets_file: None,
115 env: "development".into(),
116 envvar_prefix: "HYDRO".into(),
117 encoding: "latin-1".into(),
118 envvar_nested_sep: "__".into(),
119 },
120 );
121 remove_var("ENCODING_FOR_HYDRO");
122 remove_var("ROOT_PATH_FOR_HYDRO");
123 }
124
125 #[test]
126 fn test_one_builder_method() {
127 assert_eq!(
128 HydroSettings::default()
129 .set_root_path(PathBuf::from("~/test/dir")),
130 HydroSettings {
131 root_path: Some(PathBuf::from("~/test/dir")),
132 settings_file: None,
133 secrets_file: None,
134 env: "development".into(),
135 envvar_prefix: "HYDRO".into(),
136 encoding: "utf-8".into(),
137 envvar_nested_sep: "__".into(),
138 },
139 );
140 }
141
142 #[test]
143 fn test_all_builder_methods() {
144 assert_eq!(
145 HydroSettings::default()
146 .set_envvar_prefix("HY_".into())
147 .set_encoding("latin-1".into())
148 .set_secrets_file(PathBuf::from(".secrets.toml"))
149 .set_env("production".into())
150 .set_envvar_nested_sep("-".into())
151 .set_root_path(PathBuf::from("~/test/dir"))
152 .set_settings_file(PathBuf::from("settings.toml")),
153 HydroSettings {
154 root_path: Some(PathBuf::from("~/test/dir")),
155 settings_file: Some(PathBuf::from("settings.toml")),
156 secrets_file: Some(PathBuf::from(".secrets.toml")),
157 env: "production".into(),
158 envvar_prefix: "HY_".into(),
159 encoding: "latin-1".into(),
160 envvar_nested_sep: "-".into(),
161 },
162 );
163 }
164}