anthill-di-configuration-extension
Rust configuration integrated in anthill-di
Warning
Library required Rust nightly for trait as interface (Unsize)
Basic concepts
Always register the configuration source first
At this stage, json loading via serde_json is implemented, but you can extend the functionality by implementing trait IService
fn _() {
let root_context = DependencyContext::new_root()
root_context.register_source(|_| Ok(JsonFileConfiguration::<Test>::new("configuration_test.json".to_string()))).await.unwrap();
}
You can then register a config type or a custom snapshot wrapper
fn _() {
root_context.register_configuration::<Test, JsonFileConfiguration::<Test>>(DependencyLifeCycle::Transient).await.unwrap();
let configuration: Test = root_context.resolve().await.unwrap();
}
fn _() {
root_context.register_snapshot::<Test, JsonFileConfiguration::<Test>>(DependencyLifeCycle::Transient).await.unwrap();
let configuration_snapshot: ConfigurationSnapshot<Test, JsonFileConfiguration::<Test>> = root_context.resolve().await.unwrap();
}
To synchronize data with the source, ConfigurationSnapshot has a sync method
fn _() {
root_context.register_snapshot::<Test, JsonFileConfiguration::<Test>>(DependencyLifeCycle::Transient).await.unwrap();
let configuration_snapshot: ConfigurationSnapshot<Test, JsonFileConfiguration::<Test>> = root_context.resolve().await.unwrap();
configuration_snapshot.sync().await.unwrap();
}
See examples present in src/tests folder