#![recursion_limit="100"]
#![deny(missing_docs)]
#![cfg_attr(all(feature = "unstable", test), feature(test))]
mod math;
mod function;
mod operator;
mod node;
mod tree;
mod error;
mod builtin;
mod expr;
pub use expr::ExecOptions;
pub use serde_json::Value;
pub use error::Error;
pub use function::Function;
pub use expr::Expr;
use std::collections::HashMap;
use serde_json::to_value as json_to_value;
use serde::Serialize;
pub fn to_value<S: Serialize>(v: S) -> Value {
json_to_value(v).unwrap()
}
pub type Context = HashMap<String, Value>;
pub type Contexts = Vec<Context>;
pub type Functions = HashMap<String, Function>;
pub fn eval(expr: &str) -> Result<Value, Error> {
Expr::new(expr).compile()?.exec()
}
type Compiled = Box<dyn Fn(&[Context], &Functions) -> Result<Value, Error>>;
#[cfg(all(feature = "unstable", test))]
mod benches {
extern crate test;
use crate::eval;
use crate::tree::Tree;
use crate::Expr;
#[bench]
fn bench_deep_brackets(b: &mut test::Bencher) {
b.iter(|| eval("(2 + (3 + 4) + (6 + (6 + 7)) + 5)"));
}
#[bench]
fn bench_parse_pos(b: &mut test::Bencher) {
let mut tree = Tree {
raw: "(2 + (3 + 4) + (6 + (6 + 7)) + 5)".to_owned(),
..Default::default()
};
b.iter(|| tree.parse_pos().unwrap());
}
#[bench]
fn bench_parse_operators(b: &mut test::Bencher) {
let mut tree = Tree {
raw: "(2 + (3 + 4) + (6 + (6 + 7)) + 5)".to_owned(),
..Default::default()
};
tree.parse_pos().unwrap();
b.iter(|| tree.parse_operators().unwrap());
}
#[bench]
fn bench_parse_nodes(b: &mut test::Bencher) {
let mut tree = Tree {
raw: "(2 + (3 + 4) + (6 + (6 + 7)) + 5)".to_owned(),
..Default::default()
};
tree.parse_pos().unwrap();
tree.parse_operators().unwrap();
b.iter(|| tree.parse_node().unwrap());
}
#[bench]
fn bench_compile(b: &mut test::Bencher) {
b.iter(|| {
let mut tree = Tree {
raw: "(2 + (3 + 4) + (6 + (6 + 7)) + 5)".to_owned(),
..Default::default()
};
tree.parse_pos().unwrap();
tree.parse_operators().unwrap();
tree.parse_node().unwrap();
tree.compile().unwrap();
});
}
#[bench]
fn bench_exec(b: &mut test::Bencher) {
let expr = Expr::new("(2 + (3 + 4) + (6 + (6 + 7)) + 5)")
.compile()
.unwrap();
b.iter(|| expr.exec().unwrap())
}
#[bench]
fn bench_eval(b: &mut test::Bencher) {
b.iter(|| eval("(2 + (3 + 4) + (6 + (6 + 7)) + 5)"));
}
}