CFGLoader RS 🚀
A simple, powerful, and ergonomic configuration loading library for Rust applications. CFGLoader automatically loads configuration from environment variables and .env files with compile-time validation and type safety.
✨ Features
- 🔧 Simple Setup: Just derive
FromEnvon your structs - 🏗️ Type Safe: Compile-time validation and automatic type conversion
- 📁 .env Support: Automatic loading from
.envfiles withdotenvyintegration - 🎯 Flexible: Support for required fields, defaults, and custom parsing
- 📊 Array Support: Parse comma-separated values into
Vec<T> - 🔗 Nested Configs: Organize configuration into logical groups
- 🛡️ Error Handling: Descriptive error messages for missing or invalid values
- 🚀 Zero Dependencies: Minimal dependency footprint (only
dotenvyandthiserror)
🚀 Quick Start
Add CFGLoader to your Cargo.toml:
[]
= "0.1"
Basic Usage
use *;
Environment Variables
# .env file or environment variables
DATABASE_URL=postgresql://localhost/myapp
PORT=3000
API_KEY=your-secret-key
FEATURES=auth,logging,metrics,cache
📚 Examples
Nested Configuration
use *;
Array Configuration
use *;
Optional vs Required Fields
use *;
📖 Attribute Reference
#[env("ENV_VAR_NAME")]
Load value from the specified environment variable.
port: u16,
#[env("ENV_VAR_NAME", default = "value")]
Provide a default value if the environment variable is not set.
host: String,
#[env("ENV_VAR_NAME", required)]
Mark a field as required. The application will fail to start if this environment variable is not provided.
api_key: String,
#[env("ENV_VAR_NAME", split = "separator")]
Parse the environment variable as a delimited string and convert to Vec<T>.
features: ,
Nested Structs
Fields without #[env] attributes are treated as nested configuration structs:
🎯 Supported Types
CFGLoader supports any type that implements FromStr:
- Primitives:
String,bool,i32,u32,f64, etc. - Collections:
Vec<T>whereT: FromStr - Custom Types: Any type implementing
FromStr
use FromStr;
;
🔧 Error Handling
CFGLoader provides descriptive error messages:
use *;
🏗️ Architecture
CFGLoader consists of three main crates:
cfgloader: Main crate that re-exports everything you needcfgloader-core: Core functionality and error typescfgloader-macros: Procedural macros forFromEnvderive
📝 License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
🚀 Getting Started
Check out the example directory for a complete working example, or run:
For detailed API documentation, visit docs.rs/cfgloader_rs.