Crates.io | envconfgen |
lib.rs | envconfgen |
version | 1.0.2 |
source | src |
created_at | 2024-07-02 02:04:10.660697 |
updated_at | 2024-07-02 22:13:54.545205 |
description | Config struct generator. |
homepage | |
repository | https://github.com/h4-h/envconf |
max_upload_size | |
id | 1289159 |
size | 9,193 |
Simple and small crate for configuration structs.
Crate generates impl
with:
new
public method that fetches values from environment#field_name
public getters for values-config
?My crate panics if it can't found variable in environment and default
is not specified and idk... it contains only 76 LOC.
Anyway, there is no big reason why to use my crate instead of cool and big X-config
.
cargo add envconfgen
# Cargo.toml
[dependencies]
envconfgen = "1.0.2"
#[derive(envconfgen::EnvConfig)]
struct Config {
// `var` specifies the environment variable name to fetch the value from.
// If not provided, the uppercase field name (`TEST_VAR`) will be used.
#[var("MY_TEST_VAR")]
test_var: String,
// `default` provides a fallback value if the environment variable is not set.
// If not provided and the variable is missing, it will panic.
//
// This field does not use `var`, so it defaults to searching for `DATABASE_URL`.
#[default("nothing")]
database_url: String,
}
fn main() {
let config = Config::new(); // Generated factory method
// Crate also generates a public getters for fields
// I don't think it's a good idea to use public fields for config struct
println!("test_var: {}", config.test_var());
println!("database_url: {}", config.database_url());
}
MIT
Feel free to open issue or send PR.