Compare commits

...

2 Commits

4 changed files with 72 additions and 10 deletions

View File

@ -21,13 +21,12 @@ readme = "../README.md"
[features]
default = []
macro = ["enve_mod"]
number = []
bool = []
vec = []
macro = ["enve_mod"]
[dependencies]
enve_mod = { version = "1.1", path = "./enve_mod", optional = true }
@ -42,3 +41,7 @@ maintenance = { status = "actively-developed" }
[package.metadata.docs.rs]
all-features = true
[[example]]
name = "calc"
required-features = ["number", "vec"]

15
examples/calc.rs Normal file
View File

@ -0,0 +1,15 @@
use enve::core::SepVec;
type PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>;
fn main() -> Result<(), enve::Error> {
let res: f32 = enve::get::<PlusVec<MulVec<f32>>>("E")?
.iter()
.map(|m| m.iter().product::<f32>())
.sum();
println!("result: {}", res);
Ok(())
}

View File

@ -51,6 +51,7 @@ where
fn try_from(value: EString) -> Result<Self, Self::Error> {
let inner = value
.split(SEP)
.map(|p| p.trim())
.map(EString::from)
.map(T::try_from)
.collect::<Result<Vec<_>, _>>()?;

View File

@ -91,9 +91,19 @@ mod tests {
use super::*;
#[test]
fn should_throw_parse_error() {
fn should_return_parsed_num() {
let en = TestCase::<4>.to_string();
env::set_var(&en, "-10");
match get::<i32>(&en) {
Ok(res) => assert_eq!(res, -10),
_ => unreachable!(),
};
}
#[test]
fn should_throw_parse_error() {
let en = TestCase::<5>.to_string();
env::set_var(&en, "-10");
match get::<u32>(&en) {
Err(Error::Parse(orig)) => {
assert_eq!(orig, String::from("-10"))
@ -104,7 +114,7 @@ mod tests {
#[test]
fn should_set_default_num_if_var_is_no_present() {
let en = TestCase::<5>.to_string();
let en = TestCase::<6>.to_string();
let orig = 10;
match get_or_set_default(&en, orig) {
Ok(res) => {
@ -122,7 +132,7 @@ mod tests {
#[test]
fn should_parse_bool_variable() {
let en = TestCase::<5>.to_string();
let en = TestCase::<7>.to_string();
[
("y", true),
@ -150,11 +160,11 @@ mod tests {
#[cfg(feature = "vec")]
mod vector {
use super::*;
use crate::core::vec::{CommaVec, SepVec};
use crate::core::vec::{CommaVec, SemiVec, SepVec};
#[test]
fn should_return_var_as_vector() {
let en = TestCase::<6>.to_string();
let en = TestCase::<8>.to_string();
env::set_var(&en, "1,2,3,4,5");
match get::<CommaVec<i32>>(&en) {
@ -163,9 +173,42 @@ mod tests {
};
}
#[test]
fn should_trim_identations_before_parsing() {
let en = TestCase::<9>.to_string();
let input = "
1 , 2, 3,
4,5";
env::set_var(&en, input);
match get::<CommaVec<i32>>(&en) {
Ok(res) => assert_eq!(*res, vec![1, 2, 3, 4, 5]),
_ => unreachable!(),
};
}
#[test]
fn should_return_vector_of_vectors() {
let en = TestCase::<10>.to_string();
env::set_var(&en, "1,2; 3,4,5; 6,7");
match get::<SemiVec<CommaVec<i32>>>(&en) {
Ok(res) => assert_eq!(
res,
SemiVec::from(vec![
CommaVec::from(vec![1, 2]),
CommaVec::from(vec![3, 4, 5]),
CommaVec::from(vec![6, 7])
])
),
_ => unreachable!(),
};
}
#[test]
fn should_throw_parse_vec_error() {
let en = TestCase::<7>.to_string();
let en = TestCase::<11>.to_string();
env::set_var(&en, "1,2,3,4,5");
match get::<SepVec<i32, '+'>>(&en) {
Err(Error::Parse(orig)) => {
@ -177,7 +220,7 @@ mod tests {
#[test]
fn should_set_default_vector_if_var_is_no_present() {
let en = TestCase::<8>.to_string();
let en = TestCase::<12>.to_string();
let orig = CommaVec::from(vec![1, 2, 3, 4]);
match get_or_set_default(&en, orig.clone()) {
Ok(res) => {