[update] added validation for AppSettings
with ConfigError
, ensuring non-zero port enforcement in config.rs
This commit is contained in:
@@ -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");
|
||||
|
Reference in New Issue
Block a user