Crates.io | dotenv_config |
lib.rs | dotenv_config |
version | 0.2.1 |
source | src |
created_at | 2022-09-09 09:02:09.163695 |
updated_at | 2024-11-01 09:14:15.342152 |
description | parse `env` to config struct for Rust |
homepage | https://github.com/openobserve/dotenv-config/ |
repository | https://github.com/openobserve/dotenv-config/ |
max_upload_size | |
id | 661729 |
size | 34,728 |
use .env
as config file and parse environments to config struct.
use dotenv_config::EnvConfig;
use dotenvy::dotenv;
#[derive(Debug, PartialEq)]
enum Color {
Red,
Green,
Blue,
}
impl std::str::FromStr for Color {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"red" => Ok(Color::Red),
"green" => Ok(Color::Green),
"blue" => Ok(Color::Blue),
_ => Err("no match color"),
}
}
}
#[derive(Debug, EnvConfig)]
struct Config {
#[env_config(default = "192.168.2.1")]
server_addr: String,
server_mode: bool,
#[env_config(name = "ZINC_ENABLE", default = true)]
enable: bool,
#[env_config(name = "ZINC_NUMBER", default = 123456, help = "this is for demo")]
num: Option<i64>,
#[env_config(parse, default = "green")] // or parse=true
color: Color,
}
fn main() {
dotenv().ok();
let cfg = Config::init().unwrap();
println!("{:#?}", cfg);
// print config help
let help = Config::get_help();
println!("{:#?}", help);
}
you can use macro attribute set field attribute
.env
file config it.ZINC_ENABLE=false
ZINC_NUMBER=8787878
default load environment key is: structName_fieldName
do UpperSnake, like above struct, default config key is:
CONFIG_SERVER_ADDR
CONFIG_SERVER_MODE
ZINC_ENABLE
ZINC_NUMBER