diff --git a/examples/calc.rs b/examples/calc.rs new file mode 100644 index 0000000..858d835 --- /dev/null +++ b/examples/calc.rs @@ -0,0 +1,15 @@ +use enve::core::SepVec; + +type PlusVec = SepVec; +type MulVec = SepVec; + +fn main() -> Result<(), enve::Error> { + let res: f32 = enve::get::>>("E")? + .iter() + .map(|m| m.iter().product::()) + .sum(); + + println!("result: {}", res); + + Ok(()) +} diff --git a/src/core/vec.rs b/src/core/vec.rs index 0857285..cc2a76a 100644 --- a/src/core/vec.rs +++ b/src/core/vec.rs @@ -51,6 +51,7 @@ where fn try_from(value: EString) -> Result { let inner = value .split(SEP) + .map(|p| p.trim()) .map(EString::from) .map(T::try_from) .collect::, _>>()?; diff --git a/src/utils.rs b/src/utils.rs index 1f06865..b12ea48 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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::(&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::(&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::>(&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::>(&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::>>(&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::>(&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) => {