From 60f91bf9151915c2f683217555580b43d8a477c7 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 29 Dec 2019 10:35:50 +0300 Subject: [PATCH] feat: improve macro --- README.md | 2 +- itconfig/Cargo.toml | 2 +- itconfig/src/lib.rs | 67 ++++++++++++++------------------------------- 3 files changed, 22 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 7c923f3..4f896e0 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.5.0-orange.svg?longCache=true)](https://crates.io/crates/itconfig) +[![Crates.io](https://img.shields.io/badge/crates.io-v0.5.1-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/itconfig/Cargo.toml b/itconfig/Cargo.toml index 6575af8..4feab02 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "itconfig" -version = "0.5.0" +version = "0.5.1" 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/lib.rs b/itconfig/src/lib.rs index 0d1d734..2ded831 100644 --- a/itconfig/src/lib.rs +++ b/itconfig/src/lib.rs @@ -298,33 +298,11 @@ macro_rules! __itconfig_parse_variables { } }; - // Find variable with default value + // Find variable ( tokens = [ $(#$meta:tt)* - $name:ident : $ty:ty => $default:expr, - $($rest:tt)* - ], - $($args:tt)* - ) => { - __itconfig_parse_variables! { - current_variable = { - unparsed_meta = [$(#$meta)*], - meta = [], - name = $name, - ty = $ty, - default = $default, - }, - tokens = [$($rest)*], - $($args)* - } - }; - - // Find variable without default value - ( - tokens = [ - $(#$meta:tt)* - $name:ident : $ty:ty, + $name:ident : $ty:ty$( => $default:expr)?, $($rest:tt)* ], $($args:tt)* @@ -335,6 +313,7 @@ macro_rules! __itconfig_parse_variables { meta = [], name = $name, ty = $ty, + $(default = $default,)? }, tokens = [$($rest)*], $($args)* @@ -534,38 +513,18 @@ 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, - ty = $ty, - default = panic!(format!(r#"Cannot read "{}" environment variable"#, $env_name)), - } - }; - - // Add method with default value + // Add method ( meta = [$(#$meta:tt,)*], name = $name:ident, env_prefix = $env_prefix:expr, env_name = $env_name:expr, ty = $ty:ty, - default = $default:expr, + $(default = $default:expr,)? ) => { $(#$meta)* pub fn $name() -> $ty { - env::var($env_name) - .map(|val| EnvValue::from(val).into()) - .unwrap_or_else(|_| $default) + env_or!($env_name$(, $default)?) } }; @@ -575,3 +534,17 @@ macro_rules! __itconfig_variable { }; } + +#[macro_export] +macro_rules! env_or { + ($env_name:expr) => { + env_or!($env_name, panic!(format!(r#"Cannot read "{}" environment variable"#, $env_name))); + }; + + ($env_name:expr, $default:expr) => { + env::var($env_name) + .map(|val| EnvValue::from(val).into()) + .unwrap_or_else(|_| $default) + }; +} +