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, Default)]
15pub enum Environment {
16    /// 生产环境
17    #[default]
18    Production,
19    /// 测试环境
20    Test,
21}
22
23impl Environment {
24    /// 从环境变量检测当前环境
25    ///
26    /// 环境变量优先级:
27    /// 1. NUWAX_CLI_ENV=testing 或 NUWAX_CLI_ENV=test → Test
28    /// 2. NUWAX_CLI_ENV=production 或未设置 → Production
29    ///
30    /// # Examples
31    /// ```
32    /// use client_core::environment::Environment;
33    ///
34    /// // 获取当前环境
35    /// let env = Environment::from_env();
36    /// println!("Current environment: {:?}", env);
37    /// ```
38    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            // 对于未知值,默认为生产环境以确保安全
47            _ => {
48                tracing::warn!("Unknown NUWAX_CLI_ENV value, defaulting to Production environment");
49                Environment::Production
50            }
51        }
52    }
53
54    /// 检查是否为测试环境
55    pub fn is_testing(&self) -> bool {
56        matches!(self, Environment::Test)
57    }
58
59    /// 检查是否为生产环境
60    pub fn is_production(&self) -> bool {
61        matches!(self, Environment::Production)
62    }
63
64    /// 获取环境的字符串表示
65    pub fn as_str(&self) -> &'static str {
66        match self {
67            Environment::Production => "production",
68            Environment::Test => "testing",
69        }
70    }
71
72    /// 获取环境的显示名称(用户友好)
73    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        // 测试未设置环境变量时默认为生产环境
94        unsafe {
95            std::env::remove_var("NUWAX_CLI_ENV");
96        }
97        assert_eq!(Environment::from_env(), Environment::Production);
98
99        // 测试显式设置生产环境
100        unsafe {
101            std::env::set_var("NUWAX_CLI_ENV", "production");
102        }
103        assert_eq!(Environment::from_env(), Environment::Production);
104
105        // 测试生产环境简写
106        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        // 测试测试环境
115        unsafe {
116            std::env::set_var("NUWAX_CLI_ENV", "testing");
117        }
118        assert_eq!(Environment::from_env(), Environment::Test);
119
120        // 测试测试环境简写
121        unsafe {
122            std::env::set_var("NUWAX_CLI_ENV", "test");
123        }
124        assert_eq!(Environment::from_env(), Environment::Test);
125
126        // 测试大小写不敏感
127        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        // 测试未知值时默认为生产环境
141        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}