diff --git a/backend-rust/src/config.rs b/backend-rust/src/config.rs index e671988..af57368 100644 --- a/backend-rust/src/config.rs +++ b/backend-rust/src/config.rs @@ -1,5 +1,5 @@ use serde::Deserialize; -use std::env; +use std::{env, fmt}; use std::path::PathBuf; use toml::Value; use tracing::{error, info}; @@ -28,7 +28,29 @@ struct ConfigFile { server: Server, } +#[derive(Debug)] +pub enum ConfigError { + InvalidPort, +} + +impl fmt::Display for ConfigError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ConfigError::InvalidPort => write!(f, "Invalid port: port cannot be 0"), + } + } +} + +impl std::error::Error for ConfigError {} + impl AppSettings { + pub fn validate(&self) -> Result<(), ConfigError> { + if self.config.server.port == 0 { + return Err(ConfigError::InvalidPort); + } + Ok(()) + } + pub fn get_app_settings() -> Self { let config_file = Self::load_config_file().unwrap_or_else(|| { info!("Using default config values");