Crates.io | ground-env |
lib.rs | ground-env |
version | 0.2.0 |
source | src |
created_at | 2024-06-11 14:54:39.515442 |
updated_at | 2024-06-24 09:52:15.079851 |
description | Parse env variables by defining a struct |
homepage | |
repository | https://github.com/Jezza/ground |
max_upload_size | |
id | 1268280 |
size | 12,892 |
ground-env
is a Rust crate that provides a convenient way to load environment variables into your structs. It uses custom derive macros to automatically map environment variables to struct fields, with support for various features such as renaming fields, default values, flattening nested structures, and handling different delimiters.
Add ground-env
to your Cargo.toml
:
[dependencies]
ground-env = "0.1.0"
Annotate your structs with the #[derive(FromEnv)]
macro and configure the mapping using attributes:
#[derive(FromEnv)]
struct Config {
text: String,
optional_text: Option<String>,
// The default delimiter is set to ","
list: Vec<String>,
#[env(delimiter = " ")] // But you can customise it.
names: Vec<String>,
number: i64,
// Supports renaming.
#[env(rename = "EMAIL_ADDRESS")]
email: String,
#[env(default)] // Defaults to 0 when no explicit value is provided.
count: i64,
#[env(default = "64")] // Defaults to 64 when not provided.
background_tasks: i64,
#[env(flatten)] // You can flatten other structs
admin_credentials: Credentials,
#[env(flatten = "DB_")] // You can also provide a prefix.
db_args: Credentials,
}
#[derive(FromEnv)]
struct Credentials {
username: String,
password: String,
}
fn main() -> anyhow::Result<()> {
let t = Config::from_env()?;
Ok(())
}
This project is licensed under the MIT License. See the LICENSE file for details.