Config loader for Rust
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
A wrapper around dotenvy that provides the FromEnv derive macro and utilities to simplify the complexity of reading environment variables.
- Simple derive macro for automatic configuration loading
- Type-safe parsing with compile-time validation
- Built-in support for required fields, defaults, and custom parsing
- Array support with configurable separators
- Nested configuration structures
- Descriptive error handling
🚀 Quick Start
Add cfgloader_rs to your Cargo.toml:
[]
= "1.0"
Basic Usage
use *;
Multiple .env Fallback
You can use load_iter to try multiple .env files in order:
let config = load_iter?;
This will try .env.local first, then .env if the first is not found.
Environment Variables
🧬 API Reference
load(env_path: &Path): Load config from a single .env fileload_iter<I, P>(paths: I): Try multiple paths, return on first success
# .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#[env("ENV_VAR_NAME", default = "value")]- Provide a default value if the environment variable is not set#[env("ENV_VAR_NAME", required)]- Mark a field as required. The application will fail to start if this environment variable is not provided#[env("ENV_VAR_NAME", split = "separator")]- Parse the environment variable as a delimited string and convert toVec<T>- 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_rs: Main crate that re-exports everything you needcfgloader-core: Core functionality and error typescfgloader_rs_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.
Development Setup
To contribute to this project, initialize the repository for development:
This will:
- Install Git hooks that automatically run
cargo fmt,cargo clippy, and tests before each push - Install useful development tools (
cargo-audit,cargo-outdated,cargo-expand)
You can run all CI checks manually with:
🎯 Getting Started
Check out the example directory for a complete working example, or run:
For detailed API documentation, visit docs.rs/cfgloader_rs.