Skip to main content

client_core/
environment.rs

1/// 环境检测模块
2///
3/// 此模块提供环境检测功能,支持通过环境变量 NUWAX_CLI_ENV 来区分不同的运行环境
4///
5/// 支持的环境:
6/// - Production: 生产环境(默认)
7/// - Test: 测试环境
8///
9/// 使用方式:
10/// - 不设置环境变量或设置 NUWAX_CLI_ENV=production → 生产环境
11/// - 设置 NUWAX_CLI_ENV=testing 或 NUWAX_CLI_ENV=test → 测试环境
12
13/// 环境类型枚举
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum Environment {
16    /// 生产环境
17    Production,
18    /// 测试环境
19    Test,
20}
21
22impl Environment {
23    /// 从环境变量检测当前环境
24    ///
25    /// 环境变量优先级:
26    /// 1. NUWAX_CLI_ENV=testing 或 NUWAX_CLI_ENV=test → Test
27    /// 2. NUWAX_CLI_ENV=production 或未设置 → Production
28    ///
29    /// # Examples
30    /// ```
31    /// use client_core::environment::Environment;
32    ///
33    /// // 模拟环境变量
34    /// std::env::set_var("NUWAX_CLI_ENV", "testing");
35    /// assert_eq!(Environment::from_env(), Environment::Test);
36    ///
37    /// std::env::set_var("NUWAX_CLI_ENV", "production");
38    /// assert_eq!(Environment::from_env(), Environment::Production);
39    ///
40    /// std::env::remove_var("NUWAX_CLI_ENV");
41    /// assert_eq!(Environment::from_env(), Environment::Production);
42    /// ```
43    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            // 对于未知值,默认为生产环境以确保安全
52            _ => {
53                tracing::warn!("Unknown NUWAX_CLI_ENV value, defaulting to Production environment");
54                Environment::Production
55            }
56        }
57    }
58
59    /// 检查是否为测试环境
60    pub fn is_testing(&self) -> bool {
61        matches!(self, Environment::Test)
62    }
63
64    /// 检查是否为生产环境
65    pub fn is_production(&self) -> bool {
66        matches!(self, Environment::Production)
67    }
68
69    /// 获取环境的字符串表示
70    pub fn as_str(&self) -> &'static str {
71        match self {
72            Environment::Production => "production",
73            Environment::Test => "testing",
74        }
75    }
76
77    /// 获取环境的显示名称(用户友好)
78    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        // 测试未设置环境变量时默认为生产环境
105        unsafe {
106            std::env::remove_var("NUWAX_CLI_ENV");
107        }
108        assert_eq!(Environment::from_env(), Environment::Production);
109
110        // 测试显式设置生产环境
111        unsafe {
112            std::env::set_var("NUWAX_CLI_ENV", "production");
113        }
114        assert_eq!(Environment::from_env(), Environment::Production);
115
116        // 测试生产环境简写
117        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        // 测试测试环境
126        unsafe {
127            std::env::set_var("NUWAX_CLI_ENV", "testing");
128        }
129        assert_eq!(Environment::from_env(), Environment::Test);
130
131        // 测试测试环境简写
132        unsafe {
133            std::env::set_var("NUWAX_CLI_ENV", "test");
134        }
135        assert_eq!(Environment::from_env(), Environment::Test);
136
137        // 测试大小写不敏感
138        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        // 测试未知值时默认为生产环境
152        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}