#env-file #values #structs #cli #arguments #command-line-arguments #populate

from_env

Populate structs with values given by .env file or CLI arguments

2 releases

0.1.1 Aug 6, 2023
0.1.0 Aug 6, 2023

#43 in #env-file

34 downloads per month

MIT license

7KB
105 lines

Populate structs from cli args and/or .env file

Intended to be used like this:

use from_env::FromEnv;
use lazy_static::lazy_static;
use serde::Deserialize;

fn cred_file() -> String {
    "credentials.json".into()
}

fn server_url() -> String {
    "127.0.0.1:8080".into()
}

#[derive(Debug, Clone, Deserialize)]
pub struct Constants {
    #[serde(default = "cred_file")]
    pub cred_file: String,
    #[serde(default = "server_url")]
    pub server_url: String,
}

lazy_static! {
    pub static ref CONSTANTS: Constants =
        Constants::from_env().expect("Please provide valid args for constants");
}

Now you can either provide values for cred_file and server_url via CLI or .env file, or a mix of both. Any value can be left out. CLI values override .env files, which in turn override defaults.

with a .env file:

cred_file = credentials.json

or directly in the CLI:

cargo run -- --server_url localhost://8080

It uses serde_json under the hood.

Dependencies

~0.5–0.9MB
~19K SLoC