Expand description
§CFGLoader
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.
§Installation
Add this to your Cargo.toml
:
[dependencies]
cfgloader_rs = "1.0"
§Quick Start
§Basic Usage
use cfgloader_rs::*;
#[derive(FromEnv, Debug)]
struct Config {
#[env("DATABASE_URL", default = "sqlite://app.db")]
database_url: String,
#[env("PORT", default = "8080")]
port: u16,
#[env("API_KEY", required)]
api_key: String,
#[env("FEATURES", default = "auth,logging", split = ",")]
features: Vec<String>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::load(std::path::Path::new(".env"))?;
println!("Config: {:#?}", config);
Ok(())
}
§Nested Configuration
Organize your configuration into logical groups:
use cfgloader_rs::*;
#[derive(FromEnv, Debug)]
struct AppConfig {
#[env("APP_NAME", required)]
name: String,
// Nested structs automatically call their own load() method
server: ServerConfig,
database: DatabaseConfig,
}
#[derive(FromEnv, Debug)]
struct ServerConfig {
#[env("SERVER_HOST", default = "127.0.0.1")]
host: String,
#[env("SERVER_PORT", default = "8080")]
port: u16,
}
#[derive(FromEnv, Debug)]
struct DatabaseConfig {
#[env("DB_URL", required)]
url: String,
#[env("DB_MAX_CONNECTIONS", default = "10")]
max_connections: u32,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = AppConfig::load(std::path::Path::new(".env"))?;
println!("App: {}", config.name);
println!("Server: {}:{}", config.server.host, config.server.port);
println!("Database: {}", config.database.url);
Ok(())
}
Enums§
Traits§
Functions§
- get_env
- Utility function for macros: read env and return
Option<String>
- load_
env_ file - Utility function for macros: load .env file
- load_
env_ file_ iter - Load .env from multiple paths (any iterable), return on first success. If none found, return error.
- parse_
scalar - Utility function for macros: parse string to T
- parse_
vec - Split string and parse each part to
Vec<T>