load_dotenv/
lib.rs

1//! This is a small procedural macro to load your `.env` file at compile time. That way you can use
2//! [`std::env!`][] to load environment variables and fail the build if a variable is missing.
3//!
4//! All it does is call the [dotenv](https://crates.io/crates/dotenv) crate.
5//!
6//! # Example
7//!
8//! `.env` file:
9//!
10//! ```text
11//! KEY=value
12//! ```
13//!
14//! Rust:
15//!
16//! ```
17//! use load_dotenv::load_dotenv;
18//!
19//! load_dotenv!();
20//!
21//! fn main() {
22//!     assert_eq!("value", env!("KEY"));
23//! }
24//! ```
25//!
26//! [`std::env!`]: https://doc.rust-lang.org/std/macro.env.html
27
28#![doc(html_root_url = "https://docs.rs/load-dotenv/0.1.2")]
29
30use proc_macro::TokenStream;
31
32/// Load the `.env` file and panic if the file is missing.
33#[proc_macro]
34pub fn load_dotenv(_: TokenStream) -> TokenStream {
35    dotenv::dotenv().expect("Failed to load .env file");
36
37    TokenStream::new()
38}
39
40/// Load the `.env` file but don't panic if the file is missing.
41#[proc_macro]
42pub fn try_load_dotenv(_: TokenStream) -> TokenStream {
43    dotenv::dotenv().ok();
44
45    TokenStream::new()
46}