1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! WASM evaluation functions
use super::core::console_log;
use super::types::JSONEvalWasm;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
impl JSONEvalWasm {
/// Evaluate schema with provided data (does not return schema - use getEvaluatedSchema() for that)
///
/// @param data - JSON data string
/// @param context - Optional context data JSON string
/// @throws Error if evaluation fails
#[wasm_bindgen]
pub fn evaluate(
&mut self,
data: &str,
context: Option<String>,
paths: Option<Vec<String>>,
) -> Result<(), JsValue> {
let ctx = context.as_deref();
let paths_refs: Option<Vec<String>> = paths;
// Convert Vec<String> to &[String] for evaluate
// We need to keep the Vec alive if it exists
let token = self.reset_token();
match self
.inner
.evaluate(data, ctx, paths_refs.as_deref(), token.as_ref())
{
Ok(_) => Ok(()),
Err(e) => {
let error_msg = format!("Evaluation failed: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
Err(JsValue::from_str(&error_msg))
}
}
}
/// Evaluate and return as JsValue for direct JavaScript object access
///
/// @param data - JSON data string
/// @param context - Optional context data JSON string
/// @returns Evaluated schema as JavaScript object
#[wasm_bindgen(js_name = evaluateJS)]
pub fn evaluate_js(
&mut self,
data: &str,
context: Option<String>,
paths: Option<Vec<String>>,
) -> Result<JsValue, JsValue> {
let ctx = context.as_deref();
let paths_refs: Option<Vec<String>> = paths;
let token = self.reset_token();
match self
.inner
.evaluate(data, ctx, paths_refs.as_deref(), token.as_ref())
{
Ok(_) => {
let result = self.inner.get_evaluated_schema(false);
super::to_value(&result).map_err(|e| {
let error_msg =
format!("Failed to convert evaluation result to JsValue: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
JsValue::from_str(&error_msg)
})
}
Err(e) => {
let error_msg = format!("Evaluation failed: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
Err(JsValue::from_str(&error_msg))
}
}
}
/// Evaluate dependents when a field changes (returns array of changes as JSON string)
///
/// @param changedPath - Path of the field that changed
/// @param data - Optional updated JSON data string
/// @param context - Optional context data JSON string
/// @returns Array of dependent change objects as JSON string
#[wasm_bindgen(js_name = evaluateDependents)]
pub fn evaluate_dependents(
&mut self,
changed_path: &str,
data: Option<String>,
context: Option<String>,
re_evaluate: bool,
include_subforms: Option<bool>,
) -> Result<String, JsValue> {
let data_str = data.as_deref();
let ctx = context.as_deref();
// Wrap single path in a Vec for the new API
let paths = vec![changed_path.to_string()];
let token = self.reset_token();
match self.inner.evaluate_dependents(
&paths,
data_str,
ctx,
re_evaluate,
token.as_ref(),
None,
include_subforms.unwrap_or(true),
) {
Ok(result) => serde_json::to_string(&result).map_err(|e| {
let error_msg = format!("Failed to serialize dependents: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
JsValue::from_str(&error_msg)
}),
Err(e) => {
let error_msg = format!("Failed to evaluate dependents: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
Err(JsValue::from_str(&error_msg))
}
}
}
/// Evaluate dependents and return as JavaScript object
///
/// @param changedPathsJson - JSON array of field paths that changed
/// @param data - Optional updated JSON data string
/// @param context - Optional context data JSON string
/// @param reEvaluate - If true, performs full evaluation after processing dependents
/// @returns Array of dependent change objects as JavaScript object
#[wasm_bindgen(js_name = evaluateDependentsJS)]
pub fn evaluate_dependents_js(
&mut self,
changed_paths_json: &str,
data: Option<String>,
context: Option<String>,
re_evaluate: bool,
include_subforms: Option<bool>,
) -> Result<JsValue, JsValue> {
// Parse JSON array of paths
let paths: Vec<String> = serde_json::from_str(changed_paths_json).map_err(|e| {
let error_msg = format!("Failed to parse paths JSON: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
JsValue::from_str(&error_msg)
})?;
let data_str = data.as_deref();
let ctx = context.as_deref();
let token = self.reset_token();
match self.inner.evaluate_dependents(
&paths,
data_str,
ctx,
re_evaluate,
token.as_ref(),
None,
include_subforms.unwrap_or(true),
) {
Ok(result) => super::to_value(&result).map_err(|e| {
let error_msg = format!("Failed to serialize dependents: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
JsValue::from_str(&error_msg)
}),
Err(e) => {
let error_msg = format!("Failed to evaluate dependents: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
Err(JsValue::from_str(&error_msg))
}
}
}
/// Compile and run JSON logic from a JSON logic string
/// @param logic_str - JSON logic expression as a string
/// @param data - Optional JSON data string
/// @param context - Optional JSON context string
/// @returns Result as JavaScript object
#[wasm_bindgen(js_name = compileAndRunLogic)]
pub fn compile_and_run_logic(
&mut self,
logic_str: &str,
data: Option<String>,
context: Option<String>,
) -> Result<JsValue, JsValue> {
let data_str = data.as_deref();
let context_str = context.as_deref();
match self
.inner
.compile_and_run_logic(logic_str, data_str, context_str)
{
Ok(result) => super::to_value(&result).map_err(|e| {
let error_msg = format!("Failed to convert logic result: {}", e);
JsValue::from_str(&error_msg)
}),
Err(e) => Err(JsValue::from_str(&e)),
}
}
/// Compile JSON logic and return a global ID
/// @param logic_str - JSON logic expression as a string
/// @returns Compiled logic ID as number (u64)
#[wasm_bindgen(js_name = compileLogic)]
pub fn compile_logic(&self, logic_str: &str) -> Result<f64, JsValue> {
match self.inner.compile_logic(logic_str) {
Ok(id) => Ok(id.as_u64() as f64), // JavaScript number
Err(e) => Err(JsValue::from_str(&e)),
}
}
/// Run pre-compiled logic by ID
/// @param logic_id - Compiled logic ID from compileLogic
/// @param data - Optional JSON data string
/// @param context - Optional JSON context string
/// @returns Result as JavaScript object
#[wasm_bindgen(js_name = runLogic)]
pub fn run_logic(
&mut self,
logic_id: f64,
data: Option<String>,
context: Option<String>,
) -> Result<JsValue, JsValue> {
let id = crate::CompiledLogicId::from_u64(logic_id as u64);
let data_value = if let Some(data_str) = data {
match serde_json::from_str(&data_str) {
Ok(v) => Some(v),
Err(e) => return Err(JsValue::from_str(&format!("Failed to parse data: {}", e))),
}
} else {
None
};
let context_value = if let Some(ctx_str) = context {
match serde_json::from_str(&ctx_str) {
Ok(v) => Some(v),
Err(e) => {
return Err(JsValue::from_str(&format!(
"Failed to parse context: {}",
e
)))
}
}
} else {
None
};
match self
.inner
.run_logic(id, data_value.as_ref(), context_value.as_ref())
{
Ok(result) => super::to_value(&result).map_err(|e| {
let error_msg = format!("Failed to convert logic result: {}", e);
JsValue::from_str(&error_msg)
}),
Err(e) => Err(JsValue::from_str(&e)),
}
}
/// Static helper to evaluate logic without creating an instance
/// @param logic_str - JSON logic expression string
/// @param data - Optional JSON data string
/// @param context - Optional JSON context string
/// @returns Result as JavaScript object
#[wasm_bindgen(js_name = evaluateLogic)]
pub fn evaluate_logic_static(
logic_str: &str,
data: Option<String>,
context: Option<String>,
) -> Result<JsValue, JsValue> {
let data_str = data.as_deref();
let ctx = context.as_deref();
match crate::jsoneval::logic::evaluate_logic_pure(logic_str, data_str, ctx) {
Ok(result) => super::to_value(&result).map_err(|e| {
let error_msg = format!("Failed to convert logic result: {}", e);
JsValue::from_str(&error_msg)
}),
Err(e) => Err(JsValue::from_str(&e)),
}
}
}