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 for Environment { type Error = String; fn try_from(value: String) -> Result { 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 { s.to_owned().try_into() } }