10 releases (4 stable)

1.0.4 Dec 24, 2024
1.0.3 Dec 8, 2024
0.4.3 Oct 5, 2024
0.4.2 Sep 16, 2024
0.3.2 Aug 30, 2024

#132 in Configuration

Download history 149/week @ 2024-09-14 35/week @ 2024-09-21 8/week @ 2024-09-28 142/week @ 2024-10-05 17/week @ 2024-10-12 2/week @ 2024-10-19 5/week @ 2024-11-02 1/week @ 2024-11-16 123/week @ 2024-11-30 368/week @ 2024-12-07 17/week @ 2024-12-14 137/week @ 2024-12-21 12/week @ 2024-12-28

657 downloads per month
Used in envman_test

MIT license

7KB

EnvMan: Environments (variables) Manager

EnvMan on crates.io EnvMan on docs.rs

This crate adds a macro for easy management of environment variables.

If you would like to see more details, please see these codes.

Example

use envman::EnvMan;

unsafe {
  std::env::set_var("F0", "-1");
  std::env::set_var("f1", "1");
}

// The type of field can be set if FromStr is implemented
#[derive(EnvMan)]
struct Foo {
  f0: i32,
  #[envman(rename = "f1")]
  f_1: u8,
  #[envman(default = "default value".to_string())]
  f_n: String,
  f_o: Option<i32>,
  #[envman(default = 1, test = 2)]
  f_test: u8,
}

// If rename is not set, it will be an upper case
let foo = Foo::load().unwrap();
// This value is taken from “F0”.
let f0 = foo.f0;
// This value is taken from “f1”.
let f_1 = foo.f_1;
// This value is taken from “F_N” and if it is not set, it will be set to “default value”.
let f_n = foo.f_n;
// This value is taken from “F_O” and if it is not set, it will be set to None.
let f_o = foo.f_o;
// This value is taken from “F_TEST” and if it is not set, it will be set to 1.
// and if it under test, it will be set to 2.
let f_test = foo.f_test;

Usecase

use std::{net::SocketAddr, sync::LazyLock};

use envman::EnvMan;

fn main() {
    // this unsafe block is necessary for test
    // and it is not necessary in production
    unsafe {
        std::env::set_var("JWT_SECRET", "secret");
    }

    // initialize
    let _ = &*ENVIRONMENTS;

    println!("API_URL: {}", ENVIRONMENTS.api_url);
}

pub static ENVIRONMENTS: LazyLock<Environments> = LazyLock::new(|| Environments::load().unwrap());

#[derive(EnvMan)]
pub struct Environments {
    #[envman(default = "127.0.0.1:8080", alltime_parse)]
    pub api_url: SocketAddr,
    #[envman(test = "secret".to_string())]
    pub jwt_secret: String,
}

Field Attributes

  • rename : rename = "new name" (default: upper case)
  • parser: parser = constants::default_parser (default: FromStr::from_str)

    parser type is fn(&str) -> Result<T, E> and E has impl std::error::Error

  • group_test: (default: None)

    if under test, use this value (Priority is first)

    • test: test Use Default::default()
    • test: test = Default::default() (put anything of expr)
  • group_default: (default: None)

    if not found in env, use this value if a test exists and is under test, use the test

    • default: default
    • default: default = Default::default() (put anything of expr)
  • alltime_parse: alltime_parse (default: false)

    The normal default (and test) return value is the field type if this is set, the return value is a string and the parser is used

License

Licensed under

Dependencies

~255–710KB
~16K SLoC