Skip to main content

ConfigBuilder

Struct ConfigBuilder 

Source
pub struct ConfigBuilder<S> { /* private fields */ }
Expand description

Builder for layered, typed configuration.

The state parameter makes loading impossible until a layer exists: ConfigBuilder::new returns ConfigBuilder<Empty>, every layer call returns ConfigBuilder<Ready>, and only the ready state exposes load / load_strict.

§Precedence

Layers apply in the order added; later layers win on colliding keys, with tables merging key-by-key (deep merge). Driven in the documented order this gives overrides > env > file:

ConfigBuilder::new()
    .layer(ConfigLayer::file("app.toml"))   // base
    .layer(ConfigLayer::env_prefix("APP_")) // deployment env wins over file
    .layer(ConfigLayer::overrides(over))    // in-process wins over both
    .load::<AppSettings>()?;

§Example

use config_kit::{ConfigBuilder, ConfigLayer};
use serde::Deserialize;
use std::collections::BTreeMap;

#[derive(Deserialize)]
struct Settings {
    port: u16,
    debug: bool,
}

std::env::set_var("CKIT_DOC_PORT", "8080");
std::env::set_var("CKIT_DOC_DEBUG", "true");
let mut over: BTreeMap<String, toml::Value> = BTreeMap::new();
over.insert("port".into(), toml::Value::Integer(9090));

let settings: Settings = ConfigBuilder::new()
    .layer(ConfigLayer::env_prefix("CKIT_DOC_"))
    .layer(ConfigLayer::overrides(over))
    .load()?;

assert_eq!(settings.port, 9090); // overrides beat env
assert!(settings.debug); // env value inferred as bool

Implementations§

Source§

impl ConfigBuilder<Empty>

Source

pub fn new() -> Self

A builder with no layers. Add at least one ConfigLayer to unlock loading.

Source§

impl<S> ConfigBuilder<S>

Source

pub fn layer(self, layer: ConfigLayer) -> ConfigBuilder<Ready>

Adds a layer and transitions the builder to the Ready state.

Layers take effect in the order added; see the type-level documentation for precedence and merge semantics.

Source§

impl ConfigBuilder<Ready>

Source

pub fn load<T: DeserializeOwned>(self) -> Result<T, ConfigError>

Deserializes the merged configuration into T.

Unknown keys are ignored (use load_strict to deny them); types must line up with T’s fields.

§Errors
  • ConfigError::Io when a file layer cannot be read,
  • ConfigError::Parse when a file layer fails to parse, names a format whose feature is disabled, or when the merged values do not fit T,
  • ConfigError::EnvVar when a matching environment variable has a non-Unicode value.
Source

pub fn load_strict<T: DeserializeOwned>(self) -> Result<T, ConfigError>

Strict load: fails with ConfigError::UnknownFields when any top-level key of the merged configuration is not consumed by a field of T.

The precise, honest contract:

  • Detection is container-agnostic — it works for any T: DeserializeOwned by instrumenting deserialization with serde_ignored and reporting which top-level map keys no field visited. It is not #[serde(deny_unknown_fields)] semantics (that derive is per-struct; config-kit never sees your schema declaration sites).
  • Top level only. Unknown keys nested inside tables ([db] → rogue = 1) are passed through to serde’s normal handling and are not reported. Keys introduced by env or override layers are checked identically to file keys.
  • Types using #[serde(flatten)] collect unknown keys into the flattened field before instrumentation can see them, so flattened content is exempt from this check.
  • If T itself derives deny_unknown_fields, its own error wins and surfaces as ConfigError::Parse.

On success the deserialized value is returned directly — there is no second parse pass.

§Errors

Everything load reports, plus ConfigError::UnknownFields when unconsumed top-level keys exist.

Trait Implementations§

Source§

impl<S: Clone> Clone for ConfigBuilder<S>

Source§

fn clone(&self) -> ConfigBuilder<S>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S: Debug> Debug for ConfigBuilder<S>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ConfigBuilder<Empty>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<S> Freeze for ConfigBuilder<S>
where PhantomData<S>: Freeze,

§

impl<S> RefUnwindSafe for ConfigBuilder<S>

§

impl<S> Send for ConfigBuilder<S>
where PhantomData<S>: Send,

§

impl<S> Sync for ConfigBuilder<S>
where PhantomData<S>: Sync,

§

impl<S> Unpin for ConfigBuilder<S>
where PhantomData<S>: Unpin,

§

impl<S> UnsafeUnpin for ConfigBuilder<S>

§

impl<S> UnwindSafe for ConfigBuilder<S>

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.