pub struct Config { /* private fields */ }Expand description
High-level configuration manager with change tracking and validation
Config provides a comprehensive API for managing NOML configurations
throughout their lifecycle. It maintains both the raw AST (for preserving
comments and formatting) and resolved values (for fast access).
§Key Features
- 🔄 Change Tracking - Automatic detection of modifications
- 💾 Source Preservation - Comments and formatting maintained
- 🎯 Path Access - Dot-notation for nested value access
- ✅ Type Safety - Built-in conversion with error handling
- 📁 File Management - Load, modify, and save operations
- 🔗 Schema Validation - Ensure configuration correctness
§Lifecycle Management
use noml::Config;
// 1. Load configuration
let mut config = Config::from_file("app.noml")?;
// 2. Check if modifications are needed
if !config.contains_key("version") {
config.set("version", "1.0.0")?;
}
// 3. Save only if modified
if config.is_modified() {
config.save()?;
config.mark_clean();
}
§Thread Safety
Config implements Clone for sharing across threads. Each clone
maintains independent modification tracking but shares the underlying data.
Implementations§
Source§impl Config
impl Config
Sourcepub fn from_string(content: &str) -> Result<Self>
pub fn from_string(content: &str) -> Result<Self>
Load configuration from a string
Sourcepub fn builder() -> ConfigBuilder
pub fn builder() -> ConfigBuilder
Create a configuration builder
Sourcepub fn get(&self, key: &str) -> Option<&Value>
pub fn get(&self, key: &str) -> Option<&Value>
Get a value by key path
Returns None if the key doesn’t exist.
§Example
let config = Config::from_string(r#"
[database]
host = "localhost"
port = 5432
"#)?;
let host = config.get("database.host").unwrap();
assert_eq!(host.as_string().unwrap(), "localhost");
let port = config.get("database.port").unwrap();
assert_eq!(port.as_integer().unwrap(), 5432);Sourcepub fn get_or<T>(&self, key: &str, _default: T) -> Result<&Value>
pub fn get_or<T>(&self, key: &str, _default: T) -> Result<&Value>
Get a value by key path with a default
Returns the default value if the key doesn’t exist or cannot be converted to the target type.
§Example
let config = Config::from_string(r#"
[server]
port = 8080
"#)?;
// Key exists
let port = config.get_or("server.port", 3000)?;
assert_eq!(port.as_integer().unwrap(), 8080);Sourcepub fn get_or_insert<T>(&mut self, key: &str, default: T) -> Result<&Value>
pub fn get_or_insert<T>(&mut self, key: &str, default: T) -> Result<&Value>
Get a value or insert a default if it doesn’t exist
Sourcepub fn set<T>(&mut self, key: &str, value: T) -> Result<()>
pub fn set<T>(&mut self, key: &str, value: T) -> Result<()>
Set a value by key path
Creates intermediate tables as needed and marks the configuration as modified.
§Example
let mut config = Config::new();
config.set("database.host", "localhost")?;
config.set("database.port", 5432)?;
config.set("server.debug", true)?;
assert_eq!(config.get("database.host").unwrap().as_string().unwrap(), "localhost");
assert_eq!(config.get("database.port").unwrap().as_integer().unwrap(), 5432);
assert_eq!(config.get("server.debug").unwrap().as_bool().unwrap(), true);Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Check if a key 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 source_path(&self) -> Option<&Path>
pub fn source_path(&self) -> Option<&Path>
Get the source file path (if loaded from file)
Sourcepub fn save(&self) -> Result<()>
pub fn save(&self) -> Result<()>
Save the configuration to its source file
Only works if the configuration was loaded from a file.
§Example
let mut config = Config::from_file("app.noml")?;
config.set("version", "2.0.0")?;
config.save()?; // Saves back to app.nomlSourcepub 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
§Example
let config = Config::from_string("name = \"MyApp\"")?;
config.save_to_file("output.noml")?;Sourcepub fn into_value(self) -> Value
pub fn into_value(self) -> Value
Convert to owned Value
Sourcepub fn validate_schema(&self, schema: &Schema) -> Result<()>
pub fn validate_schema(&self, schema: &Schema) -> Result<()>
Validate configuration against a schema
§Example
use noml::{Config, Schema, FieldType, SchemaBuilder};
let config = Config::from_string(r#"
app_name = "MyApp"
port = 8080
debug = true
"#)?;
let schema = SchemaBuilder::new()
.require_string("app_name")
.require_integer("port")
.optional_bool("debug")
.build();
config.validate_schema(&schema)?;Sourcepub fn as_document(&self) -> &Document
pub fn as_document(&self) -> &Document
Get the underlying Document
Sourcepub fn merge(&mut self, other: &Config) -> Result<()>
pub fn merge(&mut self, other: &Config) -> Result<()>
Merge another configuration into this one
Values from the other configuration will overwrite values in this one.
Sourcepub fn stats(&self) -> ConfigStats
pub fn stats(&self) -> ConfigStats
Get configuration statistics