webserver_base/base_settings/
environment.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(PartialEq, Eq, Clone)]
4pub enum Environment {
5    Development,
6    Production,
7}
8
9impl Default for Environment {
10    fn default() -> Self {
11        Self::Development
12    }
13}
14
15impl Environment {
16    #[must_use]
17    pub const fn as_str(&self) -> &'static str {
18        match self {
19            Self::Development => "development",
20            Self::Production => "production",
21        }
22    }
23}
24
25impl TryFrom<String> for Environment {
26    type Error = String;
27
28    fn try_from(s: String) -> Result<Self, Self::Error> {
29        match s.to_lowercase().as_str() {
30            "development" => Ok(Self::Development),
31            "production" => Ok(Self::Production),
32            other => Err(format!(
33                "{other} is not a supported environment. Use either `development` or `production`."
34            )),
35        }
36    }
37}
38
39impl Display for Environment {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        match self {
42            Self::Development => write!(f, "development"),
43            Self::Production => write!(f, "production"),
44        }
45    }
46}