pub struct ConfigLoader { /* private fields */ }Expand description
Configuration loader with precedence-based resolution
Implements the 5-layer configuration precedence system:
- Environment variables (highest priority)
- Project-specific config (
{workspace}/config/{module}.{env}.toml) - User config (
~/.config/iron/{module}.toml) - Workspace defaults (
{workspace}/config/{module}.default.toml) - Crate defaults (lowest priority)
§Examples
ⓘ
use iron_config_loader::ConfigLoader;
use serde::Deserialize;
#[derive(Deserialize)]
struct DatabaseConfig {
url: String,
max_connections: u32,
}
let loader = ConfigLoader::new("iron_token_manager")?;
let db_config: DatabaseConfig = loader.get("database")?;Implementations§
Source§impl ConfigLoader
impl ConfigLoader
Sourcepub fn with_defaults(module: impl Into<String>, defaults: &str) -> Result<Self>
pub fn with_defaults(module: impl Into<String>, defaults: &str) -> Result<Self>
Create configuration loader with custom default values
§Arguments
module- Module namedefaults- Default configuration as TOML string
§Examples
ⓘ
let defaults = r#"
[database]
url = "sqlite:///:memory:"
max_connections = 5
"#;
let loader = ConfigLoader::with_defaults("iron_token_manager", defaults)?;Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<T>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<T>
Sourcepub fn get_section<T: DeserializeOwned>(&self, prefix: &str) -> Result<T>
pub fn get_section<T: DeserializeOwned>(&self, prefix: &str) -> Result<T>
Get configuration subtree as struct
§Arguments
prefix- Key prefix (e.g., “database”)
§Errors
Returns error if subtree cannot be deserialized.
§Examples
ⓘ
#[derive(Deserialize)]
struct DatabaseConfig {
url: String,
max_connections: u32,
}
let db_config: DatabaseConfig = loader.get_section("database")?;Sourcepub fn get_with_source<T: DeserializeOwned>(
&self,
key: &str,
) -> Result<(T, String)>
pub fn get_with_source<T: DeserializeOwned>( &self, key: &str, ) -> Result<(T, String)>
Sourcepub fn debug_summary(&self) -> String
pub fn debug_summary(&self) -> String
Print configuration summary for debugging
Shows all resolved configuration values with their sources.