Accept y/n, True/False, 1/0 as booleans in environment vars
This commit is contained in:
parent
84ed185579
commit
d212dfe735
|
@ -2,7 +2,7 @@ use std::process::exit;
|
|||
use std::sync::RwLock;
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::util::get_env;
|
||||
use crate::util::{get_env, get_env_bool};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref CONFIG: Config = Config::load().unwrap_or_else(|e| {
|
||||
|
@ -23,7 +23,7 @@ macro_rules! make_config {
|
|||
$group:ident $(: $group_enabled:ident)? {
|
||||
$(
|
||||
$(#[doc = $doc:literal])+
|
||||
$name:ident : $ty:ty, $editable:literal, $none_action:ident $(, $default:expr)?;
|
||||
$name:ident : $ty:ident, $editable:literal, $none_action:ident $(, $default:expr)?;
|
||||
)+},
|
||||
)+) => {
|
||||
pub struct Config { inner: RwLock<Inner> }
|
||||
|
@ -50,7 +50,7 @@ macro_rules! make_config {
|
|||
|
||||
let mut builder = ConfigBuilder::default();
|
||||
$($(
|
||||
builder.$name = get_env(&stringify!($name).to_uppercase());
|
||||
builder.$name = make_config! { @getenv &stringify!($name).to_uppercase(), $ty };
|
||||
)+)+
|
||||
|
||||
builder
|
||||
|
@ -189,6 +189,10 @@ macro_rules! make_config {
|
|||
let f: &dyn Fn(&ConfigItems) -> _ = &$default_fn;
|
||||
f($config)
|
||||
}};
|
||||
|
||||
( @getenv $name:expr, bool ) => { get_env_bool($name) };
|
||||
( @getenv $name:expr, $ty:ident ) => { get_env($name) };
|
||||
|
||||
}
|
||||
|
||||
//STRUCTURE:
|
||||
|
|
11
src/util.rs
11
src/util.rs
|
@ -309,6 +309,17 @@ where
|
|||
try_parse_string(env::var(key))
|
||||
}
|
||||
|
||||
const TRUE_VALUES: &[&str] = &["true", "t", "yes", "y", "1"];
|
||||
const FALSE_VALUES: &[&str] = &["false", "f", "no", "n", "0"];
|
||||
|
||||
pub fn get_env_bool(key: &str) -> Option<bool> {
|
||||
match env::var(key) {
|
||||
Ok(val) if TRUE_VALUES.contains(&val.to_lowercase().as_ref()) => Some(true),
|
||||
Ok(val) if FALSE_VALUES.contains(&val.to_lowercase().as_ref()) => Some(false),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Date util methods
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue