use crate::facade::config::live::statics::StaticLiveConfig;
use crate::{AppConfig, AppConfigError};
use config::ConfigBuilder;
use serde::de::DeserializeOwned;
#[cfg(feature = "config-async")]
use config::builder::AsyncState;
#[cfg(not(feature = "config-async"))]
use config::builder::DefaultState;
pub mod statics;
pub struct AppLiveConfig;
#[cfg(not(feature = "config-async"))]
impl AppLiveConfig {
pub fn get() -> AppConfig {
Self::try_get().unwrap_or_else(|error| {
panic!(
"failed to load or parse the application’s live configuration: {}",
error,
);
})
}
pub fn try_get() -> Result<AppConfig, AppConfigError> {
StaticLiveConfig::get_config_lock()
.read()
.clone()?
.try_deserialize()
.map_err(AppConfigError::from)
}
pub fn section<T>(key: impl AsRef<str>) -> T
where
T: DeserializeOwned,
{
let key = key.as_ref();
Self::try_section(key).unwrap_or_else(|error| {
panic!(
"failed to load or parse the application’s live configuration section '{}': {}",
key, error,
);
})
}
pub fn try_section<T>(key: impl AsRef<str>) -> Result<T, AppConfigError>
where
T: DeserializeOwned,
{
StaticLiveConfig::get_config_lock()
.read()
.as_ref()
.map_err(AppConfigError::clone)?
.get(key.as_ref())
.map_err(AppConfigError::from)
}
pub fn clone_builder() -> ConfigBuilder<DefaultState> {
StaticLiveConfig::read_builder().as_ref().unwrap().clone()
}
pub fn set_builder(builder: ConfigBuilder<DefaultState>) {
StaticLiveConfig::set_builder(builder);
}
pub fn refresh() {
StaticLiveConfig::refresh_config();
}
}
#[cfg(feature = "config-async")]
impl AppLiveConfig {
pub async fn get() -> AppConfig {
Self::try_get().await.unwrap_or_else(|error| {
panic!(
"failed to load or parse the application’s live configuration: {}",
error,
);
})
}
pub async fn try_get() -> Result<AppConfig, AppConfigError> {
StaticLiveConfig::get_config_lock()
.await
.read()
.await
.clone()?
.try_deserialize()
.map_err(AppConfigError::from)
}
pub async fn section<T>(key: impl AsRef<str>) -> T
where
T: DeserializeOwned,
{
let key = key.as_ref();
Self::try_section(key).await.unwrap_or_else(|error| {
panic!(
"failed to load or parse the application’s live configuration section '{}': {}",
key, error,
);
})
}
pub async fn try_section<T>(key: impl AsRef<str>) -> Result<T, AppConfigError>
where
T: DeserializeOwned,
{
StaticLiveConfig::get_config_lock()
.await
.read()
.await
.as_ref()
.map_err(AppConfigError::clone)?
.get(key.as_ref())
.map_err(AppConfigError::from)
}
pub fn clone_builder() -> ConfigBuilder<AsyncState> {
StaticLiveConfig::read_builder().as_ref().unwrap().clone()
}
pub fn set_builder(builder: ConfigBuilder<AsyncState>) {
StaticLiveConfig::set_builder(builder);
}
pub async fn refresh() {
StaticLiveConfig::refresh_config().await;
}
}