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 ConfigSources, 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.

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>

source

pub fn new(schema: &'a ConfigSchema) -> Self

Creates an empty config repo based on the provided schema.

source

pub fn schema(&self) -> &'a ConfigSchema

Returns the wrapped configuration schema.

source

pub fn deserializer_options(&mut self) -> &mut DeserializerOptions

Accesses options used during serde-powered deserialization.

source

pub fn with<S: ConfigSource>(self, source: S) -> Self

Extends this environment with a new configuration source.

source

pub fn with_all(self, sources: ConfigSources) -> Self

Extends this environment with a multiple configuration sources.

source

pub fn sources(&self) -> &[SourceInfo]

Provides information about sources merged in this repository.

source

pub fn iter(&self) -> impl Iterator<Item = ConfigParser<'_, ()>> + '_

Iterates over parsers for all configs in the schema.

source

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.

source

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>

source§

fn clone(&self) -> ConfigRepository<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for ConfigRepository<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto 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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more