pub mod compiled;
pub mod compiled_logic_store;
pub mod config;
pub mod evaluator;
use serde_json::Value;
pub use compiled::{CompiledLogic, CompiledLogicStore, LogicId};
pub use compiled_logic_store::{CompiledLogicId, CompiledLogicStoreStats};
pub use config::RLogicConfig;
pub use evaluator::{Evaluator, TableScopeGuard};
pub struct RLogic {
store: CompiledLogicStore,
evaluator: Evaluator,
}
impl RLogic {
pub fn new() -> Self {
Self::with_config(RLogicConfig::default())
}
pub fn with_config(config: RLogicConfig) -> Self {
Self {
store: CompiledLogicStore::new(),
evaluator: Evaluator::new().with_config(config),
}
}
pub fn set_static_arrays(
&mut self,
static_arrays: std::sync::Arc<indexmap::IndexMap<String, std::sync::Arc<Value>>>,
) {
self.evaluator.set_static_arrays(static_arrays);
}
pub fn compile(&mut self, logic: &Value) -> Result<LogicId, String> {
self.store.compile(logic)
}
pub fn run(&self, logic_id: &LogicId, data: &Value) -> Result<Value, String> {
let logic = self
.store
.get(logic_id)
.ok_or_else(|| "Logic ID not found".to_string())?;
self.evaluator.evaluate(logic, data)
}
pub fn run_with_context(
&self,
logic_id: &LogicId,
user_data: &Value,
internal_context: &Value,
) -> Result<Value, String> {
let logic = self
.store
.get(logic_id)
.ok_or_else(|| "Logic ID not found".to_string())?;
self.evaluator
.evaluate_with_internal_context(logic, user_data, internal_context)
}
pub fn run_precompiled_with_context(
&self,
logic: &CompiledLogic,
user_data: &Value,
internal_context: &Value,
) -> Result<Value, String> {
self.evaluator
.evaluate_with_internal_context(logic, user_data, internal_context)
}
pub fn run_with_config(
&self,
logic_id: &LogicId,
data: &Value,
config: &RLogicConfig,
) -> Result<Value, String> {
let logic = self
.store
.get(logic_id)
.ok_or_else(|| "Logic ID not found".to_string())?;
let evaluator = Evaluator::new().with_config(*config);
evaluator.evaluate(logic, data)
}
pub fn evaluate(&self, logic: &Value, data: &Value) -> Result<Value, String> {
let compiled = CompiledLogic::compile(logic)?;
self.evaluator.evaluate(&compiled, data)
}
pub fn get_referenced_vars(&self, logic_id: &LogicId) -> Option<Vec<String>> {
self.store
.get(logic_id)
.map(|logic| logic.referenced_vars())
}
pub fn has_forward_reference(&self, logic_id: &LogicId) -> bool {
self.store
.get(logic_id)
.map(|logic| logic.has_forward_reference())
.unwrap_or(false)
}
pub fn index_table(&self, name: &str, data: &Value) {
self.evaluator.index_table(name, data);
}
pub fn clear_indices(&self) {
self.evaluator.clear_indices();
}
pub fn enter_table_scope<'a>(&'a self, path: String, rows: &Vec<Value>) -> TableScopeGuard<'a> {
self.evaluator.enter_table_scope(path, rows)
}
pub fn update_table_scope_rows(&self, rows: &Vec<Value>) {
self.evaluator.update_table_scope_rows(rows);
}
pub fn set_table_scope_row(&self, row_idx: Option<usize>) {
self.evaluator.set_table_scope_row(row_idx);
}
}
impl Default for RLogic {
fn default() -> Self {
Self::new()
}
}