diff --git a/src/config.rs b/src/config.rs index d5c6d4d..f310180 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,3 +39,69 @@ pub enum Repository { pub enum Metrics { Prometheus(metrics::Config), } + +pub fn validate(config: &Config) -> Result<(), Box> { + if config.clock_cycle_interval.is_zero() { + return Err("clock cycle interval cannot be zero".into()); + } + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn config() -> Config { + Config { + automigrate: true, + log_level: log::Level::Info, + clock_cycle_interval: time::Duration::from_millis(100), + metrics: Metrics::Prometheus(metrics::Config { + port: 3000, + endpoint: String::from("/metrics"), + }), + repository: Repository::InMemory, + reset_state: true, + transmitter: Transmitter::Nats(nats::Config { + port: 3001, + host: String::from("127.0.0.1"), + }), + transport: Transport::Grpc(grpc::Config { port: 3002 }), + } + } + + #[test] + fn test_validate_config() { + struct TestCase { + name: String, + config: Config, + expected_valid: bool, + } + + let test_cases = vec![ + TestCase { + name: String::from("valid"), + config: config(), + expected_valid: true, + }, + TestCase { + name: String::from("zero duration"), + config: Config { + clock_cycle_interval: time::Duration::from_secs(0), + ..config() + }, + expected_valid: false, + }, + ]; + + for test_case in test_cases { + let valid = validate(&test_case.config).is_ok(); + assert_eq!( + valid, test_case.expected_valid, + "test case failed: {}", + test_case.name + ); + } + } +} diff --git a/src/load_config.rs b/src/load_config.rs index c5c51cb..f723629 100644 --- a/src/load_config.rs +++ b/src/load_config.rs @@ -63,7 +63,11 @@ pub fn load_config(file_path: &str) -> Result> { let secrets = load_secrets_from_env()?; - derive_config(config, secrets) + let config = derive_config(config, secrets)?; + + config::validate(&config)?; + + Ok(config) } fn derive_config(config: FileConfig, secrets: EnvConfig) -> Result> {