Struct confik::ConfigBuilder

source ·
pub struct ConfigBuilder<'a, Target: Configuration> { /* private fields */ }
Expand description

Used to accumulate ordered sources from which its Target is to be built.

An instance of this can be created via Configuration::builder or ConfigBuilder::<T>::default.

§Examples

Using Configuration::builder:

use confik::{Configuration, TomlSource};

#[derive(Debug, PartialEq, Configuration)]
struct MyConfigType {
    param: String,
}

let config = MyConfigType::builder()
    .override_with(TomlSource::new(r#"param = "Hello World""#))
    .try_build()
    .expect("Failed to build");

assert_eq!(config.param, "Hello World");

Using ConfigBuilder::<T>::default:

use confik::{ConfigBuilder, Configuration, TomlSource};

#[derive(Debug, PartialEq, Configuration)]
struct MyConfigType {
    param: String,
}

let config = ConfigBuilder::<MyConfigType>::default()
    .override_with(TomlSource::new(r#"param = "Hello World""#))
    .try_build()
    .expect("Failed to build");

assert_eq!(config.param, "Hello World");

Implementations§

source§

impl<'a, Target: Configuration> ConfigBuilder<'a, Target>

source

pub fn override_with(&mut self, source: impl Source + 'a) -> &mut Self

Add a single Source to the list of sources.

The source is added at the end of the list, overriding existing sources.

use confik::{Configuration, TomlSource};
#[derive(Debug, PartialEq, Configuration)]
struct MyConfigType {
    param: String,
}

let config = MyConfigType::builder()
    .override_with(TomlSource::new(r#"param = "Hello World""#))
    .override_with(TomlSource::new(r#"param = "Hello Universe""#))
    .try_build()
    .expect("Failed to build");

assert_eq!(config.param, "Hello Universe");
source

pub fn try_build(&mut self) -> Result<Target, Error>

Attempt to build from the provided sources.

Examples found in repository?
examples/defaulting.rs (line 26)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fn main() {
    let config = Config::builder()
        .try_build()
        .expect("Failed to parse config");

    assert_eq!(
        config,
        Config {
            field1: 5,
            field2: String::from("Hello World"),
            data: Data {
                elements: vec![4, 3, 2, 1],
            },
        }
    );
}

Trait Implementations§

source§

impl<'a, Target: Configuration> Default for ConfigBuilder<'a, Target>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, Target> Freeze for ConfigBuilder<'a, Target>

§

impl<'a, Target> !RefUnwindSafe for ConfigBuilder<'a, Target>

§

impl<'a, Target> !Send for ConfigBuilder<'a, Target>

§

impl<'a, Target> !Sync for ConfigBuilder<'a, Target>

§

impl<'a, Target> Unpin for ConfigBuilder<'a, Target>

§

impl<'a, Target> !UnwindSafe for ConfigBuilder<'a, Target>

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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