prima_tracing/config/
environment.rs

1use std::{
2    error::Error,
3    fmt::{Display, Formatter},
4    str::FromStr,
5};
6
7/// All the possible environments in which the application can run.
8#[derive(Eq, PartialEq, Debug, Clone, Copy)]
9pub enum Environment {
10    Dev,
11    Staging,
12    Production,
13}
14
15impl FromStr for Environment {
16    type Err = EnvironmentParseError;
17
18    fn from_str(s: &str) -> Result<Self, Self::Err> {
19        match s {
20            "dev" => Ok(Self::Dev),
21            "staging" => Ok(Self::Staging),
22            "production" => Ok(Self::Production),
23            _ => Err(EnvironmentParseError(s.to_string())),
24        }
25    }
26}
27
28impl Display for Environment {
29    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30        let str = match self {
31            Self::Dev => "dev",
32            Self::Staging => "staging",
33            Self::Production => "production",
34        };
35        f.write_str(str)
36    }
37}
38
39#[derive(Debug)]
40pub struct EnvironmentParseError(String);
41
42impl Error for EnvironmentParseError {}
43
44impl Display for EnvironmentParseError {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        f.write_fmt(format_args!(
47            "{} is not a valid environment string. Allowed strings are 'dev', 'staging' and 'production'.",
48            &self.0
49        ))
50    }
51}