menva

Crates.iomenva
lib.rsmenva
version1.1.0
sourcesrc
created_at2023-10-07 19:50:48.618331+00
updated_at2024-12-24 23:57:47.177745+00
descriptionManage environment variables
homepage
repositoryhttps://github.com/lucas-montes/menva
max_upload_size
id996557
size8,165
Lucas Montes (lucas-montes)

documentation

README

Menva

Set your environment variables from your .env file

use menva::read_env_file;

fn main() {
    read_env_file(".env");
    do_cool_things();
}

Get your environment variables with their type

use menva::{get_env, get_bool_env};

pub fn do_cool_things() {
    match get_bool_env("SHOULD_I") {
        true => println!("{}", get_env("SECRET_TEXT")),
        false => todo!(),
    }
}

You can also use it with a struct

use menva::FromEnv;

#[derive(Debug, PartialEq)]
enum Env {
    Dev,
    Prod,
}

impl FromStr for Env {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "dev" => Ok(Env::Dev),
            "prod" => Ok(Env::Prod),
            _ => Err(format!("Invalid value for enum Env: {}", s)),
        }
    }
}

#[derive(Debug, FromEnv)]
struct Config {
    ip: Ipv4Addr,
    env: Env,
}

fn main() {
    std::env::set_var("IP", "0.0.0.0");
    std::env::set_var("ENV", "Prod");
    std::env::set_var("WEB_IP", "1.1.1.1");
    std::env::set_var("WEB_ENV", "dev");

    let config = Config::from_env_with_prefix("");
    let config2 = Config::from_env_with_prefix("WEB_");

    assert!(config.ip.eq(&Ipv4Addr::new(0, 0, 0, 0)));
    assert!(config.env == Env::Prod);

    assert!(config2.ip.eq(&Ipv4Addr::new(1, 1, 1, 1)));
    assert!(config2.env == Env::Dev);
}
Commit count: 7

cargo fmt