use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::Arc;
use datumesh_core::{Cell, FunctionDescriptor, Scalar};
use crate::error::EvalError;
use crate::runtime::{Budget, Inputs, Runtime};
pub const LANG: &str = "native_test";
pub type NativeFn = dyn Fn(&Cell, &mut dyn Inputs, &mut Budget) -> Result<Scalar, EvalError> + Send + Sync;
#[derive(Clone, Default)]
pub struct NativeTest {
fns: HashMap<String, Arc<NativeFn>>,
asof_dependent: HashSet<String>,
}
impl NativeTest {
pub fn new() -> NativeTest {
NativeTest::default()
}
pub fn register(
&mut self,
fn_hash: impl Into<String>,
f: impl Fn(&Cell, &mut dyn Inputs, &mut Budget) -> Result<Scalar, EvalError> + Send + Sync + 'static,
) -> &mut Self {
self.fns.insert(fn_hash.into(), Arc::new(f));
self
}
pub fn set_asof_dependent(&mut self, fn_hash: impl Into<String>) -> &mut Self {
self.asof_dependent.insert(fn_hash.into());
self
}
}
impl fmt::Debug for NativeTest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NativeTest").field("functions", &self.fns.len()).finish()
}
}
impl Runtime for NativeTest {
fn lang(&self) -> &str {
LANG
}
fn asof_dependent(&self, descriptor: &FunctionDescriptor) -> bool {
self.asof_dependent.contains(&descriptor.hash())
}
fn evaluate(
&self,
_descriptor: &FunctionDescriptor,
cell: &Cell,
inputs: &mut dyn Inputs,
budget: &mut Budget,
) -> Result<Scalar, EvalError> {
match self.fns.get(&cell.fn_hash) {
Some(f) => {
budget.charge(1)?;
f(cell, inputs, budget)
}
None => Err(EvalError::Runtime(format!("no native function registered for {}", cell.fn_hash))),
}
}
}