example/calc: add help message
This commit is contained in:
parent
04b777e214
commit
7b1746ef83
2 changed files with 39 additions and 3 deletions
15
examples/README.md
Normal file
15
examples/README.md
Normal 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`.
|
|
@ -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(|m| m.iter().product::<f32>())
|
.map(|p| {
|
||||||
.sum();
|
p.iter()
|
||||||
|
.map(|m| m.iter().product::<f32>())
|
||||||
|
.reduce(|acc, v| acc - v)
|
||||||
|
.unwrap_or_default()
|
||||||
|
})
|
||||||
|
.sum::<f32>();
|
||||||
|
|
||||||
println!("result: {}", res);
|
println!("result: {}", res);
|
||||||
|
|
||||||
|
|
Reference in a new issue