doido_cache/
environment.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Environment {
9 Development,
10 Test,
11 Production,
12}
13
14impl Environment {
15 pub fn get_env() -> Environment {
20 match std::env::var("DOIDO_ENV").as_deref() {
21 Ok("production") => Environment::Production,
22 Ok("test") => Environment::Test,
23 _ => Environment::Development,
24 }
25 }
26
27 pub fn as_str(&self) -> &'static str {
29 match self {
30 Environment::Development => "development",
31 Environment::Test => "test",
32 Environment::Production => "production",
33 }
34 }
35}
36
37impl std::fmt::Display for Environment {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 f.write_str(self.as_str())
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn as_str_maps_each_variant() {
49 assert_eq!(Environment::Development.as_str(), "development");
50 assert_eq!(Environment::Test.as_str(), "test");
51 assert_eq!(Environment::Production.as_str(), "production");
52 }
53
54 #[test]
55 fn display_matches_as_str() {
56 assert_eq!(Environment::Production.to_string(), "production");
57 }
58}