Context
Context is an important mechanism that allows for different default values, merge strategies, and validation rules to be used, for the same configuration struct, depending on context!
To begin, a context is a struct with a default implementation.
#![allow(unused)] fn main() { #[derive(Default)] struct ExampleContext { pub some_value: bool, pub another_value: usize, } }
Context must then be associated with a
Config
derived struct through the
context
attribute field.
#![allow(unused)] fn main() { #[derive(Config)] #[config(context = ExampleContext)] struct ExampleConfig { // ... } }
And then passed to the
ConfigLoader::load_with_context()
method.
#![allow(unused)] fn main() { let context = ExampleContext { some_value: true, another_value: 10, }; let result = ConfigLoader::<ExampleConfig>::new() .url(url_to_config)? .load_with_context(&context)?; }
Refer to the default values, merge strategies, and validation rules sections for more information on how to use context.