| Crates.io | env2config |
| lib.rs | env2config |
| version | 1.0.2 |
| created_at | 2025-03-10 18:59:06.565873+00 |
| updated_at | 2025-03-12 19:26:24.856625+00 |
| description | Load environment variables into structs |
| homepage | |
| repository | https://github.com/gholami-mohammad/env2config-rs |
| max_upload_size | |
| id | 1587064 |
| size | 8,758 |
env2config is a Rust crate that allows you to load environment variables into structs. This is useful for configuration purposes, where settings can be provided through environment variables.
String, bool, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, and Vec<T>Add env2config to your Cargo.toml:
[dependencies]
env2config = "1.0.2"
use env2config::FromEnv;
use std::env::set_var;
#[derive(FromEnv)]
struct Config {
#[env("DATABASE_URL")]
database_url: String,
#[env("HOST", "127.0.0.1")] // HOST is env variable name and 127.0.0.1 is default value if HOST is not provided
host: String,
#[env("PORT")]
port: i32,
// loading comma separated values into vectors of these types:
// String, bool, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128
#[env("IDS", "1,2,3")]
ids: Vec<i32>,
#[env("SYMBOLS")]
symbols: Vec<String>,
}
fn main() {
unsafe {
set_var("PORT", "5544");
set_var("SYMBOLS", "A,B,C");
}
let cfg = Config::from_env();
assert_eq!(cfg.host, "127.0.0.1");
assert_eq!(cfg.port, 5544);
assert_eq!(cfg.ids.len(), 3);
assert_eq!(cfg.ids[0], 1);
assert_eq!(cfg.symbols.len(), 3);
assert_eq!(cfg.symbols[0], "A");
assert_eq!(cfg.symbols[1], "B");
assert_eq!(cfg.symbols[2], "C");
}
License
This project is licensed under the MIT License - see the LICENSE file for details.