From 8d460ba24c1cc112761db7cfa38ba37ba369ca41 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 11 Feb 2020 22:15:05 +0300 Subject: [PATCH] fix(static): fix static feature for macro --- README.md | 2 +- examples/hyper/Cargo.toml | 4 ++ itconfig/Cargo.toml | 2 +- itconfig/src/macro.rs | 105 +++++++++++++++++++++++--------------- 4 files changed, 70 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 9569be4..511b893 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # itconfig [![Build Status](https://travis-ci.org/icetemple/itconfig-rs.svg?branch=master)](https://travis-ci.org/icetemple/itconfig-rs) [![Documentation](https://docs.rs/itconfig/badge.svg)](https://docs.rs/itconfig) -[![Crates.io](https://img.shields.io/badge/crates.io-v0.11.1-orange.svg?longCache=true)](https://crates.io/crates/itconfig) +[![Crates.io](https://img.shields.io/badge/crates.io-v0.11.2-orange.svg?longCache=true)](https://crates.io/crates/itconfig) [![Join the chat at https://gitter.im/icetemple/itconfig-rs](https://badges.gitter.im/icetemple/itconfig-rs.svg)](https://gitter.im/icetemple/itconfig-rs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) Easy build a configs from environment variables and use it in globally. diff --git a/examples/hyper/Cargo.toml b/examples/hyper/Cargo.toml index 640780a..2fcf87f 100644 --- a/examples/hyper/Cargo.toml +++ b/examples/hyper/Cargo.toml @@ -14,3 +14,7 @@ tokio = { version = "0.2", features = ["macros"] } bytes = "0.5" futures-util = { version = "0.3", default-features = false } pretty_env_logger = "0.3" + +[features] +default = ["static"] +static = [] diff --git a/itconfig/Cargo.toml b/itconfig/Cargo.toml index ff4d912..7476318 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "itconfig" -version = "0.11.1" +version = "0.11.2" authors = ["Dmitriy Pleshevskiy "] description = "Easy build a configs from environment variables and use it in globally." categories = ["config", "web-programming"] diff --git a/itconfig/src/macro.rs b/itconfig/src/macro.rs index bb43b1f..f366e6f 100644 --- a/itconfig/src/macro.rs +++ b/itconfig/src/macro.rs @@ -301,14 +301,6 @@ macro_rules! __itconfig_invalid_syntax { `https://docs.rs/itconfig/latest/itconfig/macro.config.html`" ); }; - - (feature "static") => { - compile_error!( - "Feature `static` is required for enable this macro function.\ - Please see the `config!` macro docs for more info.\ - `https://docs.rs/itconfig/latest/itconfig/macro.config.html`" - ); - }; } #[macro_export] @@ -352,6 +344,55 @@ macro_rules! __itconfig_parse_module { } +#[cfg(feature = "static")] +#[macro_export] +#[doc(hidden)] +macro_rules! __itconfig_impl_static_feature { + ( + unparsed_meta = $meta:tt, + unparsed_concat = $concat:tt, + name = $name:ident, + ty = $ty:ty, + $(default = $default:expr,)? + tokens = $tokens:tt, + args = [$($args:tt)*], + ) => { + __itconfig_parse_variables! { + current_variable = { + unparsed_meta = $meta, + meta = [], + unparsed_concat = $concat, + concat = [], + name = $name, + ty = $ty, + is_static = true, + $(default = $default,)? + }, + tokens = $tokens, + $($args)* + } + }; + + (@import_modules) => { + use $crate::lazy_static::lazy_static; + }; +} + + +#[cfg(not(feature = "static"))] +#[macro_export] +#[doc(hidden)] +macro_rules! __itconfig_impl_static_feature { + ($($tt:tt)*) => { + compile_error!( + "Feature `static` is required for enable this macro function.\ + Please see the `config!` macro docs for more info.\ + `https://docs.rs/itconfig/latest/itconfig/macro.config.html`" + ); + }; +} + + #[macro_export] #[doc(hidden)] macro_rules! __itconfig_get_ty_or_default { @@ -397,23 +438,14 @@ macro_rules! __itconfig_parse_variables { ], $($args:tt)* ) => { - #[cfg(feature = "static")] - __itconfig_parse_variables! { - current_variable = { - unparsed_meta = [$(#$meta)*], - meta = [], - unparsed_concat = [$($inner)+], - concat = [], - name = $name, - ty = String, - is_static = true, - }, + __itconfig_impl_static_feature! { + unparsed_meta = [$(#$meta)*], + unparsed_concat = [$($inner)+], + name = $name, + ty = String, tokens = [$($rest)*], - $($args)* + args = [$($args)*], } - - #[cfg(not(feature = "static"))] - __itconfig_invalid_syntax!(feature "static"); }; // Find concatenated variable @@ -449,24 +481,15 @@ macro_rules! __itconfig_parse_variables { ], $($args:tt)* ) => { - #[cfg(feature = "static")] - __itconfig_parse_variables! { - current_variable = { - unparsed_meta = [$(#$meta)*], - meta = [], - unparsed_concat = [], - concat = [], - name = $name, - ty = __itconfig_get_ty_or_default!($($ty)?), - is_static = true, - $(default = $default,)? - }, + __itconfig_impl_static_feature! { + unparsed_meta = [$(#$meta)*], + unparsed_concat = [], + name = $name, + ty = __itconfig_get_ty_or_default!($($ty)?), + $(default = $default,)? tokens = [$($rest)*], - $($args)* + args = [$($args)*], } - - #[cfg(not(feature = "static"))] - __itconfig_invalid_syntax!(feature "static"); }; // Find variable @@ -664,8 +687,8 @@ macro_rules! __itconfig_impl_namespace { $(#$meta)* pub mod $mod_name { #![allow(non_snake_case)] - #[cfg(feature = "static")] - use $crate::lazy_static::lazy_static; + + __itconfig_impl_static_feature!( @import_modules ); $(__itconfig_impl_namespace! { variables = $ns_variable,