Struct smart_config::ConfigRepository
source · pub struct ConfigRepository<'a> { /* private fields */ }
Expand description
Configuration repository containing zero or more configuration sources.
Sources are preprocessed and merged according to the provided ConfigSchema
.
§Merging sources
Self::with()
merges a new source into this repo. The new source has higher priority and will overwrite
values defined in old sources, including via parameter aliases.
§Type coercion
When processing ConfigSource
s, values can be coerced depending on the expected type
at the corresponding location as indicated by the param deserializer.
Currently, coercion only happens if the original value is a string.
- If the expected type is
BasicTypes::INTEGER
,BasicTypes::FLOAT
, orBasicTypes::BOOL
, the number / Boolean is parsed from the string. If parsing succeeds, the value is replaced.
Coercion is not performed if the param deserializer doesn’t specify an expected type.
This means that it’s possible to supply values for structured params from env vars without much hassle:
use smart_config::{testing, DescribeConfig, DeserializeConfig, Environment};
#[derive(Debug, DescribeConfig, DeserializeConfig)]
struct CoercingConfig {
flag: bool,
ints: Vec<u64>,
map: HashMap<String, u32>,
}
let mut env = Environment::from_iter("APP_", [
("APP_FLAG", "true"),
("APP_INTS__JSON", "[2, 3, 5]"),
("APP_MAP__JSON", r#"{ "value": 5 }"#),
]);
// Coerce `__json`-suffixed env vars to JSON
env.coerce_json()?;
// `testing` functions create a repository internally
let config: CoercingConfig = testing::test(env)?;
assert!(config.flag);
assert_eq!(config.ints, [2, 3, 5]);
assert_eq!(config.map, HashMap::from([("value".into(), 5)]));
§Other preprocessing
Besides type coercion, sources undergo a couple of additional transforms:
- Garbage collection: All values not corresponding to params or their ancestor objects are removed.
- Hiding secrets: Values corresponding to secret params are wrapped in opaque, zero-on-drop wrappers.
Implementations§
source§impl<'a> ConfigRepository<'a>
impl<'a> ConfigRepository<'a>
sourcepub fn new(schema: &'a ConfigSchema) -> Self
pub fn new(schema: &'a ConfigSchema) -> Self
Creates an empty config repo based on the provided schema.
sourcepub fn schema(&self) -> &'a ConfigSchema
pub fn schema(&self) -> &'a ConfigSchema
Returns the wrapped configuration schema.
sourcepub fn deserializer_options(&mut self) -> &mut DeserializerOptions
pub fn deserializer_options(&mut self) -> &mut DeserializerOptions
Accesses options used during serde
-powered deserialization.
sourcepub fn with<S: ConfigSource>(self, source: S) -> Self
pub fn with<S: ConfigSource>(self, source: S) -> Self
Extends this environment with a new configuration source.
sourcepub fn with_all(self, sources: ConfigSources) -> Self
pub fn with_all(self, sources: ConfigSources) -> Self
Extends this environment with a multiple configuration sources.
sourcepub fn sources(&self) -> &[SourceInfo]
pub fn sources(&self) -> &[SourceInfo]
Provides information about sources merged in this repository.
sourcepub fn iter(&self) -> impl Iterator<Item = ConfigParser<'_, ()>> + '_
pub fn iter(&self) -> impl Iterator<Item = ConfigParser<'_, ()>> + '_
Iterates over parsers for all configs in the schema.
sourcepub fn single<C: DeserializeConfig>(&self) -> Result<ConfigParser<'_, C>>
pub fn single<C: DeserializeConfig>(&self) -> Result<ConfigParser<'_, C>>
Returns a parser for the single configuration of the specified type.
§Errors
Errors if the config is not a part of the schema or is mounted to multiple locations.
sourcepub fn get<'s, C: DeserializeConfig>(
&'s self,
prefix: &'s str,
) -> Option<ConfigParser<'s, C>>
pub fn get<'s, C: DeserializeConfig>( &'s self, prefix: &'s str, ) -> Option<ConfigParser<'s, C>>
Gets a parser for a configuration of the specified type mounted at the canonical prefix
.
If the config is not present at prefix
, returns None
.
Trait Implementations§
source§impl<'a> Clone for ConfigRepository<'a>
impl<'a> Clone for ConfigRepository<'a>
source§fn clone(&self) -> ConfigRepository<'a>
fn clone(&self) -> ConfigRepository<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl<'a> Freeze for ConfigRepository<'a>
impl<'a> !RefUnwindSafe for ConfigRepository<'a>
impl<'a> Send for ConfigRepository<'a>
impl<'a> Sync for ConfigRepository<'a>
impl<'a> Unpin for ConfigRepository<'a>
impl<'a> !UnwindSafe for ConfigRepository<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)