doc: update readme and docs

This commit is contained in:
Dmitriy Pleshevskiy 2020-02-08 20:43:33 +03:00
parent 175798acf3
commit 54f8d2bde8
7 changed files with 157 additions and 56 deletions

View file

@ -12,7 +12,7 @@ before_cache:
script: script:
- | - |
rustc --version && rustc --version &&
cargo test cargo test --all-features
matrix: matrix:
allow_failures: allow_failures:
- rust: nightly - rust: nightly

View file

@ -43,14 +43,16 @@ config! {
), ),
APP { APP {
static BASE_URL => "/api", // &'static str by default
ARTICLE { ARTICLE {
PER_PAGE: u32 => 15, static PER_PAGE: u32 => 15,
} }
#[cfg(feature = "companies")] #[cfg(feature = "companies")]
COMPANY { COMPANY {
#[env_name = "INSTITUTIONS_PER_PAGE"] #[env_name = "INSTITUTIONS_PER_PAGE"]
PER_PAGE: u32 => 15, static PER_PAGE: u32 => 15,
} }
} }
@ -117,30 +119,30 @@ cargo test
## Available features ## Available features
* default = ["macro", "primitives"] * **default** - ["macro", "primitives"]
* macro = [] * **macro** - Activates `config!` macros for easy configure web application.
* array = ["serde_json"] * **static** - Add `static` option to `config!` macros (uses optional `lazy_static` package).
* primitives = ["numbers", "bool"] * **array** - Add EnvString impl for vector type (uses optional `serde_json` package).
* numbers = ["int", "uint", "float"] * **primitives** - Group for features: `numbers` and `bool`.
* int = ["i8", "i16", "i32", "i64", "i128", "isize"] * **numbers** - Group for features: `int`, `uint` and `float`.
* uint = ["u8", "u16", "u32", "u64", "u128", "usize"] * **int** - Group for features: `i8`, `i16`, `i32`, `i64`, `i128` and `isize`.
* float = ["f32", "f64"] * **uint** - Group for features: `u8`, `u16`, `u32`, `u64`, `u128` and `usize`.
* i8 = [] * **float** - Group for features: `f32` and `f64`
* i16 = [] * **i8** - impl EnvString for i8 type
* i32 = [] * **i16** - impl EnvString for i16 type
* i64 = [] * **i32** - impl EnvString for i32 type
* i128 = [] * **i64** - impl EnvString for i64 type
* isize = [] * **i128** - impl EnvString for i128 type
* u8 = [] * **isize** - impl EnvString for isize type
* u16 = [] * **u8** - impl EnvString for u8 type
* u32 = [] * **u16** - impl EnvString for u16 type
* u64 = [] * **u32** - impl EnvString for u32 type
* u128 = [] * **u64** - impl EnvString for u64 type
* usize = [] * **u128** - impl EnvString for u128 type
* f32 = [] * **usize** - impl EnvString for usize type
* f64 = [] * **f32** - impl EnvString for f32 type
* bool = [] * **f64** - impl EnvString for f64 type
* **bool** - impl EnvString for bool type
## License ## License

View file

@ -20,7 +20,7 @@ lazy_static = { version = "1.4.0", optional = true }
serde_json = { version = "1.0.44", optional = true } serde_json = { version = "1.0.44", optional = true }
[features] [features]
default = ["macro", "primitives", "static"] default = ["macro", "primitives"]
macro = [] macro = []
static = ["lazy_static"] static = ["lazy_static"]

View file

@ -30,14 +30,16 @@ config! {
), ),
APP { APP {
static BASE_URL => "/api", // &'static str by default
ARTICLE { ARTICLE {
PER_PAGE: u32 => 15, static PER_PAGE: u32 => 15,
} }
#[cfg(feature = "companies")] #[cfg(feature = "companies")]
COMPANY { COMPANY {
#[env_name = "INSTITUTIONS_PER_PAGE"] #[env_name = "INSTITUTIONS_PER_PAGE"]
PER_PAGE: u32 => 15, static PER_PAGE: u32 => 15,
} }
} }
@ -98,29 +100,30 @@ fn main() {
## Available features ## Available features
* default = ["macro", "primitives"] * **default** - ["macro", "primitives"]
* macro = [] * **macro** - Activates `config!` macros for easy configure web application.
* array = ["serde_json"] * **static** - Add `static` option to `config!` macros (uses optional `lazy_static` package).
* primitives = ["numbers", "bool"] * **array** - Add EnvString impl for vector type (uses optional `serde_json` package).
* numbers = ["int", "uint", "float"] * **primitives** - Group for features: `numbers` and `bool`.
* int = ["i8", "i16", "i32", "i64", "i128", "isize"] * **numbers** - Group for features: `int`, `uint` and `float`.
* uint = ["u8", "u16", "u32", "u64", "u128", "usize"] * **int** - Group for features: `i8`, `i16`, `i32`, `i64`, `i128` and `isize`.
* float = ["f32", "f64"] * **uint** - Group for features: `u8`, `u16`, `u32`, `u64`, `u128` and `usize`.
* i8 = [] * **float** - Group for features: `f32` and `f64`
* i16 = [] * **i8** - impl EnvString for i8 type
* i32 = [] * **i16** - impl EnvString for i16 type
* i64 = [] * **i32** - impl EnvString for i32 type
* i128 = [] * **i64** - impl EnvString for i64 type
* isize = [] * **i128** - impl EnvString for i128 type
* u8 = [] * **isize** - impl EnvString for isize type
* u16 = [] * **u8** - impl EnvString for u8 type
* u32 = [] * **u16** - impl EnvString for u16 type
* u64 = [] * **u32** - impl EnvString for u32 type
* u128 = [] * **u64** - impl EnvString for u64 type
* usize = [] * **u128** - impl EnvString for u128 type
* f32 = [] * **usize** - impl EnvString for usize type
* f64 = [] * **f32** - impl EnvString for f32 type
* bool = [] * **f64** - impl EnvString for f64 type
* **bool** - impl EnvString for bool type
## License ## License

View file

@ -76,6 +76,7 @@ impl FromEnvString for bool {
#[cfg(feature = "array")] #[cfg(feature = "array")]
#[derive(Debug)]
pub enum ArrayEnvError { pub enum ArrayEnvError {
InvalidType, InvalidType,
FailedToParse, FailedToParse,

View file

@ -35,14 +35,16 @@
//! ), //! ),
//! //!
//! APP { //! APP {
//! static BASE_URL => "/api", // &'static str by default
//!
//! ARTICLE { //! ARTICLE {
//! PER_PAGE: u32 => 15, //! static PER_PAGE: u32 => 15,
//! } //! }
//! //!
//! #[cfg(feature = "companies")] //! #[cfg(feature = "companies")]
//! COMPANY { //! COMPANY {
//! #[env_name = "INSTITUTIONS_PER_PAGE"] //! #[env_name = "INSTITUTIONS_PER_PAGE"]
//! PER_PAGE: u32 => 15, //! static PER_PAGE: u32 => 15,
//! } //! }
//! } //! }
//! //!
@ -62,6 +64,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(), String::from("postgres://user:pass@localhost:5432/test")); //! 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::APP::ARTICLE::PER_PAGE(), 15);
//! assert_eq!(cfg::FEATURE::NEW_MENU(), true); //! 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. // Rustc lints.
//#![deny(unused_imports)] #![deny(
missing_debug_implementations,
unsafe_code,
unstable_features,
unused_imports,
unused_qualifications,
)]
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////

View file

@ -203,6 +203,49 @@
/// cfg::init(); /// 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: /// 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`" `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] #[macro_export]
@ -346,6 +397,7 @@ macro_rules! __itconfig_parse_variables {
], ],
$($args:tt)* $($args:tt)*
) => { ) => {
#[cfg(feature = "static")]
__itconfig_parse_variables! { __itconfig_parse_variables! {
current_variable = { current_variable = {
unparsed_meta = [$(#$meta)*], unparsed_meta = [$(#$meta)*],
@ -359,6 +411,9 @@ macro_rules! __itconfig_parse_variables {
tokens = [$($rest)*], tokens = [$($rest)*],
$($args)* $($args)*
} }
#[cfg(not(feature = "static"))]
__itconfig_invalid_syntax!(feature "static");
}; };
// Find concatenated variable // Find concatenated variable
@ -394,6 +449,7 @@ macro_rules! __itconfig_parse_variables {
], ],
$($args:tt)* $($args:tt)*
) => { ) => {
#[cfg(feature = "static")]
__itconfig_parse_variables! { __itconfig_parse_variables! {
current_variable = { current_variable = {
unparsed_meta = [$(#$meta)*], unparsed_meta = [$(#$meta)*],
@ -408,6 +464,9 @@ macro_rules! __itconfig_parse_variables {
tokens = [$($rest)*], tokens = [$($rest)*],
$($args)* $($args)*
} }
#[cfg(not(feature = "static"))]
__itconfig_invalid_syntax!(feature "static");
}; };
// Find variable // Find variable