feat: improve macro

This commit is contained in:
Dmitriy Pleshevskiy 2019-12-29 10:35:50 +03:00
parent 6330ef0d00
commit 60f91bf915
3 changed files with 22 additions and 49 deletions

View file

@ -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.

View file

@ -1,6 +1,6 @@
[package]
name = "itconfig"
version = "0.5.0"
version = "0.5.1"
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
description = "Easy build a configs from environment variables and use it in globally."
categories = ["config", "web-programming"]

View file

@ -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)
};
}