| Crates.io | dumbo_config |
| lib.rs | dumbo_config |
| version | 0.3.2 |
| created_at | 2025-06-30 07:17:01.210592+00 |
| updated_at | 2026-01-20 00:59:55.090384+00 |
| description | a config loader |
| homepage | |
| repository | https://github.com/cao5zy/dumbo-config |
| max_upload_size | |
| id | 1731497 |
| size | 69,805 |
dumbo-config is a config loader.
Load project configurations
Enhanced logging with loading source information
Environment variable support with prefix and custom separator
SHOW_SETTINGS environment variable for debugging configuration
Comprehensive error handling with运维-friendly error messages
Your configuration file.
name: "test config"
value: 32
The following file names is qualified.
ENV is the value of the environment variable "ENV". If "ENV" is not set, it defaults to searching config.yml and config.yaml.You can also use load_named_config with specified config file.
Rust file for loading TestConfig
use dumbo_config::{load_config, load_named_config};
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct TestConfig {
name: String,
value: i32,
}
...
let config: Option<TestConfig> = load_config();
let config_path: Path = ...;
let config: Option<TestConfig> = load_named_config(&config_path);
For more advanced configuration loading scenarios, you can use the load_config_with_param function with LoadingParam.
The LoadingParam struct allows you to specify both file and environment variable sources:
use dumbo_config::{LoadingParam, EnvConfig, load_config_with_param};
use serde::Deserialize;
use std::path::Path;
#[derive(Debug, Deserialize, PartialEq)]
struct AppConfig {
database_url: String,
port: u16,
debug: bool,
}
// Load from file only
let param = LoadingParam {
file: Some(Path::new("config.yaml")),
env_prefix: None,
};
let config: AppConfig = load_config_with_param(¶m)?;
// Load from environment variables only
let param = LoadingParam {
file: None,
env_prefix: Some(EnvConfig::new("MY_APP".to_string(), None)),
};
let config: AppConfig = load_config_with_param(¶m)?;
// Load from both file and environment variables (env vars take precedence)
let param = LoadingParam {
file: Some(Path::new("config.yaml")),
env_prefix: Some(EnvConfig::new("MY_APP".to_string(), None)),
};
let config: AppConfig = load_config_with_param(¶m)?;
The EnvConfig struct defines how to load configuration from environment variables:
name: The prefix for environment variables (e.g., "MYAPP" for variables like "MYAPP_DATABASE_URL")separator: The separator character used in environment variable names. 鉴于Rust中的字段名称通常也用"_"分割,因此,环境变量的分割符默认为"__"Example with MY_APP prefix: Given the following configuration structure:
struct DatabaseConfig {
host: String,
port: u16,
credentials: Credentials,
}
struct Credentials {
username: String,
password: String,
}
With EnvConfig::new("MY_APP".to_string(), None), the corresponding environment variables would be:
# Top-level fields
export MY_APP__HOST="localhost"
export MY_APP__PORT="5432"
# Nested fields (using double underscore as separator)
export MY_APP__CREDENTIALS__USERNAME="myuser"
export MY_APP__CREDENTIALS__PASSWORD="mypass"
Note: The environment variable prefix should not contain the separator character. For example, if your prefix is "RESUME_AGENT" and separator is "_", this will cause a configuration loading error.
The library provides detailed logging at the INFO level:
env_prefix is set and the SHOW_SETTINGS environment variable is set to "true" (case-insensitive), logs that configuration was loaded successfullyNote: The SHOW_SETTINGS environment variable is only checked when env_prefix is configured. This is because if no env_prefix is set, it means the user is loading configuration directly from files, and they can simply inspect the configuration files directly to view the settings. When environment variables are used for configuration (via env_prefix), the actual values may not be easily visible, so SHOW_SETTINGS provides a way to log the loaded configuration for debugging purposes.
To enable configuration debugging, set the SHOW_SETTINGS environment variable if the env_prefix is 'MY_APP'
export MY_APP__SHOW_SETTINGS=true
./your-application
Supported values for SHOW_SETTINGS (case-insensitive): "true", "1", "yes", "on"
The library provides comprehensive error handling with运维-friendly error messages:
All errors are wrapped in the ConfigError enum and implement the standard Error trait.