Merge pull request #4 from icetemple/custom-meta
feat: add custom meta for vars
This commit is contained in:
commit
10b76c10fe
7 changed files with 97 additions and 10 deletions
|
@ -2,4 +2,4 @@
|
||||||
members = [
|
members = [
|
||||||
"itconfig",
|
"itconfig",
|
||||||
"itconfig_tests"
|
"itconfig_tests"
|
||||||
]
|
]
|
||||||
|
|
|
@ -24,6 +24,10 @@ config! {
|
||||||
FOO: bool,
|
FOO: bool,
|
||||||
|
|
||||||
BAR: i32 => 10,
|
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] Add namespace for variables
|
||||||
* [x] Custom env name
|
* [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
|
## License
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "itconfig"
|
name = "itconfig"
|
||||||
version = "0.4.0"
|
version = "0.5.0"
|
||||||
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
|
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
|
||||||
description = "Easy build a configs from environment variables and use it in globally."
|
description = "Easy build a configs from environment variables and use it in globally."
|
||||||
categories = ["config", "web-programming"]
|
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
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[badges.travis-ci]
|
[badges]
|
||||||
repository = "icetemple/itconfig-rs"
|
travis-ci = { repository = "icetemple/itconfig-rs" }
|
||||||
|
maintenance = { status = "actively-developed" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -21,6 +21,10 @@ config! {
|
||||||
FOO: bool,
|
FOO: bool,
|
||||||
|
|
||||||
BAR: i32 => 10,
|
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] Add namespace for variables
|
||||||
* [x] Custom env name
|
* [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
|
## License
|
||||||
|
|
|
@ -172,6 +172,20 @@ impl From<EnvValue> for String {
|
||||||
/// assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95);
|
/// 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:
|
/// This module will also contain helper method:
|
||||||
///
|
///
|
||||||
|
@ -296,6 +310,7 @@ macro_rules! __itconfig_parse_variables {
|
||||||
__itconfig_parse_variables! {
|
__itconfig_parse_variables! {
|
||||||
current_variable = {
|
current_variable = {
|
||||||
unparsed_meta = [$(#$meta)*],
|
unparsed_meta = [$(#$meta)*],
|
||||||
|
meta = [],
|
||||||
name = $name,
|
name = $name,
|
||||||
ty = $ty,
|
ty = $ty,
|
||||||
default = $default,
|
default = $default,
|
||||||
|
@ -317,6 +332,7 @@ macro_rules! __itconfig_parse_variables {
|
||||||
__itconfig_parse_variables! {
|
__itconfig_parse_variables! {
|
||||||
current_variable = {
|
current_variable = {
|
||||||
unparsed_meta = [$(#$meta)*],
|
unparsed_meta = [$(#$meta)*],
|
||||||
|
meta = [],
|
||||||
name = $name,
|
name = $name,
|
||||||
ty = $ty,
|
ty = $ty,
|
||||||
},
|
},
|
||||||
|
@ -325,12 +341,14 @@ macro_rules! __itconfig_parse_variables {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Find meta with custom env name
|
||||||
(
|
(
|
||||||
current_variable = {
|
current_variable = {
|
||||||
unparsed_meta = [
|
unparsed_meta = [
|
||||||
#[env_name = $env_name:expr]
|
#[env_name = $env_name:expr]
|
||||||
$($meta:tt)*
|
$($rest:tt)*
|
||||||
],
|
],
|
||||||
|
meta = $meta:tt,
|
||||||
name = $name:ident,
|
name = $name:ident,
|
||||||
$($current_variable:tt)*
|
$($current_variable:tt)*
|
||||||
},
|
},
|
||||||
|
@ -338,7 +356,8 @@ macro_rules! __itconfig_parse_variables {
|
||||||
) => {
|
) => {
|
||||||
__itconfig_parse_variables! {
|
__itconfig_parse_variables! {
|
||||||
current_variable = {
|
current_variable = {
|
||||||
unparsed_meta = [$($meta)*],
|
unparsed_meta = [$($rest)*],
|
||||||
|
meta = $meta,
|
||||||
name = $name,
|
name = $name,
|
||||||
env_name = $env_name,
|
env_name = $env_name,
|
||||||
$($current_variable)*
|
$($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
|
// Done parsing variable
|
||||||
(
|
(
|
||||||
current_variable = {
|
current_variable = {
|
||||||
|
@ -412,12 +455,14 @@ macro_rules! __itconfig_parse_variables {
|
||||||
macro_rules! __itconfig_impl {
|
macro_rules! __itconfig_impl {
|
||||||
(
|
(
|
||||||
variables = [$({
|
variables = [$({
|
||||||
|
meta = $var_meta:tt,
|
||||||
name = $var_name:ident,
|
name = $var_name:ident,
|
||||||
$($variable:tt)*
|
$($variable:tt)*
|
||||||
},)*],
|
},)*],
|
||||||
namespaces = [$({
|
namespaces = [$({
|
||||||
variables = [$({
|
variables = [$({
|
||||||
name = $ns_var_name:tt,
|
meta = $ns_var_meta:tt,
|
||||||
|
name = $ns_var_name:ident,
|
||||||
$($ns_variables:tt)*
|
$($ns_variables:tt)*
|
||||||
},)*],
|
},)*],
|
||||||
env_prefix = $ns_env_prefix:expr,
|
env_prefix = $ns_env_prefix:expr,
|
||||||
|
@ -439,6 +484,7 @@ macro_rules! __itconfig_impl {
|
||||||
use $crate::EnvValue;
|
use $crate::EnvValue;
|
||||||
|
|
||||||
$(__itconfig_variable! {
|
$(__itconfig_variable! {
|
||||||
|
meta = $ns_var_meta,
|
||||||
name = $ns_var_name,
|
name = $ns_var_name,
|
||||||
env_prefix = $ns_env_prefix,
|
env_prefix = $ns_env_prefix,
|
||||||
$($ns_variables)*
|
$($ns_variables)*
|
||||||
|
@ -453,6 +499,7 @@ macro_rules! __itconfig_impl {
|
||||||
}
|
}
|
||||||
|
|
||||||
$(__itconfig_variable! {
|
$(__itconfig_variable! {
|
||||||
|
meta = $var_meta,
|
||||||
name = $var_name,
|
name = $var_name,
|
||||||
env_prefix = $env_prefix,
|
env_prefix = $env_prefix,
|
||||||
$($variable)*
|
$($variable)*
|
||||||
|
@ -471,12 +518,14 @@ macro_rules! __itconfig_impl {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
macro_rules! __itconfig_variable {
|
macro_rules! __itconfig_variable {
|
||||||
(
|
(
|
||||||
|
meta = $meta:tt,
|
||||||
name = $name:ident,
|
name = $name:ident,
|
||||||
env_prefix = $env_prefix:expr,
|
env_prefix = $env_prefix:expr,
|
||||||
ty = $ty:ty,
|
ty = $ty:ty,
|
||||||
$($args:tt)*
|
$($args:tt)*
|
||||||
) => {
|
) => {
|
||||||
__itconfig_variable! {
|
__itconfig_variable! {
|
||||||
|
meta = $meta,
|
||||||
name = $name,
|
name = $name,
|
||||||
env_prefix = $env_prefix,
|
env_prefix = $env_prefix,
|
||||||
env_name = concat!($env_prefix, stringify!($name)).to_uppercase(),
|
env_name = concat!($env_prefix, stringify!($name)).to_uppercase(),
|
||||||
|
@ -487,12 +536,14 @@ macro_rules! __itconfig_variable {
|
||||||
|
|
||||||
// Add method without default value
|
// Add method without default value
|
||||||
(
|
(
|
||||||
|
meta = $meta:tt,
|
||||||
name = $name:ident,
|
name = $name:ident,
|
||||||
env_prefix = $env_prefix:expr,
|
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! {
|
||||||
|
meta = $meta,
|
||||||
name = $name,
|
name = $name,
|
||||||
env_prefix = $env_prefix,
|
env_prefix = $env_prefix,
|
||||||
env_name = $env_name,
|
env_name = $env_name,
|
||||||
|
@ -503,12 +554,14 @@ macro_rules! __itconfig_variable {
|
||||||
|
|
||||||
// Add method with default value
|
// Add method with default value
|
||||||
(
|
(
|
||||||
|
meta = [$(#$meta:tt,)*],
|
||||||
name = $name:ident,
|
name = $name:ident,
|
||||||
env_prefix = $env_prefix:expr,
|
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,
|
||||||
) => {
|
) => {
|
||||||
|
$(#$meta)*
|
||||||
pub fn $name() -> $ty {
|
pub fn $name() -> $ty {
|
||||||
env::var($env_name)
|
env::var($env_name)
|
||||||
.map(|val| EnvValue::from(val).into())
|
.map(|val| EnvValue::from(val).into())
|
||||||
|
|
|
@ -9,4 +9,4 @@ publish = false
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
itconfig = { path = '../itconfig' }
|
itconfig = { path = '../itconfig' }
|
||||||
|
|
|
@ -217,4 +217,27 @@ fn custom_environment_name_for_variable() {
|
||||||
assert_eq!(cfg::PER_PAGE(), 95);
|
assert_eq!(cfg::PER_PAGE(), 95);
|
||||||
assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95);
|
assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95);
|
||||||
env::remove_var("MY_CUSTOM_NAME");
|
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