refac: reuse init variable macro
This commit is contained in:
parent
900c5ef2a8
commit
dead8e444b
1 changed files with 15 additions and 17 deletions
|
@ -407,6 +407,21 @@ macro_rules! __itconfig_impl {
|
|||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! __itconfig_variable {
|
||||
// Add method without default value
|
||||
(
|
||||
name = $name:ident,
|
||||
env_name = $env_name:expr,
|
||||
ty = $ty:ty,
|
||||
) => {
|
||||
__itconfig_variable! {
|
||||
name = $name,
|
||||
env_name = $env_name,
|
||||
ty = $ty,
|
||||
default = panic!(format!(r#"Cannot read "{}" environment variable"#,
|
||||
$env_name.to_uppercase())),
|
||||
}
|
||||
};
|
||||
|
||||
// Add method with default value
|
||||
(
|
||||
name = $name:ident,
|
||||
|
@ -421,23 +436,6 @@ macro_rules! __itconfig_variable {
|
|||
}
|
||||
};
|
||||
|
||||
// Add method without default value
|
||||
(
|
||||
name = $name:ident,
|
||||
env_name = $env_name:expr,
|
||||
ty = $ty:ty,
|
||||
) => {
|
||||
pub fn $name() -> $ty {
|
||||
env::var($env_name.to_uppercase())
|
||||
.map(|val| EnvValue::from(val).into())
|
||||
.unwrap_or_else(|_| {
|
||||
panic!(format!(r#"Cannot read "{}" environment variable"#,
|
||||
$env_name.to_uppercase()))
|
||||
})
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// Invalid syntax
|
||||
($($tokens:tt)*) => {
|
||||
__itconfig_invalid_syntax!();
|
||||
|
|
Reference in a new issue