scsys_config/types/
environment.rs

1/*
2    Appellation: environment <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5
6#[derive(
7    Clone,
8    Copy,
9    Debug,
10    Default,
11    Eq,
12    Hash,
13    Ord,
14    PartialEq,
15    PartialOrd,
16    strum::AsRefStr,
17    strum::Display,
18    strum::EnumCount,
19    strum::EnumIs,
20    strum::EnumIter,
21    strum::EnumString,
22    strum::VariantArray,
23    strum::VariantNames,
24)]
25#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
26#[cfg_attr(
27    feature = "serde",
28    derive(serde::Deserialize, serde::Serialize),
29    serde(rename_all = "lowercase")
30)]
31#[strum(serialize_all = "lowercase")]
32pub enum Environment {
33    #[default]
34    #[cfg_attr(feature = "serde", serde(alias = "d", alias = "dev"))]
35    Development,
36    #[cfg_attr(feature = "serde", serde(alias = "s", alias = "stag"))]
37    Staging,
38    #[cfg_attr(feature = "serde", serde(alias = "p", alias = "prod"))]
39    Production,
40}
41
42impl Environment {
43    /// returns a new instance of [`Development`](Environment::Development) variant
44    pub const fn development() -> Self {
45        Self::Development
46    }
47    /// returns a new instance of [`Staging`](Environment::Staging) variant
48    pub const fn staging() -> Self {
49        Self::Staging
50    }
51    /// returns a new instance of [`Production`](Environment::Production) variant
52    pub const fn production() -> Self {
53        Self::Production
54    }
55    #[cfg(feature = "std")]
56    pub fn from_env() -> Self {
57        Self::from_env_with_varname("APP_MODE")
58    }
59    #[cfg(feature = "std")]
60    pub fn from_env_with_varname(var: &str) -> Self {
61        use core::str::FromStr;
62        std::env::var(var)
63            .map(|m| Self::from_str(&m).ok())
64            .ok()
65            .flatten()
66            .unwrap_or_default()
67    }
68}