from_env

Crates.iofrom_env
lib.rsfrom_env
version0.1.1
sourcesrc
created_at2023-08-06 15:39:46.096452
updated_at2023-08-06 15:40:39.64213
descriptionPopulate structs with values given by .env file or CLI arguments
homepagehttps://github.com/tadeohepperle/from_env
repository
max_upload_size
id937229
size6,857
Tadeo Hepperle (tadeohepperle)

documentation

README

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.

Commit count: 0

cargo fmt