From c4f78bc51f0bdc9c35025d0f284a931fd3e2793c Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 21 Jan 2020 11:12:12 +0300 Subject: [PATCH] feat(array): add new feature for impl vec --- README.md | 29 ++++++++++++++++++++++++++++- examples/diesel/Cargo.toml | 2 +- itconfig/Cargo.toml | 17 +++++++++++------ itconfig/README.md | 28 +++++++++++++++++++++++++++- itconfig/src/envstr.rs | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index df5cfbc..50fd7df 100644 --- a/README.md +++ b/README.md @@ -109,12 +109,39 @@ cargo test * [x] Concat env variables to one variable * [x] Add nested namespaces * [x] Support meta for namespaces -* [ ] Support array type +* [x] Support array type * [ ] Support hashmap type * [ ] Support custom env type * [ ] Common configuration for namespace variables +## 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 = [] + + ## License [MIT] © [Ice Temple](https://github.com/icetemple) diff --git a/examples/diesel/Cargo.toml b/examples/diesel/Cargo.toml index 4e115d8..6964233 100644 --- a/examples/diesel/Cargo.toml +++ b/examples/diesel/Cargo.toml @@ -7,6 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -itconfig = { path = "../../itconfig" } +itconfig = { path = "../../itconfig", default-features = false, features = ["macro"] } dotenv = "0.15.0" diesel = { version = "1.4.3", features = ["postgres"] } \ No newline at end of file diff --git a/itconfig/Cargo.toml b/itconfig/Cargo.toml index 49130e6..fbd46e5 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "itconfig" -version = "0.10.1" +version = "0.10.2" authors = ["Dmitriy Pleshevskiy "] description = "Easy build a configs from environment variables and use it in globally." categories = ["config", "web-programming"] @@ -14,17 +14,17 @@ readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[badges] -travis-ci = { repository = "icetemple/itconfig-rs" } -maintenance = { status = "actively-developed" } - [dependencies] failure = { version = "0.1.6", features = ["derive"]} +serde_json = { version = "1.0.44", optional = true } [features] -default = ["macro", "numbers", "bool"] +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"] @@ -48,3 +48,8 @@ f32 = [] f64 = [] bool = [] + + +[badges] +travis-ci = { repository = "icetemple/itconfig-rs" } +maintenance = { status = "actively-developed" } diff --git a/itconfig/README.md b/itconfig/README.md index 7bd6029..e751c66 100644 --- a/itconfig/README.md +++ b/itconfig/README.md @@ -90,12 +90,38 @@ fn main() { * [x] Concat env variables to one variable * [x] Add nested namespaces * [x] Support meta for namespaces -* [ ] Support array type +* [x] Support array type * [ ] Support hashmap type * [ ] Support custom env type * [ ] Common configuration for namespace variables +## 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 = [] + ## License diff --git a/itconfig/src/envstr.rs b/itconfig/src/envstr.rs index 05896a0..4fa7016 100644 --- a/itconfig/src/envstr.rs +++ b/itconfig/src/envstr.rs @@ -74,6 +74,42 @@ impl FromEnvString for bool { } + +#[cfg(feature = "array")] +pub enum ArrayEnvError { + InvalidType, + FailedToParse, +} + + +#[cfg(feature = "array")] +impl FromEnvString for Vec + where T: FromEnvString +{ + type Err = ArrayEnvError; + + fn from_env_string(s: &EnvString) -> Result { + serde_json::from_str::>(s.trim()) + .map(|vec| { + vec.iter().map(|v| v.to_string()).collect::>() + }) + .or_else(|_| { + serde_json::from_str::>(s.trim()) + }) + .map_err(|_| ArrayEnvError::InvalidType) + .and_then(|vec| { + vec.iter() + .map(|v| { + v.to_env_string() + .parse::() + .map_err(|_| ArrayEnvError::FailedToParse) + }) + .collect::, _>>() + }) + } +} + + impl FromEnvString for String { type Err = ();