| Crates.io | from_env |
| lib.rs | from_env |
| version | 0.1.1 |
| created_at | 2023-08-06 15:39:46.096452+00 |
| updated_at | 2023-08-06 15:40:39.64213+00 |
| description | Populate structs with values given by .env file or CLI arguments |
| homepage | https://github.com/tadeohepperle/from_env |
| repository | |
| max_upload_size | |
| id | 937229 |
| size | 6,857 |
.env fileIntended 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.
.env file:cred_file = credentials.json
cargo run -- --server_url localhost://8080
It uses serde_json under the hood.