pub struct Config { /* private fields */ }Expand description
High-level configuration manager with format preservation and change tracking
Config provides a comprehensive API for managing configurations
throughout their lifecycle. It maintains both the resolved values (for fast access)
and format-specific preservation data (for round-trip editing).
§Key Features
- Format Preservation: Maintains comments, whitespace, and original formatting
- Change Tracking: Automatic detection of modifications
- Type Safety: Rich type conversion with comprehensive error handling
- Path-based Access: Dot notation for nested value access
- Multi-format Support: CONF, TOML, JSON, NOML formats
- Schema Validation: Optional schema validation and enforcement
- Async Support: Non-blocking file operations (with feature flag)
§Examples
use config_lib::Config;
// Load from string
let mut config = Config::from_string("port = 8080\nname = \"MyApp\"", None)?;
// Access values
let port = config.get("port").unwrap().as_integer()?;
let name = config.get("name").unwrap().as_string()?;
// Modify values
config.set("port", 9000)?;
Implementations§
Source§impl Config
impl Config
Sourcepub fn from_string(source: &str, format: Option<&str>) -> Result<Self>
pub fn from_string(source: &str, format: Option<&str>) -> Result<Self>
Load configuration from a string
Sourcepub fn get_mut(&mut self, path: &str) -> Result<&mut Value>
pub fn get_mut(&mut self, path: &str) -> Result<&mut Value>
Get a mutable reference to a value by path
Sourcepub fn set<V: Into<Value>>(&mut self, path: &str, value: V) -> Result<()>
pub fn set<V: Into<Value>>(&mut self, path: &str, value: V) -> Result<()>
Set a value by path
§Errors
Returns an error if:
- The configuration was constructed with
ConfigOptions::read_only - The path is invalid (e.g. attempts to insert into a non-table value)
Sourcepub fn remove(&mut self, path: &str) -> Result<Option<Value>>
pub fn remove(&mut self, path: &str) -> Result<Option<Value>>
Remove a value by path
§Errors
Returns an error if:
- The configuration was constructed with
ConfigOptions::read_only - The path is malformed
Sourcepub fn contains_key(&self, path: &str) -> bool
pub fn contains_key(&self, path: &str) -> bool
Check if a path exists
Sourcepub fn is_modified(&self) -> bool
pub fn is_modified(&self) -> bool
Check if the configuration has been modified
Sourcepub fn mark_clean(&mut self)
pub fn mark_clean(&mut self)
Mark the configuration as unmodified
Sourcepub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>
Save the configuration to a specific file
Sourcepub fn merge(&mut self, other: &Config) -> Result<()>
pub fn merge(&mut self, other: &Config) -> Result<()>
Merge another configuration into this one
§Errors
Returns an error if the configuration was constructed with
ConfigOptions::read_only.
Sourcepub fn key(&self, path: &str) -> ConfigValue<'_>
pub fn key(&self, path: &str) -> ConfigValue<'_>
Get a value by path with a more ergonomic API
Source§impl Config
impl Config
Sourcepub fn with_options(options: ConfigOptions) -> Self
pub fn with_options(options: ConfigOptions) -> Self
Construct a new empty Config with the supplied
ConfigOptions.
This is the explicit opt-out constructor. For the canonical
defaults (caching on, writes allowed), prefer Config::new.
Sourcepub fn options(&self) -> &ConfigOptions
pub fn options(&self) -> &ConfigOptions
Return the ConfigOptions currently in effect on this config.
Sourcepub fn is_read_only(&self) -> bool
pub fn is_read_only(&self) -> bool
Returns true if this configuration was constructed read-only
(see ConfigOptions::read_only).