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
use crate::error::{AppError, Result};
use anyhow::Context;
use std::{
    env,
    ffi::OsStr,
    io::ErrorKind,
    path::{Path, PathBuf},
};

/// App environment
#[derive(Debug)]
pub enum Env {
    /// Development
    Dev,
    /// Test
    Test,
    /// production
    Prod,
}

impl Env {
    pub fn from_env() -> Self {
        match env::var("SPRING_ENV") {
            Ok(var) => Self::from_string(var),
            Err(_) => Self::Dev,
        }
    }

    pub fn from_string(str: String) -> Self {
        match str {
            str if str.eq_ignore_ascii_case("dev") => Self::Dev,
            str if str.eq_ignore_ascii_case("test") => Self::Test,
            str if str.eq_ignore_ascii_case("prod") => Self::Prod,
            _ => Self::Dev,
        }
    }

    pub(crate) fn get_config_path(&self, path: &Path) -> Result<PathBuf> {
        let stem = path.file_stem().and_then(OsStr::to_str).unwrap_or("");
        let ext = path.extension().and_then(OsStr::to_str).unwrap_or("");
        let canonicalize = path
            .canonicalize()
            .with_context(|| format!("canonicalize {:?} failed", path))?;
        let parent = canonicalize
            .parent()
            .ok_or_else(|| AppError::from_io(ErrorKind::NotFound, "config file path not found"))?;
        Ok(match self {
            Self::Dev => parent.join(format!("{}-dev.{}", stem, ext)),
            Self::Test => parent.join(format!("{}-test.{}", stem, ext)),
            Self::Prod => parent.join(format!("{}-prod.{}", stem, ext)),
        })
    }
}

pub fn init() -> Result<Env> {
    match dotenvy::dotenv() {
        Ok(path) => log::debug!(
            "Loaded the environment variable file under the path: \"{:?}\"",
            path
        ),
        Err(e) => log::debug!("Environment variable file not found: {}", e),
    }

    Ok(Env::from_env())
}