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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
use crate::Error;
use crate::MAX_PARSE_DEPTH;
use crate::ast::{Value, is_valid_symbol};
use crate::builtinops::{BuiltinOp, find_jsonlogic_op, find_scheme_op, get_list_op, get_quote_op};
use crate::evaluator::Arity;
/// Indicates the compilation context for JSON values
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CompilationContext {
/// Normal JSONLogic compilation - objects become operations, arrays become lists
Normal,
/// Array element context - used for elements inside JSONLogic arrays (operations still work)
ArrayElement,
/// Quote context - everything becomes literal data, no operations compiled
Quote,
}
/// Parse JSONLogic expression into AST value for evaluation
pub fn parse_jsonlogic(input: &str) -> Result<Value, Error> {
let json_value: serde_json::Value =
serde_json::from_str(input).map_err(|e| Error::ParseError(format!("Invalid JSON: {e}")))?;
compile_json_to_ast(json_value)
}
/// Compile a serde_json::Value to AST value
fn compile_json_to_ast(json: serde_json::Value) -> Result<Value, Error> {
compile_json_to_ast_with_context(json, CompilationContext::Normal, 0)
}
fn compile_json_to_ast_with_context(
json: serde_json::Value,
context: CompilationContext,
depth: usize,
) -> Result<Value, Error> {
if depth >= MAX_PARSE_DEPTH {
return Err(Error::ParseError(format!(
"JSONLogic expression too deeply nested (max depth: {MAX_PARSE_DEPTH})"
)));
}
match json {
// Primitives
serde_json::Value::Null => Err(Error::ParseError(
"null values are not supported in this JSONLogic implementation".into(),
)),
serde_json::Value::Bool(b) => Ok(Value::Bool(b)),
serde_json::Value::Number(n) => {
if let Some(i) = n.as_i64() {
Ok(Value::Number(i))
} else {
Err(Error::ParseError(format!(
"Number too large or not integer: {n}"
)))
}
}
serde_json::Value::String(s) => Ok(Value::String(s)),
serde_json::Value::Array(arr) => {
// Determine context for array elements
let element_context = match context {
CompilationContext::Quote => CompilationContext::Quote, // Quote context propagates
_ => CompilationContext::ArrayElement, // Normal/ArrayElement becomes ArrayElement
};
let converted: Vec<Value> = arr
.into_iter()
.map(|v| compile_json_to_ast_with_context(v, element_context, depth + 1))
.collect::<Result<Vec<_>, _>>()?;
match context {
CompilationContext::Quote => {
// In quote context, arrays become literal list data without "list" symbol
// This matches the Scheme representation: (quote ("+" 1 2)) -> just the list elements
Ok(Value::List(converted))
}
_ => {
// Arrays always become list operations in JSONLogic
let list_op = get_list_op();
Ok(Value::PrecompiledOp {
op: list_op,
op_id: list_op.scheme_id.into(),
args: converted,
})
}
}
}
serde_json::Value::Object(obj) => {
let (operator, operands) = {
let mut iter = obj.into_iter();
// Attempt to get two elements from this object - hoping to get only 1 item back
match (iter.next(), iter.next()) {
(Some((op, val)), None) => (op, val), // Exactly one item
_ => {
return Err(Error::ParseError(
"JSONLogic operations must have exactly one operator".into(),
));
}
}
};
match context {
CompilationContext::Quote => {
// In quote context, only {"var": "symbol"} objects are allowed
// All other objects (operations) are forbidden to ensure canonical list representation
if operator == "var" {
// {"var": "symbol"} becomes a symbol in quote context
match operands {
serde_json::Value::String(var_name) if is_valid_symbol(&var_name) => {
Ok(Value::Symbol(var_name))
}
_ => Err(Error::ParseError(
"Variable must be a valid symbol string".into(),
)),
}
} else {
// Reject all other object form operations in quote context
Err(Error::ParseError(format!(
"Object form operations like '{{\"{operator}\":...}}' are not allowed in quote context. Use list form like '[\"{operator}\", ...]' instead"
)))
}
}
CompilationContext::Normal | CompilationContext::ArrayElement => {
// Normal compilation context - compile as operation
compile_jsonlogic_operation(&operator, operands, depth)
}
}
}
}
}
/// Compile JSONLogic operations to S-expression function calls
fn compile_jsonlogic_operation(
operator: &str,
operands: serde_json::Value,
depth: usize,
) -> Result<Value, Error> {
// Helper function to extract operands as a list, ignoring arity checks
let extract_operand_list = |operands: serde_json::Value| -> Result<Vec<Value>, Error> {
match operands {
serde_json::Value::Array(arr) => {
// Convert each operand - arrays that appear as operands should use list constructors as data
// (because they represent data values, not argument lists to be expanded)
// Non-arrays are scalar arguments and should not use list constructors
arr.into_iter()
.map(|v| {
let context = if matches!(v, serde_json::Value::Array(_)) {
CompilationContext::ArrayElement
} else {
CompilationContext::Normal
};
compile_json_to_ast_with_context(v, context, depth + 1)
})
.collect::<Result<Vec<_>, _>>()
}
// Single operand - use list constructor if it's an array (data), don't modify scalars
single => {
let context = if matches!(single, serde_json::Value::Array(_)) {
CompilationContext::ArrayElement
} else {
CompilationContext::Normal
};
Ok(vec![compile_json_to_ast_with_context(
single,
context,
depth + 1,
)?])
}
}
};
// Helper function to create PrecompiledOp from a known builtin operation
let create_precompiled_op = |builtin_op: &'static BuiltinOp, args: Vec<Value>| -> Value {
Value::PrecompiledOp {
op: builtin_op,
op_id: builtin_op.scheme_id.into(),
args,
}
};
// Special cases that need manual handling
// Note: In this implementation == is an alias to === and != to !==. While these operations normally have
// significantly different semantics, in this implementation, there is no type coercion so their behavior is identical!
match operator {
// !== and != need special handling with binary arity validation, since equal? takes arbitrary arguments
"!==" | "!=" => {
let args = extract_operand_list(operands)?;
// Validate that we have exactly 2 arguments for not-equal operation
Arity::Exact(2).validate(args.len())?;
let equal_builtin = find_jsonlogic_op("===").expect("=== builtin should exist");
let equal_op = create_precompiled_op(equal_builtin, args);
let not_builtin = find_jsonlogic_op("!").expect("! builtin should exist");
Ok(create_precompiled_op(not_builtin, vec![equal_op]))
}
// == is an alias for === (handled here to avoid duplicate registry entries)
"==" => {
let args = extract_operand_list(operands)?;
let equal_builtin = find_jsonlogic_op("===").expect("=== builtin should exist");
// Validate arity using the builtin operation's definition
equal_builtin.validate_arity(args.len())?;
Ok(create_precompiled_op(equal_builtin, args))
}
// Variable access (JSONLogic specific)
"var" => match operands {
serde_json::Value::String(var_name) if is_valid_symbol(&var_name) => {
Ok(Value::Symbol(var_name))
}
_ => Err(Error::ParseError(
"Variable must be a valid symbol string".into(),
)),
},
// Quote special form - CRITICAL: preserve quoted content as literal data
"scheme-quote" => {
// Quote must have exactly one operand and must NOT compile/evaluate it
let operand = match operands {
serde_json::Value::Array(arr) => match arr.as_slice() {
[single_operand] => single_operand.clone(),
_ => {
return Err(Error::ParseError("quote requires one operand".into()));
}
},
single_operand => single_operand, // Single operand without array wrapper
};
// Convert the operand to literal data WITHOUT compilation/evaluation
// This preserves the quoted content as pure data
let quoted_data =
compile_json_to_ast_with_context(operand, CompilationContext::Quote, depth + 1)?;
// Create a PrecompiledOp wrapper to preserve quote information for roundtrip
// This allows us to distinguish quoted data from regular list data
let quote_op = get_quote_op();
Ok(Value::PrecompiledOp {
op: quote_op,
op_id: quote_op.scheme_id.into(),
args: vec![quoted_data],
})
}
// For all other operations, look them up in the registry
_ => {
// Look up operation by JSONLogic id
if let Some(builtin_op) = find_jsonlogic_op(operator) {
let args = extract_operand_list(operands)?;
// Validate arity using the builtin operation's definition
builtin_op.validate_arity(args.len())?;
Ok(create_precompiled_op(builtin_op, args))
} else {
// Check if this unknown JSONLogic operator happens to be a known Scheme builtin
// If so, reject it to prevent accidental access to Scheme symbols via JSONLogic
if find_scheme_op(operator).is_some() {
Err(Error::ParseError(format!(
"JSONLogic operator '{operator}' is not supported. Use the JSONLogic equivalent instead (e.g., use '!' instead of 'not')."
)))
} else {
// Operation not in registry and not a Scheme builtin - emit as regular list for custom operations
// But first validate that the operator name is a valid symbol
if !is_valid_symbol(operator) {
return Err(Error::ParseError(format!(
"Invalid operator name: '{operator}'"
)));
}
let args = extract_operand_list(operands)?;
let mut result = vec![Value::Symbol(operator.into())];
result.extend(args);
Ok(Value::List(result))
}
}
}
}
}
/// Convert an AST value back to JSONLogic format
pub fn ast_to_jsonlogic(value: &Value) -> Result<String, Error> {
let json_value = ast_to_json_value(value)?;
serde_json::to_string(&json_value)
.map_err(|e| Error::EvalError(format!("Failed to serialize JSON: {e}")))
}
/// Convert an AST value to serde_json::Value for JSONLogic output
fn ast_to_json_value(value: &Value) -> Result<serde_json::Value, Error> {
ast_to_json_value_with_context(value, false)
}
/// Convert an AST value to serde_json::Value with optional quote context
fn ast_to_json_value_with_context(
value: &Value,
in_quote_context: bool,
) -> Result<serde_json::Value, Error> {
match value {
Value::Number(n) => Ok(serde_json::Value::Number(serde_json::Number::from(*n))),
Value::String(s) => Ok(serde_json::Value::String(s.clone())),
Value::Bool(b) => Ok(serde_json::Value::Bool(*b)),
Value::Symbol(s) => Ok(serde_json::json!({"var": s})),
Value::List(list) if list.is_empty() => Ok(serde_json::Value::Array(vec![])),
Value::List(list) => {
if in_quote_context {
// In quote context, lists always become arrays
let converted: Result<Vec<serde_json::Value>, Error> = list
.iter()
.map(|v| ast_to_json_value_with_context(v, true))
.collect();
Ok(serde_json::Value::Array(converted?))
} else if let [Value::Symbol(op), args @ ..] = list.as_slice() {
let args: Result<Vec<serde_json::Value>, Error> =
args.iter().map(ast_to_json_value).collect();
let args = args?;
match op.as_str() {
"list" => Ok(serde_json::Value::Array(args)),
_ => {
let jsonlogic_op = find_scheme_op(op)
.map_or(op.as_str(), |builtin_op| builtin_op.jsonlogic_id);
Ok(serde_json::json!({jsonlogic_op: args}))
}
}
} else {
let converted: Result<Vec<serde_json::Value>, Error> =
list.iter().map(ast_to_json_value).collect();
Ok(serde_json::Value::Array(converted?))
}
}
Value::BuiltinFunction { .. } => {
Err(Error::EvalError("Cannot convert builtin function".into()))
}
Value::PrecompiledOp { op, args, .. } => {
if in_quote_context {
return Err(Error::EvalError("PrecompiledOp in quote context".into()));
}
match op.scheme_id {
"list" => {
let converted: Result<Vec<serde_json::Value>, Error> =
args.iter().map(ast_to_json_value).collect();
Ok(serde_json::Value::Array(converted?))
}
"quote" => match args.as_slice() {
[single_arg] => {
let quoted_content = ast_to_json_value_with_context(single_arg, true)?;
Ok(serde_json::json!({"scheme-quote": [quoted_content]}))
}
_ => Err(Error::EvalError("Quote needs one argument".to_owned())),
},
_ => {
let converted_args: Result<Vec<serde_json::Value>, Error> =
args.iter().map(ast_to_json_value).collect();
Ok(serde_json::json!({op.jsonlogic_id: converted_args?}))
}
}
}
Value::Function { .. } => Err(Error::EvalError("Cannot convert user function".to_owned())),
Value::Unspecified => Err(Error::EvalError(
"Cannot convert unspecified value".to_owned(),
)),
}
}
#[cfg(test)]
#[cfg(feature = "scheme")]
mod tests {
use core::panic;
use super::*;
use crate::evaluator::{create_global_env, eval};
use crate::scheme::parse_scheme;
#[derive(Debug)]
enum TestResult {
Identical(&'static str),
IdenticalWithEvalError(&'static str), // Parsing succeeds, test AST equivalence, but evaluation must fail
SemanticallyEquivalent(&'static str),
Error,
SpecificError(&'static str), // Parsing should fail with an error message containing this string
}
use TestResult::*;
#[test]
#[expect(clippy::too_many_lines)] // Comprehensive test coverage is intentionally thorough
fn test_jsonlogic_to_scheme_equivalence() {
// Test cases as tuples: (jsonlogic, scheme_equivalent)
let test_cases = vec![
// Basic primitives
("true", Identical("#t")),
("false", Identical("#f")),
("42", Identical("42")),
(r#""hello""#, Identical(r#""hello""#)),
// Array literals are converted to list constructor calls (list 1 2 3)
// which preserves them as data structures rather than executable code
(r#"[1,2,3]"#, Identical("(list 1 2 3)")),
(r#"["a","b"]"#, Identical(r#"(list "a" "b")"#)),
(r#"[]"#, Identical("(list)")),
// Arithmetic operations
(r#"{"+": [1, 2, 3]}"#, Identical("(+ 1 2 3)")),
(r#"{"+": []}"#, Identical("(+)")),
(r#"{"-": [10, 3]}"#, Identical("(- 10 3)")),
(r#"{"*": [2, 3, 4]}"#, Identical("(* 2 3 4)")),
// Equality operations - === is canonical, == is alias
(r#"{"===": [1, 2]}"#, Identical("(equal? 1 2)")),
(r#"{"==": [1, 2]}"#, SemanticallyEquivalent("(equal? 1 2)")),
(r#"{">": [5, 3]}"#, Identical("(> 5 3)")),
(r#"{"<": [2, 5]}"#, Identical("(< 2 5)")),
(r#"{">=": [5, 5]}"#, Identical("(>= 5 5)")),
(r#"{"<=": [3, 5]}"#, Identical("(<= 3 5)")),
// Logical operations
(r#"{"and": [true, false]}"#, Identical("(and #t #f)")),
(r#"{"or": [false, false]}"#, Identical("(or #f #f)")),
(r#"{"!": [true]}"#, Identical("(not #t)")),
// Not-equal operations - !== is canonical, != is alias (both expand to (not (equal? ...)))
(
r#"{"!==": [1, 2]}"#,
SemanticallyEquivalent("(not (equal? 1 2))"),
),
(
r#"{"!=": [1, 2]}"#,
SemanticallyEquivalent("(not (equal? 1 2))"),
),
// Conditional operations
(
r#"{"if": [true, "yes", "no"]}"#,
Identical(r#"(if #t "yes" "no")"#),
),
(
r#"{"if": [false, "yes", "no"]}"#,
Identical(r#"(if #f "yes" "no")"#),
),
// Variable operations (simple symbol conversion)
(r#"{"var": "age"}"#, IdenticalWithEvalError("age")),
// Nested operations
(
r#"{"and": [true, {">": [5, 3]}]}"#,
Identical("(and #t (> 5 3))"),
),
(
r#"{"if": [{">": [10, 5]}, "big", "small"]}"#,
Identical(r#"(if (> 10 5) "big" "small")"#),
),
// String operations
(
r#"{"cat": "hello"}"#,
SemanticallyEquivalent(r#"(string-append "hello")"#),
),
(
r#"{"cat": ["hello", " ", "world"]}"#,
Identical(r#"(string-append "hello" " " "world")"#),
),
(r#"{"cat": []}"#, Identical("(string-append)")),
// Math operations
(r#"{"max": [1, 2, 3]}"#, Identical("(max 1 2 3)")),
(r#"{"max": 5}"#, SemanticallyEquivalent("(max 5)")),
(r#"{"min": [1, 2, 3]}"#, Identical("(min 1 2 3)")),
(r#"{"min": 5}"#, SemanticallyEquivalent("(min 5)")),
// Unknown operations should still be emitted
(
r#"{"unknown": [1, 2, 3]}"#,
IdenticalWithEvalError("(unknown 1 2 3)"),
),
(
r#"{"unknown_zero_args": []}"#,
IdenticalWithEvalError("(unknown_zero_args)"),
),
// Error cases
("null", Error), // Null values should be rejected
(r#"{"!==": [1]}"#, SpecificError("ArityError")), // Not equal with wrong arity should fail
(r#"{"!==": [1, 2, 3]}"#, SpecificError("ArityError")), // Not equal with too many args should fail
(r#"{"!=": [1]}"#, SpecificError("ArityError")), // != alias also fails with wrong arity
(r#"{"!=": [1, 2, 3]}"#, SpecificError("ArityError")), // != alias also fails with too many args
(r#"{"if": [true, "yes"]}"#, SpecificError("ArityError")), // If with wrong arity should fail
("invalid json", Error), // Invalid JSON should fail
(r#"{"and": true, "or": false}"#, Error), // Multiple keys in object should fail
// Quote context error cases - object form operations should be rejected
(
r#"{"scheme-quote": [{"+": [1, 2]}]}"#,
SpecificError("Object form operations"),
),
(
r#"{"scheme-quote": [{"if": [true, "yes", "no"]}]}"#,
SpecificError("not allowed in quote context"),
),
(
r#"{"scheme-quote": [{"not": true}]}"#,
SpecificError("Object form operations"),
),
(
r#"{"scheme-quote": [{"and": [true, false]}]}"#,
SpecificError("not allowed in quote context"),
),
(
r#"{"scheme-quote": [{"car": [1, 2, 3]}]}"#,
SpecificError("Object form operations"),
),
// Nested object form operations should also be rejected
(
r#"{"scheme-quote": [["list", {"+": [1, 2]}]]}"#,
SpecificError("Object form operations"),
),
// Empty string variable names should be rejected
(
r#"{"scheme-quote": [{"var": ""}]}"#,
SpecificError("Variable must be a valid symbol string"),
),
// Invalid operator names should be rejected
(
r#"{"invalid op": [1, 2]}"#,
SpecificError("Invalid operator name"),
),
(
r#"{"123invalid": [1, 2]}"#,
SpecificError("Invalid operator name"),
),
// Design validation tests - operations intentionally rejected/different
(
r#"{"unknown_not": [true]}"#,
IdenticalWithEvalError("(unknown_not #t)"),
), // Unknown operator becomes list
(r#"{"/": [8, 2]}"#, IdenticalWithEvalError("(/ 8 2)")), // Division operator becomes unknown operation (not a Scheme builtin)
(r#"{"car": [[1, 2, 3]]}"#, Error), // Scheme builtin 'car' should be rejected
(r#"{"define": ["x", 42]}"#, Error), // Scheme builtin 'define' should be rejected
(r#"{"!!": [[]]}"#, IdenticalWithEvalError("(!! (list))")), // Double negation becomes unknown operation (not a Scheme builtin)
(r#"{"!!": ["0"]}"#, IdenticalWithEvalError("(!! \"0\")")), // Double negation becomes unknown operation
(r#"{"!!": [0]}"#, IdenticalWithEvalError("(!! 0)")), // Double negation becomes unknown operation
(r#"{"!!": [1]}"#, IdenticalWithEvalError("(!! 1)")), // Double negation becomes unknown operation
(r#"{"!!": [""]}"#, IdenticalWithEvalError("(!! \"\")")), // Double negation becomes unknown operation
(
r#"{"!!": ["hello"]}"#,
IdenticalWithEvalError("(!! \"hello\")"),
), // Double negation becomes unknown operation
(r#"{"var": "field"}"#, IdenticalWithEvalError("field")), // Variable access converts to symbol
(r#"{"if": [null, "yes", "no"]}"#, Error), // Null in JSONLogic expression should be rejected
// JSONLogic syntactic sugar - unary operators without arrays (roundtrip normalizes)
(r#"{"!": true}"#, SemanticallyEquivalent("(not #t)")), // Unary NOT without array
(r#"{"!": false}"#, SemanticallyEquivalent("(not #f)")), // Unary NOT without array
(r#"{"-": 2}"#, SemanticallyEquivalent("(- 2)")), // Unary minus
(r#"{"-": -2}"#, SemanticallyEquivalent("(- -2)")), // Unary minus of negative
// Type coercion edge cases - our language rejects type coercion with strict typing
(
r#"{"===": [1, "1"]}"#,
IdenticalWithEvalError("(equal? 1 \"1\")"),
), // Strict equality: different types cause type error (no coercion)
(
r#"{"===": [0, false]}"#,
IdenticalWithEvalError("(equal? 0 #f)"),
), // Strict equality: different types cause type error (no coercion)
// Test that == alias works the same as === (but roundtrips to === canonical form)
// Type mismatch cases are handled by the alias behavior - no special tests needed
// Test additional === cases (canonical equality)
(r#"{"===": [1, 1]}"#, Identical("(equal? 1 1)")), // Canonical strict equality
(r#"{"===": [true, true]}"#, Identical("(equal? #t #t)")), // Boolean equality
(
r#"{"===": ["hello", "hello"]}"#,
Identical("(equal? \"hello\" \"hello\")"),
), // String equality
// !== expands to (not (equal? ...)) so it won't roundtrip identically
// Type mismatch cases are handled by the underlying equal? behavior
// != type mismatch cases are handled by the alias behavior - no special tests needed
// Between operations (chained comparisons)
(r#"{"<": [1, 2, 3]}"#, Identical("(< 1 2 3)")), // Between exclusive (1 < 2 < 3)
(r#"{"<": [1, 1, 3]}"#, Identical("(< 1 1 3)")), // Between exclusive fails at equality
(r#"{"<=": [1, 2, 3]}"#, Identical("(<= 1 2 3)")), // Between inclusive (1 <= 2 <= 3)
(r#"{"<=": [1, 1, 3]}"#, Identical("(<= 1 1 3)")), // Between inclusive allows equality
// Array literals become list constructor calls (data, not executable code)
(r#"[1, 2, 3]"#, Identical("(list 1 2 3)")), // Array literal converts to list call
// Operations with array arguments (arrays become list constructors as data)
(
r#"{"and": [true, [1, 2]]}"#,
IdenticalWithEvalError("(and #t (list 1 2))"),
), // Operation with array argument
(
r#"{"unknown": [[1, 2], 3]}"#,
IdenticalWithEvalError("(unknown (list 1 2) 3)"),
), // Unknown operation with array argument
(
r#"{"test": [42, [], "end"]}"#,
IdenticalWithEvalError(r#"(test 42 (list) "end")"#),
), // Mixed arguments including empty array
// Complex nested array structures
(
r#"[[1, 2], [3, 4]]"#,
Identical("(list (list 1 2) (list 3 4))"),
), // Nested arrays - should become nested list constructor calls
(
r#"[[], [1], [1, 2]]"#,
Identical("(list (list) (list 1) (list 1 2))"),
), // Arrays with different lengths
(r#"[[[]]]"#, Identical("(list (list (list)))")), // Triply nested empty arrays
(r#"[1, [2, [3]]]"#, Identical("(list 1 (list 2 (list 3)))")), // Right-nested structure
(r#"[[[1], 2], 3]"#, Identical("(list (list (list 1) 2) 3)")), // Left-nested structure
// Arrays that could be mistaken for operator calls if not using list constructors
(r#"["+", 1, 2]"#, Identical(r#"(list "+" 1 2)"#)), // Would be (+ 1 2) if not using list constructor
(
r#"["if", true, "yes", "no"]"#,
Identical(r#"(list "if" #t "yes" "no")"#),
), // Would be if statement if not using list constructor
(
r#"["and", true, false]"#,
Identical(r#"(list "and" #t #f)"#),
), // Would be logical and if not using list constructor
(r#"["not", true]"#, Identical(r#"(list "not" #t)"#)), // Would be negation if not using list constructor
(r#"["equal?", 1, 1]"#, Identical(r#"(list "equal?" 1 1)"#)), // Would be equality test if not using list constructor
// Arrays with Scheme builtin names that should remain as data
(
r#"["car", "cdr", "cons"]"#,
Identical(r#"(list "car" "cdr" "cons")"#),
),
(
r#"["define", "lambda", "let"]"#,
Identical(r#"(list "define" "lambda" "let")"#),
),
(
r#"["quote", "unquote", "eval"]"#,
Identical(r#"(list "quote" "unquote" "eval")"#),
),
// Operations with nested array arguments
(
r#"{"test_nested": [[1, 2]]}"#,
IdenticalWithEvalError("(test_nested (list 1 2))"),
),
(
r#"{"fn": [[1], [2, 3]]}"#,
IdenticalWithEvalError("(fn (list 1) (list 2 3))"),
),
(
r#"{"mixed": [42, [1, 2], "hello"]}"#,
IdenticalWithEvalError(r#"(mixed 42 (list 1 2) "hello")"#),
),
(
r#"{"complex": [true, [], [1], "end"]}"#,
IdenticalWithEvalError(r#"(complex #t (list) (list 1) "end")"#),
),
// Deeply nested structures with operations
(
r#"{"outer": [{"inner": [1, 2]}, [3, 4]]}"#,
IdenticalWithEvalError("(outer (inner 1 2) (list 3 4))"),
),
(
r#"[{"op": [1]}, {"op2": [2]}]"#,
IdenticalWithEvalError("(list (op 1) (op2 2))"),
),
// Arrays in JSON are not function calls in Scheme - they become list constructor calls
(
r#"["eval", "(+ 1 2)"]"#,
Identical(r#"(list "eval" "(+ 1 2)")"#),
),
(
r#"["load", "dangerous-file.scm"]"#,
Identical(r#"(list "load" "dangerous-file.scm")"#),
),
// ===== QUOTE OPERATION TESTS (CRITICAL FOR EVALUATION CONTROL) =====
// Quote should preserve content as literal data without PrecompiledOps/Functions
(
r#"{"scheme-quote": ["hello"]}"#,
Identical(r#"(quote "hello")"#),
),
(r#"{"scheme-quote": [42]}"#, Identical("(quote 42)")),
(r#"{"scheme-quote": [true]}"#, Identical("(quote #t)")),
(
r#"{"scheme-quote": [[1, 2, 3]]}"#,
Identical("(quote (1 2 3))"),
),
// Quote should prevent evaluation of operations (using list form only)
(
r#"{"scheme-quote": [["+", 1, 2]]}"#,
Identical("(quote (\"+\" 1 2))"),
),
(
r#"{"scheme-quote": [["not", true]]}"#,
Identical("(quote (\"not\" #t))"),
),
(
r#"{"scheme-quote": [["if", true, "yes", "no"]]}"#,
Identical(r#"(quote ("if" #t "yes" "no"))"#),
),
// Quote with nested operations (should all become data)
(
r#"{"scheme-quote": [["car", ["list", 1, 2, 3]]]}"#,
Identical(r#"(quote ("car" ("list" 1 2 3)))"#),
),
// Quote with arrays containing operations
(
r#"{"scheme-quote": [[["+", 1, 2], ["-", 3, 1]]]}"#,
Identical(r#"(quote (("+" 1 2) ("-" 3 1)))"#),
),
// Quote preserves symbol structure ({"var": "x"} is allowed in quotes)
(
r#"{"scheme-quote": [{"var": "x"}]}"#,
Identical("(quote x)"),
),
(
r#"{"scheme-quote": [["and", true, false]]}"#,
Identical(r#"(quote ("and" #t #f))"#),
),
(
r#"{"scheme-quote": [[{"var": "func"}, 1, 2]]}"#,
Identical(r#"(quote (func 1 2))"#),
),
(
r#"{"scheme-quote": [["list", {"var": "symbol"}, 1, 2]]}"#,
Identical(r#"(quote ("list" symbol 1 2))"#),
), // List with symbol in quote context
// Complex nested lambda-like structures
(
r#"["lambda", ["x"], ["*", "x", "x"]]"#,
Identical(r#"(list "lambda" (list "x") (list "*" "x" "x"))"#),
),
// Quote with non-array operand (normalizes to array form on roundtrip)
(
r#"{"scheme-quote": 42}"#,
SemanticallyEquivalent("(quote 42)"),
),
// Quote with wrong number of array operands
(
r#"{"scheme-quote": [1, 2]}"#,
SpecificError("quote requires one operand"),
),
// Float number that isn't an integer
(r#"3.14"#, SpecificError("not integer")),
// Large number overflow
(r#"99999999999999999999"#, SpecificError("too large")),
];
run_data_driven_tests(&test_cases);
}
#[test]
fn test_jsonlogic_depth_limits() {
// Create depth limit test strings
let arrays_under_limit = format!(
"{}{}{}",
"[".repeat(MAX_PARSE_DEPTH - 1),
r#"{"var": "unbound"}"#,
"]".repeat(MAX_PARSE_DEPTH - 1)
);
let objects_under_limit = format!(
"{}{}{}",
r#"{"op": "#.repeat(MAX_PARSE_DEPTH - 1),
r#"{"var": "unbound"}"#,
"}".repeat(MAX_PARSE_DEPTH - 1)
);
let deep_arrays_at_limit = format!(
"{}{}{}",
"[".repeat(MAX_PARSE_DEPTH),
"1",
"]".repeat(MAX_PARSE_DEPTH)
);
let deep_objects_at_limit = format!(
"{}{}{}",
r#"{"op": "#.repeat(MAX_PARSE_DEPTH),
r#"{"end": 1}"#,
"}".repeat(MAX_PARSE_DEPTH)
);
let depth_test_cases = vec![
// At/over limit should fail with specific depth error
(
deep_arrays_at_limit.as_str(),
SpecificError("too deeply nested"),
),
(
deep_objects_at_limit.as_str(),
SpecificError("too deeply nested"),
),
];
run_data_driven_tests(&depth_test_cases);
// Test that under-limit cases parse successfully (separate verification)
let arrays_result = parse_jsonlogic(&arrays_under_limit);
let objects_result = parse_jsonlogic(&objects_under_limit);
assert!(
arrays_result.is_ok(),
"Arrays just under depth limit should parse successfully"
);
assert!(
objects_result.is_ok(),
"Objects just under depth limit should parse successfully"
);
}
#[test]
fn test_ast_to_jsonlogic_error_paths() {
let mut env = create_global_env();
eval(&parse_scheme("(define f +)").unwrap(), &mut env).unwrap();
// Values that cannot be converted to JSONLogic
let unconvertible: Vec<(&str, Value)> = vec![
(
"BuiltinFunction",
eval(&parse_scheme("f").unwrap(), &mut env).unwrap(),
),
(
"Function",
eval(&parse_scheme("(lambda (x) (+ x 1))").unwrap(), &mut env).unwrap(),
),
("Unspecified", Value::Unspecified),
];
for (label, val) in &unconvertible {
assert!(
ast_to_jsonlogic(val).is_err(),
"{label} should fail conversion"
);
}
// Non-symbol list converts to JSON array
let list_val = Value::List(vec![Value::Number(1), Value::Number(2)]);
assert_eq!(ast_to_jsonlogic(&list_val).unwrap(), "[1,2]");
}
/// Helper function to test AST equivalence and roundtrip (shared by Identical and IdenticalWithEvalError)
fn test_ast_equivalence_and_roundtrip(
jsonlogic: &str,
jsonlogic_ast: &Value,
expected_scheme: &str,
) {
let scheme_ast = parse_scheme(expected_scheme).unwrap();
assert!(jsonlogic_ast == &scheme_ast);
// Test JSONLogic -> AST -> JSONLogic roundtrip (should always work)
let back_to_json = ast_to_jsonlogic(jsonlogic_ast).unwrap();
// Parse both JSON strings to compare structure rather than formatting
let original_json: serde_json::Value = serde_json::from_str(jsonlogic).unwrap();
let roundtrip_json: serde_json::Value = serde_json::from_str(&back_to_json).unwrap();
assert_eq!(
roundtrip_json, original_json,
"Roundtrip failed: {jsonlogic} -> {back_to_json} (expected {jsonlogic})"
);
}
/// Helper function to run data-driven tests
fn run_data_driven_tests(test_cases: &[(&str, TestResult)]) {
for (jsonlogic, expected_result) in test_cases {
println!("Testing JSONLogic: {jsonlogic}, expected: {expected_result:?}");
match (parse_jsonlogic(jsonlogic), expected_result) {
(Ok(jsonlogic_ast), Identical(expected_scheme)) => {
test_ast_equivalence_and_roundtrip(jsonlogic, &jsonlogic_ast, expected_scheme);
// Test evaluation of AST as well
match eval(&jsonlogic_ast, &mut create_global_env()) {
Ok(_) => {} // Good
Err(e) => panic!("Evaluation failed for {}: {:?}", jsonlogic, e),
}
}
(Ok(jsonlogic_ast), IdenticalWithEvalError(expected_scheme)) => {
test_ast_equivalence_and_roundtrip(jsonlogic, &jsonlogic_ast, expected_scheme);
// Verify that evaluation actually fails as expected
if let Ok(result) = eval(&jsonlogic_ast, &mut create_global_env()) {
panic!(
"Expected evaluation to fail for {}, but got: {}",
jsonlogic, result
)
}
// Expected failure
}
(Ok(jsonlogic_ast), SemanticallyEquivalent(expected_scheme)) => {
let scheme_ast = parse_scheme(expected_scheme).unwrap();
let jsonlogic_val = eval(&jsonlogic_ast, &mut create_global_env()).unwrap();
let scheme_val = eval(&scheme_ast, &mut create_global_env()).unwrap();
assert_eq!(jsonlogic_val, scheme_val);
// For semantic equivalence tests, also verify that roundtrip produces different
// but semantically equivalent JSONLogic (like != expanding to not+equal)
if let Ok(back_to_json) = ast_to_jsonlogic(&jsonlogic_ast)
&& back_to_json != *jsonlogic
{
// Verify that the roundtrip version also evaluates to the same result
let roundtrip_parsed = parse_jsonlogic(&back_to_json).unwrap();
let roundtrip_result =
eval(&roundtrip_parsed, &mut create_global_env()).unwrap();
assert_eq!(jsonlogic_val, roundtrip_result);
}
}
(Err(_), Error) => {
// Expected error
}
(Err(e), SpecificError(expected_error_text)) => {
let error_msg = format!("{e:?}");
assert!(
error_msg.contains(expected_error_text),
"Error message should contain '{expected_error_text}', but got: {error_msg}"
);
}
(Ok(_), SpecificError(expected_error_text)) => {
panic!(
"Expected error containing '{}' for JSONLogic '{}', but parsing succeeded",
expected_error_text, jsonlogic
);
}
(_, _) => {
panic!(
"Test failed for JSONLogic: {} Expected: {:?}",
jsonlogic, expected_result
);
}
}
}
}
}