use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, RwLock};
use super::{ConfigError, ConfigSource};
pub type HydrateFn =
fn(Arc<dyn ConfigSource>) -> Pin<Box<dyn Future<Output = Result<(), ConfigError>> + Send>>;
static REGISTRY: RwLock<Vec<HydrateFn>> = RwLock::new(Vec::new());
#[doc(hidden)]
pub fn register(hydrate: HydrateFn) {
REGISTRY
.write()
.expect("config hydrate registry")
.push(hydrate);
}
pub async fn load(source: impl ConfigSource + 'static) -> Result<(), ConfigError> {
let source: Arc<dyn ConfigSource> = Arc::new(source);
source.load().await?;
let hydrates = REGISTRY.read().expect("config hydrate registry").clone();
for hydrate in hydrates {
hydrate(source.clone()).await?;
}
Ok(())
}