pub struct ConfigLoader { /* private fields */ }Expand description
Configuration loader with merge semantics.
Loads configuration from multiple sources with proper precedence:
- Hardcoded defaults (
Config::default()) - User config file (~/.config/rec/config.toml)
- Environment variables (REC_*,
NO_COLOR) - CLI flags (handled later in CLI layer)
Implementations§
Source§impl ConfigLoader
impl ConfigLoader
Sourcepub fn load(&self) -> Result<Config>
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.
Sourcepub fn save(&self, config: &Config) -> Result<()>
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.
Sourcepub fn create_default_if_missing(&self) -> Result<bool>
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.
Sourcepub fn get_key(&self, key: &str) -> Result<ConfigValue>
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.
Sourcepub fn set_key(&self, key: &str, value: &str) -> Result<()>
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
--editinstead) - The value fails round-trip validation
- File I/O fails
Sourcepub fn list_config(&self) -> Result<Vec<ConfigValue>>
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.