#environment #env #dotenv #settings #config

macro dotenv_config

parse env to config struct for Rust

4 releases

Uses new Rust 2021

0.1.3 Sep 11, 2022
0.1.2 Sep 9, 2022
0.1.1 Sep 9, 2022
0.1.0 Sep 9, 2022

#232 in Configuration

Download history 133/week @ 2022-10-07 133/week @ 2022-10-14 205/week @ 2022-10-21 138/week @ 2022-10-28 117/week @ 2022-11-04 232/week @ 2022-11-11 148/week @ 2022-11-18 222/week @ 2022-11-25 176/week @ 2022-12-02 163/week @ 2022-12-09 104/week @ 2022-12-16 185/week @ 2022-12-23 134/week @ 2022-12-30 63/week @ 2023-01-06 116/week @ 2023-01-13 52/week @ 2023-01-20

424 downloads per month

MIT license

12KB
181 lines

Dot Env Config

use .env as config file and parse environments to config struct.

Usage

derive EnvConfig

use dotenv::dotenv;
use dotenv_config::EnvConfig;

#[derive(Debug, EnvConfig)]
struct Config {
    #[env_config(default = "192.168.2.1")]
    server_addr: String,
    server_mode: bool,
    #[env_config(name = "ZINC_FOO", default = true)]
    foo: bool,
    #[env_config(name = "ZINC_BAR", default = 123456)]
    bar: Option<i64>,
}

fn main() {
    dotenv().ok();
    let cfg = Config::init().unwrap();
    println!("{:#?}", cfg);
}

attribute env_config

you can use macro attribute set field attribute

  • name: change default environment key
  • default: if not set, used as default value

you can though system environments or .env file config it.

ZINC_FOO=false
ZINC_BAR=8787878

default load environment key is: structName_fieldName do UpperSnake, like above struct, default config key is:

CONFIG_SERVER_ADDR
CONFIG_SERVER_MODE
ZINC_FOO
ZINC_BAR

Dependencies

~2.5MB
~54K SLoC