Skip to content

Commit cd43858

Browse files
committed
feat: added env handling
1 parent 90c7de0 commit cd43858

File tree

3 files changed

+301
-0
lines changed

3 files changed

+301
-0
lines changed

src/flow_config/environment.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::str::FromStr;
2+
3+
#[derive(Debug, PartialEq, Eq)]
4+
pub enum Environment {
5+
Development,
6+
Staging,
7+
Production,
8+
}
9+
10+
impl FromStr for Environment {
11+
type Err = ();
12+
13+
fn from_str(s: &str) -> Result<Self, Self::Err> {
14+
match s.to_lowercase().as_str() {
15+
"staging" => Ok(Environment::Staging),
16+
"production" => Ok(Environment::Production),
17+
"development" => Ok(Environment::Development),
18+
_ => Ok(Environment::Development),
19+
}
20+
}
21+
}

src/flow_config/mod.rs

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
use std::{fmt::Debug, str::FromStr};
2+
3+
pub mod environment;
4+
pub mod mode;
5+
6+
pub fn env_with_default<T: FromStr + Debug>(key: &str, default: T) -> T {
7+
match std::env::var(key) {
8+
Ok(string) => match T::from_str(&string) {
9+
Ok(value) => {
10+
log::info!("Found env: {} with value: {:?}", key, &value);
11+
value
12+
}
13+
Err(_) => {
14+
log::warn!("Failed to parse env: {} with value: {:?}", key, &string);
15+
default
16+
}
17+
},
18+
Err(_) => {
19+
log::warn!("Failed to find env: {}", key);
20+
default
21+
}
22+
}
23+
}
24+
25+
pub fn load_env_file() {
26+
match dotenv::dotenv() {
27+
Ok(path) => log::info!("Found Env. file at {:?} ", path),
28+
Err(e) => log::error!("Failed to load .env file. Reason: {:?}", e),
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
use serial_test::serial;
36+
use std::env;
37+
38+
#[test]
39+
#[serial]
40+
fn test_env_with_default_string_exists() {
41+
let key = "TEST_STRING_VAR";
42+
let expected = "test_value";
43+
44+
unsafe {
45+
env::set_var(key, expected);
46+
}
47+
48+
let result = env_with_default(key, "default".to_string());
49+
assert_eq!(result, expected);
50+
51+
unsafe {
52+
env::remove_var(key);
53+
}
54+
}
55+
56+
#[test]
57+
#[serial]
58+
fn test_env_with_default_string_missing() {
59+
let key = "TEST_MISSING_STRING_VAR";
60+
unsafe {
61+
env::remove_var(key);
62+
}
63+
64+
let default = "default_value".to_string();
65+
let result = env_with_default(key, default.clone());
66+
assert_eq!(result, default);
67+
}
68+
69+
#[test]
70+
#[serial]
71+
fn test_env_with_default_integer_exists() {
72+
let key = "TEST_INT_VAR";
73+
let expected = 42;
74+
unsafe {
75+
env::set_var(key, expected.to_string());
76+
}
77+
78+
let result = env_with_default(key, 0i32);
79+
assert_eq!(result, expected);
80+
81+
unsafe {
82+
env::remove_var(key);
83+
}
84+
}
85+
86+
#[test]
87+
#[serial]
88+
fn test_env_with_default_integer_missing() {
89+
let key = "TEST_MISSING_INT_VAR";
90+
unsafe {
91+
env::remove_var(key);
92+
}
93+
94+
let default = 123i32;
95+
let result = env_with_default(key, default);
96+
assert_eq!(result, default);
97+
}
98+
99+
#[test]
100+
#[serial]
101+
fn test_env_with_default_boolean_exists_true() {
102+
let key = "TEST_BOOL_TRUE_VAR";
103+
unsafe {
104+
env::set_var(key, "true");
105+
}
106+
107+
let result = env_with_default(key, false);
108+
assert_eq!(result, true);
109+
110+
unsafe {
111+
env::remove_var(key);
112+
}
113+
}
114+
115+
#[test]
116+
#[serial]
117+
fn test_env_with_default_boolean_exists_false() {
118+
let key = "TEST_BOOL_FALSE_VAR";
119+
unsafe {
120+
env::set_var(key, "false");
121+
}
122+
123+
let result = env_with_default(key, true);
124+
assert_eq!(result, false);
125+
126+
unsafe {
127+
env::remove_var(key);
128+
}
129+
}
130+
131+
#[test]
132+
#[serial]
133+
fn test_env_with_default_boolean_missing() {
134+
let key = "TEST_MISSING_BOOL_VAR";
135+
unsafe {
136+
env::remove_var(key);
137+
}
138+
139+
let default = true;
140+
let result = env_with_default(key, default);
141+
assert_eq!(result, default);
142+
}
143+
144+
#[test]
145+
#[serial]
146+
fn test_env_with_default_boolean_invalid() {
147+
let key = "TEST_INVALID_BOOL_VAR";
148+
unsafe {
149+
env::set_var(key, "maybe");
150+
}
151+
152+
let result = env_with_default(key, false);
153+
assert_eq!(result, false);
154+
155+
unsafe {
156+
env::remove_var(key);
157+
}
158+
}
159+
160+
#[test]
161+
#[serial]
162+
fn test_env_with_default_u32_exists() {
163+
let key = "TEST_U32_VAR";
164+
let expected = 42u32;
165+
unsafe {
166+
env::set_var(key, expected.to_string());
167+
}
168+
169+
let result = env_with_default(key, 0u32);
170+
assert_eq!(result, expected);
171+
172+
unsafe {
173+
env::remove_var(key);
174+
}
175+
}
176+
177+
#[test]
178+
#[serial]
179+
fn test_env_with_default_u32_negative_invalid() {
180+
let key = "TEST_U32_NEGATIVE_VAR";
181+
unsafe {
182+
env::set_var(key, "-42");
183+
}
184+
185+
let result = env_with_default(key, 0u32);
186+
assert_eq!(result, 0u32);
187+
188+
unsafe {
189+
env::remove_var(key);
190+
}
191+
}
192+
193+
#[test]
194+
#[serial]
195+
fn test_env_with_default_empty_string() {
196+
let key = "TEST_EMPTY_STRING_VAR";
197+
unsafe {
198+
env::set_var(key, "");
199+
}
200+
201+
let result = env_with_default(key, "default".to_string());
202+
assert_eq!(result, "");
203+
204+
unsafe {
205+
env::remove_var(key);
206+
}
207+
}
208+
209+
#[test]
210+
#[serial]
211+
fn test_env_with_default_whitespace_string() {
212+
let key = "TEST_WHITESPACE_VAR";
213+
unsafe {
214+
env::set_var(key, " whitespace ");
215+
}
216+
217+
let result = env_with_default(key, "default".to_string());
218+
assert_eq!(result, " whitespace ");
219+
220+
unsafe {
221+
env::remove_var(key);
222+
}
223+
}
224+
225+
#[test]
226+
#[serial]
227+
fn test_env_with_environment() {
228+
let key = "TEST_ENVIRONMENT";
229+
unsafe {
230+
env::set_var(key, "DEVELOPMENT");
231+
}
232+
233+
let result = env_with_default(key, environment::Environment::Development);
234+
assert_eq!(result, environment::Environment::Development);
235+
236+
unsafe {
237+
env::remove_var(key);
238+
}
239+
}
240+
241+
#[test]
242+
#[serial]
243+
fn test_env_with_mode() {
244+
let key = "TEST_MODE";
245+
unsafe {
246+
env::set_var(key, "STATIC");
247+
}
248+
249+
let result = env_with_default(key, mode::Mode::STATIC);
250+
assert_eq!(result, mode::Mode::STATIC);
251+
252+
unsafe {
253+
env::remove_var(key);
254+
}
255+
}
256+
}

src/flow_config/mode.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::str::FromStr;
2+
3+
/// STATIC:
4+
/// The service will start with no Sagittarius in mind
5+
///
6+
/// DYNAMIC:
7+
/// The service will start with Sagittarius in mind
8+
#[derive(PartialEq, Eq, Debug)]
9+
pub enum Mode {
10+
STATIC,
11+
DYNAMIC,
12+
}
13+
14+
impl FromStr for Mode {
15+
type Err = ();
16+
17+
fn from_str(s: &str) -> Result<Self, Self::Err> {
18+
match s.to_lowercase().as_str() {
19+
"static" => Ok(Mode::STATIC),
20+
"dynamic" => Ok(Mode::DYNAMIC),
21+
_ => Ok(Mode::STATIC),
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)