caretta-sync/core/src/global/config.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

2025-08-20 06:50:35 +09:00
#[cfg(any(test,feature="test"))]
2025-08-18 08:22:20 +09:00
use tempfile::TempDir;
use tokio::sync::OnceCell;
2025-06-20 07:28:51 +09:00
2025-09-03 13:22:31 +09:00
use crate::{config::{Config, ConfigError, PartialIrohConfig, PartialRpcConfig, PartialStorageConfig, StorageConfig}, error::Error};
2025-06-20 07:28:51 +09:00
2025-08-18 08:22:20 +09:00
pub static CONFIG: GlobalConfig = GlobalConfig::const_new();
pub struct GlobalConfig {
inner: OnceCell<Config>
}
2025-06-22 11:29:44 +09:00
2025-08-18 08:22:20 +09:00
impl GlobalConfig {
pub const fn const_new() -> Self {
Self{
inner: OnceCell::const_new()
}
}
2025-08-21 07:40:33 +09:00
pub async fn get_or_init<T>(&'static self, config: Config) -> &'static Config where
T: Into<Config>{
2025-08-18 08:22:20 +09:00
self.inner.get_or_init(|| async {
2025-08-21 07:40:33 +09:00
config.into()
2025-08-18 08:22:20 +09:00
}).await
}
2025-08-20 06:50:35 +09:00
pub async fn get_or_try_init<T, E>(&'static self, config: T) -> Result<&'static Config, <T as TryInto<Config>>::Error> where
T: TryInto<Config>,
{
self.inner.get_or_try_init(|| async {
config.try_into()
}).await
}
2025-08-18 08:22:20 +09:00
pub fn get(&'static self) -> Option<&'static Config> {
self.inner.get()
}
pub fn get_unchecked(&'static self) -> &'static Config {
self.get().expect("Config must be initialized before use!")
}
2025-06-22 11:29:44 +09:00
}