envload
A derive macro for automatically filling a struct based on the current environment.
Example
// Setup environment variables...
set_var;
set_var;
remove_var;
// ... Struct can now be loaded from current environment.
// Any missing non-`Option` field results in a panic.
// Field names are converted to SCREAMING_SNAKE_CASE, i.e. `secret_key` will load the `SECRET_KEY` env var.
let env = load_env;
assert_eq!;
// Add data for `optional_data` field...
set_var;
// ... And it's now available!
let env = load_env;
assert_eq!;
Motivation
In almost every codebase where I rely on environment variable, I end up writing a Env struct which fill its fields
based on what's currently in the environment.
Usually, I have to define a list of mandatory variables, and then I have to convert the data myself.
I thought that given how powerful Rust's macros are, it would be a good fit for a first proc macro!
Combined with dotenv, this makes for relatively painless environment variables management!
Future features
Result-based API (no panic)- Per-field options (specify a name without defaulting to SCREAMING_SNAKE_CASE)
- Feature: cache env struct through
lazy_staticor similar