use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
pub type CustomOp =
dyn Fn(&[Value]) -> Result<Value, Box<dyn std::error::Error + Send + Sync>>
+ Send
+ Sync;
pub struct EvalCtx {
pub fuel: usize,
pub ops: HashMap<String, Arc<CustomOp>>,
}
impl EvalCtx {
pub fn new() -> Self {
Self {
fuel: 200,
ops: HashMap::new(),
}
}
pub fn with_fuel(mut self, fuel: usize) -> Self {
self.fuel = fuel;
self
}
pub fn with_op<F>(mut self, name: &str, f: F) -> Self
where
F: Fn(&[Value]) -> Result<Value, Box<dyn std::error::Error + Send + Sync>>
+ Send
+ Sync
+ 'static,
{
self.ops.insert(name.to_string(), Arc::new(f));
self
}
}
impl Default for EvalCtx {
fn default() -> Self {
Self::new()
}
}