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
impl Config
Sourcepub fn from_pairs<K, V>(pairs: impl IntoIterator<Item = (K, V)>) -> Self
pub fn from_pairs<K, V>(pairs: impl IntoIterator<Item = (K, V)>) -> Self
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.
Sourcepub fn from_value(value: Value) -> Result<Self>
pub fn from_value(value: Value) -> Result<Self>
Creates configuration from a JSON object value.
Sourcepub fn from_json_str(source: &str) -> Result<Self>
pub fn from_json_str(source: &str) -> Result<Self>
Creates configuration from a JSON object string.
Sourcepub fn from_json_file(path: impl AsRef<Path>) -> Result<Self>
pub fn from_json_file(path: impl AsRef<Path>) -> Result<Self>
Creates configuration from a JSON object file.
Sourcepub fn from_env_prefix(prefix: &str) -> Self
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.
Sourcepub fn from_prefixed_vars<K, V>(
prefix: &str,
vars: impl IntoIterator<Item = (K, V)>,
) -> Self
pub fn from_prefixed_vars<K, V>( prefix: &str, vars: impl IntoIterator<Item = (K, V)>, ) -> Self
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.
Sourcepub fn insert_value(&mut self, key: impl Into<String>, value: Value)
pub fn insert_value(&mut self, key: impl Into<String>, value: Value)
Inserts a raw JSON configuration value.
Sourcepub fn get_typed<T>(&self, key: &str) -> Result<Option<T>>where
T: DeserializeOwned,
pub fn get_typed<T>(&self, key: &str) -> Result<Option<T>>where
T: DeserializeOwned,
Deserializes a top-level configuration value into a typed value.
Sourcepub fn get_required_typed<T>(&self, key: &str) -> Result<T>where
T: DeserializeOwned,
pub fn get_required_typed<T>(&self, key: &str) -> Result<T>where
T: DeserializeOwned,
Deserializes a required top-level configuration value into a typed value.
Sourcepub fn get_path<I, S>(&self, path: I) -> Option<&Value>
pub fn get_path<I, S>(&self, path: I) -> Option<&Value>
Returns a nested raw configuration value by path.
Object segments match keys. Array segments are zero-based numeric
indexes such as "0".
Sourcepub fn get_path_typed<I, S, T>(&self, path: I) -> Result<Option<T>>
pub fn get_path_typed<I, S, T>(&self, path: I) -> Result<Option<T>>
Deserializes a nested configuration value into a typed value.
Sourcepub fn get_required_path_typed<I, S, T>(&self, path: I) -> Result<T>
pub fn get_required_path_typed<I, S, T>(&self, path: I) -> Result<T>
Deserializes a required nested configuration value into a typed value.
Sourcepub fn merge(self, other: Self) -> Self
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.
Sourcepub fn merge_from(&mut self, other: Self)
pub fn merge_from(&mut self, other: Self)
Merges another configuration source into this configuration in place.
Sourcepub fn deserialize<T>(&self) -> Result<T>where
T: DeserializeOwned,
pub fn deserialize<T>(&self) -> Result<T>where
T: DeserializeOwned,
Deserializes the configuration into a strongly typed settings struct.