anthill-di-configuration-extension

Crates.ioanthill-di-configuration-extension
lib.rsanthill-di-configuration-extension
version1.2.5
sourcesrc
created_at2022-04-22 14:20:28.004753
updated_at2022-05-05 11:53:46.35878
descriptionRust configuration integrated in anthill-di
homepagehttps://github.com/Vidrochka/anthill-di-configuration-extension
repositoryhttps://github.com/Vidrochka/anthill-di-configuration-extension
max_upload_size
id572179
size20,513
(Vidrochka)

documentation

README

build & tests

anthill-di-configuration-extension

Rust configuration integrated in anthill-di


Warning

Library required Rust nightly for anthill-di


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(), false))).await.unwrap();
}

You can then register a config type or a custom snapshot wrapper

fn _() {
    //let root_context = DependencyContext::new_root()
    //root_context.register_source(|_| Ok(JsonFileConfiguration::<Test>::new("configuration_test.json".to_string()))).await.unwrap();

    root_context.register_configuration::<Test, JsonFileConfiguration::<Test>>(DependencyLifeCycle::Transient).await.unwrap();

    let configuration: Test = root_context.resolve().await.unwrap();
}
fn _() {
    //let root_context = DependencyContext::new_root()
    //root_context.register_source(|_| Ok(JsonFileConfiguration::<Test>::new("configuration_test.json".to_string()))).await.unwrap();

    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 To save data, call store

fn _() {
    //let root_context = DependencyContext::new_root()
    //root_context.register_source(|_| Ok(JsonFileConfiguration::<Test>::new("configuration_test.json".to_string()))).await.unwrap();

    root_context.register_snapshot::<Test, JsonFileConfiguration::<Test>>(DependencyLifeCycle::Transient).await.unwrap();

    let configuration_snapshot: ConfigurationSnapshot<Test, JsonFileConfiguration::<Test>> = root_context.resolve().await.unwrap();

    // we have some configuration_snapshot.value from file
    // now change file
    // configuration_snapshot.value stay the same

    configuration_snapshot.sync().await.unwrap();

    // now we have configuration_snapshot.value from changed file

    // change configuration value
    configuration_snapshot.value.test_value = "new string".to_string();

    // store new value to file
    configuration_snapshot.store().await.unwrap();

    // now we have changed file
}

See examples present in src/tests folder

Minimal required version of anthill-di 1.2.3

Commit count: 8

cargo fmt