pub struct ConfigManager { /* private fields */ }Expand description
Configuration manager
ConfigManager provides comprehensive configuration management functionality,
including loading from multiple sources, hierarchical property resolution,
type-safe access, and integration with environment profiles.
§Examples
use verdure_context::{ConfigManager, ConfigSource};
use std::collections::HashMap;
let mut manager = ConfigManager::new();
// Add configuration from properties
let mut props = HashMap::new();
props.insert("app.name".to_string(), "MyApp".to_string());
props.insert("app.port".to_string(), "8080".to_string());
manager.add_source(ConfigSource::Properties(props)).unwrap();
assert_eq!(manager.get_string("app.name").unwrap(), "MyApp");
assert_eq!(manager.get_integer("app.port").unwrap(), 8080);Implementations§
Source§impl ConfigManager
impl ConfigManager
Sourcepub fn add_source(&self, source: ConfigSource) -> ContextResult<()>
pub fn add_source(&self, source: ConfigSource) -> ContextResult<()>
Adds a configuration source
Sourcepub fn load_from_toml_file<P: AsRef<Path>>(
&mut self,
path: P,
) -> ContextResult<()>
pub fn load_from_toml_file<P: AsRef<Path>>( &mut self, path: P, ) -> ContextResult<()>
Sourcepub fn load_from_yaml_file<P: AsRef<Path>>(
&mut self,
path: P,
) -> ContextResult<()>
pub fn load_from_yaml_file<P: AsRef<Path>>( &mut self, path: P, ) -> ContextResult<()>
Sourcepub fn load_from_properties_file<P: AsRef<Path>>(
&mut self,
path: P,
) -> ContextResult<()>
pub fn load_from_properties_file<P: AsRef<Path>>( &mut self, path: P, ) -> ContextResult<()>
Sourcepub fn load_from_config_file<P: AsRef<Path>>(
&mut self,
path: P,
) -> ContextResult<()>
pub fn load_from_config_file<P: AsRef<Path>>( &mut self, path: P, ) -> ContextResult<()>
Loads configuration from a file with automatic format detection
The format is detected based on the file extension:
.toml-> TOML format.yamlor.yml-> YAML format.properties-> Properties format- Others -> Attempts to parse as TOML first, then YAML, then Properties
§Arguments
path- Path to the configuration file
§Examples
use verdure_context::ConfigManager;
let mut manager = ConfigManager::new();
manager.load_from_config_file("config/app.yaml").unwrap();
manager.load_from_config_file("config/database.properties").unwrap();
manager.load_from_config_file("config/server.toml").unwrap();Sourcepub fn get(&self, key: &str) -> Option<ConfigValue>
pub fn get(&self, key: &str) -> Option<ConfigValue>
Gets a configuration value by key
§Arguments
key- The configuration key (e.g., “database.url”)
§Returns
The configuration value if found, None otherwise
§Examples
use verdure_context::{ConfigManager, ConfigSource};
use std::collections::HashMap;
let mut manager = ConfigManager::new();
let mut props = HashMap::new();
props.insert("test.key".to_string(), "test.value".to_string());
manager.add_source(ConfigSource::Properties(props)).unwrap();
let value = manager.get("test.key");
assert!(value.is_some());Gets a configuration value
Sourcepub fn get_string(&self, key: &str) -> ContextResult<String>
pub fn get_string(&self, key: &str) -> ContextResult<String>
Gets a configuration value as a string
§Arguments
key- The configuration key
§Returns
The configuration value as a string
§Errors
Returns an error if the key is not found or cannot be converted to a string
§Examples
use verdure_context::{ConfigManager, ConfigSource};
use std::collections::HashMap;
let mut manager = ConfigManager::new();
let mut props = HashMap::new();
props.insert("app.name".to_string(), "MyApp".to_string());
manager.add_source(ConfigSource::Properties(props)).unwrap();
assert_eq!(manager.get_string("app.name").unwrap(), "MyApp");Sourcepub fn get_integer(&self, key: &str) -> ContextResult<i64>
pub fn get_integer(&self, key: &str) -> ContextResult<i64>
Gets a configuration value as an integer
§Arguments
key- The configuration key
§Returns
The configuration value as an integer
§Errors
Returns an error if the key is not found or cannot be converted to an integer
§Examples
use verdure_context::{ConfigManager, ConfigSource};
use std::collections::HashMap;
let mut manager = ConfigManager::new();
let mut props = HashMap::new();
props.insert("app.port".to_string(), "8080".to_string());
manager.add_source(ConfigSource::Properties(props)).unwrap();
assert_eq!(manager.get_integer("app.port").unwrap(), 8080);Sourcepub fn get_float(&self, key: &str) -> ContextResult<f64>
pub fn get_float(&self, key: &str) -> ContextResult<f64>
Sourcepub fn get_boolean(&self, key: &str) -> ContextResult<bool>
pub fn get_boolean(&self, key: &str) -> ContextResult<bool>
Gets a configuration value as a boolean
§Arguments
key- The configuration key
§Returns
The configuration value as a boolean
§Errors
Returns an error if the key is not found or cannot be converted to a boolean
§Examples
use verdure_context::{ConfigManager, ConfigSource};
use std::collections::HashMap;
let mut manager = ConfigManager::new();
let mut props = HashMap::new();
props.insert("app.debug".to_string(), "true".to_string());
manager.add_source(ConfigSource::Properties(props)).unwrap();
assert_eq!(manager.get_boolean("app.debug").unwrap(), true);Sourcepub fn get_string_or_default(&self, key: &str, default: &str) -> String
pub fn get_string_or_default(&self, key: &str, default: &str) -> String
Gets a configuration value with a default fallback
§Arguments
key- The configuration keydefault- The default value to return if key is not found
§Examples
use verdure_context::ConfigManager;
let manager = ConfigManager::new();
assert_eq!(manager.get_string_or_default("missing.key", "default"), "default");Sourcepub fn get_integer_or_default(&self, key: &str, default: i64) -> i64
pub fn get_integer_or_default(&self, key: &str, default: i64) -> i64
Gets an integer configuration value with a default fallback
§Arguments
key- The configuration keydefault- The default value to return if key is not found
Sourcepub fn get_boolean_or_default(&self, key: &str, default: bool) -> bool
pub fn get_boolean_or_default(&self, key: &str, default: bool) -> bool
Gets a boolean configuration value with a default fallback
§Arguments
key- The configuration keydefault- The default value to return if key is not found
Sourcepub fn set(&self, key: &str, value: ConfigValue)
pub fn set(&self, key: &str, value: ConfigValue)
Sets a runtime configuration value
Sourcepub fn sources_count(&self) -> usize
pub fn sources_count(&self) -> usize
Sourcepub fn invalidate_cache(&self)
pub fn invalidate_cache(&self)
Invalidates the configuration cache
Sourcepub fn invalidate_keys(&self, keys: &[String])
pub fn invalidate_keys(&self, keys: &[String])
Invalidates specific cache keys
Trait Implementations§
Source§impl Clone for ConfigManager
impl Clone for ConfigManager
Source§fn clone(&self) -> ConfigManager
fn clone(&self) -> ConfigManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more