Skip to main content

webserver_base/base_settings/
environment.rs

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