diff --git a/README.md b/README.md index 5b173ad..c7cdc5f 100644 --- a/README.md +++ b/README.md @@ -12,22 +12,28 @@ We recommend you start with the [documentation]. ```rust #[macro_use] extern crate itconfig; -use dotenv::dotenv; +use std::env; +//use dotenv::dotenv; config! { DEBUG: bool => true, HOST: String => "127.0.0.1".to_string(), NAMESPACE { - FOO: bool => true, + #[env_name = "MY_CUSTOM_NAME"] + FOO: bool, + BAR: i32 => 10, } } fn main () { - dotenv().ok(); + // dotenv().ok(); + env::set_var("MY_CUSTOM_NAME", "t"); + cfg::init(); assert_eq(cfg::HOST(), String::from("127.0.0.1")); + assert_eq(cfg::NAMESPACE::FOO(), true); } ``` @@ -41,7 +47,7 @@ cargo test ## Roadmap * [x] Add namespace for variables -* [ ] Custom env name +* [x] Custom env name * [ ] Add if condition for feature variables diff --git a/itconfig/Cargo.toml b/itconfig/Cargo.toml index 2dae76f..e651ce7 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "itconfig" -version = "0.3.0" +version = "0.4.0" authors = ["Dmitriy Pleshevskiy "] description = "Easy build a configs from environment variables and use it in globally." categories = ["config", "web-programming"] diff --git a/itconfig/README.md b/itconfig/README.md index 78c1adc..ac38888 100644 --- a/itconfig/README.md +++ b/itconfig/README.md @@ -9,22 +9,28 @@ We recommend you start with the [documentation]. ```rust #[macro_use] extern crate itconfig; -// use dotenv::dotenv; +use std::env; +//use dotenv::dotenv; config! { DEBUG: bool => true, HOST: String => "127.0.0.1".to_string(), NAMESPACE { - FOO: bool => true, + #[env_name = "MY_CUSTOM_NAME"] + FOO: bool, + BAR: i32 => 10, } } fn main () { // dotenv().ok(); + env::set_var("MY_CUSTOM_NAME", "t"); + cfg::init(); assert_eq(cfg::HOST(), String::from("127.0.0.1")); + assert_eq(cfg::NAMESPACE::FOO(), true); } ``` @@ -32,7 +38,7 @@ fn main () { ## Roadmap * [x] Add namespace for variables -* [ ] Custom env name +* [x] Custom env name * [ ] Add if condition for feature variables diff --git a/itconfig/src/lib.rs b/itconfig/src/lib.rs index 0b3aa66..3cb50d7 100644 --- a/itconfig/src/lib.rs +++ b/itconfig/src/lib.rs @@ -7,6 +7,7 @@ //! //! ```rust //! #[macro_use] extern crate itconfig; +//! use std::env; //! // use dotenv::dotenv; //! //! config! { @@ -14,15 +15,20 @@ //! HOST: String => "127.0.0.1".to_string(), //! //! NAMESPACE { -//! FOO: bool => true, +//! #[env_name = "MY_CUSTOM_NAME"] +//! FOO: bool, +//! //! BAR: i32 => 10, //! } //! } //! //! fn main () { //! // dotenv().ok(); +//! env::set_var("MY_CUSTOM_NAME", "t"); +//! //! cfg::init(); //! assert_eq!(cfg::HOST(), String::from("127.0.0.1")); +//! assert_eq!(cfg::NAMESPACE::FOO(), true); //! } //! ``` @@ -142,6 +148,30 @@ impl From for String { /// # cfg::init() /// ``` /// +/// If you want to read custom env name for variable you can change it manually. +/// +/// **A variable in the nameespace will lose environment prefix** +/// +/// ```rust +/// # #[macro_use] extern crate itconfig; +/// # use std::env; +/// env::set_var("MY_CUSTOM_NAME", "95"); +/// +/// config! { +/// #[env_name = "MY_CUSTOM_NAME"] +/// PER_PAGE: i32, +/// +/// APP { +/// #[env_name = "MY_CUSTOM_NAME"] +/// RECIPES_PER_PAGE: i32, +/// } +/// } +/// +/// cfg::init(); +/// assert_eq!(cfg::PER_PAGE(), 95); +/// assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95); +/// ``` +/// /// /// This module will also contain helper method: /// @@ -257,6 +287,7 @@ macro_rules! __itconfig_parse_variables { // Find variable with default value ( tokens = [ + $(#$meta:tt)* $name:ident : $ty:ty => $default:expr, $($rest:tt)* ], @@ -264,6 +295,7 @@ macro_rules! __itconfig_parse_variables { ) => { __itconfig_parse_variables! { current_variable = { + unparsed_meta = [$(#$meta)*], name = $name, ty = $ty, default = $default, @@ -276,6 +308,7 @@ macro_rules! __itconfig_parse_variables { // Find variable without default value ( tokens = [ + $(#$meta:tt)* $name:ident : $ty:ty, $($rest:tt)* ], @@ -283,6 +316,7 @@ macro_rules! __itconfig_parse_variables { ) => { __itconfig_parse_variables! { current_variable = { + unparsed_meta = [$(#$meta)*], name = $name, ty = $ty, }, @@ -291,9 +325,32 @@ macro_rules! __itconfig_parse_variables { } }; + ( + current_variable = { + unparsed_meta = [ + #[env_name = $env_name:expr] + $($meta:tt)* + ], + name = $name:ident, + $($current_variable:tt)* + }, + $($args:tt)* + ) => { + __itconfig_parse_variables! { + current_variable = { + unparsed_meta = [$($meta)*], + name = $name, + env_name = $env_name, + $($current_variable)* + }, + $($args)* + } + }; + // Done parsing variable ( current_variable = { + unparsed_meta = [], $($current_variable:tt)* }, tokens = $tokens:tt, diff --git a/itconfig_tests/tests/config_macro.rs b/itconfig_tests/tests/config_macro.rs index ea1f9ac..9b9ac87 100644 --- a/itconfig_tests/tests/config_macro.rs +++ b/itconfig_tests/tests/config_macro.rs @@ -196,4 +196,25 @@ fn configuration_variables_and_namespace_in_lowercase() { assert_eq!(cfg::namespace::foo(), true); env::remove_var("TESTING"); env::remove_var("NAMESPACE_FOO"); +} + + +#[test] +fn custom_environment_name_for_variable() { + env::set_var("MY_CUSTOM_NAME", "95"); + + config! { + #[env_name = "MY_CUSTOM_NAME"] + PER_PAGE: i32, + + APP { + #[env_name = "MY_CUSTOM_NAME"] + RECIPES_PER_PAGE: i32, + } + } + + cfg::init(); + assert_eq!(cfg::PER_PAGE(), 95); + assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95); + env::remove_var("MY_CUSTOM_NAME"); } \ No newline at end of file