mathlex 0.3.2

Mathematical expression parser for LaTeX and plain text notation, producing a language-agnostic AST
Documentation
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
//! FFI bridge for Swift bindings using swift-bridge.
//!
//! This module provides a C-compatible FFI layer that allows Swift code
//! to parse mathematical expressions and interact with the AST.
//!
//! The bridge is only compiled when the `ffi` feature is enabled.
//!
//! ## JSON serialization
//!
//! When both the `ffi` and `serde` features are enabled, [`expression_to_json`]
//! and [`expression_to_json_pretty`] serialize any `Expression` to a JSON string
//! following the schema documented in `docs/json-ast-schema.md`. Swift consumers
//! can decode this JSON into `Decodable` types for evaluation or further
//! processing without needing direct access to the opaque Rust type.

#[cfg(feature = "ffi")]
#[allow(clippy::unnecessary_cast)]
#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        type Expression;

        #[swift_bridge(swift_name = "parseText")]
        fn parse_text(input: &str) -> Result<Expression, String>;

        #[swift_bridge(swift_name = "parseLatex")]
        fn parse_latex_ffi(input: &str) -> Result<Expression, String>;

        #[swift_bridge(swift_name = "toString")]
        fn expression_to_string(expr: &Expression) -> String;

        #[swift_bridge(swift_name = "toLatex")]
        fn expression_to_latex(expr: &Expression) -> String;

        #[swift_bridge(swift_name = "findVariables")]
        fn expression_find_variables(expr: &Expression) -> Vec<String>;

        #[swift_bridge(swift_name = "findFunctions")]
        fn expression_find_functions(expr: &Expression) -> Vec<String>;

        #[swift_bridge(swift_name = "findConstants")]
        fn expression_find_constants(expr: &Expression) -> Vec<String>;

        #[swift_bridge(swift_name = "depth")]
        fn expression_depth(expr: &Expression) -> usize;

        #[swift_bridge(swift_name = "nodeCount")]
        fn expression_node_count(expr: &Expression) -> usize;

        #[swift_bridge(swift_name = "toJSON")]
        fn expression_to_json(expr: &Expression) -> Result<String, String>;

        #[swift_bridge(swift_name = "toJSONPretty")]
        fn expression_to_json_pretty(expr: &Expression) -> Result<String, String>;
    }
}

#[cfg(feature = "ffi")]
use crate::{parse, parser::parse_latex as parse_latex_internal, Expression};

#[cfg(feature = "ffi")]
use crate::parser::{
    latex::parse_latex_equation_system, text::parse_equation_system as parse_text_equation_system,
};

/// FFI wrapper for parsing plain text expressions.
///
/// Converts ParseError into a String for FFI compatibility.
#[cfg(feature = "ffi")]
pub fn parse_text(input: &str) -> Result<Expression, String> {
    parse(input).map_err(|e| e.to_string())
}

/// FFI wrapper for parsing LaTeX expressions.
///
/// Converts ParseError into a String for FFI compatibility.
#[cfg(feature = "ffi")]
pub fn parse_latex_ffi(input: &str) -> Result<Expression, String> {
    parse_latex_internal(input).map_err(|e| e.to_string())
}

/// FFI wrapper for converting expression to string.
///
/// Uses the Display implementation internally.
#[cfg(feature = "ffi")]
pub fn expression_to_string(expr: &Expression) -> String {
    format!("{}", expr)
}

/// FFI wrapper for converting expression to LaTeX.
///
/// Uses the ToLatex trait implementation internally.
#[cfg(feature = "ffi")]
pub fn expression_to_latex(expr: &Expression) -> String {
    use crate::latex::ToLatex;
    expr.to_latex()
}

/// FFI wrapper for finding variables.
///
/// Converts HashSet to Vec for FFI compatibility.
#[cfg(feature = "ffi")]
pub fn expression_find_variables(expr: &Expression) -> Vec<String> {
    expr.find_variables().into_iter().collect()
}

/// FFI wrapper for finding functions.
///
/// Converts HashSet to Vec for FFI compatibility.
#[cfg(feature = "ffi")]
pub fn expression_find_functions(expr: &Expression) -> Vec<String> {
    expr.find_functions().into_iter().collect()
}

/// FFI wrapper for finding constants.
///
/// Returns constant names as strings for FFI compatibility.
#[cfg(feature = "ffi")]
pub fn expression_find_constants(expr: &Expression) -> Vec<String> {
    expr.find_constants()
        .into_iter()
        .map(|c| format!("{}", c))
        .collect()
}

/// FFI wrapper for calculating depth.
#[cfg(feature = "ffi")]
pub fn expression_depth(expr: &Expression) -> usize {
    expr.depth()
}

/// FFI wrapper for counting nodes.
#[cfg(feature = "ffi")]
pub fn expression_node_count(expr: &Expression) -> usize {
    expr.node_count()
}

/// FFI wrapper for serializing an expression to compact JSON.
///
/// Returns the JSON string on success, or an error string if serialization fails
/// or the `serde` feature is not enabled.
#[cfg(feature = "ffi")]
pub fn expression_to_json(expr: &Expression) -> Result<String, String> {
    #[cfg(feature = "serde")]
    {
        serde_json::to_string(expr).map_err(|e| e.to_string())
    }
    #[cfg(not(feature = "serde"))]
    {
        let _ = expr;
        Err("serde feature is not enabled".to_string())
    }
}

/// FFI wrapper for serializing an expression to pretty-printed JSON.
///
/// Returns the indented JSON string on success, or an error string if
/// serialization fails or the `serde` feature is not enabled.
#[cfg(feature = "ffi")]
pub fn expression_to_json_pretty(expr: &Expression) -> Result<String, String> {
    #[cfg(feature = "serde")]
    {
        serde_json::to_string_pretty(expr).map_err(|e| e.to_string())
    }
    #[cfg(not(feature = "serde"))]
    {
        let _ = expr;
        Err("serde feature is not enabled".to_string())
    }
}

/// FFI wrapper for parsing semicolon-delimited equation systems (plain text).
///
/// Returns a JSON-serialized array of expressions for FFI compatibility,
/// since swift-bridge does not support `Vec<OpaqueRustType>` directly.
#[cfg(feature = "ffi")]
pub fn parse_equation_system_ffi(input: &str) -> Result<Vec<Expression>, String> {
    parse_text_equation_system(input).map_err(|e| e.to_string())
}

/// FFI wrapper for parsing semicolon-delimited equation systems (LaTeX).
#[cfg(feature = "ffi")]
pub fn parse_latex_equation_system_ffi(input: &str) -> Result<Vec<Expression>, String> {
    parse_latex_equation_system(input).map_err(|e| e.to_string())
}

#[cfg(all(test, feature = "ffi"))]
mod tests {
    use super::*;

    #[test]
    fn test_parse_text_success() {
        let result = parse_text("2 + 3");
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_text_error() {
        let result = parse_text("2 +");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_latex_success() {
        let result = parse_latex_ffi(r"\frac{1}{2}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_latex_error() {
        let result = parse_latex_ffi(r"\frac{1}");
        assert!(result.is_err());
    }

    #[test]
    fn test_expression_to_string() {
        let expr = parse_text("2 + 3").unwrap();
        let s = expression_to_string(&expr);
        assert!(s.contains("2") && s.contains("3"));
    }

    #[test]
    fn test_expression_to_latex() {
        let expr = parse_text("1/2").unwrap();
        let latex = expression_to_latex(&expr);
        assert!(latex.contains("frac"));
    }

    #[test]
    fn test_expression_find_variables() {
        let expr = parse_text("x + y").unwrap();
        let vars = expression_find_variables(&expr);
        assert_eq!(vars.len(), 2);
        assert!(vars.contains(&"x".to_string()));
        assert!(vars.contains(&"y".to_string()));
    }

    #[test]
    fn test_expression_find_functions() {
        let expr = parse_text("sin(x) + cos(y)").unwrap();
        let funcs = expression_find_functions(&expr);
        assert_eq!(funcs.len(), 2);
        assert!(funcs.contains(&"sin".to_string()));
        assert!(funcs.contains(&"cos".to_string()));
    }

    #[test]
    fn test_expression_depth() {
        let expr = parse_text("2 + 3").unwrap();
        assert!(expression_depth(&expr) > 0);
    }

    #[test]
    fn test_expression_node_count() {
        let expr = parse_text("2 + 3").unwrap();
        assert!(expression_node_count(&expr) >= 3);
    }
}

#[cfg(all(test, feature = "ffi", feature = "serde"))]
mod json_tests {
    use super::*;
    use crate::ast::{
        BinaryOp, Direction, IntegralBounds, MathConstant, NumberSet, SetOp, UnaryOp,
    };
    use crate::Expression;

    // ----------------------------------------------------------------
    // Basic value types
    // ----------------------------------------------------------------

    #[test]
    fn test_json_integer() {
        let expr = parse_text("42").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert_eq!(json, r#"{"Integer":42}"#);
    }

    #[test]
    fn test_json_float() {
        let expr = parse_text("3.14").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("Float"),
            "expected Float variant, got: {json}"
        );
    }

    #[test]
    fn test_json_variable() {
        let expr = parse_text("x").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert_eq!(json, r#"{"Variable":"x"}"#);
    }

    // ----------------------------------------------------------------
    // All MathConstant variants
    // ----------------------------------------------------------------

    #[test]
    fn test_json_constant_pi() {
        let expr = parse_text("pi").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Constant"), "expected Constant, got: {json}");
        assert!(json.contains("Pi"), "expected Pi, got: {json}");
    }

    #[test]
    fn test_json_constant_e() {
        let expr = Expression::Constant(MathConstant::E);
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Constant"), "expected Constant, got: {json}");
        let val: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(val["Constant"], "E");
    }

    #[test]
    fn test_json_constant_i() {
        let expr = Expression::Constant(MathConstant::I);
        let json = expression_to_json(&expr).unwrap();
        let val: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(val["Constant"], "I");
    }

    #[test]
    fn test_json_constant_j() {
        let expr = Expression::Constant(MathConstant::J);
        let json = expression_to_json(&expr).unwrap();
        let val: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(val["Constant"], "J");
    }

    #[test]
    fn test_json_constant_k() {
        let expr = Expression::Constant(MathConstant::K);
        let json = expression_to_json(&expr).unwrap();
        let val: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(val["Constant"], "K");
    }

    #[test]
    fn test_json_constant_infinity() {
        let expr = Expression::Constant(MathConstant::Infinity);
        let json = expression_to_json(&expr).unwrap();
        let val: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(val["Constant"], "Infinity");
    }

    #[test]
    fn test_json_constant_neg_infinity() {
        let expr = Expression::Constant(MathConstant::NegInfinity);
        let json = expression_to_json(&expr).unwrap();
        let val: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(val["Constant"], "NegInfinity");
    }

    #[test]
    fn test_json_constant_nan() {
        let expr = Expression::Constant(MathConstant::NaN);
        let json = expression_to_json(&expr).unwrap();
        let val: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(val["Constant"], "NaN");
    }

    // ----------------------------------------------------------------
    // All BinaryOp variants
    // ----------------------------------------------------------------

    fn assert_binary_op(input: &str, expected_op: &str) {
        let expr = parse_text(input).unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Binary"), "expected Binary, got: {json}");
        assert!(
            json.contains(expected_op),
            "expected op {expected_op}, got: {json}"
        );
    }

    #[test]
    fn test_json_binary_add() {
        assert_binary_op("x + y", "Add");
    }

    #[test]
    fn test_json_binary_sub() {
        assert_binary_op("x - y", "Sub");
    }

    #[test]
    fn test_json_binary_mul() {
        assert_binary_op("x * y", "Mul");
    }

    #[test]
    fn test_json_binary_div() {
        assert_binary_op("x / y", "Div");
    }

    #[test]
    fn test_json_binary_pow() {
        assert_binary_op("x^2", "Pow");
    }

    #[test]
    fn test_json_binary_mod() {
        assert_binary_op("x % y", "Mod");
    }

    #[test]
    fn test_json_binary_plus_minus() {
        let expr = Expression::Binary {
            op: BinaryOp::PlusMinus,
            left: Box::new(Expression::Variable("x".to_string())),
            right: Box::new(Expression::Integer(1)),
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("PlusMinus"),
            "expected PlusMinus, got: {json}"
        );
    }

    // ----------------------------------------------------------------
    // Unary operators
    // ----------------------------------------------------------------

    #[test]
    fn test_json_unary_neg() {
        let expr = parse_text("-x").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("Neg") || json.contains("Unary"),
            "expected negation, got: {json}"
        );
    }

    #[test]
    fn test_json_unary_factorial() {
        let expr = Expression::Unary {
            op: UnaryOp::Factorial,
            operand: Box::new(Expression::Variable("n".to_string())),
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("Factorial"),
            "expected Factorial, got: {json}"
        );
    }

    // ----------------------------------------------------------------
    // Function call
    // ----------------------------------------------------------------

    #[test]
    fn test_json_function_call() {
        let expr = parse_text("sin(x)").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Function"), "expected Function, got: {json}");
        assert!(json.contains("sin"), "expected sin, got: {json}");
    }

    #[test]
    fn test_json_function_multi_arg() {
        let expr = parse_text("max(a, b)").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Function"), "expected Function, got: {json}");
        assert!(json.contains("max"), "expected max, got: {json}");
    }

    // ----------------------------------------------------------------
    // Calculus nodes (constructed directly to avoid parser syntax coupling)
    // ----------------------------------------------------------------

    #[test]
    fn test_json_derivative_round_trip() {
        let expr = Expression::Derivative {
            expr: Box::new(Expression::Binary {
                op: BinaryOp::Pow,
                left: Box::new(Expression::Variable("x".to_string())),
                right: Box::new(Expression::Integer(2)),
            }),
            var: "x".to_string(),
            order: 1,
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("Derivative"),
            "expected Derivative, got: {json}"
        );
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_partial_derivative_round_trip() {
        let expr = Expression::PartialDerivative {
            expr: Box::new(Expression::Variable("f".to_string())),
            var: "x".to_string(),
            order: 2,
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("PartialDerivative"),
            "expected PartialDerivative, got: {json}"
        );
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_integral_indefinite_round_trip() {
        let expr = Expression::Integral {
            integrand: Box::new(Expression::Variable("x".to_string())),
            var: "x".to_string(),
            bounds: None,
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Integral"), "expected Integral, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_integral_definite_round_trip() {
        let expr = Expression::Integral {
            integrand: Box::new(Expression::Binary {
                op: BinaryOp::Pow,
                left: Box::new(Expression::Variable("x".to_string())),
                right: Box::new(Expression::Integer(2)),
            }),
            var: "x".to_string(),
            bounds: Some(IntegralBounds {
                lower: Box::new(Expression::Integer(0)),
                upper: Box::new(Expression::Integer(1)),
            }),
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("bounds"),
            "expected bounds field, got: {json}"
        );
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_limit_round_trip() {
        let expr = Expression::Limit {
            expr: Box::new(Expression::Binary {
                op: BinaryOp::Div,
                left: Box::new(Expression::Function {
                    name: "sin".to_string(),
                    args: vec![Expression::Variable("x".to_string())],
                }),
                right: Box::new(Expression::Variable("x".to_string())),
            }),
            var: "x".to_string(),
            to: Box::new(Expression::Integer(0)),
            direction: Direction::Both,
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Limit"), "expected Limit, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_sum_round_trip() {
        let expr = Expression::Sum {
            index: "i".to_string(),
            lower: Box::new(Expression::Integer(1)),
            upper: Box::new(Expression::Variable("n".to_string())),
            body: Box::new(Expression::Variable("i".to_string())),
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Sum"), "expected Sum, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_product_round_trip() {
        let expr = Expression::Product {
            index: "k".to_string(),
            lower: Box::new(Expression::Integer(1)),
            upper: Box::new(Expression::Variable("n".to_string())),
            body: Box::new(Expression::Variable("k".to_string())),
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Product"), "expected Product, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    // ----------------------------------------------------------------
    // Linear algebra
    // ----------------------------------------------------------------

    #[test]
    fn test_json_vector_round_trip() {
        let expr = Expression::Vector(vec![
            Expression::Integer(1),
            Expression::Integer(2),
            Expression::Integer(3),
        ]);
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Vector"), "expected Vector, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_matrix_round_trip() {
        let expr = Expression::Matrix(vec![
            vec![Expression::Integer(1), Expression::Integer(0)],
            vec![Expression::Integer(0), Expression::Integer(1)],
        ]);
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Matrix"), "expected Matrix, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    // ----------------------------------------------------------------
    // Set operations
    // ----------------------------------------------------------------

    #[test]
    fn test_json_set_operation_round_trip() {
        let expr = Expression::SetOperation {
            op: SetOp::Union,
            left: Box::new(Expression::NumberSetExpr(NumberSet::Real)),
            right: Box::new(Expression::NumberSetExpr(NumberSet::Integer)),
        };
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("SetOperation"),
            "expected SetOperation, got: {json}"
        );
        assert!(json.contains("Union"), "expected Union, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_empty_set_round_trip() {
        let expr = Expression::EmptySet;
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("EmptySet"), "expected EmptySet, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    // ----------------------------------------------------------------
    // Equation and Inequality
    // ----------------------------------------------------------------

    #[test]
    fn test_json_equation_round_trip() {
        let expr = parse_text("x = 5").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(json.contains("Equation"), "expected Equation, got: {json}");
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_inequality_round_trip() {
        let expr = parse_text("x < 5").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("Inequality"),
            "expected Inequality, got: {json}"
        );
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    // ----------------------------------------------------------------
    // Nested / complex expressions
    // ----------------------------------------------------------------

    #[test]
    fn test_json_nested_expression() {
        let expr = parse_text("sin(x)^2 + cos(x)^2").unwrap();
        let json = expression_to_json(&expr).unwrap();
        assert!(
            json.contains("sin"),
            "expected sin in nested expr, got: {json}"
        );
        assert!(
            json.contains("cos"),
            "expected cos in nested expr, got: {json}"
        );
    }

    #[test]
    fn test_json_deeply_nested_round_trip() {
        // Build a deeply nested expression: ((x + 1) * (y - 2)) ^ 3
        let expr = Expression::Binary {
            op: BinaryOp::Pow,
            left: Box::new(Expression::Binary {
                op: BinaryOp::Mul,
                left: Box::new(Expression::Binary {
                    op: BinaryOp::Add,
                    left: Box::new(Expression::Variable("x".to_string())),
                    right: Box::new(Expression::Integer(1)),
                }),
                right: Box::new(Expression::Binary {
                    op: BinaryOp::Sub,
                    left: Box::new(Expression::Variable("y".to_string())),
                    right: Box::new(Expression::Integer(2)),
                }),
            }),
            right: Box::new(Expression::Integer(3)),
        };
        let json = expression_to_json(&expr).unwrap();
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    // ----------------------------------------------------------------
    // Pretty-print output
    // ----------------------------------------------------------------

    #[test]
    fn test_json_pretty_is_multiline() {
        let expr = parse_text("x + y").unwrap();
        let pretty = expression_to_json_pretty(&expr).unwrap();
        assert!(pretty.contains('\n'), "pretty JSON should be multi-line");
    }

    #[test]
    fn test_json_pretty_contains_same_data() {
        let expr = parse_text("x + y").unwrap();
        let compact = expression_to_json(&expr).unwrap();
        let pretty = expression_to_json_pretty(&expr).unwrap();
        let compact_val: serde_json::Value = serde_json::from_str(&compact).unwrap();
        let pretty_val: serde_json::Value = serde_json::from_str(&pretty).unwrap();
        assert_eq!(compact_val, pretty_val);
    }

    // ----------------------------------------------------------------
    // Round-trip tests
    // ----------------------------------------------------------------

    #[test]
    fn test_json_round_trip() {
        let expr = parse_text("2 * x + 3").unwrap();
        let json = expression_to_json(&expr).unwrap();
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_round_trip_function() {
        let expr = parse_text("sin(x)").unwrap();
        let json = expression_to_json(&expr).unwrap();
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    #[test]
    fn test_json_round_trip_nested() {
        let expr = parse_text("sin(x)^2 + cos(x)^2").unwrap();
        let json = expression_to_json(&expr).unwrap();
        let restored: Expression = serde_json::from_str(&json).unwrap();
        assert_eq!(expr, restored);
    }

    // ----------------------------------------------------------------
    // Error cases
    // ----------------------------------------------------------------

    #[test]
    fn test_json_deserialize_invalid_json_returns_error() {
        let result: Result<Expression, _> = serde_json::from_str("not valid json");
        assert!(result.is_err(), "deserializing invalid JSON should fail");
    }

    #[test]
    fn test_json_deserialize_unknown_variant_returns_error() {
        let result: Result<Expression, _> = serde_json::from_str(r#"{"UnknownVariant": 42}"#);
        assert!(result.is_err(), "deserializing unknown variant should fail");
    }
}