envmode/
lib.rs

1use serde::Deserialize;
2use std::borrow::Cow;
3use std::sync::Arc;
4use validator::ValidationError;
5
6/// Represent the environment mode of the application
7#[derive(Debug, Deserialize)]
8#[serde(rename_all = "lowercase")]
9pub enum EnvMode {
10    // Development mode
11    Dev,
12    // Production mode
13    Prd,
14    // Staging mode
15    Stg,
16}
17
18impl From<&Arc<str>> for EnvMode {
19    fn from(value: &Arc<str>) -> Self {
20        match value.as_ref() {
21            "dev" => EnvMode::Dev,
22            "prd" => EnvMode::Prd,
23            "stg" => EnvMode::Stg,
24            _ => unreachable!("invalid environment mode"),
25        }
26    }
27}
28
29impl EnvMode {
30    // Check if the mode is development
31    pub fn is_dev(mode: &str) -> bool {
32        mode == "dev"
33    }
34
35    // Check if the mode is production
36    pub fn is_prd(mode: &str) -> bool {
37        mode == "prd"
38    }
39
40    // Check if the mode is staging
41    pub fn is_stg(mode: &str) -> bool {
42        mode == "stg"
43    }
44
45    // Check if the mode is valid
46    pub fn is_valid(mode: &str) -> bool {
47        Self::is_dev(mode) || Self::is_prd(mode) || Self::is_stg(mode)
48    }
49}
50
51pub fn verify(mode: &str) -> Result<(), ValidationError> {
52    if !EnvMode::is_valid(mode) {
53        return Err(
54            ValidationError::new("env_mode").with_message(Cow::Owned(String::from(
55                "Please provide a valid environment mode",
56            ))),
57        );
58    }
59
60    Ok(())
61}