refac: change macro for init env_name
This commit is contained in:
parent
cb757d0464
commit
99244a01cb
1 changed files with 32 additions and 11 deletions
|
@ -216,6 +216,7 @@ macro_rules! __itconfig_parse_module {
|
||||||
variables = [],
|
variables = [],
|
||||||
namespaces = [],
|
namespaces = [],
|
||||||
module = {
|
module = {
|
||||||
|
env_prefix = "",
|
||||||
name = $name,
|
name = $name,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -242,10 +243,11 @@ macro_rules! __itconfig_parse_variables {
|
||||||
__itconfig_parse_variables! {
|
__itconfig_parse_variables! {
|
||||||
tokens = [$($ns_tokens)*],
|
tokens = [$($ns_tokens)*],
|
||||||
variables = [],
|
variables = [],
|
||||||
callback = {
|
module = {
|
||||||
current_namespace = {
|
env_prefix = concat!(stringify!($ns_name), "_"),
|
||||||
name = $ns_name,
|
name = $ns_name,
|
||||||
},
|
},
|
||||||
|
callback = {
|
||||||
tokens = [$($rest)*],
|
tokens = [$($rest)*],
|
||||||
$($args)*
|
$($args)*
|
||||||
},
|
},
|
||||||
|
@ -309,10 +311,10 @@ macro_rules! __itconfig_parse_variables {
|
||||||
(
|
(
|
||||||
tokens = [],
|
tokens = [],
|
||||||
variables = $ns_variables:tt,
|
variables = $ns_variables:tt,
|
||||||
callback = {
|
module = {
|
||||||
current_namespace = {
|
|
||||||
$($current_namespace:tt)*
|
$($current_namespace:tt)*
|
||||||
},
|
},
|
||||||
|
callback = {
|
||||||
tokens = $tokens:tt,
|
tokens = $tokens:tt,
|
||||||
variables = $variables:tt,
|
variables = $variables:tt,
|
||||||
namespaces = [$($namespaces:tt,)*],
|
namespaces = [$($namespaces:tt,)*],
|
||||||
|
@ -361,9 +363,11 @@ macro_rules! __itconfig_impl {
|
||||||
name = $ns_var_name:tt,
|
name = $ns_var_name:tt,
|
||||||
$($ns_variables:tt)*
|
$($ns_variables:tt)*
|
||||||
},)*],
|
},)*],
|
||||||
|
env_prefix = $ns_env_prefix:expr,
|
||||||
name = $ns_name:ident,
|
name = $ns_name:ident,
|
||||||
},)*],
|
},)*],
|
||||||
module = {
|
module = {
|
||||||
|
env_prefix = $env_prefix:expr,
|
||||||
name = $mod_name:ident,
|
name = $mod_name:ident,
|
||||||
},
|
},
|
||||||
) => {
|
) => {
|
||||||
|
@ -379,7 +383,7 @@ macro_rules! __itconfig_impl {
|
||||||
|
|
||||||
$(__itconfig_variable! {
|
$(__itconfig_variable! {
|
||||||
name = $ns_var_name,
|
name = $ns_var_name,
|
||||||
env_name = concat!(stringify!($ns_name), "_", stringify!($ns_var_name)),
|
env_prefix = $ns_env_prefix,
|
||||||
$($ns_variables)*
|
$($ns_variables)*
|
||||||
})*
|
})*
|
||||||
}
|
}
|
||||||
|
@ -393,7 +397,7 @@ macro_rules! __itconfig_impl {
|
||||||
|
|
||||||
$(__itconfig_variable! {
|
$(__itconfig_variable! {
|
||||||
name = $var_name,
|
name = $var_name,
|
||||||
env_name = stringify!($var_name),
|
env_prefix = $env_prefix,
|
||||||
$($variable)*
|
$($variable)*
|
||||||
})*
|
})*
|
||||||
}
|
}
|
||||||
|
@ -409,30 +413,47 @@ macro_rules! __itconfig_impl {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
macro_rules! __itconfig_variable {
|
macro_rules! __itconfig_variable {
|
||||||
|
(
|
||||||
|
name = $name:ident,
|
||||||
|
env_prefix = $env_prefix:expr,
|
||||||
|
ty = $ty:ty,
|
||||||
|
$($args:tt)*
|
||||||
|
) => {
|
||||||
|
__itconfig_variable! {
|
||||||
|
name = $name,
|
||||||
|
env_prefix = $env_prefix,
|
||||||
|
env_name = concat!($env_prefix, stringify!($name)).to_uppercase(),
|
||||||
|
ty = $ty,
|
||||||
|
$($args)*
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Add method without default value
|
// Add method without default value
|
||||||
(
|
(
|
||||||
name = $name:ident,
|
name = $name:ident,
|
||||||
|
env_prefix = $env_prefix:expr,
|
||||||
env_name = $env_name:expr,
|
env_name = $env_name:expr,
|
||||||
ty = $ty:ty,
|
ty = $ty:ty,
|
||||||
) => {
|
) => {
|
||||||
__itconfig_variable! {
|
__itconfig_variable! {
|
||||||
name = $name,
|
name = $name,
|
||||||
|
env_prefix = $env_prefix,
|
||||||
env_name = $env_name,
|
env_name = $env_name,
|
||||||
ty = $ty,
|
ty = $ty,
|
||||||
default = panic!(format!(r#"Cannot read "{}" environment variable"#,
|
default = panic!(format!(r#"Cannot read "{}" environment variable"#, $env_name)),
|
||||||
$env_name.to_uppercase())),
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add method with default value
|
// Add method with default value
|
||||||
(
|
(
|
||||||
name = $name:ident,
|
name = $name:ident,
|
||||||
|
env_prefix = $env_prefix:expr,
|
||||||
env_name = $env_name:expr,
|
env_name = $env_name:expr,
|
||||||
ty = $ty:ty,
|
ty = $ty:ty,
|
||||||
default = $default:expr,
|
default = $default:expr,
|
||||||
) => {
|
) => {
|
||||||
pub fn $name() -> $ty {
|
pub fn $name() -> $ty {
|
||||||
env::var($env_name.to_uppercase())
|
env::var($env_name)
|
||||||
.map(|val| EnvValue::from(val).into())
|
.map(|val| EnvValue::from(val).into())
|
||||||
.unwrap_or_else(|_| $default)
|
.unwrap_or_else(|_| $default)
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue