doc: add documentation for concat vars

This commit is contained in:
Dmitriy Pleshevskiy 2020-01-07 16:14:45 +03:00
parent 5993375bff
commit 2d9b2bf0a1
3 changed files with 154 additions and 22 deletions

View file

@ -1,10 +1,10 @@
[package] [package]
name = "itconfig" name = "itconfig"
version = "0.6.0" version = "0.7.0"
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"]
keywords = ["config", "env", "macro", "configuration", "environment"] keywords = ["config", "env", "macro", "configuration", "environment", "namespace", "concat", "concatinated"]
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"
repository = "https://github.com/icetemple/itconfig-rs" repository = "https://github.com/icetemple/itconfig-rs"

View file

@ -12,7 +12,18 @@
//! //!
//! config! { //! config! {
//! DEBUG: bool => true, //! DEBUG: bool => true,
//! HOST: String => "127.0.0.1".to_string(), //! HOST: String => "127.0.0.1",
//!
//! DATABASE_URL < (
//! "postgres://",
//! POSTGRES_USERNAME => "user",
//! ":",
//! POSTGRES_PASSWORD => "pass",
//! "@",
//! POSTGRES_HOST => "localhost:5432",
//! "/",
//! POSTGRES_DB => "test",
//! ),
//! //!
//! NAMESPACE { //! NAMESPACE {
//! #[env_name = "MY_CUSTOM_NAME"] //! #[env_name = "MY_CUSTOM_NAME"]
@ -28,6 +39,7 @@
//! //!
//! cfg::init(); //! cfg::init();
//! assert_eq!(cfg::HOST(), String::from("127.0.0.1")); //! assert_eq!(cfg::HOST(), String::from("127.0.0.1"));
//! assert_eq!(cfg::DATABASE_URL(), "postgres://user:pass@localhost:5432/test");
//! assert_eq!(cfg::NAMESPACE::FOO(), true); //! assert_eq!(cfg::NAMESPACE::FOO(), true);
//! } //! }
//! ``` //! ```
@ -83,6 +95,8 @@ impl From<EnvValue> for String {
/// All variables are required and program will panic if some variables haven't value, but you /// All variables are required and program will panic if some variables haven't value, but you
/// can add default value for specific variable. /// can add default value for specific variable.
/// ///
/// Starts with v0.6.0 if you don't have an optional variable, the variable is set automatically.
///
/// Example usage /// Example usage
/// ------------- /// -------------
/// ///
@ -104,7 +118,7 @@ impl From<EnvValue> for String {
/// # env::set_var("DATABASE_URL", "postgres://u:p@localhost:5432/db"); /// # env::set_var("DATABASE_URL", "postgres://u:p@localhost:5432/db");
/// config! { /// config! {
/// DATABASE_URL: String, /// DATABASE_URL: String,
/// HOST: String => "127.0.0.1".to_string(), /// HOST: String => "127.0.0.1",
/// } /// }
/// # cfg::init() /// # cfg::init()
/// ``` /// ```
@ -127,6 +141,9 @@ impl From<EnvValue> for String {
/// assert_eq!(configuration::DEBUG(), true); /// assert_eq!(configuration::DEBUG(), true);
/// ``` /// ```
/// ///
/// Namespaces
/// ----------
///
/// You can use namespaces for env variables /// You can use namespaces for env variables
/// ///
/// ```rust /// ```rust
@ -148,6 +165,9 @@ impl From<EnvValue> for String {
/// # cfg::init() /// # cfg::init()
/// ``` /// ```
/// ///
/// Meta
/// ----
///
/// If you want to read custom env name for variable you can change it manually. /// If you want to read custom env name for variable you can change it manually.
/// ///
/// **A variable in the nameespace will lose environment prefix** /// **A variable in the nameespace will lose environment prefix**
@ -186,6 +206,57 @@ impl From<EnvValue> for String {
/// # fn main() {} /// # fn main() {}
/// ``` /// ```
/// ///
/// Concatenate
/// -----------
///
/// Try to concatenate env variable or strings or both to you env variable. It's easy!
///
/// ```rust
/// # #[macro_use] extern crate itconfig;
/// # use std::env;
/// env::set_var("POSTGRES_USERNAME", "user");
/// env::set_var("POSTGRES_PASSWORD", "pass");
///
/// config! {
/// DATABASE_URL < (
/// "postgres://",
/// POSTGRES_USERNAME,
/// ":",
/// POSTGRES_PASSWORD,
/// "@",
/// POSTGRES_HOST => "localhost:5432",
/// "/",
/// POSTGRES_DB => "test",
/// ),
/// }
///
/// cfg::init();
/// assert_eq!(cfg::DATABASE_URL(), "postgres://user:pass@localhost:5432/test".to_string())
/// ```
///
/// Concatinated variables can be only strings and support all features like namespaces and meta.
///
/// ```rust
/// # #[macro_use] extern crate itconfig;
/// config! {
/// CONCATED_NAMESPACE {
/// #[env_name = "DATABASE_URL"]
/// CONCAT_ENVVAR < (
/// "postgres://",
/// NOT_DEFINED_PG_USERNAME => "user",
/// ":",
/// NOT_DEFINED_PG_PASSWORD => "pass",
/// "@",
/// NOT_DEFINED_PG_HOST => "localhost:5432",
/// "/",
/// NOT_DEFINED_PG_DB => "test",
/// ),
/// }
/// }
///
/// cfg::init();
/// ```
///
/// --- /// ---
/// ///
/// This module will also contain helper method: /// This module will also contain helper method:
@ -211,7 +282,7 @@ impl From<EnvValue> for String {
/// ///
/// config! { /// config! {
/// DEBUG: bool => true, /// DEBUG: bool => true,
/// HOST: String => "127.0.0.1".to_string(), /// HOST: String => "127.0.0.1",
/// } /// }
/// ///
/// fn main () { /// fn main () {
@ -565,14 +636,17 @@ macro_rules! __itconfig_impl {
#[macro_export] #[macro_export]
#[doc(hidden)] #[doc(hidden)]
macro_rules! __itconfig_concat_param { macro_rules! __itconfig_concat_param {
// Find env parameter with default value
($env_name:ident => $default:expr) => { ($env_name:ident => $default:expr) => {
__itconfig_env_or!(stringify!($env_name).to_uppercase(), $default, default) __itconfig_variable_helper!(stringify!($env_name).to_uppercase(), $default, default)
}; };
// Find env parameter without default value
($env_name:ident) => { ($env_name:ident) => {
env_or!(stringify!($env_name).to_uppercase()) env_or!(stringify!($env_name).to_uppercase())
}; };
// Find string parameter
($str:expr) => ( $str.to_string() ); ($str:expr) => ( $str.to_string() );
// Invalid syntax // Invalid syntax
@ -605,6 +679,7 @@ macro_rules! __itconfig_variable {
} }
}; };
// Add method for concatenated variable
( (
meta = [$(#$meta:tt,)*], meta = [$(#$meta:tt,)*],
concat = [$($concat:expr,)+], concat = [$($concat:expr,)+],
@ -618,11 +693,11 @@ macro_rules! __itconfig_variable {
pub fn $name() -> $ty { pub fn $name() -> $ty {
let value_parts: Vec<String> = vec!($($concat),+); let value_parts: Vec<String> = vec!($($concat),+);
let value = value_parts.join(""); let value = value_parts.join("");
__itconfig_env_or!(@setenv $env_name, value) __itconfig_variable_helper!(@setenv $env_name, value)
} }
}; };
// Add method // Add method for env variable
( (
meta = [$(#$meta:tt,)*], meta = [$(#$meta:tt,)*],
concat = [], concat = [],
@ -663,12 +738,12 @@ macro_rules! __itconfig_variable {
macro_rules! env_or { macro_rules! env_or {
// Env without default value // Env without default value
($env_name:expr) => { ($env_name:expr) => {
__itconfig_env_or!($env_name, format!(r#"Cannot read "{}" environment variable"#, $env_name), panic); __itconfig_variable_helper!($env_name, format!(r#"Cannot read "{}" environment variable"#, $env_name), panic);
}; };
// Env with default value // Env with default value
($env_name:expr, $default:expr) => { ($env_name:expr, $default:expr) => {
__itconfig_env_or!($env_name, $default, setenv); __itconfig_variable_helper!($env_name, $default, setenv);
}; };
// Invalid syntax // Invalid syntax
@ -691,24 +766,29 @@ macro_rules! __itconfig_env_or_invalid_syntax {
#[macro_export] #[macro_export]
macro_rules! __itconfig_env_or { #[doc(hidden)]
macro_rules! __itconfig_variable_helper {
// Get env variable
($env_name:expr, $default:expr, $token:tt) => {{ ($env_name:expr, $default:expr, $token:tt) => {{
use std::env; use std::env;
use itconfig::EnvValue; use itconfig::EnvValue;
env::var($env_name) env::var($env_name)
.map(|val| EnvValue::from(val).into()) .map(|val| __itconfig_variable_helper!(val))
.unwrap_or_else(|_| __itconfig_env_or!(@$token $env_name, $default)) .unwrap_or_else(|_| __itconfig_variable_helper!(@$token $env_name, $default))
}}; }};
(@default $env_name:expr, $default:expr) => {{ // Returns converted env variable
$default ($(@default $env_name:expr,)? $default:expr) => {{
EnvValue::from($default.to_string()).into()
}}; }};
// Set default value for env variable and returns default
(@setenv $env_name:expr, $default:expr) => {{ (@setenv $env_name:expr, $default:expr) => {{
env::set_var($env_name, $default.to_string()); env::set_var($env_name, $default.to_string());
$default __itconfig_variable_helper!($default)
}}; }};
// Make panic for env variable
(@panic $env_name:expr, $default:expr) => { (@panic $env_name:expr, $default:expr) => {
panic!($default); panic!($default);
}; };

View file

@ -1,4 +1,5 @@
use std::env; use std::env;
use std::env::VarError;
#[macro_use] #[macro_use]
extern crate itconfig; extern crate itconfig;
@ -55,13 +56,15 @@ fn different_types_with_default_value() {
config! { config! {
NUMBER: i32 => 30, NUMBER: i32 => 30,
BOOL: bool => true, BOOL: bool => true,
STR: String => "str",
STRING: String => "string".to_string(), STRING: String => "string".to_string(),
} }
cfg::init(); cfg::init();
assert_eq!(cfg::NUMBER(), 30); assert_eq!(cfg::NUMBER(), 30);
assert_eq!(cfg::BOOL(), true); assert_eq!(cfg::BOOL(), true);
assert_eq!(cfg::STRING(), "string"); assert_eq!(cfg::STR(), "str".to_string());
assert_eq!(cfg::STRING(), "string".to_string());
} }
#[test] #[test]
@ -240,7 +243,7 @@ fn stranger_meta_data() {
#[test] #[test]
fn setting_default_env_variable() { fn setting_default_env_variable() {
config! { config! {
DEFAULT_ENV_STRING: String => "localhost".to_string(), DEFAULT_ENV_STRING: String => "localhost",
DEFAULT_ENV_BOOLEAN: bool => true, DEFAULT_ENV_BOOLEAN: bool => true,
DEFAULT_ENV_UINT: u32 => 40, DEFAULT_ENV_UINT: u32 => 40,
DEFAULT_ENV_FLOAT: f64 => 40.9, DEFAULT_ENV_FLOAT: f64 => 40.9,
@ -321,13 +324,13 @@ fn default_value_for_concatenate_env_parameter() {
config! { config! {
CONCATENATED_DATABASE_URL < ( CONCATENATED_DATABASE_URL < (
"postgres://", "postgres://",
NOT_DEFINED_PG_USERNAME => "user".to_string(), NOT_DEFINED_PG_USERNAME => "user",
":", ":",
NOT_DEFINED_PG_PASSWORD => "pass".to_string(), NOT_DEFINED_PG_PASSWORD => "pass",
"@", "@",
NOT_DEFINED_PG_HOST => "localhost:5432".to_string(), NOT_DEFINED_PG_HOST => "localhost:5432",
"/", "/",
NOT_DEFINED_PG_DB => "test".to_string(), NOT_DEFINED_PG_DB => "test",
), ),
} }
@ -337,3 +340,52 @@ fn default_value_for_concatenate_env_parameter() {
Ok("postgres://user:pass@localhost:5432/test".to_string()) Ok("postgres://user:pass@localhost:5432/test".to_string())
); );
} }
#[test]
fn envname_meta_for_concatenated_env_variable() {
config! {
#[env_name = "CUSTOM_CONCAT_ENVNAME"]
CONCAT_ENVVAR < (
"postgres://",
NOT_DEFINED_PG_USERNAME => "user",
":",
NOT_DEFINED_PG_PASSWORD => "pass",
"@",
NOT_DEFINED_PG_HOST => "localhost:5432",
"/",
NOT_DEFINED_PG_DB => "test",
),
}
cfg::init();
assert_eq!(
env::var("CUSTOM_CONCAT_ENVNAME"),
Ok("postgres://user:pass@localhost:5432/test".to_string())
);
assert_eq!(env::var("CONCAT_ENVVAR"), Err(VarError::NotPresent));
}
#[test]
fn concatenated_environment_variable_in_namespace() {
config! {
CONCATED_NAMESPACE {
CONCAT_ENVVAR < (
"postgres://",
NOT_DEFINED_PG_USERNAME => "user",
":",
NOT_DEFINED_PG_PASSWORD => "pass",
"@",
NOT_DEFINED_PG_HOST => "localhost:5432",
"/",
NOT_DEFINED_PG_DB => "test",
),
}
}
cfg::init();
assert_eq!(
env::var("CONCATED_NAMESPACE_CONCAT_ENVVAR"),
Ok("postgres://user:pass@localhost:5432/test".to_string())
);
assert_eq!(env::var("CONCAT_ENVVAR"), Err(VarError::NotPresent));
}