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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
pub mod optimize;
use crate::node::{
CompiledLogic, CompiledNode, MetadataHint, PathSegment, ReduceHint, node_is_static,
};
use crate::opcode::OpCode;
use crate::{ContextStack, DataLogic, Result};
use regex::Regex;
use serde_json::{Value, json};
use std::sync::Arc;
impl CompiledLogic {
/// Compiles a JSON value into a compiled logic structure.
///
/// This method performs basic compilation without static evaluation.
/// For optimal performance, use `compile_with_static_eval` instead.
///
/// # Arguments
///
/// * `logic` - The JSON logic expression to compile
///
/// # Returns
///
/// A compiled logic structure, or an error if compilation fails.
pub fn compile(logic: &Value) -> Result<Self> {
let root = Self::compile_node(logic, None, false)?;
Ok(Self::new(root))
}
/// Compiles for tracing without static evaluation.
///
/// This method compiles the logic without performing static evaluation,
/// ensuring that all operators remain in the tree for step-by-step debugging.
/// Use this when you need to trace execution through operators that would
/// otherwise be pre-evaluated at compile time.
///
/// # Arguments
///
/// * `logic` - The JSON logic expression to compile
/// * `preserve_structure` - Whether to preserve unknown object structure
///
/// # Returns
///
/// A compiled logic structure without static optimizations.
pub fn compile_for_trace(logic: &Value, preserve_structure: bool) -> Result<Self> {
let root = Self::compile_node(logic, None, preserve_structure)?;
Ok(Self::new(root))
}
/// Compiles with static evaluation using the provided engine.
///
/// This method performs optimizations including:
/// - Static evaluation of constant expressions
/// - OpCode assignment for built-in operators
/// - Structure preservation based on engine settings
///
/// # Arguments
///
/// * `logic` - The JSON logic expression to compile
/// * `engine` - The DataLogic engine for static evaluation
///
/// # Returns
///
/// An optimized compiled logic structure, or an error if compilation fails.
pub fn compile_with_static_eval(logic: &Value, engine: &DataLogic) -> Result<Self> {
let root = Self::compile_node(logic, Some(engine), engine.preserve_structure())?;
Ok(Self::new(root))
}
/// Compiles a single JSON value into a CompiledNode.
///
/// This recursive method handles all node types:
/// - Objects with operators
/// - Arrays
/// - Primitive values
/// - Structured objects (in preserve mode)
///
/// # Arguments
///
/// * `value` - The JSON value to compile
/// * `engine` - Optional engine for static evaluation
/// * `preserve_structure` - Whether to preserve unknown object structure
///
/// # Returns
///
/// A compiled node, or an error if the value is invalid.
fn compile_node(
value: &Value,
engine: Option<&DataLogic>,
preserve_structure: bool,
) -> Result<CompiledNode> {
match value {
Value::Object(obj) if obj.len() > 1 => {
if preserve_structure {
// In preserve_structure mode, treat multi-key objects as structured objects
// We'll create a special StructuredObject node that gets evaluated field by field
let fields: Vec<_> = obj
.iter()
.map(|(key, val)| {
Self::compile_node(val, engine, preserve_structure)
.map(|compiled_val| (key.clone(), compiled_val))
})
.collect::<Result<Vec<_>>>()?;
Ok(CompiledNode::StructuredObject(Box::new(
crate::node::StructuredObjectData {
fields: fields.into_boxed_slice(),
},
)))
} else {
// Multi-key objects are not valid operators
Err(crate::error::Error::InvalidOperator(
"Unknown Operator".to_string(),
))
}
}
Value::Object(obj) if obj.len() == 1 => {
// Single key object is an operator
let (op_name, args_value) = obj.iter().next().unwrap();
// Try to parse as built-in operator first
if let Ok(opcode) = op_name.parse::<OpCode>() {
// Check if this operator requires array arguments
let requires_array = matches!(opcode, OpCode::And | OpCode::Or | OpCode::If);
// For operators that require arrays, check the raw value
if requires_array && !matches!(args_value, Value::Array(_)) {
// Create a special marker node for invalid arguments
let invalid_value = json!({
"__invalid_args__": true,
"value": args_value
});
let value_node = CompiledNode::Value {
value: invalid_value,
};
let args = vec![value_node].into_boxed_slice();
return Ok(CompiledNode::BuiltinOperator { opcode, args });
}
// Special handling for preserve operator - don't compile its arguments
let args = if opcode == OpCode::Preserve {
// Preserve takes raw values, not compiled logic
match args_value {
Value::Array(arr) => arr
.iter()
.map(|v| CompiledNode::Value { value: v.clone() })
.collect::<Vec<_>>()
.into_boxed_slice(),
_ => vec![CompiledNode::Value {
value: args_value.clone(),
}]
.into_boxed_slice(),
}
} else {
Self::compile_args(args_value, engine, preserve_structure)?
};
// Try to optimize variable access operators at compile time
if matches!(opcode, OpCode::Var | OpCode::Val | OpCode::Exists) {
let optimized = match opcode {
OpCode::Var => Self::try_compile_var(&args),
OpCode::Val => Self::try_compile_val(&args),
OpCode::Exists => Self::try_compile_exists(&args),
_ => None,
};
if let Some(node) = optimized {
return Ok(node);
}
}
// Pre-compile regex for split operator when delimiter is a static pattern
if opcode == OpCode::Split
&& let Some(node) = Self::try_compile_split_regex(&args)
{
return Ok(node);
}
// Pre-compile throw with literal string into CompiledThrow
if opcode == OpCode::Throw
&& args.len() == 1
&& let CompiledNode::Value {
value: Value::String(s),
} = &args[0]
{
return Ok(CompiledNode::CompiledThrow(Box::new(
serde_json::json!({"type": s}),
)));
}
let mut node = CompiledNode::BuiltinOperator { opcode, args };
// Run optimization passes when engine is available
if let std::option::Option::Some(eng) = engine {
node = optimize::optimize(node, eng);
}
// If engine is provided and node is static, evaluate it
if let std::option::Option::Some(eng) = engine
&& node_is_static(&node)
{
// Evaluate with empty context since it's static
let mut context = ContextStack::new(Arc::new(Value::Null));
match eng.evaluate_node(&node, &mut context) {
Ok(value) => {
return Ok(CompiledNode::Value { value });
}
// If evaluation fails, keep as operator node
Err(_) => return Ok(node),
}
}
Ok(node)
} else if preserve_structure {
// In preserve_structure mode, we need to distinguish between:
// 1. Custom operators (should be evaluated as operators)
// 2. Unknown keys (should be preserved as structured object fields)
//
// Check if this is a custom operator first
if let Some(eng) = engine
&& eng.has_custom_operator(op_name)
{
// It's a registered custom operator - compile as CustomOperator
// This ensures custom operators work correctly in preserve_structure mode,
// e.g., {"result": {"custom_op": arg}} will evaluate custom_op properly
let args = Self::compile_args(args_value, engine, preserve_structure)?;
return Ok(CompiledNode::CustomOperator(Box::new(
crate::node::CustomOperatorData {
name: op_name.clone(),
args,
},
)));
}
// Not a built-in operator or custom operator - treat as structured object field
// This allows dynamic object generation like {"name": {"var": "user.name"}}
let compiled_val = Self::compile_node(args_value, engine, preserve_structure)?;
let fields = vec![(op_name.clone(), compiled_val)].into_boxed_slice();
Ok(CompiledNode::StructuredObject(Box::new(
crate::node::StructuredObjectData { fields },
)))
} else {
let args = Self::compile_args(args_value, engine, preserve_structure)?;
// Fall back to custom operator - don't pre-evaluate custom operators
Ok(CompiledNode::CustomOperator(Box::new(
crate::node::CustomOperatorData {
name: op_name.clone(),
args,
},
)))
}
}
Value::Array(arr) => {
// Array of logic expressions
let nodes = arr
.iter()
.map(|v| Self::compile_node(v, engine, preserve_structure))
.collect::<Result<Vec<_>>>()?;
let nodes_boxed = nodes.into_boxed_slice();
let node = CompiledNode::Array { nodes: nodes_boxed };
// If engine is provided and array is static, evaluate it
if let std::option::Option::Some(eng) = engine
&& node_is_static(&node)
{
let mut context = ContextStack::new(Arc::new(Value::Null));
if let Ok(value) = eng.evaluate_node(&node, &mut context) {
return Ok(CompiledNode::Value { value });
}
}
Ok(node)
}
_ => {
// Static value
Ok(CompiledNode::Value {
value: value.clone(),
})
}
}
}
/// Compile operator arguments
fn compile_args(
value: &Value,
engine: Option<&DataLogic>,
preserve_structure: bool,
) -> Result<Box<[CompiledNode]>> {
match value {
Value::Array(arr) => arr
.iter()
.map(|v| Self::compile_node(v, engine, preserve_structure))
.collect::<Result<Vec<_>>>()
.map(Vec::into_boxed_slice),
_ => {
// Single argument - compile it
Ok(vec![Self::compile_node(value, engine, preserve_structure)?].into_boxed_slice())
}
}
}
/// Parse a dot-separated path into pre-parsed segments (for var, which uses dot notation).
/// Numeric segments become FieldOrIndex to handle both object keys and array indices.
fn parse_path_segments(path: &str) -> Vec<PathSegment> {
if path.is_empty() {
return Vec::new();
}
if !path.contains('.') {
if let Ok(idx) = path.parse::<usize>() {
return vec![PathSegment::FieldOrIndex(path.into(), idx)];
}
return vec![PathSegment::Field(path.into())];
}
path.split('.')
.map(|part| {
if let Ok(idx) = part.parse::<usize>() {
PathSegment::FieldOrIndex(part.into(), idx)
} else {
PathSegment::Field(part.into())
}
})
.collect()
}
/// Parse a var path and determine the reduce hint.
fn parse_var_path(path: &str) -> (ReduceHint, Vec<PathSegment>) {
if path == "current" {
(
ReduceHint::Current,
vec![PathSegment::Field("current".into())],
)
} else if path == "accumulator" {
(
ReduceHint::Accumulator,
vec![PathSegment::Field("accumulator".into())],
)
} else if let Some(rest) = path.strip_prefix("current.") {
let mut segs = vec![PathSegment::Field("current".into())];
segs.extend(Self::parse_path_segments(rest));
(ReduceHint::CurrentPath, segs)
} else if let Some(rest) = path.strip_prefix("accumulator.") {
let mut segs = vec![PathSegment::Field("accumulator".into())];
segs.extend(Self::parse_path_segments(rest));
(ReduceHint::AccumulatorPath, segs)
} else {
(ReduceHint::None, Self::parse_path_segments(path))
}
}
/// Try to compile a var operator into a CompiledVar node.
fn try_compile_var(args: &[CompiledNode]) -> Option<CompiledNode> {
if args.is_empty() {
return Some(CompiledNode::CompiledVar {
scope_level: 0,
segments: Box::new([]),
reduce_hint: ReduceHint::None,
metadata_hint: MetadataHint::None,
default_value: None,
});
}
let (segments, reduce_hint) = match &args[0] {
CompiledNode::Value {
value: Value::String(s),
} => {
let (hint, segs) = Self::parse_var_path(s);
(segs, hint)
}
CompiledNode::Value {
value: Value::Number(n),
} => {
let s = n.to_string();
let segs = Self::parse_path_segments(&s);
(segs, ReduceHint::None)
}
_ => return None, // dynamic path
};
let default_value = if args.len() > 1 {
Some(Box::new(args[1].clone()))
} else {
None
};
Some(CompiledNode::CompiledVar {
scope_level: 0,
segments: segments.into_boxed_slice(),
reduce_hint,
metadata_hint: MetadataHint::None,
default_value,
})
}
/// Try to compile a val operator into a CompiledVar node.
fn try_compile_val(args: &[CompiledNode]) -> Option<CompiledNode> {
if args.is_empty() {
return Some(CompiledNode::CompiledVar {
scope_level: 0,
segments: Box::new([]),
reduce_hint: ReduceHint::None,
metadata_hint: MetadataHint::None,
default_value: None,
});
}
// Val does NOT support dot-path notation. Each arg is a literal key/index.
// Case 2: Single non-empty string → single Field segment (literal key)
// Empty string has dual behavior (try key "" then whole-context fallback) — keep as BuiltinOperator.
if args.len() == 1 {
if let CompiledNode::Value {
value: Value::String(s),
} = &args[0]
&& !s.is_empty()
{
let reduce_hint = if s == "current" {
ReduceHint::Current
} else if s == "accumulator" {
ReduceHint::Accumulator
} else {
ReduceHint::None
};
let segment = if let Ok(idx) = s.parse::<usize>() {
PathSegment::FieldOrIndex(s.as_str().into(), idx)
} else {
PathSegment::Field(s.as_str().into())
};
return Some(CompiledNode::CompiledVar {
scope_level: 0,
segments: vec![segment].into_boxed_slice(),
reduce_hint,
metadata_hint: MetadataHint::None,
default_value: None,
});
}
return None;
}
// Case 3: First arg is [[level]] array
if let CompiledNode::Value {
value: Value::Array(level_arr),
} = &args[0]
&& let Some(Value::Number(level_num)) = level_arr.first()
&& let Some(level) = level_num.as_i64()
{
let scope_level = level.unsigned_abs() as u32;
// Check metadata hints for 2-arg case
let mut metadata_hint = MetadataHint::None;
if args.len() == 2
&& let CompiledNode::Value {
value: Value::String(s),
} = &args[1]
{
if s == "index" {
metadata_hint = MetadataHint::Index;
} else if s == "key" {
metadata_hint = MetadataHint::Key;
}
}
return Self::try_compile_val_segments(&args[1..], scope_level, metadata_hint);
}
// Case 4: 2+ args with all literal path segments — compile as path chain.
if let Some(first_seg) = Self::val_arg_to_segment(&args[0]) {
let reduce_hint = match &args[0] {
CompiledNode::Value {
value: Value::String(s),
} if s == "current" => ReduceHint::CurrentPath,
CompiledNode::Value {
value: Value::String(s),
} if s == "accumulator" => ReduceHint::AccumulatorPath,
_ => ReduceHint::None,
};
let mut segments = vec![first_seg];
if let Some(compiled) =
Self::try_collect_val_segments(&args[1..], &mut segments, reduce_hint)
{
return Some(compiled);
}
}
None
}
/// Convert a val argument into a PathSegment.
/// Val treats string args as literal keys (no dot-splitting), and numbers as indices.
/// Numeric strings get FieldOrIndex to handle both object key and array index access.
fn val_arg_to_segment(arg: &CompiledNode) -> Option<PathSegment> {
match arg {
CompiledNode::Value {
value: Value::String(s),
} => {
if let Ok(idx) = s.parse::<usize>() {
Some(PathSegment::FieldOrIndex(s.as_str().into(), idx))
} else {
Some(PathSegment::Field(s.as_str().into()))
}
}
CompiledNode::Value {
value: Value::Number(n),
} => n.as_u64().map(|idx| PathSegment::Index(idx as usize)),
_ => None,
}
}
/// Try to compile val path segments (used by level-access and path-chain cases).
fn try_compile_val_segments(
args: &[CompiledNode],
scope_level: u32,
metadata_hint: MetadataHint,
) -> Option<CompiledNode> {
let mut segments = Vec::new();
for arg in args {
segments.push(Self::val_arg_to_segment(arg)?);
}
Some(CompiledNode::CompiledVar {
scope_level,
segments: segments.into_boxed_slice(),
reduce_hint: ReduceHint::None,
metadata_hint,
default_value: None,
})
}
/// Try to collect remaining val args into segments and build a CompiledVar.
fn try_collect_val_segments(
args: &[CompiledNode],
segments: &mut Vec<PathSegment>,
reduce_hint: ReduceHint,
) -> Option<CompiledNode> {
for arg in args {
segments.push(Self::val_arg_to_segment(arg)?);
}
Some(CompiledNode::CompiledVar {
scope_level: 0,
segments: std::mem::take(segments).into_boxed_slice(),
reduce_hint,
metadata_hint: MetadataHint::None,
default_value: None,
})
}
/// Try to compile an exists operator into a CompiledExists node.
fn try_compile_exists(args: &[CompiledNode]) -> Option<CompiledNode> {
if args.is_empty() {
return Some(CompiledNode::CompiledExists(Box::new(
crate::node::CompiledExistsData {
scope_level: 0,
segments: Box::new([]),
},
)));
}
if args.len() == 1 {
if let CompiledNode::Value {
value: Value::String(s),
} = &args[0]
{
return Some(CompiledNode::CompiledExists(Box::new(
crate::node::CompiledExistsData {
scope_level: 0,
segments: vec![PathSegment::Field(s.as_str().into())].into_boxed_slice(),
},
)));
}
return None;
}
// Multiple args - all must be literal strings
let mut segments = Vec::new();
for arg in args {
if let CompiledNode::Value {
value: Value::String(s),
} = arg
{
segments.push(PathSegment::Field(s.as_str().into()));
} else {
return None;
}
}
Some(CompiledNode::CompiledExists(Box::new(
crate::node::CompiledExistsData {
scope_level: 0,
segments: segments.into_boxed_slice(),
},
)))
}
/// Try to pre-compile a split operator's regex pattern at compile time.
///
/// When the delimiter (second arg) is a static string containing named capture
/// groups (`(?P<...>`), the regex is compiled once here instead of on every evaluation.
fn try_compile_split_regex(args: &[CompiledNode]) -> Option<CompiledNode> {
if args.len() < 2 {
return None;
}
// Check if the delimiter is a static string with named capture groups
let pattern = match &args[1] {
CompiledNode::Value {
value: Value::String(s),
} if s.contains("(?P<") => s.as_str(),
_ => return None,
};
// Try to compile the regex
let re = Regex::new(pattern).ok()?;
let capture_names: Vec<Box<str>> = re.capture_names().flatten().map(|n| n.into()).collect();
// Only optimize if there are named capture groups
if capture_names.is_empty() {
return None;
}
// Keep only the text argument (first arg)
let text_args = vec![args[0].clone()].into_boxed_slice();
Some(CompiledNode::CompiledSplitRegex(Box::new(
crate::node::CompiledSplitRegexData {
args: text_args,
regex: Arc::new(re),
capture_names: capture_names.into_boxed_slice(),
},
)))
}
}