Skip to main content

Config

Struct Config 

Source
pub struct Config { /* private fields */ }
Expand description

Typed configuration document assembled from explicit sources.

Sources are explicit and merge in the order you choose. Later sources override earlier values, while nested objects merge recursively.

use nidus_config::Config;
use serde::Deserialize;

#[derive(Deserialize, Debug, PartialEq, Eq)]
struct Settings {
    port: u16,
    database: DatabaseSettings,
}

#[derive(Deserialize, Debug, PartialEq, Eq)]
struct DatabaseSettings {
    url: String,
    pool_size: u32,
}

let file = Config::from_json_str(r#"{
    "port": 3000,
    "database": { "url": "postgres://localhost/app", "pool_size": 5 }
}"#)?;

let env = Config::from_prefixed_vars("APP", [
    ("APP_PORT", "8080"),
    ("APP_DATABASE__POOL_SIZE", "10"),
]);

let settings: Settings = file.merge(env).deserialize()?;
assert_eq!(settings.port, 8080);
assert_eq!(settings.database.url, "postgres://localhost/app");
assert_eq!(settings.database.pool_size, 10);

Implementations§

Source§

impl Config

Source

pub fn new() -> Self

Creates an empty configuration document.

Source

pub fn from_pairs<K, V>(pairs: impl IntoIterator<Item = (K, V)>) -> Self
where K: Into<String>, V: AsRef<str>,

Creates configuration from key/value pairs.

Values are parsed as JSON-like scalars, so strings such as "true", "42", and "null" become boolean, number, and null values.

Source

pub fn from_value(value: Value) -> Result<Self>

Creates configuration from a JSON object value.

Source

pub fn from_json_str(source: &str) -> Result<Self>

Creates configuration from a JSON object string.

Source

pub fn from_json_file(path: impl AsRef<Path>) -> Result<Self>

Creates configuration from a JSON object file.

Source

pub fn from_env_prefix(prefix: &str) -> Self

Creates configuration from process environment variables with a prefix.

For prefix APP, APP_PORT=3000 maps to port, and APP_DATABASE__URL=... maps to database.url.

Source

pub fn from_prefixed_vars<K, V>( prefix: &str, vars: impl IntoIterator<Item = (K, V)>, ) -> Self
where K: AsRef<str>, V: AsRef<str>,

Creates configuration from prefixed key/value variables.

This is useful for deterministic tests and for loading from custom environment sources. Prefix matching is case-sensitive; keys are normalized to lowercase after the prefix is removed. Double underscores create nested paths; empty path segments are ignored.

Source

pub fn insert_value(&mut self, key: impl Into<String>, value: Value)

Inserts a raw JSON configuration value.

Source

pub fn get(&self, key: &str) -> Option<&Value>

Returns a top-level raw configuration value.

Source

pub fn get_typed<T>(&self, key: &str) -> Result<Option<T>>

Deserializes a top-level configuration value into a typed value.

Source

pub fn get_required_typed<T>(&self, key: &str) -> Result<T>

Deserializes a required top-level configuration value into a typed value.

Source

pub fn get_path<I, S>(&self, path: I) -> Option<&Value>
where I: IntoIterator<Item = S>, S: AsRef<str>,

Returns a nested raw configuration value by path.

Object segments match keys. Array segments are zero-based numeric indexes such as "0".

Source

pub fn get_path_typed<I, S, T>(&self, path: I) -> Result<Option<T>>
where I: IntoIterator<Item = S>, S: AsRef<str>, T: DeserializeOwned,

Deserializes a nested configuration value into a typed value.

Source

pub fn get_required_path_typed<I, S, T>(&self, path: I) -> Result<T>
where I: IntoIterator<Item = S>, S: AsRef<str>, T: DeserializeOwned,

Deserializes a required nested configuration value into a typed value.

Source

pub fn merge(self, other: Self) -> Self

Merges another configuration source into this one.

Values from other take precedence. Nested objects are merged recursively so later sources can override one field without replacing an entire nested configuration section.

Source

pub fn merge_from(&mut self, other: Self)

Merges another configuration source into this configuration in place.

Source

pub fn deserialize<T>(&self) -> Result<T>

Deserializes the configuration into a strongly typed settings struct.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

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 Debug for Config

Source§

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

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

impl Default for Config

Source§

fn default() -> Config

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

Auto Trait Implementations§

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

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.