2个版本
0.1.1 | 2024年2月27日 |
---|---|
0.1.0 | 2024年2月27日 |
1850 在 过程宏 中
每月 22 次下载
7KB
82 行
Envar
Envar是一个简单的库,用于管理Rust中的环境变量。它提供了一个derive宏,可以在编译时自动将环境变量解析到结构体中。这种方法的优点是它允许你在编译时捕获错误,而不是在尝试访问不存在的环境变量时出现运行时错误。
使用方法
use envar::Envar;
#[derive(Envar)]
struct Config {
#[env = "DB_CONNECTION_PORT"]
port: u16,
#[env = "DB_CONNECTION_HOST"]
host: String,
debug: Option<bool>,
}
fn main() {
let config = Config::new();
println!("Port: {}", config.port);
println!("Host: {}", config.host);
// If PORT and HOST are not found in the environment, the program will not compile.
// If DEBUG is not found, it will be None.
println!("Debug: {:?}", config.debug);
}
依赖项
~1.5MB
~35K SLoC