docs: update macro docs
This commit is contained in:
parent
60f91bf915
commit
237f762a3d
2 changed files with 35 additions and 14 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "itconfig"
|
name = "itconfig"
|
||||||
version = "0.5.1"
|
version = "0.5.2"
|
||||||
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"]
|
||||||
|
|
|
@ -186,14 +186,24 @@ impl From<EnvValue> for String {
|
||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
/// ---
|
||||||
///
|
///
|
||||||
/// This module will also contain helper method:
|
/// This module will also contain helper method:
|
||||||
|
/// --------------------------------------------
|
||||||
///
|
///
|
||||||
/// `init`
|
/// ```rust
|
||||||
|
/// pub fn init();
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Run this at the main function for check all required variables without default value.
|
||||||
|
///
|
||||||
|
/// Panics
|
||||||
/// ------
|
/// ------
|
||||||
///
|
///
|
||||||
/// If you miss some required variables your application will panic at startup.
|
/// If you miss some required variables your application will panic at startup.
|
||||||
/// Run this at the main function for check all required variables without default value.
|
///
|
||||||
|
/// Examples
|
||||||
|
/// --------
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// #[macro_use] extern crate itconfig;
|
/// #[macro_use] extern crate itconfig;
|
||||||
|
@ -211,7 +221,7 @@ impl From<EnvValue> for String {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
#[macro_export]
|
#[macro_export(local_inner_macros)]
|
||||||
macro_rules! config {
|
macro_rules! config {
|
||||||
($($tokens:tt)*) => {
|
($($tokens:tt)*) => {
|
||||||
__itconfig_parse_module! {
|
__itconfig_parse_module! {
|
||||||
|
@ -454,14 +464,8 @@ macro_rules! __itconfig_impl {
|
||||||
) => {
|
) => {
|
||||||
pub mod $mod_name {
|
pub mod $mod_name {
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
use std::env;
|
|
||||||
use $crate::EnvValue;
|
|
||||||
|
|
||||||
$(
|
$(
|
||||||
pub mod $ns_name {
|
pub mod $ns_name {
|
||||||
use std::env;
|
|
||||||
use $crate::EnvValue;
|
|
||||||
|
|
||||||
$(__itconfig_variable! {
|
$(__itconfig_variable! {
|
||||||
meta = $ns_var_meta,
|
meta = $ns_var_meta,
|
||||||
name = $ns_var_name,
|
name = $ns_var_name,
|
||||||
|
@ -496,6 +500,7 @@ macro_rules! __itconfig_impl {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
macro_rules! __itconfig_variable {
|
macro_rules! __itconfig_variable {
|
||||||
|
// Set default env name
|
||||||
(
|
(
|
||||||
meta = $meta:tt,
|
meta = $meta:tt,
|
||||||
name = $name:ident,
|
name = $name:ident,
|
||||||
|
@ -534,17 +539,33 @@ macro_rules! __itconfig_variable {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This macro returns environment variable by name and converts variable to desired type
|
||||||
#[macro_export]
|
/// or returns default value.
|
||||||
|
///
|
||||||
|
/// Panics
|
||||||
|
/// ------
|
||||||
|
/// If you don't pass default value, macro will panic
|
||||||
|
///
|
||||||
|
/// Examples
|
||||||
|
/// --------
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use] extern crate itconfig;
|
||||||
|
/// let url: String = env_or!("DATABASE_URL", "127.0.0.1".to_string());
|
||||||
|
/// assert_eq!(url, "127.0.0.1".to_string());
|
||||||
|
/// ```
|
||||||
|
#[macro_export(local_inner_macro)]
|
||||||
macro_rules! env_or {
|
macro_rules! env_or {
|
||||||
($env_name:expr) => {
|
($env_name:expr) => {
|
||||||
env_or!($env_name, panic!(format!(r#"Cannot read "{}" environment variable"#, $env_name)));
|
env_or!($env_name, panic!(format!(r#"Cannot read "{}" environment variable"#, $env_name)));
|
||||||
};
|
};
|
||||||
|
|
||||||
($env_name:expr, $default:expr) => {
|
($env_name:expr, $default:expr) => {{
|
||||||
|
use std::env;
|
||||||
|
use itconfig::EnvValue;
|
||||||
env::var($env_name)
|
env::var($env_name)
|
||||||
.map(|val| EnvValue::from(val).into())
|
.map(|val| EnvValue::from(val).into())
|
||||||
.unwrap_or_else(|_| $default)
|
.unwrap_or_else(|_| $default)
|
||||||
};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue