diff --git a/.travis.yml b/.travis.yml index 123739b..5e42406 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ before_cache: script: - | rustc --version && - cargo test + cargo test --all-features matrix: allow_failures: - rust: nightly diff --git a/README.md b/README.md index f7d8039..51af513 100644 --- a/README.md +++ b/README.md @@ -43,14 +43,16 @@ config! { ), APP { + static BASE_URL => "/api", // &'static str by default + ARTICLE { - PER_PAGE: u32 => 15, + static PER_PAGE: u32 => 15, } #[cfg(feature = "companies")] COMPANY { #[env_name = "INSTITUTIONS_PER_PAGE"] - PER_PAGE: u32 => 15, + static PER_PAGE: u32 => 15, } } @@ -117,30 +119,30 @@ cargo test ## Available features -* default = ["macro", "primitives"] -* macro = [] -* array = ["serde_json"] -* primitives = ["numbers", "bool"] -* numbers = ["int", "uint", "float"] -* int = ["i8", "i16", "i32", "i64", "i128", "isize"] -* uint = ["u8", "u16", "u32", "u64", "u128", "usize"] -* float = ["f32", "f64"] -* i8 = [] -* i16 = [] -* i32 = [] -* i64 = [] -* i128 = [] -* isize = [] -* u8 = [] -* u16 = [] -* u32 = [] -* u64 = [] -* u128 = [] -* usize = [] -* f32 = [] -* f64 = [] -* bool = [] - +* **default** - ["macro", "primitives"] +* **macro** - Activates `config!` macros for easy configure web application. +* **static** - Add `static` option to `config!` macros (uses optional `lazy_static` package). +* **array** - Add EnvString impl for vector type (uses optional `serde_json` package). +* **primitives** - Group for features: `numbers` and `bool`. +* **numbers** - Group for features: `int`, `uint` and `float`. +* **int** - Group for features: `i8`, `i16`, `i32`, `i64`, `i128` and `isize`. +* **uint** - Group for features: `u8`, `u16`, `u32`, `u64`, `u128` and `usize`. +* **float** - Group for features: `f32` and `f64` +* **i8** - impl EnvString for i8 type +* **i16** - impl EnvString for i16 type +* **i32** - impl EnvString for i32 type +* **i64** - impl EnvString for i64 type +* **i128** - impl EnvString for i128 type +* **isize** - impl EnvString for isize type +* **u8** - impl EnvString for u8 type +* **u16** - impl EnvString for u16 type +* **u32** - impl EnvString for u32 type +* **u64** - impl EnvString for u64 type +* **u128** - impl EnvString for u128 type +* **usize** - impl EnvString for usize type +* **f32** - impl EnvString for f32 type +* **f64** - impl EnvString for f64 type +* **bool** - impl EnvString for bool type ## License diff --git a/itconfig/Cargo.toml b/itconfig/Cargo.toml index 6e92a30..6d39480 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -20,7 +20,7 @@ lazy_static = { version = "1.4.0", optional = true } serde_json = { version = "1.0.44", optional = true } [features] -default = ["macro", "primitives", "static"] +default = ["macro", "primitives"] macro = [] static = ["lazy_static"] diff --git a/itconfig/README.md b/itconfig/README.md index e751c66..6af0f83 100644 --- a/itconfig/README.md +++ b/itconfig/README.md @@ -30,14 +30,16 @@ config! { ), APP { + static BASE_URL => "/api", // &'static str by default + ARTICLE { - PER_PAGE: u32 => 15, + static PER_PAGE: u32 => 15, } #[cfg(feature = "companies")] COMPANY { #[env_name = "INSTITUTIONS_PER_PAGE"] - PER_PAGE: u32 => 15, + static PER_PAGE: u32 => 15, } } @@ -98,29 +100,30 @@ fn main() { ## Available features -* default = ["macro", "primitives"] -* macro = [] -* array = ["serde_json"] -* primitives = ["numbers", "bool"] -* numbers = ["int", "uint", "float"] -* int = ["i8", "i16", "i32", "i64", "i128", "isize"] -* uint = ["u8", "u16", "u32", "u64", "u128", "usize"] -* float = ["f32", "f64"] -* i8 = [] -* i16 = [] -* i32 = [] -* i64 = [] -* i128 = [] -* isize = [] -* u8 = [] -* u16 = [] -* u32 = [] -* u64 = [] -* u128 = [] -* usize = [] -* f32 = [] -* f64 = [] -* bool = [] +* **default** - ["macro", "primitives"] +* **macro** - Activates `config!` macros for easy configure web application. +* **static** - Add `static` option to `config!` macros (uses optional `lazy_static` package). +* **array** - Add EnvString impl for vector type (uses optional `serde_json` package). +* **primitives** - Group for features: `numbers` and `bool`. +* **numbers** - Group for features: `int`, `uint` and `float`. +* **int** - Group for features: `i8`, `i16`, `i32`, `i64`, `i128` and `isize`. +* **uint** - Group for features: `u8`, `u16`, `u32`, `u64`, `u128` and `usize`. +* **float** - Group for features: `f32` and `f64` +* **i8** - impl EnvString for i8 type +* **i16** - impl EnvString for i16 type +* **i32** - impl EnvString for i32 type +* **i64** - impl EnvString for i64 type +* **i128** - impl EnvString for i128 type +* **isize** - impl EnvString for isize type +* **u8** - impl EnvString for u8 type +* **u16** - impl EnvString for u16 type +* **u32** - impl EnvString for u32 type +* **u64** - impl EnvString for u64 type +* **u128** - impl EnvString for u128 type +* **usize** - impl EnvString for usize type +* **f32** - impl EnvString for f32 type +* **f64** - impl EnvString for f64 type +* **bool** - impl EnvString for bool type ## License diff --git a/itconfig/src/envstr.rs b/itconfig/src/envstr.rs index c46a1b2..b20e343 100644 --- a/itconfig/src/envstr.rs +++ b/itconfig/src/envstr.rs @@ -76,6 +76,7 @@ impl FromEnvString for bool { #[cfg(feature = "array")] +#[derive(Debug)] pub enum ArrayEnvError { InvalidType, FailedToParse, diff --git a/itconfig/src/lib.rs b/itconfig/src/lib.rs index 56cfe99..e260566 100644 --- a/itconfig/src/lib.rs +++ b/itconfig/src/lib.rs @@ -35,14 +35,16 @@ //! ), //! //! APP { +//! static BASE_URL => "/api", // &'static str by default +//! //! ARTICLE { -//! PER_PAGE: u32 => 15, +//! static PER_PAGE: u32 => 15, //! } //! //! #[cfg(feature = "companies")] //! COMPANY { //! #[env_name = "INSTITUTIONS_PER_PAGE"] -//! PER_PAGE: u32 => 15, +//! static PER_PAGE: u32 => 15, //! } //! } //! @@ -62,6 +64,7 @@ //! cfg::init(); //! assert_eq!(cfg::HOST(), String::from("127.0.0.1")); //! assert_eq!(cfg::DATABASE_URL(), String::from("postgres://user:pass@localhost:5432/test")); +//! assert_eq!(cfg::APP::BASE_URL(), "/api"); //! assert_eq!(cfg::APP::ARTICLE::PER_PAGE(), 15); //! assert_eq!(cfg::FEATURE::NEW_MENU(), true); //! } @@ -84,10 +87,43 @@ //! } //! ``` //! +//! ## Available features +//! +//! * **default** - ["macro", "primitives"] +//! * **macro** - Activates `config!` macros for easy configure web application. +//! * **static** - Add `static` option to `config!` macros (uses optional `lazy_static` package). +//! * **array** - Add EnvString impl for vector type (uses optional `serde_json` package). +//! * **primitives** - Group for features: `numbers` and `bool`. +//! * **numbers** - Group for features: `int`, `uint` and `float`. +//! * **int** - Group for features: `i8`, `i16`, `i32`, `i64`, `i128` and `isize`. +//! * **uint** - Group for features: `u8`, `u16`, `u32`, `u64`, `u128` and `usize`. +//! * **float** - Group for features: `f32` and `f64` +//! * **i8** - impl EnvString for i8 type +//! * **i16** - impl EnvString for i16 type +//! * **i32** - impl EnvString for i32 type +//! * **i64** - impl EnvString for i64 type +//! * **i128** - impl EnvString for i128 type +//! * **isize** - impl EnvString for isize type +//! * **u8** - impl EnvString for u8 type +//! * **u16** - impl EnvString for u16 type +//! * **u32** - impl EnvString for u32 type +//! * **u64** - impl EnvString for u64 type +//! * **u128** - impl EnvString for u128 type +//! * **usize** - impl EnvString for usize type +//! * **f32** - impl EnvString for f32 type +//! * **f64** - impl EnvString for f64 type +//! * **bool** - impl EnvString for bool type +//!a // Rustc lints. -//#![deny(unused_imports)] +#![deny( + missing_debug_implementations, + unsafe_code, + unstable_features, + unused_imports, + unused_qualifications, +)] ///////////////////////////////////////////////////////////////////////////// diff --git a/itconfig/src/macro.rs b/itconfig/src/macro.rs index 47118f5..a4b7a84 100644 --- a/itconfig/src/macro.rs +++ b/itconfig/src/macro.rs @@ -203,6 +203,49 @@ /// cfg::init(); /// ``` /// +/// Static +/// ------ +/// +/// `with feauter = "static"` +/// +/// Starting with 0.11 version you can use lazy static for improve speed of variable. This is very +/// useful, if you use variable more than once. +/// +/// ```rust +/// # #[macro_use] extern crate itconfig; +/// # use std::env; +/// env::set_var("APP_BASE_URL", "/api/v1"); +/// +/// config! { +/// static APP_BASE_URL => "/api", +/// } +/// +/// cfg::init(); +/// assert_eq!(cfg::APP_BASE_URL(), "/api/v1"); +/// ``` +/// +/// You also can use static with concat variables +/// +/// ```rust +/// # #[macro_use] extern crate itconfig; +/// config! { +/// static CONNECTION_STRING < ( +/// "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!(cfg::CONNECTION_STRING(), "postgres://user:pass@localhost:5432/test".to_string()); +/// ``` +/// +/// /// --- /// /// This module will also contain helper method: @@ -258,6 +301,14 @@ macro_rules! __itconfig_invalid_syntax { `https://docs.rs/itconfig/latest/itconfig/macro.config.html`" ); }; + + (feature "static") => { + compile_error!( + "Feature `static` is required for enable this macro function.\ + Please see the `config!` macro docs for more info.\ + `https://docs.rs/itconfig/latest/itconfig/macro.config.html`" + ); + }; } #[macro_export] @@ -346,6 +397,7 @@ macro_rules! __itconfig_parse_variables { ], $($args:tt)* ) => { + #[cfg(feature = "static")] __itconfig_parse_variables! { current_variable = { unparsed_meta = [$(#$meta)*], @@ -359,6 +411,9 @@ macro_rules! __itconfig_parse_variables { tokens = [$($rest)*], $($args)* } + + #[cfg(not(feature = "static"))] + __itconfig_invalid_syntax!(feature "static"); }; // Find concatenated variable @@ -394,6 +449,7 @@ macro_rules! __itconfig_parse_variables { ], $($args:tt)* ) => { + #[cfg(feature = "static")] __itconfig_parse_variables! { current_variable = { unparsed_meta = [$(#$meta)*], @@ -408,6 +464,9 @@ macro_rules! __itconfig_parse_variables { tokens = [$($rest)*], $($args)* } + + #[cfg(not(feature = "static"))] + __itconfig_invalid_syntax!(feature "static"); }; // Find variable