use crate::engine::error::Result;
use crate::engine::functions::{MapConfig, ValidationConfig};
use crate::engine::message::{Change, Message};
use datalogic_rs::{CompiledLogic, DataLogic};
use log::error;
use serde_json::Value;
use std::sync::Arc;
pub struct InternalExecutor {
datalogic: Arc<DataLogic>,
logic_cache: Vec<Arc<CompiledLogic>>,
}
impl InternalExecutor {
pub fn new(datalogic: Arc<DataLogic>, logic_cache: Vec<Arc<CompiledLogic>>) -> Self {
Self {
datalogic,
logic_cache,
}
}
pub fn datalogic(&self) -> &Arc<DataLogic> {
&self.datalogic
}
pub fn logic_cache(&self) -> &Vec<Arc<CompiledLogic>> {
&self.logic_cache
}
pub fn execute_map(
&self,
message: &mut Message,
config: &MapConfig,
) -> Result<(usize, Vec<Change>)> {
config.execute(message, &self.datalogic, &self.logic_cache)
}
pub fn execute_validation(
&self,
message: &mut Message,
config: &ValidationConfig,
) -> Result<(usize, Vec<Change>)> {
config.execute(message, &self.datalogic, &self.logic_cache)
}
pub fn evaluate_condition(
&self,
condition_index: Option<usize>,
context: Arc<Value>,
) -> Result<bool> {
match condition_index {
Some(index) if index < self.logic_cache.len() => {
let compiled_logic = &self.logic_cache[index];
let result = self.datalogic.evaluate(compiled_logic, context);
match result {
Ok(value) => {
Ok(value == Value::Bool(true))
}
Err(e) => {
error!("Failed to evaluate condition: {:?}", e);
Ok(false)
}
}
}
_ => Ok(true), }
}
}