Skip to main content

ConfigLoader

Struct ConfigLoader 

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

Configuration loader with merge semantics.

Loads configuration from multiple sources with proper precedence:

  1. Hardcoded defaults (Config::default())
  2. User config file (~/.config/rec/config.toml)
  3. Environment variables (REC_*, NO_COLOR)
  4. CLI flags (handled later in CLI layer)

Implementations§

Source§

impl ConfigLoader

Source

pub fn new(paths: Paths) -> Self

Create a new ConfigLoader with the given paths.

Source

pub fn load(&self) -> Result<Config>

Load config with merge semantics: defaults < user config < env vars.

Starts with default config, merges user config if it exists, then applies environment variable overrides.

§Errors

Returns an error if the config file exists but is invalid TOML.

Source

pub fn save(&self, config: &Config) -> Result<()>

Save config to the default location.

Creates the config directory if it doesn’t exist.

§Errors

Returns an error if directory creation or file write fails.

Source

pub fn create_default_if_missing(&self) -> Result<bool>

Create a default config file if it doesn’t exist.

Returns true if the file was created, false if it already existed.

§Errors

Returns an error if directory creation or file write fails.

Source

pub fn get_key(&self, key: &str) -> Result<ConfigValue>

Get a single config key with source information.

Reads the TOML file to find the file value, checks env overrides, and returns the effective value with source annotation.

§Errors

Returns an error if the key is unknown or the file cannot be read.

Source

pub fn set_key(&self, key: &str, value: &str) -> Result<()>

Set a config key to a new value, preserving TOML comments and formatting.

Uses toml_edit::DocumentMut for format-preserving writes. Creates sections if they don’t exist. Validates the full config after modification by deserializing (round-trip check).

§Errors

Returns an error if:

  • The key is unknown
  • The key is an array type (use --edit instead)
  • The value fails round-trip validation
  • File I/O fails
Source

pub fn list_config(&self) -> Result<Vec<ConfigValue>>

List all config keys with their values and sources.

For each known key, determines the effective value and whether it comes from the default, file, or an environment variable.

§Errors

Returns an error if the config file exists but cannot be read.

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