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 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
Sourcepub fn key(&self, path: &str) -> ConfigValue<'_>
pub fn key(&self, path: &str) -> ConfigValue<'_>
Get a value by path with a more ergonomic API
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Config
impl RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl UnwindSafe for Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more