feat: improve macro
This commit is contained in:
parent
6330ef0d00
commit
60f91bf915
3 changed files with 22 additions and 49 deletions
|
@ -1,7 +1,7 @@
|
||||||
# itconfig
|
# itconfig
|
||||||
[![Build Status](https://travis-ci.org/icetemple/itconfig-rs.svg?branch=master)](https://travis-ci.org/icetemple/itconfig-rs)
|
[![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)
|
[![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)
|
[![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.
|
Easy build a configs from environment variables and use it in globally.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "itconfig"
|
name = "itconfig"
|
||||||
version = "0.5.0"
|
version = "0.5.1"
|
||||||
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"]
|
||||||
|
|
|
@ -298,33 +298,11 @@ macro_rules! __itconfig_parse_variables {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Find variable with default value
|
// Find variable
|
||||||
(
|
(
|
||||||
tokens = [
|
tokens = [
|
||||||
$(#$meta:tt)*
|
$(#$meta:tt)*
|
||||||
$name:ident : $ty:ty => $default:expr,
|
$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,
|
|
||||||
$($rest:tt)*
|
$($rest:tt)*
|
||||||
],
|
],
|
||||||
$($args:tt)*
|
$($args:tt)*
|
||||||
|
@ -335,6 +313,7 @@ macro_rules! __itconfig_parse_variables {
|
||||||
meta = [],
|
meta = [],
|
||||||
name = $name,
|
name = $name,
|
||||||
ty = $ty,
|
ty = $ty,
|
||||||
|
$(default = $default,)?
|
||||||
},
|
},
|
||||||
tokens = [$($rest)*],
|
tokens = [$($rest)*],
|
||||||
$($args)*
|
$($args)*
|
||||||
|
@ -534,38 +513,18 @@ macro_rules! __itconfig_variable {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add method without default value
|
// Add method
|
||||||
(
|
|
||||||
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
|
|
||||||
(
|
(
|
||||||
meta = [$(#$meta:tt,)*],
|
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)*
|
$(#$meta)*
|
||||||
pub fn $name() -> $ty {
|
pub fn $name() -> $ty {
|
||||||
env::var($env_name)
|
env_or!($env_name$(, $default)?)
|
||||||
.map(|val| EnvValue::from(val).into())
|
|
||||||
.unwrap_or_else(|_| $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)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue