This repository has been archived on 2022-07-24. You can view files and clone it, but cannot push or open issues or pull requests.
itconfig/examples/calc.rs

37 lines
907 B
Rust
Raw Permalink Normal View History

2022-07-23 14:05:53 +03:00
use enve::estr::SepVec;
2022-07-22 18:39:29 +03:00
2022-07-22 19:15:38 +03:00
type MinusVec<T> = SepVec<T, '-'>;
2022-07-22 18:39:29 +03:00
type PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>;
2022-07-22 19:15:38 +03:00
const HELP_MESSAGE: &str = "
USAGE:
E=10+10*2+4 cargo run --example calc --all-features
";
2022-07-22 18:39:29 +03:00
fn main() -> Result<(), enve::Error> {
2022-07-22 19:15:38 +03:00
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()
2022-07-22 18:39:29 +03:00
.iter()
2022-07-22 19:15:38 +03:00
.map(|p| {
p.iter()
.map(|m| m.iter().product::<f32>())
.reduce(|acc, v| acc - v)
.unwrap_or_default()
})
.sum::<f32>();
2022-07-22 18:39:29 +03:00
println!("result: {}", res);
Ok(())
}