lazy-env

Crates.iolazy-env
lib.rslazy-env
version0.1.0
created_at2025-11-15 05:10:35.813249+00
updated_at2025-11-15 05:10:35.813249+00
descriptionRead .env file and environment variables as static globals
homepage
repositoryhttps://github.com/Fancyflame/lazy-env.git
max_upload_size
id1934045
size16,699
FancyFlame (Fancyflame)

documentation

https://docs.rs/lazy-env

README

Allows you to read environment variables and .env files at runtime directly in your crate using static variables.

Example

Please ensure you are in the lazy_env/examples directory, as a .env file is provided there. Alternatively, you can create a new .env file in your current working directory and copy-paste the following content into it.

# .env
PARSE_ADDR=192.168.1.1:80
MUST_PROVIDE=provided
ORIGIN_NAME=114514
# main.rs
use std::net::SocketAddr;
use lazy_env::lazy_env;

lazy_env! {
    pub static ref PARSE_ADDR: SocketAddr = ([127, 0, 0, 1], 8080).into();
    pub static ref MUST_PROVIDE: String = panic!("MUST_PROVIDE is not provided");
    static ref USE_ALIAS(concat!("ORIGIN", "_", "NAME")): u32 = 123;
    static ref USE_DEFAULT: bool = true;
}

fn main() {
    env_logger::builder().is_test(true).init();

    if lazy_env::dotenvy::dotenv().is_err() {
        panic!(
            "Please ensure that your current working directory contains \
            a .env file in its recursive parent directories"
        );
    }

    assert_eq!(
        (&*PARSE_ADDR, &*MUST_PROVIDE, &*USE_ALIAS, &*USE_DEFAULT),
        (
            &SocketAddr::from(([192, 168, 1, 1], 80)),
            &"provided".to_string(),
            &114514,
            &true,
        )
    );
}

Commit count: 0

cargo fmt