Gonfig
A unified configuration management library for Rust that seamlessly integrates environment variables, configuration files, and CLI arguments with a clean, intuitive API.
Features
- 🎯 Multiple Configuration Sources: Environment variables, config files (JSON/YAML/TOML), and CLI arguments
- 🔧 Flexible Prefix Management: Configure environment variable prefixes at struct and field levels
- 🚀 Derive Macro Support: Easy configuration with
#[derive(Gonfig)] - 🔀 Merge Strategies: Deep merge, replace, or append configurations
- 🛡️ Type Safety: Fully type-safe configuration with serde
- ✅ Validation: Built-in validation support for your configurations
- ⚙️ Granular Control: Enable/disable sources at struct or field level
- 🚫 Skip Support: Exclude sensitive or runtime fields from configuration
Quick Start
Add to your Cargo.toml:
[]
= "0.1.6"
= { = "1.0", = ["derive"] }
Basic Example
use Gonfig;
use ;
Advanced Example
use ;
use ;
Environment Variable Naming
Environment variables follow a hierarchical naming pattern:
Pattern: {PREFIX}_{STRUCT}_{FIELD}
Field Overrides
Derive Attributes
Struct-level Attributes
| Attribute | Description | Example |
|---|---|---|
env_prefix = "PREFIX" |
Set environment variable prefix | #[Gonfig(env_prefix = "APP")] |
allow_cli |
Enable CLI argument support | #[Gonfig(allow_cli)] |
allow_config |
Enable config file support | #[Gonfig(allow_config)] |
Field-level Attributes
| Attribute | Description | Example |
|---|---|---|
env_name = "NAME" |
Override environment variable name | #[gonfig(env_name = "DB_URL")] |
cli_name = "name" |
Override CLI argument name | #[gonfig(cli_name = "database-url")] |
#[skip] |
Skip field from all sources | #[skip] |
#[skip_gonfig] |
Alternative skip syntax | #[skip_gonfig] |
Skip Attributes
Use skip attributes to exclude fields from configuration:
Common Skip Use Cases
- Non-serializable types: Database connections, thread pools
- Runtime state: Caches, temporary data
- Sensitive data: API keys loaded from secure vaults
- Computed fields: Values calculated from other config
- Implementation details: Internal buffers, state machines
CLI Argument Naming
CLI arguments use kebab-case naming:
Usage: cargo run -- --database-url postgres://localhost --max-connections 100
Configuration Sources & Priority
Sources are merged with the following priority (higher number wins):
- Default values (Priority: 0)
- Config files (Priority: 1)
- Environment variables (Priority: 2)
- CLI arguments (Priority: 3)
Merge Strategies
use MergeStrategy;
new
.with_merge_strategy // Merge nested objects
.with_merge_strategy // Replace entire values
.with_merge_strategy // Append arrays
Validation
Add custom validation logic:
new
.validate_with
.?;
Config File Support
Gonfig supports multiple config file formats:
TOML
# config.toml
= "postgres://localhost/prod"
= 8080
[]
= "admin"
= "secret"
YAML
# config.yaml
database_url: postgres://localhost/prod
port: 8080
mongo:
username: admin
password: secret
JSON
Examples
See the examples/ directory for more comprehensive examples:
your_usecase.rs- Your exact use case implementationskip_attributes.rs- Comprehensive skip examplesmadara_usecase.rs- Complex hierarchical configurationsimple.rs- Basic usage example
Run examples:
Error Handling
Gonfig provides detailed error types:
use Error;
match config_result
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.# gonfig