gridline_engine/engine/
eval.rs1use rhai::Engine;
8use std::sync::Arc;
9
10use super::{AST, Dynamic, Grid, SpillMap};
11
12pub fn create_engine(grid: Arc<Grid>) -> Engine {
14 let spill_map = Arc::new(SpillMap::new());
15 create_engine_with_spill(grid, spill_map)
16}
17
18pub fn create_engine_with_spill(grid: Arc<Grid>, spill_map: Arc<SpillMap>) -> Engine {
20 let mut engine = Engine::new();
21 crate::builtins::register_builtins(&mut engine, grid, spill_map);
22 engine
23}
24
25pub fn create_engine_with_functions(
29 grid: Arc<Grid>,
30 custom_script: Option<&str>,
31) -> (Engine, Option<AST>, Option<String>) {
32 let spill_map = Arc::new(SpillMap::new());
33 create_engine_with_functions_and_spill(grid, spill_map, custom_script)
34}
35
36pub fn create_engine_with_functions_and_spill(
39 grid: Arc<Grid>,
40 spill_map: Arc<SpillMap>,
41 custom_script: Option<&str>,
42) -> (Engine, Option<AST>, Option<String>) {
43 let engine = create_engine_with_spill(grid, spill_map);
44
45 let (ast, error) = if let Some(script) = custom_script {
46 match engine.compile(script) {
47 Ok(ast) => (Some(ast), None),
48 Err(e) => (None, Some(format!("Error in custom functions: {}", e))),
49 }
50 } else {
51 (None, None)
52 };
53
54 (engine, ast, error)
55}
56
57pub fn eval_with_functions(
59 engine: &Engine,
60 formula: &str,
61 custom_ast: Option<&AST>,
62) -> Result<Dynamic, String> {
63 if let Some(ast) = custom_ast {
64 match engine.compile(formula) {
65 Ok(formula_ast) => {
66 let merged = ast.clone().merge(&formula_ast);
67 engine.eval_ast(&merged).map_err(|e| e.to_string())
68 }
69 Err(e) => Err(e.to_string()),
70 }
71 } else {
72 engine.eval(formula).map_err(|e| e.to_string())
73 }
74}