example/calc: add help message

This commit is contained in:
Dmitriy Pleshevskiy 2022-07-22 19:15:38 +03:00
parent 04b777e214
commit 7b1746ef83
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
2 changed files with 39 additions and 3 deletions

15
examples/README.md Normal file
View file

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

View file

@ -1,13 +1,34 @@
use enve::core::SepVec; use enve::core::SepVec;
type MinusVec<T> = SepVec<T, '-'>;
type PlusVec<T> = SepVec<T, '+'>; type PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>; type MulVec<T> = SepVec<T, '*'>;
const HELP_MESSAGE: &str = "
USAGE:
E=10+10*2+4 cargo run --example calc --all-features
";
fn main() -> Result<(), enve::Error> { fn main() -> Result<(), enve::Error> {
let res: f32 = enve::get::<PlusVec<MulVec<f32>>>("E")? let res: f32 = enve::get::<PlusVec<MinusVec<MulVec<f32>>>>("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() .iter()
.map(|p| {
p.iter()
.map(|m| m.iter().product::<f32>()) .map(|m| m.iter().product::<f32>())
.sum(); .reduce(|acc, v| acc - v)
.unwrap_or_default()
})
.sum::<f32>();
println!("result: {}", res); println!("result: {}", res);