echoes-of-ascension/app/src/config.rs
2025-01-26 17:18:14 +02:00

48 lines
1.2 KiB
Rust

use serde::Deserialize;
use serde_aux::field_attributes::deserialize_number_from_string;
use std::{fmt::Display, str::FromStr};
#[derive(Debug, Deserialize, Clone)]
pub struct ApplicationSettings {
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
pub host: String,
}
#[derive(Debug, Clone)]
pub enum Environment {
Local,
Production,
}
impl Display for Environment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Environment::Local => write!(f, "local"),
Environment::Production => write!(f, "production"),
}
}
}
impl TryFrom<String> for Environment {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.to_lowercase().as_str() {
"local" => Ok(Self::Local),
"production" => Ok(Self::Production),
other => Err(format!(
"{} is not supported environment. \
Use either `local` or `production`.",
other
)),
}
}
}
impl FromStr for Environment {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.to_owned().try_into()
}
}