ConfigLoader

Struct ConfigLoader 

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

Configuration loader with precedence-based resolution

Implements the 5-layer configuration precedence system:

  1. Environment variables (highest priority)
  2. Project-specific config ({workspace}/config/{module}.{env}.toml)
  3. User config (~/.config/iron/{module}.toml)
  4. Workspace defaults ({workspace}/config/{module}.default.toml)
  5. 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

Source

pub fn new(module: impl Into<String>) -> Result<Self>

Create new configuration loader for module

§Arguments
  • module - Module name (e.g., “iron_token_manager”)
§Errors

Returns error if workspace cannot be detected.

§Examples
let loader = ConfigLoader::new("iron_token_manager")?;
Source

pub fn with_env( module: impl Into<String>, env: impl Into<String>, ) -> Result<Self>

Create new configuration loader with custom environment

§Arguments
  • module - Module name
  • env - Environment (e.g., “development”, “test”, “production”)
§Examples
let loader = ConfigLoader::with_env("iron_token_manager", "production")?;
Source

pub fn with_defaults(module: impl Into<String>, defaults: &str) -> Result<Self>

Create configuration loader with custom default values

§Arguments
  • module - Module name
  • defaults - 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)?;
Source

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

Get configuration value by key path

§Arguments
  • key - Configuration key path (e.g., “database.url”)
§Errors

Returns error if key not found or value cannot be deserialized.

§Examples
let url: String = loader.get("database.url")?;
let max_conn: u32 = loader.get("database.max_connections")?;
Source

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

Get optional configuration value

Returns None if key not found, otherwise same as get().

§Examples
if let Some(url) = loader.get_opt::<String>("database.url")? {
  println!("Database URL: {}", url);
}
Source

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")?;
Source

pub fn keys(&self) -> Vec<String>

Get all configuration keys

Source

pub fn get_with_source<T: DeserializeOwned>( &self, key: &str, ) -> Result<(T, String)>

Get configuration value with source information

Returns the value along with information about which layer provided it.

§Examples
let (url, source) = loader.get_with_source::<String>("database.url")?;
println!("Database URL: {} (from {})", url, source);
Source

pub fn debug_summary(&self) -> String

Print configuration summary for debugging

Shows all resolved configuration values with their sources.

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.