2023-11-29 13:27:46 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2023-11-30 00:43:56 +00:00
|
|
|
use chrono::Datelike;
|
2023-11-29 00:03:04 +00:00
|
|
|
use log::LevelFilter;
|
|
|
|
use simple_logger::SimpleLogger;
|
|
|
|
|
2023-12-05 00:42:19 +00:00
|
|
|
use crate::errors::EmgauwaError;
|
2023-11-30 00:43:56 +00:00
|
|
|
use crate::types::Weekday;
|
|
|
|
|
2023-12-05 00:42:19 +00:00
|
|
|
pub fn load_settings<T>(config_name: &str, env_prefix: &str) -> Result<T, EmgauwaError>
|
2023-11-23 15:00:24 +00:00
|
|
|
where
|
2023-11-27 11:49:40 +00:00
|
|
|
for<'de> T: serde::Deserialize<'de>,
|
2023-11-23 15:00:24 +00:00
|
|
|
{
|
2023-11-27 11:49:40 +00:00
|
|
|
let default_file = config::File::with_name(&format!("emgauwa-{}", config_name)).required(false);
|
2023-11-23 15:00:24 +00:00
|
|
|
|
|
|
|
config::Config::builder()
|
|
|
|
.add_source(default_file)
|
|
|
|
.add_source(
|
|
|
|
config::Environment::with_prefix(&format!("EMGAUWA_{}", env_prefix))
|
|
|
|
.prefix_separator("__")
|
|
|
|
.separator("__"),
|
|
|
|
)
|
2023-12-05 00:42:19 +00:00
|
|
|
.build()?
|
2023-11-23 15:00:24 +00:00
|
|
|
.try_deserialize::<T>()
|
2023-12-05 00:42:19 +00:00
|
|
|
.map_err(EmgauwaError::from)
|
2023-11-27 11:49:40 +00:00
|
|
|
}
|
2023-11-29 00:03:04 +00:00
|
|
|
|
2023-12-05 00:42:19 +00:00
|
|
|
pub fn init_logging(level: &str) -> Result<(), EmgauwaError> {
|
|
|
|
let log_level: LevelFilter = LevelFilter::from_str(level)
|
2023-12-05 00:51:06 +00:00
|
|
|
.map_err(|_| EmgauwaError::Other(format!("Invalid log level: {}", level)))?;
|
2023-11-29 00:03:04 +00:00
|
|
|
log::trace!("Log level set to {:?}", log_level);
|
|
|
|
|
|
|
|
SimpleLogger::new()
|
|
|
|
.with_level(log_level)
|
|
|
|
.init()
|
2023-12-05 00:51:06 +00:00
|
|
|
.map_err(|err| EmgauwaError::Other(format!("Failed to initialize logger: {}", err)))?;
|
2023-12-05 00:42:19 +00:00
|
|
|
|
|
|
|
Ok(())
|
2023-11-29 00:03:04 +00:00
|
|
|
}
|
2023-11-30 00:43:56 +00:00
|
|
|
|
|
|
|
pub fn get_weekday() -> Weekday {
|
|
|
|
(chrono::offset::Local::now()
|
|
|
|
.date_naive()
|
|
|
|
.weekday()
|
|
|
|
.number_from_monday()
|
|
|
|
- 1) as Weekday
|
|
|
|
}
|