Trait confgr_core::Confgr

source ·
pub trait Confgr
where Self: Sized,
{ type Layer: Default + FromEnv + Merge + FromFile + From<Self> + Into<Self>; // Provided methods fn load_config() -> Self { ... } fn deserialize_from_file() -> Result<Self::Layer, ConfgrError> { ... } fn check_file() -> Result<(), ConfgrError> { ... } fn get_env_keys() -> HashMap<String, String> { ... } fn get_file_path() -> Option<String> { ... } }
Expand description

Provides a unified approach to load configurations from environment variables, files, and default settings. This trait is typically derived using a macro to automate implementations based on struct field names and annotations.

Required Associated Types§

source

type Layer: Default + FromEnv + Merge + FromFile + From<Self> + Into<Self>

Provided Methods§

source

fn load_config() -> Self

Loads and merges configurations from files, environment variables, and default values. Order of precedence: Environment variables, file configurations, default values.

§Examples
let config = AppConfig::load_config();
assert_eq!(config.port, 8080);
source

fn deserialize_from_file() -> Result<Self::Layer, ConfgrError>

Attempts to deserialize configuration from a file. This method is a part of the file loading phase of the configuration process.

§Errors

Returns ConfgrError if the file cannot be read.

§Examples
let file_layer = AppConfig::deserialize_from_file();
match file_layer {
    Ok(layer) => println!("Configuration loaded from file."),
    Err(e) => eprintln!("Failed to load configuration: {}", e),
}
source

fn check_file() -> Result<(), ConfgrError>

Checks the accessibility of the specified configuration file.

§Returns

Ok if the file is accessible, otherwise an Err(ConfgrError) if the file cannot be found or opened.

§Examples
if AppConfig::check_file().is_ok() {
    println!("Configuration file is accessible.");
} else {
    println!("Cannot access configuration file.");
}
source

fn get_env_keys() -> HashMap<String, String>

Retrieves the map of environment variable keys associated with the configuration properties.

§Returns

A HashMap where the keys are property names and the values are the corresponding environment variable names.

§Examples
let env_keys = AppConfig::get_env_keys();
assert_eq!(env_keys["port"], "APP_PORT");
source

fn get_file_path() -> Option<String>

Gets the file path used for loading the configuration, if specified.

§Returns

An Option<String> which is Some(path) if a path is set, otherwise None.

§Examples
if let Some(path) = AppConfig::get_file_path() {
    println!("Configuration file used: {}", path);
} else {
    println!("No specific configuration file used.");
}

Object Safety§

This trait is not object safe.

Implementors§