1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use super::ConfigError;

use std::fmt;
use std::str::FromStr;
use std::env;

use self::Environment::*;

pub const CONFIG_ENV: &str = "ROCKET_ENV";

/// An enum corresponding to the valid configuration environments.
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)]
pub enum Environment {
    /// The development environment.
    Development,
    /// The staging environment.
    Staging,
    /// The production environment.
    Production,
}

impl Environment {
    /// List of all of the possible environments.
    pub(crate) const ALL: [Environment; 3] = [Development, Staging, Production];

    /// String of all valid environments.
    pub(crate) const VALID: &'static str = "development, staging, production";

    /// Retrieves the "active" environment as determined by the `ROCKET_ENV`
    /// environment variable. If `ROCKET_ENV` is not set, returns `Development`
    /// when the application was compiled in `debug` mode and `Production` when
    /// the application was compiled in `release` mode.
    ///
    /// # Errors
    ///
    /// Returns a `BadEnv` `ConfigError` if `ROCKET_ENV` is set and contains an
    /// invalid or unknown environment name.
    pub fn active() -> Result<Environment, ConfigError> {
        match env::var(CONFIG_ENV) {
            Ok(s) => s.parse().map_err(|_| ConfigError::BadEnv(s)),
            #[cfg(debug_assertions)]
            _ => Ok(Development),
            #[cfg(not(debug_assertions))]
            _ => Ok(Production),
        }
    }

    /// Returns `true` if `self` is `Environment::Development`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rocket::config::Environment;
    ///
    /// assert!(Environment::Development.is_dev());
    /// assert!(!Environment::Production.is_dev());
    /// ```
    #[inline]
    pub fn is_dev(self) -> bool {
        self == Development
    }

    /// Returns `true` if `self` is `Environment::Staging`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rocket::config::Environment;
    ///
    /// assert!(Environment::Staging.is_stage());
    /// assert!(!Environment::Production.is_stage());
    /// ```
    #[inline]
    pub fn is_stage(self) -> bool {
        self == Staging
    }

    /// Returns `true` if `self` is `Environment::Production`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rocket::config::Environment;
    ///
    /// assert!(Environment::Production.is_prod());
    /// assert!(!Environment::Staging.is_prod());
    /// ```
    #[inline]
    pub fn is_prod(self) -> bool {
        self == Production
    }
}

impl FromStr for Environment {
    type Err = ();

    /// Parses a configuration environment from a string. Should be used
    /// indirectly via `str`'s `parse` method.
    ///
    /// # Examples
    ///
    /// Parsing a development environment:
    ///
    /// ```rust
    /// use rocket::config::Environment;
    ///
    /// let env = "development".parse::<Environment>();
    /// assert_eq!(env.unwrap(), Environment::Development);
    ///
    /// let env = "dev".parse::<Environment>();
    /// assert_eq!(env.unwrap(), Environment::Development);
    ///
    /// let env = "devel".parse::<Environment>();
    /// assert_eq!(env.unwrap(), Environment::Development);
    /// ```
    ///
    /// Parsing a staging environment:
    ///
    /// ```rust
    /// use rocket::config::Environment;
    ///
    /// let env = "staging".parse::<Environment>();
    /// assert_eq!(env.unwrap(), Environment::Staging);
    ///
    /// let env = "stage".parse::<Environment>();
    /// assert_eq!(env.unwrap(), Environment::Staging);
    /// ```
    ///
    /// Parsing a production environment:
    ///
    /// ```rust
    /// use rocket::config::Environment;
    ///
    /// let env = "production".parse::<Environment>();
    /// assert_eq!(env.unwrap(), Environment::Production);
    ///
    /// let env = "prod".parse::<Environment>();
    /// assert_eq!(env.unwrap(), Environment::Production);
    /// ```
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let env = match s {
            "dev" | "devel" | "development" => Development,
            "stage" | "staging" => Staging,
            "prod" | "production" => Production,
            _ => return Err(()),
        };

        Ok(env)
    }
}

impl fmt::Display for Environment {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Development => write!(f, "development"),
            Staging => write!(f, "staging"),
            Production => write!(f, "production"),
        }
    }
}