client_core/
environment.rs1#[derive(Debug, Clone, PartialEq, Eq, Default)]
15pub enum Environment {
16 #[default]
18 Production,
19 Test,
21}
22
23impl Environment {
24 pub fn from_env() -> Self {
39 match std::env::var("NUWAX_CLI_ENV")
40 .unwrap_or_default()
41 .to_lowercase()
42 .as_str()
43 {
44 "testing" | "test" => Environment::Test,
45 "production" | "prod" | "" => Environment::Production,
46 _ => {
48 tracing::warn!("Unknown NUWAX_CLI_ENV value, defaulting to Production environment");
49 Environment::Production
50 }
51 }
52 }
53
54 pub fn is_testing(&self) -> bool {
56 matches!(self, Environment::Test)
57 }
58
59 pub fn is_production(&self) -> bool {
61 matches!(self, Environment::Production)
62 }
63
64 pub fn as_str(&self) -> &'static str {
66 match self {
67 Environment::Production => "production",
68 Environment::Test => "testing",
69 }
70 }
71
72 pub fn display_name(&self) -> &'static str {
74 match self {
75 Environment::Production => "Production",
76 Environment::Test => "Testing",
77 }
78 }
79}
80
81impl std::fmt::Display for Environment {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 write!(f, "{}", self.display_name())
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn test_from_env_production() {
93 unsafe {
95 std::env::remove_var("NUWAX_CLI_ENV");
96 }
97 assert_eq!(Environment::from_env(), Environment::Production);
98
99 unsafe {
101 std::env::set_var("NUWAX_CLI_ENV", "production");
102 }
103 assert_eq!(Environment::from_env(), Environment::Production);
104
105 unsafe {
107 std::env::set_var("NUWAX_CLI_ENV", "prod");
108 }
109 assert_eq!(Environment::from_env(), Environment::Production);
110 }
111
112 #[test]
113 fn test_from_env_testing() {
114 unsafe {
116 std::env::set_var("NUWAX_CLI_ENV", "testing");
117 }
118 assert_eq!(Environment::from_env(), Environment::Test);
119
120 unsafe {
122 std::env::set_var("NUWAX_CLI_ENV", "test");
123 }
124 assert_eq!(Environment::from_env(), Environment::Test);
125
126 unsafe {
128 std::env::set_var("NUWAX_CLI_ENV", "TESTING");
129 }
130 assert_eq!(Environment::from_env(), Environment::Test);
131
132 unsafe {
133 std::env::set_var("NUWAX_CLI_ENV", "Test");
134 }
135 assert_eq!(Environment::from_env(), Environment::Test);
136 }
137
138 #[test]
139 fn test_from_env_unknown() {
140 unsafe {
142 std::env::set_var("NUWAX_CLI_ENV", "staging");
143 }
144 assert_eq!(Environment::from_env(), Environment::Production);
145
146 unsafe {
147 std::env::set_var("NUWAX_CLI_ENV", "development");
148 }
149 assert_eq!(Environment::from_env(), Environment::Production);
150 }
151
152 #[test]
153 fn test_environment_methods() {
154 let prod = Environment::Production;
155 let test = Environment::Test;
156
157 assert!(prod.is_production());
158 assert!(!prod.is_testing());
159 assert_eq!(prod.as_str(), "production");
160 assert_eq!(prod.display_name(), "Production");
161
162 assert!(test.is_testing());
163 assert!(!test.is_production());
164 assert_eq!(test.as_str(), "testing");
165 assert_eq!(test.display_name(), "Testing");
166 }
167
168 #[test]
169 fn test_default() {
170 assert_eq!(Environment::default(), Environment::Production);
171 }
172
173 #[test]
174 fn test_display() {
175 assert_eq!(format!("{}", Environment::Production), "Production");
176 assert_eq!(format!("{}", Environment::Test), "Testing");
177 }
178}