Skip to main content

doido_controller/
environment.rs

1//! Runtime environment selection, driven by the `DOIDO_ENV` variable.
2
3/// The application environment. Selects which `config/<env>.yml` file is read.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Environment {
6    Development,
7    Test,
8    Production,
9}
10
11impl Environment {
12    /// Reads the current environment from `DOIDO_ENV`.
13    ///
14    /// Recognized values are `development`, `test`, and `production`. An unset
15    /// or unrecognized value falls back to [`Environment::Development`].
16    pub fn get_env() -> Environment {
17        match std::env::var("DOIDO_ENV").as_deref() {
18            Ok("production") => Environment::Production,
19            Ok("test") => Environment::Test,
20            _ => Environment::Development,
21        }
22    }
23
24    /// Lowercase name used for the `config/<env>.yml` file and for display.
25    pub fn as_str(&self) -> &'static str {
26        match self {
27            Environment::Development => "development",
28            Environment::Test => "test",
29            Environment::Production => "production",
30        }
31    }
32}
33
34impl std::fmt::Display for Environment {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        f.write_str(self.as_str())
37    }
38}