use std::str::FromStr; use chrono::Datelike; use log::LevelFilter; use simple_logger::SimpleLogger; use crate::errors::EmgauwaError; use crate::types::Weekday; pub fn load_settings(config_name: &str, env_prefix: &str) -> Result where for<'de> T: serde::Deserialize<'de>, { let default_file = config::File::with_name(&format!("emgauwa-{}", config_name)).required(false); config::Config::builder() .add_source(default_file) .add_source( config::Environment::with_prefix(&format!("EMGAUWA_{}", env_prefix)) .prefix_separator("__") .separator("__"), ) .build()? .try_deserialize::() .map_err(EmgauwaError::from) } pub fn init_logging(level: &str) -> Result<(), EmgauwaError> { let log_level: LevelFilter = LevelFilter::from_str(level) .map_err(|_| EmgauwaError::Other(format!("Invalid log level: {}", level)))?; log::trace!("Log level set to {:?}", log_level); SimpleLogger::new() .with_level(log_level) .init() .map_err(|err| EmgauwaError::Other(format!("Failed to initialize logger: {}", err)))?; Ok(()) } pub fn get_weekday() -> Weekday { (chrono::offset::Local::now() .date_naive() .weekday() .number_from_monday() - 1) as Weekday }