diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..d917ddd --- /dev/null +++ b/examples/README.md @@ -0,0 +1,15 @@ +# Examples + +## Calculator + +Fun calculator based on environment variables + +``` +E=2*2-1-1+5*3-10 cargo run --example calc --all-features +``` + +Limits: + +- Supports `*`, `+`, `-` +- You cannot start from a negative number. `E=-10`. Solution: start from `0`. + `E=0-10`. diff --git a/examples/calc.rs b/examples/calc.rs index 858d835..3460667 100644 --- a/examples/calc.rs +++ b/examples/calc.rs @@ -1,13 +1,34 @@ use enve::core::SepVec; +type MinusVec = SepVec; type PlusVec = SepVec; type MulVec = SepVec; +const HELP_MESSAGE: &str = " +USAGE: +E=10+10*2+4 cargo run --example calc --all-features +"; + fn main() -> Result<(), enve::Error> { - let res: f32 = enve::get::>>("E")? + let res: f32 = enve::get::>>>("E") + .map_err(|err| { + match err { + enve::Error::NotPresent => eprintln!("The expression was not found"), + rest => eprintln!("ERROR: {}", rest), + } + + eprintln!("{}", HELP_MESSAGE); + std::process::exit(0); + }) + .unwrap() .iter() - .map(|m| m.iter().product::()) - .sum(); + .map(|p| { + p.iter() + .map(|m| m.iter().product::()) + .reduce(|acc, v| acc - v) + .unwrap_or_default() + }) + .sum::(); println!("result: {}", res);