Skip to main content

rskit_config/service/
environment.rs

1use serde::{Deserialize, Serialize};
2
3/// Deployment environment.
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
5#[non_exhaustive]
6#[serde(rename_all = "lowercase")]
7pub enum Environment {
8    /// Local development environment (default).
9    #[default]
10    Development,
11    /// Pre-production / staging environment.
12    Staging,
13    /// Live production environment.
14    Production,
15}
16
17impl Environment {
18    /// Returns `true` if this is the production environment.
19    pub fn is_production(&self) -> bool {
20        *self == Environment::Production
21    }
22}
23
24impl std::fmt::Display for Environment {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Environment::Development => f.write_str("development"),
28            Environment::Staging => f.write_str("staging"),
29            Environment::Production => f.write_str("production"),
30        }
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn environment_display_development() {
40        assert_eq!(Environment::Development.to_string(), "development");
41    }
42
43    #[test]
44    fn environment_display_staging() {
45        assert_eq!(Environment::Staging.to_string(), "staging");
46    }
47
48    #[test]
49    fn environment_display_production() {
50        assert_eq!(Environment::Production.to_string(), "production");
51    }
52
53    #[test]
54    fn environment_default_is_development() {
55        assert_eq!(Environment::default(), Environment::Development);
56    }
57
58    #[test]
59    fn environment_is_production_returns_true_for_production() {
60        assert!(Environment::Production.is_production());
61    }
62
63    #[test]
64    fn environment_is_production_returns_false_for_development() {
65        assert!(!Environment::Development.is_production());
66    }
67
68    #[test]
69    fn environment_is_production_returns_false_for_staging() {
70        assert!(!Environment::Staging.is_production());
71    }
72
73    #[test]
74    fn environment_deserialize_from_lowercase_string() {
75        let dev: Environment = serde_json::from_str(r#""development""#).unwrap();
76        assert_eq!(dev, Environment::Development);
77
78        let stg: Environment = serde_json::from_str(r#""staging""#).unwrap();
79        assert_eq!(stg, Environment::Staging);
80
81        let prd: Environment = serde_json::from_str(r#""production""#).unwrap();
82        assert_eq!(prd, Environment::Production);
83    }
84
85    #[test]
86    fn environment_deserialize_unknown_string_fails() {
87        let result: Result<Environment, _> = serde_json::from_str(r#""unknown""#);
88        assert!(result.is_err());
89    }
90
91    #[test]
92    fn environment_clone_and_eq() {
93        let env = Environment::Staging;
94        let cloned = env.clone();
95        assert_eq!(env, cloned);
96    }
97}