135 lines
3.5 KiB
Rust
135 lines
3.5 KiB
Rust
|
|
/// Application environment.
|
||
|
|
#[derive(Debug, PartialEq, Eq, Default)]
|
||
|
|
pub enum Environment {
|
||
|
|
/// Development environment
|
||
|
|
#[default]
|
||
|
|
Development,
|
||
|
|
/// Production environment
|
||
|
|
Production,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl std::fmt::Display for Environment {
|
||
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
|
|
let self_str = match self {
|
||
|
|
Self::Development => "development",
|
||
|
|
Self::Production => "production",
|
||
|
|
};
|
||
|
|
write!(f, "{self_str}")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl TryFrom<String> for Environment {
|
||
|
|
type Error = String;
|
||
|
|
|
||
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||
|
|
Self::try_from(value.as_str())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl TryFrom<&str> for Environment {
|
||
|
|
type Error = String;
|
||
|
|
|
||
|
|
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||
|
|
match value.to_lowercase().as_str() {
|
||
|
|
"development" | "dev" => Ok(Self::Development),
|
||
|
|
"production" | "prod" => Ok(Self::Production),
|
||
|
|
other => Err(format!(
|
||
|
|
"{other} is not a supported environment. Use either `development` or `production`"
|
||
|
|
)),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_display_development() {
|
||
|
|
let env = Environment::Development;
|
||
|
|
assert_eq!(env.to_string(), "development");
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_display_production() {
|
||
|
|
let env = Environment::Production;
|
||
|
|
assert_eq!(env.to_string(), "production");
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_from_str_development() {
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("development").unwrap(),
|
||
|
|
Environment::Development
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("dev").unwrap(),
|
||
|
|
Environment::Development
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("Development").unwrap(),
|
||
|
|
Environment::Development
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("DEV").unwrap(),
|
||
|
|
Environment::Development
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_from_str_production() {
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("production").unwrap(),
|
||
|
|
Environment::Production
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("prod").unwrap(),
|
||
|
|
Environment::Production
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("Production").unwrap(),
|
||
|
|
Environment::Production
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("PROD").unwrap(),
|
||
|
|
Environment::Production
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_from_str_invalid() {
|
||
|
|
let result = Environment::try_from("invalid");
|
||
|
|
assert!(result.is_err());
|
||
|
|
assert!(result.unwrap_err().contains("not a supported environment"));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_from_string_development() {
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("development".to_string()).unwrap(),
|
||
|
|
Environment::Development
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_from_string_production() {
|
||
|
|
assert_eq!(
|
||
|
|
Environment::try_from("production".to_string()).unwrap(),
|
||
|
|
Environment::Production
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_from_string_invalid() {
|
||
|
|
let result = Environment::try_from("invalid".to_string());
|
||
|
|
assert!(result.is_err());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn environment_default_is_development() {
|
||
|
|
let env = Environment::default();
|
||
|
|
assert_eq!(env, Environment::Development);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|