#variables #env #env-var #own #loader #highly #customized

env_plus

A very simple crate used to load ENV variables in your program, but can also be customized to load your own files

3 releases

0.1.2 Jan 27, 2021
0.1.1 Jan 25, 2021
0.1.0 Jan 25, 2021

#13 in #customized

MIT license

12KB
97 lines

env_plus

Crates.io

A very simple and highly costumizeable env variable loader. You can specify your own files that you want to use or use its default settings.


Usage

Add this to your Cargo.toml:

[dependenices]
env_plus = "0.1.2"

.env_plus

// This is a comment!
SECRET=YOUR_SECRET

main.rs

use env_plus::EnvLoader;

fn main() {
    EnvLoader::new()
    .activate();

    let secret = std::env::var("SECRET").unwrap();
    assert_eq!(secret, String::from("YOUR_SECRET"));
}

The default settings for env_plus are:

  • File: .env_plus
  • Comments: //
  • Value delimiter: =
  • Overwrite existing variables with the same name: false

However, you are allowed to fully costumize which files to use and how to parse them.


Advanced Usage

special_file.extension

## I want to use this style as a comment!
## == will be the new value delimiter

SECRET==YOUR_SECRET

main.rs

use env_plus::EnvLoader;

fn main() {
    std::env::set_var("SECRET", "MY_SECRET");

    EnvLoader::new()
    .change_file(String::from("./special_file.extension"))
    .change_delimiter(String::from("=="))
    .change_comment(String::from("##"))
    .overwrite_envs(true)
    .activate();


    let secret = std::env::var("SECRET").unwrap();

    // SECRET has been overwritten from MY_SECRET to YOUR_SECRET
    assert_eq!(secret, String::from("YOUR_SECRET"));
}

No runtime deps