Crates.io | envar |
lib.rs | envar |
version | 0.1.1 |
source | src |
created_at | 2024-02-27 10:28:07.254902 |
updated_at | 2024-02-27 10:39:18.05103 |
description | A simple macro to read environment variables at compile time |
homepage | |
repository | https://github.com/Macs1324/envar |
max_upload_size | |
id | 1154992 |
size | 6,740 |
Envar is a simple library to manage environment variables in Rust. It provides a derive macro to automatically parse environment variables into a struct at compile time. The advantage of this approach is that it allows you to catch errors at compile time, rather than getting a runtime error when trying to access an environment variable that doesn't exist.
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);
}