Skip to main content

ConfigManager

Struct ConfigManager 

Source
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

Source

pub fn new() -> Self

Creates a new configuration manager

Source

pub fn add_source(&self, source: ConfigSource) -> ContextResult<()>

Adds a configuration source

Source

pub fn load_from_toml_file<P: AsRef<Path>>( &mut self, path: P, ) -> ContextResult<()>

Loads configuration from a TOML file

§Arguments
  • path - Path to the TOML configuration file
§Examples
use verdure_context::ConfigManager;

let mut manager = ConfigManager::new();
manager.load_from_toml_file("config/app.toml").unwrap();
Source

pub fn load_from_yaml_file<P: AsRef<Path>>( &mut self, path: P, ) -> ContextResult<()>

Loads configuration from a YAML file

§Arguments
  • path - Path to the YAML configuration file
§Examples
use verdure_context::ConfigManager;

let mut manager = ConfigManager::new();
manager.load_from_yaml_file("config/app.yaml").unwrap();
Source

pub fn load_from_properties_file<P: AsRef<Path>>( &mut self, path: P, ) -> ContextResult<()>

Loads configuration from a Properties file

§Arguments
  • path - Path to the Properties configuration file
§Examples
use verdure_context::ConfigManager;

let mut manager = ConfigManager::new();
manager.load_from_properties_file("config/app.properties").unwrap();
Source

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
  • .yaml or .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();
Source

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

Source

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");
Source

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);
Source

pub fn get_float(&self, key: &str) -> ContextResult<f64>

Gets a configuration value as a float

§Arguments
  • key - The configuration key
§Returns

The configuration value as a float

§Errors

Returns an error if the key is not found or cannot be converted to a float

Source

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);
Source

pub fn get_string_or_default(&self, key: &str, default: &str) -> String

Gets a configuration value with a default fallback

§Arguments
  • key - The configuration key
  • default - 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");
Source

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 key
  • default - The default value to return if key is not found
Source

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 key
  • default - The default value to return if key is not found
Source

pub fn set(&self, key: &str, value: ConfigValue)

Sets a runtime configuration value

Source

pub fn sources_count(&self) -> usize

Gets the number of configuration sources

§Returns

The total number of configuration sources

Source

pub fn invalidate_cache(&self)

Invalidates the configuration cache

Source

pub fn invalidate_keys(&self, keys: &[String])

Invalidates specific cache keys

Trait Implementations§

Source§

impl Clone for ConfigManager

Source§

fn clone(&self) -> ConfigManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for ConfigManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.