diff --git a/itconfig/Cargo.toml b/itconfig/Cargo.toml index 4feab02..8c4176c 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "itconfig" -version = "0.5.1" +version = "0.5.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/lib.rs b/itconfig/src/lib.rs index 2ded831..f32a7e0 100644 --- a/itconfig/src/lib.rs +++ b/itconfig/src/lib.rs @@ -186,14 +186,24 @@ impl From for String { /// # fn main() {} /// ``` /// +/// --- /// /// 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. -/// Run this at the main function for check all required variables without default value. +/// +/// Examples +/// -------- /// /// ```rust /// #[macro_use] extern crate itconfig; @@ -211,7 +221,7 @@ impl From for String { /// } /// ``` /// -#[macro_export] +#[macro_export(local_inner_macros)] macro_rules! config { ($($tokens:tt)*) => { __itconfig_parse_module! { @@ -454,14 +464,8 @@ macro_rules! __itconfig_impl { ) => { pub mod $mod_name { #![allow(non_snake_case)] - use std::env; - use $crate::EnvValue; - $( pub mod $ns_name { - use std::env; - use $crate::EnvValue; - $(__itconfig_variable! { meta = $ns_var_meta, name = $ns_var_name, @@ -496,6 +500,7 @@ macro_rules! __itconfig_impl { #[macro_export] #[doc(hidden)] macro_rules! __itconfig_variable { + // Set default env name ( meta = $meta:tt, name = $name:ident, @@ -534,17 +539,33 @@ macro_rules! __itconfig_variable { }; } - -#[macro_export] +/// This macro returns environment variable by name and converts variable to desired type +/// 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 { ($env_name:expr) => { 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) .map(|val| EnvValue::from(val).into()) .unwrap_or_else(|_| $default) - }; + }}; }