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