Crates.io | envload |
lib.rs | envload |
version | 0.1.1 |
source | src |
created_at | 2022-05-31 19:27:38.64262 |
updated_at | 2022-05-31 19:31:42.293749 |
description | Automatically fill structs from env variables |
homepage | |
repository | |
max_upload_size | |
id | 597676 |
size | 11,263 |
A derive macro for automatically filling a struct based on the current environment.
#[derive(Envload)]
struct Env {
secret_key: String,
int_data: i32, // Any type that implements `FromStr` can be used
optional_data: Option<usize>,
}
// Setup environment variables...
env::set_var("SECRET_KEY", "hunter2");
env::set_var("INT_DATA", "128");
env::remove_var("OPTIONAL_DATA");
// ... 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 = <Env as LoadEnv>::load_env();
assert_eq!(env, Env {
secret_key: String::from("hunter2"),
int_data: 128,
optional_data: None
});
// Add data for `optional_data` field...
env::set_var("OPTIONAL_DATA", "37");
// ... And it's now available!
let env = <Env as LoadEnv>::load_env();
assert_eq!(env.optional_data, Some(37));
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!
Result
-based API (no panic)lazy_static
or similar