cnfg 🧩
Configuration + Validation for Rust
cnfg makes it easy to build robust Rust applications with configuration that can be loaded from files, environment variables, and CLI arguments, and validated against a schema using simple annotations.
- ✅ Load from JSON / YAML / TOML files
- ✅ Merge with environment variables (with optional prefixes)
- ✅ Override via CLI arguments (powered by clap)
- ✅ Enforce constraints with
#[derive(Validate)] - ✅ Strongly typed configuration structs with
serde
✨ Example
use ;
use Deserialize;
use Parser;
config.yaml:
port: 8080
database_url: postgres://localhost:5432/app
Run with overrides:
MYAPP_PORT=9000
Output:
✅ Loaded config: AppConfig { port: 9000, database_url: "sqlite://test.db" }
🚀 Installation
Add to your Cargo.toml:
[]
= "0.1"
📦 Features
-
FileSource → Load from JSON, YAML, TOML
-
EnvSource → Flatten or prefix environment variables into config
-
CliSource → Auto-merge CLI arguments via clap
-
Validation → Built-in annotations:
#[validate(range(min, max))]#[validate(length(min, max))]- more coming soon!
-
Deep merging of multiple sources
🛠 Roadmap
- Pretty error messages with miette
- More validators:
regex,enum,required - Remote sources (HTTP, Consul, Vault, etc.)
- Config hot-reloading
📚 Documentation
⚖️ License
Licensed under:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
💡 Contributing
Contributions are welcome! Please open issues or pull requests on GitHub.