Crates.io | derive_environment |
lib.rs | derive_environment |
version | 1.1.0 |
source | src |
created_at | 2023-09-19 20:47:09.576026 |
updated_at | 2023-10-05 22:33:54.693487 |
description | A Rust library for modifying structs via environment variables |
homepage | |
repository | https://github.com/eievui5/derive-environment |
max_upload_size | |
id | 977216 |
size | 48,470 |
A Rust library for modifying structs via environment variables.
Unlike envy, this does not create a new object. It is used to reconfigure an existing structure (after having parsed it from a config file, for example).
If a certain field should not be configurable via environment variables, mark it with #[env(ignore)]
.
Creating a config structure:
use derive_environment::FromEnv;
#[derive(Default, FromEnv)]
pub struct Config {
// ...
}
// Creates a base configuration struct to add on to.
// Normally this would be created using `serde` from a config file.
let mut config = Config::default();
// Names the struct "MY_CONFIG", which acts as a prefix.
config.with_env("MY_CONFIG").unwrap();
Nesting fields:
use derive_environment::FromEnv;
#[derive(FromEnv)]
struct ServerConfig {
port: u16,
}
#[derive(FromEnv)]
pub struct Config {
server: ServerConfig,
}
Generates:
Vector of Nested fields:
use derive_environment::FromEnv;
// `Vec`'s `FromEnv` implementation requires `Default`.
#[derive(Default, FromEnv)]
struct ServerConfig {
port: u16,
}
#[derive(FromEnv)]
pub struct Config {
servers: Vec<ServerConfig>,
}
Generates: