feat: add custom meta for vars
This commit is contained in:
parent
8510b534ce
commit
b305b7334b
7 changed files with 97 additions and 10 deletions
|
@ -24,6 +24,10 @@ config! {
|
|||
FOO: bool,
|
||||
|
||||
BAR: i32 => 10,
|
||||
|
||||
#[cfg(feature = "feature")]
|
||||
#[env_name = "POSTGRES_CONNECTION_STRING"]
|
||||
DATABASE_URL: String
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +52,8 @@ cargo test
|
|||
|
||||
* [x] Add namespace for variables
|
||||
* [x] Custom env name
|
||||
* [ ] Add if condition for feature variables
|
||||
* [x] Support feature config and other meta directives
|
||||
* [ ] Concat env variables to one variable
|
||||
|
||||
|
||||
## License
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "itconfig"
|
||||
version = "0.4.0"
|
||||
version = "0.5.0"
|
||||
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
|
||||
description = "Easy build a configs from environment variables and use it in globally."
|
||||
categories = ["config", "web-programming"]
|
||||
|
@ -14,7 +14,8 @@ readme = "README.md"
|
|||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[badges.travis-ci]
|
||||
repository = "icetemple/itconfig-rs"
|
||||
[badges]
|
||||
travis-ci = { repository = "icetemple/itconfig-rs" }
|
||||
maintenance = { status = "actively-developed" }
|
||||
|
||||
[dependencies]
|
||||
|
|
|
@ -21,6 +21,10 @@ config! {
|
|||
FOO: bool,
|
||||
|
||||
BAR: i32 => 10,
|
||||
|
||||
#[cfg(feature = "feature")]
|
||||
#[env_name = "POSTGRES_CONNECTION_STRING"]
|
||||
DATABASE_URL: String
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,7 +43,8 @@ fn main () {
|
|||
|
||||
* [x] Add namespace for variables
|
||||
* [x] Custom env name
|
||||
* [ ] Add if condition for feature variables
|
||||
* [x] Support feature config and other meta directives
|
||||
* [ ] Concat env variables to one variable
|
||||
|
||||
|
||||
## License
|
||||
|
|
|
@ -172,6 +172,20 @@ impl From<EnvValue> for String {
|
|||
/// assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95);
|
||||
/// ```
|
||||
///
|
||||
/// Also you can add custom meta for each variable. For example feature configurations.
|
||||
///
|
||||
/// ```rust
|
||||
/// # #[macro_use] extern crate itconfig;
|
||||
/// config! {
|
||||
/// #[cfg(feature = "postgres")]
|
||||
/// DATABASE_URL: String,
|
||||
///
|
||||
/// #[cfg(not(feature = "postgres"))]
|
||||
/// DATABASE_URL: String,
|
||||
/// }
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
///
|
||||
///
|
||||
/// This module will also contain helper method:
|
||||
///
|
||||
|
@ -296,6 +310,7 @@ macro_rules! __itconfig_parse_variables {
|
|||
__itconfig_parse_variables! {
|
||||
current_variable = {
|
||||
unparsed_meta = [$(#$meta)*],
|
||||
meta = [],
|
||||
name = $name,
|
||||
ty = $ty,
|
||||
default = $default,
|
||||
|
@ -317,6 +332,7 @@ macro_rules! __itconfig_parse_variables {
|
|||
__itconfig_parse_variables! {
|
||||
current_variable = {
|
||||
unparsed_meta = [$(#$meta)*],
|
||||
meta = [],
|
||||
name = $name,
|
||||
ty = $ty,
|
||||
},
|
||||
|
@ -325,12 +341,14 @@ macro_rules! __itconfig_parse_variables {
|
|||
}
|
||||
};
|
||||
|
||||
// Find meta with custom env name
|
||||
(
|
||||
current_variable = {
|
||||
unparsed_meta = [
|
||||
#[env_name = $env_name:expr]
|
||||
$($meta:tt)*
|
||||
$($rest:tt)*
|
||||
],
|
||||
meta = $meta:tt,
|
||||
name = $name:ident,
|
||||
$($current_variable:tt)*
|
||||
},
|
||||
|
@ -338,7 +356,8 @@ macro_rules! __itconfig_parse_variables {
|
|||
) => {
|
||||
__itconfig_parse_variables! {
|
||||
current_variable = {
|
||||
unparsed_meta = [$($meta)*],
|
||||
unparsed_meta = [$($rest)*],
|
||||
meta = $meta,
|
||||
name = $name,
|
||||
env_name = $env_name,
|
||||
$($current_variable)*
|
||||
|
@ -347,6 +366,30 @@ macro_rules! __itconfig_parse_variables {
|
|||
}
|
||||
};
|
||||
|
||||
// Find stranger meta
|
||||
(
|
||||
current_variable = {
|
||||
unparsed_meta = [
|
||||
#$stranger_meta:tt
|
||||
$($rest:tt)*
|
||||
],
|
||||
meta = [$(#$meta:tt,)*],
|
||||
name = $name:ident,
|
||||
$($current_variable:tt)*
|
||||
},
|
||||
$($args:tt)*
|
||||
) => {
|
||||
__itconfig_parse_variables! {
|
||||
current_variable = {
|
||||
unparsed_meta = [$($rest)*],
|
||||
meta = [$(#$meta,)* #$stranger_meta,],
|
||||
name = $name,
|
||||
$($current_variable)*
|
||||
},
|
||||
$($args)*
|
||||
}
|
||||
};
|
||||
|
||||
// Done parsing variable
|
||||
(
|
||||
current_variable = {
|
||||
|
@ -412,12 +455,14 @@ macro_rules! __itconfig_parse_variables {
|
|||
macro_rules! __itconfig_impl {
|
||||
(
|
||||
variables = [$({
|
||||
meta = $var_meta:tt,
|
||||
name = $var_name:ident,
|
||||
$($variable:tt)*
|
||||
},)*],
|
||||
namespaces = [$({
|
||||
variables = [$({
|
||||
name = $ns_var_name:tt,
|
||||
meta = $ns_var_meta:tt,
|
||||
name = $ns_var_name:ident,
|
||||
$($ns_variables:tt)*
|
||||
},)*],
|
||||
env_prefix = $ns_env_prefix:expr,
|
||||
|
@ -439,6 +484,7 @@ macro_rules! __itconfig_impl {
|
|||
use $crate::EnvValue;
|
||||
|
||||
$(__itconfig_variable! {
|
||||
meta = $ns_var_meta,
|
||||
name = $ns_var_name,
|
||||
env_prefix = $ns_env_prefix,
|
||||
$($ns_variables)*
|
||||
|
@ -453,6 +499,7 @@ macro_rules! __itconfig_impl {
|
|||
}
|
||||
|
||||
$(__itconfig_variable! {
|
||||
meta = $var_meta,
|
||||
name = $var_name,
|
||||
env_prefix = $env_prefix,
|
||||
$($variable)*
|
||||
|
@ -471,12 +518,14 @@ macro_rules! __itconfig_impl {
|
|||
#[doc(hidden)]
|
||||
macro_rules! __itconfig_variable {
|
||||
(
|
||||
meta = $meta:tt,
|
||||
name = $name:ident,
|
||||
env_prefix = $env_prefix:expr,
|
||||
ty = $ty:ty,
|
||||
$($args:tt)*
|
||||
) => {
|
||||
__itconfig_variable! {
|
||||
meta = $meta,
|
||||
name = $name,
|
||||
env_prefix = $env_prefix,
|
||||
env_name = concat!($env_prefix, stringify!($name)).to_uppercase(),
|
||||
|
@ -487,12 +536,14 @@ macro_rules! __itconfig_variable {
|
|||
|
||||
// Add method without default value
|
||||
(
|
||||
meta = $meta:tt,
|
||||
name = $name:ident,
|
||||
env_prefix = $env_prefix:expr,
|
||||
env_name = $env_name:expr,
|
||||
ty = $ty:ty,
|
||||
) => {
|
||||
__itconfig_variable! {
|
||||
meta = $meta,
|
||||
name = $name,
|
||||
env_prefix = $env_prefix,
|
||||
env_name = $env_name,
|
||||
|
@ -503,12 +554,14 @@ macro_rules! __itconfig_variable {
|
|||
|
||||
// Add method with default value
|
||||
(
|
||||
meta = [$(#$meta:tt,)*],
|
||||
name = $name:ident,
|
||||
env_prefix = $env_prefix:expr,
|
||||
env_name = $env_name:expr,
|
||||
ty = $ty:ty,
|
||||
default = $default:expr,
|
||||
) => {
|
||||
$(#$meta)*
|
||||
pub fn $name() -> $ty {
|
||||
env::var($env_name)
|
||||
.map(|val| EnvValue::from(val).into())
|
||||
|
|
|
@ -218,3 +218,26 @@ fn custom_environment_name_for_variable() {
|
|||
assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95);
|
||||
env::remove_var("MY_CUSTOM_NAME");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stranger_meta_data() {
|
||||
env::set_var("MY_CUSTOM_NAME", "95");
|
||||
|
||||
config! {
|
||||
#[cfg(feature = "postgres")]
|
||||
#[env_name = "MY_CUSTOM_NAME"]
|
||||
DATABASE_URL: String,
|
||||
|
||||
#[cfg(not(feature = "postgres"))]
|
||||
#[env_name = "MY_CUSTOM_NAME"]
|
||||
DATABASE_URL: i32,
|
||||
}
|
||||
|
||||
cfg::init();
|
||||
#[cfg(not(feature = "postgres"))]
|
||||
assert_eq!(cfg::DATABASE_URL(), 95);
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
assert_eq!(cfg::DATABASE_URL(), "95");
|
||||
env::remove_var("MY_CUSTOM_NAME");
|
||||
}
|
Reference in a new issue