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

#![doc(html_root_url = "https://docs.rs/load-dotenv/0.1.2")]

use proc_macro::TokenStream;

/// Load the `.env` file and panic if the file is missing.
#[proc_macro]
pub fn load_dotenv(_: TokenStream) -> TokenStream {
    dotenv::dotenv().expect("Failed to load .env file");

    TokenStream::new()
}

/// Load the `.env` file but don't panic if the file is missing.
#[proc_macro]
pub fn try_load_dotenv(_: TokenStream) -> TokenStream {
    dotenv::dotenv().ok();

    TokenStream::new()
}