cool-diff 0.1.0

Compact, context-preserving diffs of structured data (serde_json::Value)
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
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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
use serde_json::Value;
use snafu::Snafu;

use crate::config::{AmbiguousMatchStrategy, ArrayMatchMode, DiffConfig};
use crate::model::{ChildKind, DiffKind, DiffNode, DiffTree, PathSegment};

type Result<T, E = Error> = std::result::Result<T, E>;

/// Named constant to signify no differences were found.
const NO_DIFFERENCES: Vec<DiffNode> = vec![];

/// Errors that can occur during diffing.
#[derive(Debug, Snafu)]
pub enum Error {
    /// An expected array element is missing the distinguished key field
    /// required for key-based matching.
    #[snafu(display(
        "expected array element at path `{path}` is missing the key field `{key_field}` required for matching"
    ))]
    MissingKeyField {
        /// The dot-separated path to the array.
        path: String,

        /// The key field that was expected.
        key_field: String,
    },

    /// Multiple actual array elements matched a single expected element
    /// and the ambiguity strategy is set to Strict.
    #[snafu(display("ambiguous match at path `{path}`: {count} candidates matched"))]
    AmbiguousMatch {
        /// The dot-separated path to the array.
        path: String,

        /// The number of candidates that matched.
        count: u16,
    },
}

/// Computes a diff tree between `actual` and `expected` values.
///
/// The walk is driven by `expected`. Only paths present in the expected
/// value are compared. Fields in `actual` that have no corresponding
/// expected entry are counted as omitted but not diffed.
pub fn diff(actual: &Value, expected: &Value, config: &DiffConfig) -> Result<DiffTree> {
    // The root of the diff tree has an empty path
    let path = "";
    let roots = match diff_values(actual, expected, config, path)? {
        // e.g. actual = 42, expected = 42
        // or actual = {...}, expected = {...}
        // or actual = [...], expected = [...]
        DiffResult::Equal => NO_DIFFERENCES,
        // e.g. actual = 42, expected = "hello"
        // Root-level leaf diffs have no parent segment, so we synthesize
        // a single-element tree without a container wrapper.
        DiffResult::Leaf(_kind) => NO_DIFFERENCES,
        // e.g. actual = {a: 1, b: 2}, expected = {a: 1, b: 3}
        DiffResult::Children { nodes, .. } => nodes,
    };
    Ok(DiffTree { roots })
}

/// The result of comparing two values. Separates "what kind of diff" from
/// node construction, since the caller provides the `PathSegment`.
enum DiffResult {
    /// Values are equal.
    Equal,

    /// A leaf-level difference (scalar mismatch or type mismatch).
    /// The caller wraps this in a `DiffNode::Leaf` with the appropriate segment.
    Leaf(DiffKind),

    /// Child diff nodes from comparing container contents (objects or arrays).
    /// The caller wraps this in a `DiffNode::Container` with the appropriate segment.
    Children {
        child_kind: ChildKind,
        nodes: Vec<DiffNode>,
        omitted_count: u16,
    },
}

/// Recursively compares two values and returns a diff result.
///
/// `path` is the dot-separated path to the current position, used to look up
/// array match configuration.
fn diff_values(
    actual: &Value,
    expected: &Value,
    config: &DiffConfig,
    path: &str,
) -> Result<DiffResult> {
    // Type mismatch at the discriminant level (e.g. string vs number,
    // object vs array).
    if std::mem::discriminant(actual) != std::mem::discriminant(expected) {
        return Ok(DiffResult::Leaf(DiffKind::type_mismatch(
            actual.clone(),
            value_type_name(actual),
            expected.clone(),
            value_type_name(expected),
        )));
    }

    match (actual, expected) {
        // Scalars: direct comparison.
        (Value::Null, Value::Null) => Ok(DiffResult::Equal),
        (Value::Bool(a), Value::Bool(e)) if a == e => Ok(DiffResult::Equal),
        (Value::Number(a), Value::Number(e)) if a == e => Ok(DiffResult::Equal),
        (Value::String(a), Value::String(e)) if a == e => Ok(DiffResult::Equal),

        // Scalar mismatch (same type, different value).
        (Value::Bool(_), Value::Bool(_))
        | (Value::Number(_), Value::Number(_))
        | (Value::String(_), Value::String(_)) => Ok(DiffResult::Leaf(DiffKind::changed(
            actual.clone(),
            expected.clone(),
        ))),

        // object comparison
        (Value::Object(actual_map), Value::Object(expected_map)) => {
            diff_objects(actual_map, expected_map, config, path)
        }

        // Array comparison, dispatched by match mode
        (Value::Array(actual_arr), Value::Array(expected_arr)) => {
            diff_arrays(actual_arr, expected_arr, config, path)
        }

        _ => unreachable!("discriminant check above ensures matching types"),
    }
}

/// Compares two objects and returns a diff result.
///
/// Iterates expected keys. For each key:
/// - Missing from actual: produces a `Missing` leaf.
/// - Present in actual: recurses via `diff_values` and wraps the result.
///
/// `omitted_count` tracks actual keys not present in expected.
fn diff_objects(
    actual_map: &serde_json::Map<String, Value>,
    expected_map: &serde_json::Map<String, Value>,
    config: &DiffConfig,
    path: &str,
) -> Result<DiffResult> {
    let mut children = Vec::new();

    // Loop through the expected map pairs and then check each against the
    // actual map for the same key.
    for (key, expected_val) in expected_map {
        // Build the dot-separated path for config lookups (e.g. "spec.containers").
        // At the root level, path is empty so we avoid a leading dot.
        let child_path = if path.is_empty() {
            key.clone()
        } else {
            format!("{path}.{key}")
        };
        let segment = PathSegment::Key(key.clone());

        match actual_map.get(key) {
            // Expected key doesn't exist in actual
            None => {
                let kind = DiffKind::missing(expected_val.clone());
                children.push(DiffNode::leaf(segment, kind));
            }

            // Key exists in both, recurse to compare values
            Some(actual_val) => {
                match diff_values(actual_val, expected_val, config, &child_path)? {
                    // Values are equal, nothing to record
                    DiffResult::Equal => {}

                    // Scalar or type mismatch, wrap as a leaf node
                    DiffResult::Leaf(kind) => {
                        children.push(DiffNode::leaf(segment, kind));
                    }

                    // Nested differences in a child object or array
                    DiffResult::Children {
                        child_kind,
                        nodes,
                        omitted_count,
                    } => {
                        children.push(DiffNode::container(
                            segment,
                            child_kind,
                            omitted_count,
                            nodes,
                        ));
                    }
                }
            }
        }
    }

    // no differences
    if children.is_empty() {
        return Ok(DiffResult::Equal);
    }

    // Count of actual keys not checked because they have no corresponding
    // expected key. The renderer uses this for "# N fields omitted" markers.
    let omitted_count = actual_map.len().saturating_sub(expected_map.len()) as u16;
    Ok(DiffResult::Children {
        child_kind: ChildKind::Fields,
        nodes: children,
        omitted_count,
    })
}

/// Compares two arrays and returns a diff result.
///
/// Looks up the `ArrayMatchMode` for the current path and dispatches
/// to the appropriate matching strategy.
fn diff_arrays(
    actual_arr: &[Value],
    expected_arr: &[Value],
    config: &DiffConfig,
    path: &str,
) -> Result<DiffResult> {
    let path_config = config.match_config().config_at(path);
    let mode = path_config
        .map(|c| c.mode())
        .unwrap_or(config.default_array_mode());
    let ambiguous_strategy = path_config
        .and_then(|c| c.ambiguous_strategy())
        .unwrap_or(config.default_ambiguous_strategy());

    match mode {
        ArrayMatchMode::Index => diff_arrays_by_index(actual_arr, expected_arr, config, path),
        ArrayMatchMode::Key(key_field) => diff_arrays_by_key(
            actual_arr,
            expected_arr,
            key_field,
            ambiguous_strategy,
            config,
            path,
        ),
        ArrayMatchMode::Contains => {
            diff_arrays_by_contains(actual_arr, expected_arr, ambiguous_strategy, config, path)
        }
    }
}

/// Index-based array matching. Compares elements at the same position.
///
/// For each expected element, if the actual array has an element at that
/// index, recurse. Otherwise, produce a `Missing` leaf.
fn diff_arrays_by_index(
    actual_arr: &[Value],
    expected_arr: &[Value],
    config: &DiffConfig,
    path: &str,
) -> Result<DiffResult> {
    let mut children = Vec::new();

    // Loop through the expected array items and then check each against the
    // actual array for the element of the same index.
    for (i, expected_elem) in expected_arr.iter().enumerate() {
        let segment = PathSegment::Index(i as u16);

        match actual_arr.get(i) {
            // Expected index is beyond the actual array length
            None => {
                let kind = DiffKind::missing(expected_elem.clone());
                children.push(DiffNode::leaf(segment, kind));
            }

            // Both sides have an element at this index, recurse
            Some(actual_elem) => {
                match diff_values(actual_elem, expected_elem, config, path)? {
                    // Values are equal, nothing to record
                    DiffResult::Equal => {}

                    // Scalar or type mismatch, wrap as a leaf node
                    DiffResult::Leaf(kind) => {
                        children.push(DiffNode::leaf(segment, kind));
                    }

                    // Nested differences in a child object or array
                    DiffResult::Children {
                        child_kind,
                        nodes,
                        omitted_count,
                    } => {
                        children.push(DiffNode::container(
                            segment,
                            child_kind,
                            omitted_count,
                            nodes,
                        ));
                    }
                }
            }
        }
    }

    // no differences
    if children.is_empty() {
        return Ok(DiffResult::Equal);
    }

    // Extra elements in actual that have no corresponding expected element.
    // The renderer uses this for "# N items omitted" markers.
    let omitted_count = actual_arr.len().saturating_sub(expected_arr.len()) as u16;
    Ok(DiffResult::Children {
        child_kind: ChildKind::Items,
        nodes: children,
        omitted_count,
    })
}

/// Key-based array matching. Matches elements by a distinguished key field.
///
/// For each expected element, extracts the value of `key_field` and scans
/// the actual array for an element with the same key value. If found,
/// recurse to compare them. If not found, produce a `Missing` leaf.
fn diff_arrays_by_key(
    actual_arr: &[Value],
    expected_arr: &[Value],
    key_field: &str,
    ambiguous_strategy: &AmbiguousMatchStrategy,
    config: &DiffConfig,
    path: &str,
) -> Result<DiffResult> {
    let mut children = Vec::new();
    // Track which actual elements were matched so we can count omitted ones
    let mut matched_count: u16 = 0;

    // Loop through the expected array items and then check each against the
    // actual array for the element with the matching key.
    for expected_elem in expected_arr {
        // Extract the key value from the expected element.
        // If the expected element doesn't have the key field, that's a
        // configuration error.
        let expected_key_val = expected_elem
            .get(key_field)
            .and_then(|v| v.as_str())
            .ok_or_else(|| Error::MissingKeyField {
                path: path.to_owned(),
                key_field: key_field.to_owned(),
            })?;

        // Find the matching element in the actual array
        let candidates: Vec<&Value> = actual_arr
            .iter()
            .filter(|elem| elem.get(key_field).and_then(|v| v.as_str()) == Some(expected_key_val))
            .collect();

        match candidates.len() {
            // No actual element has this key value
            0 => {
                let kind = DiffKind::missing(expected_elem.clone());
                children.push(DiffNode::leaf(PathSegment::Unmatched, kind));
            }

            // Exactly one match, recurse to compare
            1 => {
                matched_count += 1;
                let segment = PathSegment::NamedElement {
                    match_key: key_field.to_owned(),
                    match_value: expected_key_val.to_owned(),
                };
                match diff_values(candidates[0], expected_elem, config, path)? {
                    // Values are equal, nothing to record
                    DiffResult::Equal => {}

                    // Scalar or type mismatch, wrap as a leaf node
                    DiffResult::Leaf(kind) => {
                        children.push(DiffNode::leaf(segment, kind));
                    }

                    // Nested differences in a child object or array
                    DiffResult::Children {
                        child_kind,
                        nodes,
                        omitted_count,
                    } => {
                        children.push(DiffNode::container(
                            segment,
                            child_kind,
                            omitted_count,
                            nodes,
                        ));
                    }
                }
            }

            // Multiple actual elements share the same key value
            _ => match ambiguous_strategy {
                AmbiguousMatchStrategy::Strict => {
                    return Err(Error::AmbiguousMatch {
                        path: path.to_owned(),
                        count: candidates.len() as u16,
                    });
                }

                AmbiguousMatchStrategy::BestMatch | AmbiguousMatchStrategy::Silent => {
                    matched_count += 1;
                    let segment = PathSegment::NamedElement {
                        match_key: key_field.to_owned(),
                        match_value: expected_key_val.to_owned(),
                    };
                    // Pick the candidate with the fewest diffs
                    let best =
                        pick_best_match(candidates.iter().copied(), expected_elem, config, path)?;
                    push_diff_result(&mut children, segment, best);
                }
            },
        }
    }

    // no difference
    if children.is_empty() {
        return Ok(DiffResult::Equal);
    }

    // Elements in actual that were not matched by any expected element
    let omitted_count = (actual_arr.len() as u16).saturating_sub(matched_count);
    Ok(DiffResult::Children {
        child_kind: ChildKind::Items,
        nodes: children,
        omitted_count,
    })
}

/// Contains-based array matching. Finds a matching element anywhere in the
/// actual array.
///
/// For scalars, matches by exact value equality. For objects, matches by
/// recursive subset comparison (all expected fields must match).
fn diff_arrays_by_contains(
    actual_arr: &[Value],
    expected_arr: &[Value],
    ambiguous_strategy: &AmbiguousMatchStrategy,
    _config: &DiffConfig,
    path: &str,
) -> Result<DiffResult> {
    let mut children = Vec::new();
    // Track which actual indices were matched so we can count omitted ones
    let mut matched_count: u16 = 0;

    for expected_elem in expected_arr {
        // Find all actual elements that are equal or a superset of expected.
        // Because value_contains verifies all expected fields match recursively,
        // a matched candidate will always produce Equal from diff_values, so we
        // don't need to recurse into matched elements.
        let candidates: Vec<(usize, &Value)> = actual_arr
            .iter()
            .enumerate()
            .filter(|(_, actual_elem)| value_contains(actual_elem, expected_elem))
            .collect();

        match candidates.len() {
            // No actual element matches the expected element
            0 => {
                let kind = DiffKind::missing(expected_elem.clone());
                children.push(DiffNode::leaf(PathSegment::Unmatched, kind));
            }

            // Exactly one match. No need to diff since value_contains
            // already verified all expected fields match.
            1 => {
                matched_count += 1;
            }

            // Multiple actual elements match
            _ => match ambiguous_strategy {
                AmbiguousMatchStrategy::Strict => {
                    return Err(Error::AmbiguousMatch {
                        path: path.to_owned(),
                        count: candidates.len() as u16,
                    });
                }

                // All candidates are supersets of expected, so they all
                // produce Equal. Just count any one as matched.
                AmbiguousMatchStrategy::BestMatch | AmbiguousMatchStrategy::Silent => {
                    matched_count += 1;
                }
            },
        }
    }

    if children.is_empty() {
        return Ok(DiffResult::Equal);
    }

    // Elements in actual that were not matched by any expected element
    let omitted_count = (actual_arr.len() as u16).saturating_sub(matched_count);
    Ok(DiffResult::Children {
        child_kind: ChildKind::Items,
        nodes: children,
        omitted_count,
    })
}

/// Checks whether `actual` contains all the data in `expected`.
///
/// For scalars, this is exact equality. For objects, every key in `expected`
/// must exist in `actual` with a matching value (recursively). For arrays,
/// this is exact equality (element-by-element).
fn value_contains(actual: &Value, expected: &Value) -> bool {
    match (actual, expected) {
        // Different types never match
        _ if std::mem::discriminant(actual) != std::mem::discriminant(expected) => false,

        // Scalars: exact equality
        (Value::Null, Value::Null) => true,
        (Value::Bool(a), Value::Bool(e)) => a == e,
        (Value::Number(a), Value::Number(e)) => a == e,
        (Value::String(a), Value::String(e)) => a == e,

        // Objects: every expected key must exist and match in actual
        (Value::Object(actual_map), Value::Object(expected_map)) => {
            expected_map.iter().all(|(key, expected_val)| {
                actual_map
                    .get(key)
                    .is_some_and(|actual_val| value_contains(actual_val, expected_val))
            })
        }

        // Arrays: exact element-by-element equality
        (Value::Array(a), Value::Array(e)) => a == e,

        _ => unreachable!("discriminant check above ensures matching types"),
    }
}

/// Picks the candidate with the fewest diffs against the expected element.
///
/// Returns the `DiffResult` from comparing the best candidate. If any
/// candidate produces `Equal`, that one wins immediately.
fn pick_best_match<'a>(
    candidates: impl Iterator<Item = &'a Value>,
    expected: &Value,
    config: &DiffConfig,
    path: &str,
) -> Result<DiffResult> {
    let mut best: Option<DiffResult> = None;
    // Fewest diffs wins. None means no candidate has been evaluated yet.
    let mut best_count: Option<usize> = None;

    for candidate in candidates {
        let result = diff_values(candidate, expected, config, path)?;

        // An exact match is the best possible outcome
        if matches!(result, DiffResult::Equal) {
            return Ok(result);
        }

        // Count direct child diffs as a rough proxy for "how different".
        // A recursive leaf count would be more accurate but this is good
        // enough for picking the least-bad match.
        let count = match &result {
            DiffResult::Children { nodes, .. } => nodes.len(),
            DiffResult::Leaf(_) => 1,
            DiffResult::Equal => unreachable!("handled above"),
        };

        // When a new best match is found, update it
        if best_count.is_none() || count < best_count.expect("guarded by is_none check") {
            best = Some(result);
            best_count = Some(count);
        }
    }

    Ok(best.unwrap_or(DiffResult::Equal))
}

/// Pushes a `DiffResult` as a child node, if it represents a difference.
fn push_diff_result(children: &mut Vec<DiffNode>, segment: PathSegment, result: DiffResult) {
    match result {
        // Noop, no difference to push
        DiffResult::Equal => {}

        // Scalar or type mismatch, wrap as a leaf node
        DiffResult::Leaf(kind) => {
            children.push(DiffNode::leaf(segment, kind));
        }

        // Nested differences in a child object or array
        DiffResult::Children {
            child_kind,
            nodes,
            omitted_count,
        } => {
            children.push(DiffNode::container(segment, child_kind, omitted_count, nodes));
        }
    }
}

/// Returns a human-readable type name for a JSON value.
fn value_type_name(value: &Value) -> &'static str {
    match value {
        Value::Null => "null",
        Value::Bool(_) => "bool",
        Value::Number(_) => "number",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn default_config() -> DiffConfig {
        DiffConfig::default()
    }

    // Key ordering in objects is irrelevant because serde_json::Map uses
    // BTreeMap, which normalises key order after deserialization. These
    // tests document that guarantee so a future change (e.g. switching
    // to preserve-order) would surface here.

    #[test]
    fn object_key_order_does_not_affect_equality() {
        let actual = json!({"z": 1, "a": 2, "m": 3});
        let expected = json!({"m": 3, "z": 1, "a": 2});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");
        assert!(tree.is_empty());
    }

    #[test]
    fn object_key_order_does_not_affect_diffs() {
        // Keys written in different order, but the diff should only
        // reflect the value difference, not ordering.
        let actual = json!({"z": 1, "a": 2});
        let expected = json!({"a": 99, "z": 1});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Leaf { segment, kind } = &tree.roots[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "a"));
        assert!(matches!(kind, DiffKind::Changed { .. }));
    }

    #[test]
    fn equal_objects_produce_empty_diff() {
        let actual = json!({"a": 1, "b": "hello"});
        let expected = json!({"a": 1, "b": "hello"});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");
        assert!(tree.is_empty());
    }

    #[test]
    fn scalar_changed() {
        let actual = json!({"a": {"b": {"c": "foo"}}});
        let expected = json!({"a": {"b": {"c": "bar"}}});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        // Should produce: a -> b -> c: Changed("foo" -> "bar")
        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Container {
            segment, children, ..
        } = &tree.roots[0]
        else {
            panic!("expected Container");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "a"));

        let DiffNode::Container {
            segment, children, ..
        } = &children[0]
        else {
            panic!("expected Container");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "b"));

        let DiffNode::Leaf { segment, kind } = &children[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "c"));
        assert!(matches!(kind, DiffKind::Changed { actual, expected }
            if actual == &json!("foo") && expected == &json!("bar")
        ));
    }

    #[test]
    fn missing_key() {
        let actual = json!({"a": 1});
        let expected = json!({"a": 1, "b": 2});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Leaf { segment, kind } = &tree.roots[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "b"));
        assert!(matches!(kind, DiffKind::Missing { expected } if expected == &json!(2)));
    }

    #[test]
    fn type_mismatch() {
        let actual = json!({"a": 42});
        let expected = json!({"a": "42"});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Leaf { segment, kind } = &tree.roots[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "a"));
        assert!(matches!(
            kind,
            DiffKind::TypeMismatch {
                actual_type: "number",
                expected_type: "string",
                ..
            }
        ));
    }

    #[test]
    fn omitted_count_reflects_extra_actual_keys() {
        let actual = json!({"a": 1, "b": 2, "c": 3});
        let expected = json!({"a": 99});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Leaf { kind, .. } = &tree.roots[0] else {
            panic!("expected Leaf for Changed");
        };
        assert!(matches!(kind, DiffKind::Changed { .. }));

        // The root-level Children omitted_count should be 2 (b and c not in expected).
        // But since roots are unwrapped from Children, we need to check via diff_values directly.
        let result = diff_values(&actual, &expected, &default_config(), "")
            .expect("diff_values with valid inputs");
        assert!(matches!(
            result,
            DiffResult::Children {
                omitted_count: 2,
                ..
            }
        ));
    }

    #[test]
    fn nested_missing_key() {
        let actual = json!({"a": {"x": 1}});
        let expected = json!({"a": {"x": 1, "y": 2}});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Container {
            segment,
            children,
            omitted_count,
            ..
        } = &tree.roots[0]
        else {
            panic!("expected Container");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "a"));
        assert_eq!(*omitted_count, 0);

        assert_eq!(children.len(), 1);
        let DiffNode::Leaf { segment, kind } = &children[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "y"));
        assert!(matches!(kind, DiffKind::Missing { expected } if expected == &json!(2)));
    }

    #[test]
    fn index_based_array_equal() {
        let actual = json!({"items": [1, 2, 3]});
        let expected = json!({"items": [1, 2, 3]});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");
        assert!(tree.is_empty());
    }

    #[test]
    fn index_based_array_changed() {
        let actual = json!({"items": [1, 2, 3]});
        let expected = json!({"items": [1, 99, 3]});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        // items -> Index(1): Changed(2 -> 99)
        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Container { children, .. } = &tree.roots[0] else {
            panic!("expected Container");
        };
        assert_eq!(children.len(), 1);
        let DiffNode::Leaf { segment, kind } = &children[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Index(1)));
        assert!(matches!(kind, DiffKind::Changed { actual, expected }
            if actual == &json!(2) && expected == &json!(99)
        ));
    }

    #[test]
    fn index_based_array_missing_element() {
        let actual = json!({"items": [1]});
        let expected = json!({"items": [1, 2, 3]});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        let DiffNode::Container { children, .. } = &tree.roots[0] else {
            panic!("expected Container");
        };
        assert_eq!(children.len(), 2);

        // Index 1 is missing
        let DiffNode::Leaf { segment, kind } = &children[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Index(1)));
        assert!(matches!(kind, DiffKind::Missing { expected } if expected == &json!(2)));

        // Index 2 is missing
        let DiffNode::Leaf { segment, kind } = &children[1] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Index(2)));
        assert!(matches!(kind, DiffKind::Missing { expected } if expected == &json!(3)));
    }

    #[test]
    fn index_based_array_omitted_count() {
        // actual has 5 elements, expected checks 2. Omitted count = 3.
        let actual = json!({"items": [1, 2, 3, 4, 5]});
        let expected = json!({"items": [1, 99]});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        // Root: items Container (omitted_count=0 since both objects have 1 key)
        let DiffNode::Container {
            segment,
            children,
            omitted_count,
            ..
        } = &tree.roots[0]
        else {
            panic!("expected Container for items key");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "items"));
        assert_eq!(*omitted_count, 3);
        // Only one child: Index(1) Changed(2 -> 99). Index(0) is equal.
        assert_eq!(children.len(), 1);
        assert!(matches!(
            &children[0],
            DiffNode::Leaf {
                segment: PathSegment::Index(1),
                kind: DiffKind::Changed { .. },
            }
        ));
    }

    fn config_with_key_at(path: &str, key: &str) -> DiffConfig {
        use crate::config::{ArrayMatchConfig, ArrayMatchMode, MatchConfig};
        DiffConfig::new().with_match_config(MatchConfig::new().with_config_at(
            path,
            ArrayMatchConfig::new(ArrayMatchMode::Key(key.to_owned())),
        ))
    }

    #[test]
    fn key_based_array_equal() {
        let config = config_with_key_at("items", "name");
        let actual = json!({"items": [{"name": "a", "val": 1}, {"name": "b", "val": 2}]});
        let expected = json!({"items": [{"name": "a", "val": 1}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");
        assert!(tree.is_empty());
    }

    #[test]
    fn key_based_array_changed() {
        let config = config_with_key_at("items", "name");
        let actual = json!({"items": [{"name": "FOO", "value": "bar"}]});
        let expected = json!({"items": [{"name": "FOO", "value": "baz"}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");

        // items -> NamedElement(name=FOO) -> value: Changed("bar" -> "baz")
        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Container { children, .. } = &tree.roots[0] else {
            panic!("expected Container for items");
        };
        assert_eq!(children.len(), 1);
        let DiffNode::Container {
            segment, children, ..
        } = &children[0]
        else {
            panic!("expected Container for named element");
        };
        assert!(
            matches!(segment, PathSegment::NamedElement { match_key, match_value }
                if match_key == "name" && match_value == "FOO"
            )
        );
        assert_eq!(children.len(), 1);
        let DiffNode::Leaf { segment, kind } = &children[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Key(k) if k == "value"));
        assert!(matches!(kind, DiffKind::Changed { actual, expected }
            if actual == &json!("bar") && expected == &json!("baz")
        ));
    }

    #[test]
    fn key_based_array_missing_element() {
        let config = config_with_key_at("items", "name");
        let actual = json!({"items": [{"name": "a"}]});
        let expected = json!({"items": [{"name": "missing"}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");

        let DiffNode::Container { children, .. } = &tree.roots[0] else {
            panic!("expected Container for items");
        };
        assert_eq!(children.len(), 1);
        let DiffNode::Leaf { segment, kind } = &children[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Unmatched));
        assert!(matches!(kind, DiffKind::Missing { .. }));
    }

    #[test]
    fn key_based_array_omitted_count() {
        let config = config_with_key_at("items", "name");
        let actual = json!({"items": [{"name": "a"}, {"name": "b"}, {"name": "c"}]});
        let expected = json!({"items": [{"name": "b"}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");

        // No diffs (b matches), but omitted count should be 2 (a and c)
        // Since there are no diffs, the tree should be empty... but omitted_count
        // is only set when there ARE diffs. With all equal, we get Equal.
        assert!(tree.is_empty());

        // Check omitted count via diff_values directly with a diff present
        let actual = json!({"items": [{"name": "a"}, {"name": "b", "x": 1}, {"name": "c"}]});
        let expected = json!({"items": [{"name": "b", "x": 99}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");

        let DiffNode::Container {
            children: _children,
            omitted_count,
            ..
        } = &tree.roots[0]
        else {
            panic!("expected Container for items");
        };
        // 3 actual elements, 1 matched = 2 omitted
        assert_eq!(*omitted_count, 2);
    }

    fn config_with_contains_at(path: &str) -> DiffConfig {
        use crate::config::{ArrayMatchConfig, ArrayMatchMode, MatchConfig};
        DiffConfig::new().with_match_config(
            MatchConfig::new()
                .with_config_at(path, ArrayMatchConfig::new(ArrayMatchMode::Contains)),
        )
    }

    #[test]
    fn contains_array_scalar_equal() {
        let config = config_with_contains_at("items");
        let actual = json!({"items": ["a", "b", "c"]});
        let expected = json!({"items": ["b"]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");
        assert!(tree.is_empty());
    }

    #[test]
    fn contains_array_object_subset_equal() {
        let config = config_with_contains_at("items");
        let actual = json!({"items": [{"a": 1, "b": 2}, {"c": 3}]});
        let expected = json!({"items": [{"a": 1}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");
        assert!(tree.is_empty());
    }

    #[test]
    fn contains_array_missing_element() {
        let config = config_with_contains_at("items");
        let actual = json!({"items": ["a", "b"]});
        let expected = json!({"items": ["x"]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");

        let DiffNode::Container { children, .. } = &tree.roots[0] else {
            panic!("expected Container for items");
        };
        assert_eq!(children.len(), 1);
        let DiffNode::Leaf { segment, kind } = &children[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(segment, PathSegment::Unmatched));
        assert!(matches!(kind, DiffKind::Missing { expected } if expected == &json!("x")));
    }

    #[test]
    fn contains_array_match_not_at_first_position() {
        let config = config_with_contains_at("items");
        // {b: 1} matches the second element (index 1), not the first.
        // Since all expected fields match, the result is equal.
        let actual = json!({"items": [{"a": 1}, {"b": 1}]});
        let expected = json!({"items": [{"b": 1}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");
        assert!(tree.is_empty());
    }

    #[test]
    fn contains_array_omitted_count() {
        let config = config_with_contains_at("items");
        let actual = json!({"items": ["a", "b", "c"]});
        let expected = json!({"items": ["x"]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");

        let DiffNode::Container { omitted_count, .. } = &tree.roots[0] else {
            panic!("expected Container for items");
        };
        // 0 matched out of 3 actual = 3 omitted
        assert_eq!(*omitted_count, 3);
    }

    fn config_with_key_and_strategy(
        path: &str,
        key: &str,
        strategy: AmbiguousMatchStrategy,
    ) -> DiffConfig {
        use crate::config::{ArrayMatchConfig, ArrayMatchMode, MatchConfig};
        DiffConfig::new().with_match_config(
            MatchConfig::new().with_config_at(
                path,
                ArrayMatchConfig::new(ArrayMatchMode::Key(key.to_owned()))
                    .with_ambiguous_strategy(strategy),
            ),
        )
    }

    fn config_with_contains_and_strategy(
        path: &str,
        strategy: AmbiguousMatchStrategy,
    ) -> DiffConfig {
        use crate::config::{ArrayMatchConfig, ArrayMatchMode, MatchConfig};
        DiffConfig::new().with_match_config(MatchConfig::new().with_config_at(
            path,
            ArrayMatchConfig::new(ArrayMatchMode::Contains).with_ambiguous_strategy(strategy),
        ))
    }

    #[test]
    fn ambiguous_key_best_match_picks_fewest_diffs() {
        // Two actual elements share name "FOO". One has value "bar" (1 diff),
        // the other has value "baz" (1 diff). Best match picks the one with
        // the closest value to expected.
        let config =
            config_with_key_and_strategy("items", "name", AmbiguousMatchStrategy::BestMatch);
        let actual = json!({"items": [
            {"name": "FOO", "value": "wrong"},
            {"name": "FOO", "value": "almost"}
        ]});
        let expected = json!({"items": [{"name": "FOO", "value": "almost"}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");

        // The second candidate is an exact match, so no diff
        assert!(tree.is_empty());
    }

    #[test]
    fn ambiguous_key_best_match_with_diffs() {
        let config =
            config_with_key_and_strategy("items", "name", AmbiguousMatchStrategy::BestMatch);
        // Two candidates, both differ but second has fewer diffs
        let actual = json!({"items": [
            {"name": "FOO", "a": 1, "b": 2},
            {"name": "FOO", "a": 99, "b": 99}
        ]});
        let expected = json!({"items": [{"name": "FOO", "a": 1, "b": 99}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");

        // First candidate: b differs (1 diff). Second: a differs (1 diff).
        // Both have 1 diff, so first one seen wins. First has b: Changed.
        assert!(!tree.is_empty());
        let DiffNode::Container { children, .. } = &tree.roots[0] else {
            panic!("expected Container for items");
        };
        assert_eq!(children.len(), 1);
    }

    #[test]
    fn ambiguous_contains_best_match() {
        // Two actual elements are both supersets of expected.
        // BestMatch should accept without error.
        let config = config_with_contains_and_strategy("items", AmbiguousMatchStrategy::BestMatch);
        let actual = json!({"items": [
            {"a": 1, "b": 2},
            {"a": 1, "c": 3}
        ]});
        let expected = json!({"items": [{"a": 1}]});
        let tree = diff(&actual, &expected, &config).expect("diff with valid inputs");
        assert!(tree.is_empty());
    }

    // Null vs empty collection: YAML `foo:` (null) vs `foo: []` or `foo: {}`
    // are different JSON types and should produce TypeMismatch.

    #[test]
    fn null_vs_empty_array() {
        let actual = json!({"foo": null});
        let expected = json!({"foo": []});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Leaf { kind, .. } = &tree.roots[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(
            kind,
            DiffKind::TypeMismatch {
                actual_type: "null",
                expected_type: "array",
                ..
            }
        ));
    }

    #[test]
    fn null_vs_empty_object() {
        let actual = json!({"bar": null});
        let expected = json!({"bar": {}});
        let tree = diff(&actual, &expected, &default_config()).expect("diff with valid inputs");

        assert_eq!(tree.roots.len(), 1);
        let DiffNode::Leaf { kind, .. } = &tree.roots[0] else {
            panic!("expected Leaf");
        };
        assert!(matches!(
            kind,
            DiffKind::TypeMismatch {
                actual_type: "null",
                expected_type: "object",
                ..
            }
        ));
    }

    // Error cases

    #[test]
    fn missing_key_field_returns_error() {
        let config = config_with_key_at("items", "name");
        let actual = json!({"items": [{"name": "a"}]});
        // Expected element is missing the "name" key field
        let expected = json!({"items": [{"value": "foo"}]});
        let result = diff(&actual, &expected, &config);

        let Err(err) = result else {
            panic!("expected an error");
        };
        assert!(matches!(err, Error::MissingKeyField { .. }));
        assert!(err.to_string().contains("missing the key field `name`"));
    }

    #[test]
    fn strict_ambiguous_key_match_returns_error() {
        let config = config_with_key_and_strategy("items", "name", AmbiguousMatchStrategy::Strict);
        let actual = json!({"items": [
            {"name": "FOO", "value": "a"},
            {"name": "FOO", "value": "b"}
        ]});
        let expected = json!({"items": [{"name": "FOO", "value": "a"}]});
        let result = diff(&actual, &expected, &config);

        let Err(err) = result else {
            panic!("expected an error");
        };
        assert!(matches!(err, Error::AmbiguousMatch { count: 2, .. }));
    }

    #[test]
    fn strict_ambiguous_contains_match_returns_error() {
        let config = config_with_contains_and_strategy("items", AmbiguousMatchStrategy::Strict);
        let actual = json!({"items": [
            {"a": 1, "b": 2},
            {"a": 1, "c": 3}
        ]});
        let expected = json!({"items": [{"a": 1}]});
        let result = diff(&actual, &expected, &config);

        let Err(err) = result else {
            panic!("expected an error");
        };
        assert!(matches!(err, Error::AmbiguousMatch { count: 2, .. }));
    }
}