json_eval_rs/jsoneval/
logic.rs

1use super::JSONEval;
2use crate::jsoneval::json_parser;
3use crate::rlogic::{
4    CompiledLogicId, Evaluator, compiled_logic_store
5};
6use crate::utils::clean_float_noise;
7use serde_json::Value;
8
9impl JSONEval {
10    /// Run pre-compiled logic against current data
11    pub fn run_logic(
12        &mut self, 
13        logic_id: CompiledLogicId,
14        data: Option<&Value>,
15        context: Option<&Value>
16    ) -> Result<Value, String> {
17        // Get compiled logic from global store
18        let compiled_logic = compiled_logic_store::get_compiled_logic(logic_id)
19            .ok_or_else(|| format!("Compiled logic ID {:?} not found in store", logic_id))?;
20
21        // If custom data/context provided, update eval_data
22        let run_data = if let Some(input_data) = data {
23             let context_value = context.unwrap_or(&self.context);
24             self.eval_data.replace_data_and_context(input_data.clone(), context_value.clone());
25             self.eval_data.data()
26        } else {
27             self.eval_data.data()
28        };
29
30        // Create an evaluator and run the pre-compiled logic with zero-clone pattern
31        let evaluator = Evaluator::new();
32        let result = evaluator.evaluate(&compiled_logic, run_data)
33             .map_err(|e| format!("Execution error: {}", e))?;
34            
35        Ok(clean_float_noise(result))
36    }
37
38    /// Compile a logic expression from a JSON string and store it globally
39    pub fn compile_logic(&self, logic_str: &str) -> Result<CompiledLogicId, String> {
40        compiled_logic_store::compile_logic(logic_str)
41    }
42
43    /// Compile a logic expression from a Value and store it globally
44    pub fn compile_logic_value(&self, logic: &Value) -> Result<CompiledLogicId, String> {
45        compiled_logic_store::compile_logic_value(logic)
46    }
47
48    /// Compile and run logic in one go (convenience method)
49    pub fn compile_and_run_logic(
50        &mut self, 
51        logic_str: &str, 
52        data: Option<&str>, 
53        context: Option<&str>
54    ) -> Result<Value, String> {
55        let id = self.compile_logic(logic_str)?;
56        
57        // Parse data and context if provided
58        let data_value = if let Some(d) = data {
59            Some(json_parser::parse_json_str(d)?)
60        } else {
61            None
62        };
63        
64        let context_value = if let Some(c) = context {
65            Some(json_parser::parse_json_str(c)?)
66        } else {
67            None
68        };
69        
70        self.run_logic(id, data_value.as_ref(), context_value.as_ref())
71    }
72}