envman_test 0.4.1

Test utilities for envman.
Documentation
  • Coverage
  • 0%
    0 out of 1 items documented0 out of 0 items with examples
  • Size
  • Source code size: 7.76 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.05 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • moriyoshi-kasuga/envman
    4 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • moriyoshi-kasuga

envman (Environments Manager)

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

Install

[dependencies]
envman = "0.4.1"

Version requirement: rustc 1.80+

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")]
  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::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 = "https://api.example.com")]
    api_url: String,
    #[envman(test = "secret")]
    jwt_secret: String,
}

License

Licensed under