rok-config-macros
Proc-macro implementation of
#[derive(Config)]for rok-config typed environment variable loading.
Part of the Rok Framework — a full-stack Rust web framework built on Axum 0.8 and SQLx 0.8.
Features
#[derive(Config)]generatesfrom_env()andfrom_env_file()implementations from struct field annotations#[env("VAR_NAME")]maps a field to an environment variable — missing required fields are collected before failing#[env("VAR_NAME", default = "value")]provides a fallback when the variable is absent#[env("VAR_NAME", secret)]suppresses the field value inDebugoutput to prevent log leakage- Nested struct support: a field with a type that implements
Configis recursively loaded - Compile-time detection of duplicate
#[env]variable names within the same struct - Clear
ConfigError::Missing(Vec<String>)if any required variables are absent at runtime
Installation
[]
= "0.2"
This crate is re-exported by
rok-config. Prefer usingrok-configdirectly:= "0.2"
Quick Start
use Config;
let config = from_env?;
println!;
Core API
What the macro generates
// Input:
// Generated (illustrative):
// Secret field: Debug impl prints "[REDACTED]" instead of the value
Supported field types
| Rust type | Parsed from |
|---|---|
String |
Raw env var string |
u8, u16, u32, u64, usize |
Parsed integer |
i8, i16, i32, i64, isize |
Parsed signed integer |
f32, f64 |
Parsed float |
bool |
"true" / "1" → true, "false" / "0" → false |
Option<T> |
None when var is absent, Some(parsed) when present |
std::time::Duration |
Parsed from seconds as u64 |
Any type implementing Config |
Recursively loaded |
Integration
rok-config-macros is a proc-macro implementation detail of rok-config. All macros are accessible through rok-config with no additional dependencies required in application code.
use Config; // re-exports the derive macro from rok-config-macros
License
MIT