Merge pull request #10 from icetemple/array-type

feat(array): add new feature for impl vec
This commit is contained in:
Dmitriy Pleshevskiy 2020-01-21 19:38:22 +03:00 committed by GitHub
commit 6f5e63ba3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 9 deletions

View file

@ -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)

View file

@ -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"] }

View file

@ -1,6 +1,6 @@
[package]
name = "itconfig"
version = "0.10.1"
version = "0.10.2"
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
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" }

View file

@ -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

View file

@ -74,6 +74,42 @@ impl FromEnvString for bool {
}
#[cfg(feature = "array")]
pub enum ArrayEnvError {
InvalidType,
FailedToParse,
}
#[cfg(feature = "array")]
impl<T> FromEnvString for Vec<T>
where T: FromEnvString
{
type Err = ArrayEnvError;
fn from_env_string(s: &EnvString) -> Result<Self, Self::Err> {
serde_json::from_str::<Vec<isize>>(s.trim())
.map(|vec| {
vec.iter().map(|v| v.to_string()).collect::<Vec<String>>()
})
.or_else(|_| {
serde_json::from_str::<Vec<String>>(s.trim())
})
.map_err(|_| ArrayEnvError::InvalidType)
.and_then(|vec| {
vec.iter()
.map(|v| {
v.to_env_string()
.parse::<T>()
.map_err(|_| ArrayEnvError::FailedToParse)
})
.collect::<Result<Vec<T>, _>>()
})
}
}
impl FromEnvString for String {
type Err = ();