spanfold 0.1.1

Temporal state-window recording, comparison, and audit primitives for Rust.
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
//! Result rows, summaries, and finality DTOs.

use super::*;

/// Diagnostic severity.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum DiagnosticSeverity {
    /// Warning-level diagnostic.
    Warning,
    /// Error-level diagnostic.
    Error,
}

/// Structured comparison diagnostic.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ComparisonDiagnostic {
    /// Diagnostic code.
    pub code: String,
    /// Diagnostic severity.
    pub severity: DiagnosticSeverity,
}

impl ComparisonDiagnostic {
    /// Returns an actionable remediation hint for this diagnostic code.
    #[must_use]
    pub fn message(&self) -> &'static str {
        match self.code.as_str() {
            "MissingName" => "set a non-empty comparison plan name",
            "MissingTarget" => "configure a target source or selector",
            "MissingAgainst" => "configure at least one comparison source or selector",
            "MissingComparator" => "configure at least one comparator",
            "FutureWindowExcluded" => "advance known-at or provide an earlier-available window",
            "MissingEventTime" => {
                "provide event timestamps or choose processing-position normalization"
            }
            "TemporalAxisMismatch" => "align the plan axis with the recorded window axis",
            "SelfComparison" => "make target and comparison selectors disjoint",
            "RuntimeNonSerializablePlan" => "use serializable selectors for portable execution",
            _ => "inspect the prepared artifact and plan fields for the invalid contract",
        }
    }
}

/// Comparator summary.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ComparatorSummary {
    /// Comparator name.
    #[serde(rename = "comparatorName")]
    pub comparator_name: String,
    /// Row count.
    #[serde(rename = "rowCount")]
    pub row_count: usize,
}

/// Exported range for a row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct RowRange {
    /// Inclusive start magnitude.
    pub start: i64,
    /// Exclusive end magnitude.
    pub end: i64,
    /// Temporal axis governing the magnitudes.
    pub axis: TemporalAxis,
    /// Timestamp clock identity, when applicable.
    pub clock: Option<String>,
}

/// Exported point for transition-based rows.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct RowPoint {
    /// Point axis.
    pub axis: TemporalAxis,
    /// Scalar point magnitude.
    pub magnitude: i64,
    /// Clock identity for timestamp points.
    pub clock: Option<String>,
}

/// The active side for a disagreement segment.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum ComparisonSide {
    /// Target side.
    Target,
    /// Comparison side.
    Against,
}

/// Containment classification for one target-active segment.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum ContainmentStatus {
    /// Segment is covered by at least one comparison window.
    Contained,
    /// Segment is not covered by comparison windows.
    NotContained,
    /// Segment starts at the left edge of the target without coverage.
    LeftOverhang,
    /// Segment ends at the right edge of the target without coverage.
    RightOverhang,
}

/// Transition point used for lead/lag measurement.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum LeadLagTransition {
    /// Compare start transitions.
    Start,
    /// Compare end transitions.
    End,
}

/// Lead/lag direction.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum LeadLagDirection {
    /// Target and comparison are equal.
    Equal,
    /// Target transition occurs first.
    TargetLeads,
    /// Target transition occurs later.
    TargetLags,
    /// No comparison transition exists.
    MissingComparison,
}

/// Summary for one lead/lag comparator declaration.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct LeadLagSummary {
    /// Transition point measured.
    #[serde(rename = "transition")]
    pub transition: LeadLagTransition,
    /// Axis used for measurement.
    #[serde(rename = "axis")]
    pub axis: TemporalAxis,
    /// Configured tolerance.
    #[serde(rename = "toleranceMagnitude")]
    pub tolerance_magnitude: i64,
    /// Number of emitted rows.
    #[serde(rename = "rowCount")]
    pub row_count: usize,
    /// Count of target-lead rows.
    #[serde(rename = "targetLeadCount")]
    pub target_lead_count: usize,
    /// Count of target-lag rows.
    #[serde(rename = "targetLagCount")]
    pub target_lag_count: usize,
    /// Count of equal rows.
    #[serde(rename = "equalCount")]
    pub equal_count: usize,
    /// Count of missing-comparison rows.
    #[serde(rename = "missingComparisonCount")]
    pub missing_comparison_count: usize,
    /// Count of rows outside tolerance.
    #[serde(rename = "outsideToleranceCount")]
    pub outside_tolerance_count: usize,
    /// Minimum signed delta when any paired transitions exist.
    #[serde(rename = "minimumDeltaMagnitude")]
    pub minimum_delta_magnitude: Option<i64>,
    /// Maximum signed delta when any paired transitions exist.
    #[serde(rename = "maximumDeltaMagnitude")]
    pub maximum_delta_magnitude: Option<i64>,
}

/// Aggregate coverage summary for one window name, key, and partition.
///
/// Unlike [`CoverageRow`], which describes one aligned target segment, this
/// type contains the grouped numerator, denominator, and ratio consumers should
/// use when reporting overall coverage. The exact integer numerator and
/// denominator are the authority for aggregate coverage.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct CoverageSummary {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Denominator magnitude.
    #[serde(rename = "targetMagnitude")]
    pub target_magnitude: f64,
    /// Exact integer denominator before presentation conversion.
    #[serde(rename = "targetMagnitudeExact")]
    pub target_magnitude_exact: i128,
    /// Covered numerator magnitude.
    #[serde(rename = "coveredMagnitude")]
    pub covered_magnitude: f64,
    /// Exact integer numerator before presentation conversion.
    #[serde(rename = "coveredMagnitudeExact")]
    pub covered_magnitude_exact: i128,
    /// Covered ratio.
    #[serde(rename = "coverageRatio")]
    pub coverage_ratio: f64,
}

/// Closed family of rows emitted by comparison results.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ComparisonRowKind {
    /// Overlap rows.
    Overlap,
    /// Residual rows.
    Residual,
    /// Missing rows.
    Missing,
    /// Coverage rows.
    Coverage,
    /// Gap rows.
    Gap,
    /// Symmetric-difference rows.
    SymmetricDifference,
    /// Containment rows.
    Containment,
    /// Lead/lag rows.
    LeadLag,
    /// As-of rows.
    AsOf,
}

impl ComparisonRowKind {
    /// Returns the canonical comparison-artifact spelling for this row kind.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Overlap => "overlap",
            Self::Residual => "residual",
            Self::Missing => "missing",
            Self::Coverage => "coverage",
            Self::Gap => "gap",
            Self::SymmetricDifference => "symmetricDifference",
            Self::Containment => "containment",
            Self::LeadLag => "leadLag",
            Self::AsOf => "asOf",
        }
    }
}

impl std::fmt::Display for ComparisonRowKind {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Error returned when a comparison row-kind label is unsupported.
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
#[error("unsupported comparison row kind '{0}'")]
pub struct ComparisonRowKindParseError(String);

impl std::str::FromStr for ComparisonRowKind {
    type Err = ComparisonRowKindParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "overlap" => Ok(Self::Overlap),
            "residual" => Ok(Self::Residual),
            "missing" => Ok(Self::Missing),
            "coverage" => Ok(Self::Coverage),
            "gap" => Ok(Self::Gap),
            "symmetricDifference" | "symmetric-difference" => Ok(Self::SymmetricDifference),
            "containment" => Ok(Self::Containment),
            "leadLag" | "lead-lag" => Ok(Self::LeadLag),
            "asOf" | "asof" => Ok(Self::AsOf),
            _ => Err(ComparisonRowKindParseError(value.to_owned())),
        }
    }
}

/// Finality state for an emitted row.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum ComparisonFinality {
    /// Row is final.
    Final,
    /// Row depends on clipped open windows.
    Provisional,
    /// Row supersedes a prior version.
    Revised,
    /// Row was removed in a later snapshot.
    Retracted,
}

/// Finality metadata for a materialized row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ComparisonRowFinality {
    /// Canonical exported row family.
    #[serde(rename = "rowType")]
    pub row_type: String,
    /// Opaque deterministic identifier assigned by the producing Rust result.
    ///
    /// Consumers should preserve this value rather than recomputing it. The
    /// current scheme is scoped to the Rust artifact/schema contract and is
    /// not promised to be permanent or identical to .NET identifiers.
    #[serde(rename = "rowId")]
    pub row_id: String,
    /// Finality state.
    pub finality: ComparisonFinality,
    /// Human-readable reason.
    pub reason: String,
    /// Metadata version.
    pub version: u32,
    /// Superseded row identifier, when any.
    #[serde(rename = "supersedesRowId")]
    pub supersedes_row_id: Option<String>,
}

impl ComparisonRowFinality {
    /// Returns the typed row kind represented by this metadata.
    ///
    /// Canonical artifact spellings and the three Rust 0.1.0 JSON Lines aliases
    /// are accepted.
    pub fn row_kind(&self) -> Result<ComparisonRowKind, ComparisonRowKindParseError> {
        self.row_type.parse()
    }
}

/// Borrowed association between a typed comparison row and its result metadata.
#[derive(Clone, Copy, Debug)]
pub struct ComparisonRowWithFinality<'a, R> {
    /// Typed row emitted by the comparison.
    pub row: &'a R,
    /// Authoritative identity and finality produced for the row.
    pub metadata: &'a ComparisonRowFinality,
}

/// Error returned when result rows and finality metadata do not share the
/// canonical count/kind layout.
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
#[error(
    "inconsistent {family:?} row metadata at index {metadata_index}: expected \
     {expected_count} {expected_kind:?} records, found {actual_count}; actual \
     kind: {actual_kind:?}"
)]
pub struct ComparisonRowMetadataError {
    /// Row family being validated.
    pub family: ComparisonRowKind,
    /// Absolute index in `ComparisonResult::row_finalities` where validation failed.
    pub metadata_index: usize,
    /// Number of metadata records expected for the family.
    pub expected_count: usize,
    /// Number of metadata records observed in the family's layout span.
    pub actual_count: usize,
    /// Row kind expected at the failing metadata index.
    pub expected_kind: ComparisonRowKind,
    /// Raw row-kind label found at the index, or `None` when metadata is absent.
    pub actual_kind: Option<String>,
}

/// As-of lookup direction.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum AsOfDirection {
    /// Match the latest comparison transition at or before the target point.
    Previous,
    /// Match the earliest comparison transition at or after the target point.
    Next,
    /// Match the nearest comparison transition on either side.
    Nearest,
}

/// As-of lookup status.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum AsOfMatchStatus {
    /// Exact point match.
    Exact,
    /// Matched within tolerance.
    Matched,
    /// No match inside tolerance.
    NoMatch,
    /// A future point existed but was rejected.
    FutureRejected,
    /// Multiple eligible matches existed; selection is deterministic.
    Ambiguous,
}

/// Overlap row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct OverlapRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Overlap range.
    pub range: RowRange,
    /// Target record IDs.
    #[serde(rename = "targetRecordIds")]
    pub target_record_ids: Vec<String>,
    /// Against record IDs.
    #[serde(rename = "againstRecordIds")]
    pub against_record_ids: Vec<String>,
}

/// Residual row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ResidualRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Target-only range.
    pub range: RowRange,
    /// Target record IDs.
    #[serde(rename = "targetRecordIds")]
    pub target_record_ids: Vec<String>,
}

/// Missing row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct MissingRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Comparison-only range.
    pub range: RowRange,
    /// Against record IDs.
    #[serde(rename = "againstRecordIds")]
    pub against_record_ids: Vec<String>,
}

/// Coverage for one aligned target-active segment.
///
/// A segment is normally either wholly covered or wholly uncovered, so
/// `covered_magnitude` is normally either zero or `target_magnitude`. Use
/// [`CoverageSummary`] rather than per-row ratios for grouped overall coverage.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct CoverageRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Target segment range.
    pub range: RowRange,
    /// Segment magnitude.
    #[serde(rename = "targetMagnitude")]
    pub target_magnitude: i64,
    /// Covered magnitude.
    #[serde(rename = "coveredMagnitude")]
    pub covered_magnitude: i64,
    /// Target record IDs.
    #[serde(rename = "targetRecordIds")]
    pub target_record_ids: Vec<String>,
    /// Against record IDs.
    #[serde(rename = "againstRecordIds")]
    pub against_record_ids: Vec<String>,
}

/// Gap row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct GapRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Gap range.
    pub range: RowRange,
}

/// Symmetric-difference row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct SymmetricDifferenceRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Disagreement range.
    pub range: RowRange,
    /// Active disagreement side.
    pub side: ComparisonSide,
    /// Target record IDs.
    #[serde(rename = "targetRecordIds")]
    pub target_record_ids: Vec<String>,
    /// Against record IDs.
    #[serde(rename = "againstRecordIds")]
    pub against_record_ids: Vec<String>,
}

/// Containment row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ContainmentRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Checked range.
    pub range: RowRange,
    /// Containment status.
    pub status: ContainmentStatus,
    /// Target record IDs.
    #[serde(rename = "targetRecordIds")]
    pub target_record_ids: Vec<String>,
    /// Container record IDs.
    #[serde(rename = "containerRecordIds")]
    pub container_record_ids: Vec<String>,
}

/// Lead/lag row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct LeadLagRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Measured transition.
    pub transition: LeadLagTransition,
    /// Measurement axis.
    pub axis: TemporalAxis,
    /// Target transition point.
    #[serde(rename = "targetPoint")]
    pub target_point: RowPoint,
    /// Comparison transition point, when any.
    #[serde(rename = "comparisonPoint")]
    pub comparison_point: Option<RowPoint>,
    /// Signed target-minus-comparison delta.
    #[serde(rename = "deltaMagnitude")]
    pub delta_magnitude: Option<i64>,
    /// Configured tolerance.
    #[serde(rename = "toleranceMagnitude")]
    pub tolerance_magnitude: i64,
    /// Whether the row is inside tolerance.
    #[serde(rename = "isWithinTolerance")]
    pub is_within_tolerance: bool,
    /// Lead/lag direction.
    pub direction: LeadLagDirection,
    /// Target record ID.
    #[serde(rename = "targetRecordId")]
    pub target_record_id: String,
    /// Comparison record ID, when any.
    #[serde(rename = "comparisonRecordId")]
    pub comparison_record_id: Option<String>,
}

/// As-of row.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct AsOfRow {
    /// Window family.
    #[serde(rename = "windowName")]
    pub window_name: String,
    /// Logical key.
    pub key: String,
    /// Optional partition.
    pub partition: Option<String>,
    /// Lookup axis.
    pub axis: TemporalAxis,
    /// Lookup direction.
    pub direction: AsOfDirection,
    /// Target lookup point.
    #[serde(rename = "targetPoint")]
    pub target_point: RowPoint,
    /// Matched comparison point, when any.
    #[serde(rename = "matchedPoint")]
    pub matched_point: Option<RowPoint>,
    /// Absolute point distance, when evaluated.
    #[serde(rename = "distanceMagnitude")]
    pub distance_magnitude: Option<i64>,
    /// Configured tolerance.
    #[serde(rename = "toleranceMagnitude")]
    pub tolerance_magnitude: i64,
    /// Match status.
    pub status: AsOfMatchStatus,
    /// Target record ID.
    #[serde(rename = "targetRecordId")]
    pub target_record_id: String,
    /// Matched comparison record ID, when any.
    #[serde(rename = "matchedRecordId")]
    pub matched_record_id: Option<String>,
}

/// Canonical comparator row collections used for storage and serialization.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ComparisonRows {
    /// Overlap rows.
    pub overlap: Arc<Vec<OverlapRow>>,
    /// Residual rows.
    pub residual: Arc<Vec<ResidualRow>>,
    /// Missing rows.
    pub missing: Arc<Vec<MissingRow>>,
    /// Coverage rows.
    pub coverage: Arc<Vec<CoverageRow>>,
    /// Gap rows.
    pub gap: Arc<Vec<GapRow>>,
    /// Symmetric-difference rows.
    #[serde(rename = "symmetricDifference")]
    pub symmetric_difference: Arc<Vec<SymmetricDifferenceRow>>,
    /// Containment rows.
    pub containment: Arc<Vec<ContainmentRow>>,
    /// Lead/lag rows.
    #[serde(rename = "leadLag")]
    pub lead_lag: Arc<Vec<LeadLagRow>>,
    /// As-of rows.
    #[serde(rename = "asOf")]
    pub as_of: Arc<Vec<AsOfRow>>,
}

impl Default for ComparisonRows {
    fn default() -> Self {
        Self {
            overlap: Arc::new(Vec::new()),
            residual: Arc::new(Vec::new()),
            missing: Arc::new(Vec::new()),
            coverage: Arc::new(Vec::new()),
            gap: Arc::new(Vec::new()),
            symmetric_difference: Arc::new(Vec::new()),
            containment: Arc::new(Vec::new()),
            lead_lag: Arc::new(Vec::new()),
            as_of: Arc::new(Vec::new()),
        }
    }
}

#[derive(Default)]
pub(super) struct RowAccumulator {
    pub(super) overlap: Vec<OverlapRow>,
    pub(super) residual: Vec<ResidualRow>,
    pub(super) missing: Vec<MissingRow>,
    pub(super) coverage: Vec<CoverageRow>,
    pub(super) gap: Vec<GapRow>,
    pub(super) symmetric_difference: Vec<SymmetricDifferenceRow>,
    pub(super) containment: Vec<ContainmentRow>,
    pub(super) lead_lag: Vec<LeadLagRow>,
    pub(super) as_of: Vec<AsOfRow>,
}

impl RowAccumulator {
    pub(super) fn into_shared(self) -> ComparisonRows {
        ComparisonRows {
            overlap: Arc::new(self.overlap),
            residual: Arc::new(self.residual),
            missing: Arc::new(self.missing),
            coverage: Arc::new(self.coverage),
            gap: Arc::new(self.gap),
            symmetric_difference: Arc::new(self.symmetric_difference),
            containment: Arc::new(self.containment),
            lead_lag: Arc::new(self.lead_lag),
            as_of: Arc::new(self.as_of),
        }
    }
}

/// Structured comparison result.
///
/// [`Self::rows`] is the canonical grouped row storage. For genuine,
/// unmodified Spanfold results, [`Self::row_finalities`] is partitioned in the
/// same family order and remains parallel to row order within each family.
/// The typed `*_rows_with_finality` views validate detectable count and kind
/// corruption before exposing that association.
///
/// Independently replacing or reordering the public row or metadata fields is
/// unsupported. Validation deliberately does not rehash rows, so it cannot
/// detect metadata reordered within the same family.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct ComparisonResult {
    /// Result schema.
    pub schema: String,
    /// Schema version.
    #[serde(rename = "schemaVersion")]
    pub schema_version: u32,
    /// Artifact kind.
    pub artifact: String,
    /// Comparison plan.
    #[serde(skip)]
    pub plan: ComparisonPlan,
    /// Comparison plan name.
    #[serde(rename = "planName")]
    pub plan_name: String,
    /// Whether the result is valid.
    #[serde(rename = "isValid")]
    pub is_valid: bool,
    /// Validation and execution diagnostics.
    pub diagnostics: Vec<ComparisonDiagnostic>,
    /// Prepared artifact, when available.
    pub prepared: Option<Value>,
    /// Aligned artifact, when available.
    pub aligned: Option<Value>,
    /// Known-at point, when available.
    #[serde(rename = "knownAt")]
    pub known_at: Option<RowPoint>,
    /// Evaluation horizon, when available.
    #[serde(rename = "evaluationHorizon")]
    pub evaluation_horizon: Option<RowPoint>,
    /// Comparator summaries.
    #[serde(rename = "comparatorSummaries")]
    pub comparator_summaries: Vec<ComparatorSummary>,
    /// Coverage summaries.
    #[serde(rename = "coverageSummaries")]
    pub coverage_summaries: Vec<CoverageSummary>,
    /// Canonical result rows grouped by family.
    pub rows: ComparisonRows,
    /// Zero-copy overlap-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub overlap_rows: Arc<Vec<OverlapRow>>,
    /// Zero-copy residual-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub residual_rows: Arc<Vec<ResidualRow>>,
    /// Zero-copy missing-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub missing_rows: Arc<Vec<MissingRow>>,
    /// Zero-copy coverage-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub coverage_rows: Arc<Vec<CoverageRow>>,
    /// Zero-copy gap-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub gap_rows: Arc<Vec<GapRow>>,
    /// Zero-copy symmetric-difference-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub symmetric_difference_rows: Arc<Vec<SymmetricDifferenceRow>>,
    /// Zero-copy containment-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub containment_rows: Arc<Vec<ContainmentRow>>,
    /// Zero-copy lead/lag-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub lead_lag_rows: Arc<Vec<LeadLagRow>>,
    /// Lead/lag summaries.
    #[serde(skip)]
    pub lead_lag_summaries: Vec<LeadLagSummary>,
    /// Zero-copy as-of-row compatibility view. Prefer [`Self::rows`].
    #[serde(skip)]
    pub as_of_rows: Arc<Vec<AsOfRow>>,
    /// Authoritative row identity and finality metadata in canonical family
    /// and row order.
    #[serde(rename = "rowFinalities")]
    pub row_finalities: Vec<ComparisonRowFinality>,
    /// Serializable extension metadata.
    #[serde(rename = "extensionMetadata")]
    pub extension_metadata: Vec<ComparisonExtensionMetadata>,
}

impl ComparisonResult {
    /// Returns overlap rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn overlap_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, OverlapRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(
            self,
            ComparisonRowKind::Overlap,
            self.rows.overlap.as_slice(),
        )
    }

    /// Returns residual rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn residual_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, ResidualRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(
            self,
            ComparisonRowKind::Residual,
            self.rows.residual.as_slice(),
        )
    }

    /// Returns missing rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn missing_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, MissingRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(
            self,
            ComparisonRowKind::Missing,
            self.rows.missing.as_slice(),
        )
    }

    /// Returns coverage rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn coverage_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, CoverageRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(
            self,
            ComparisonRowKind::Coverage,
            self.rows.coverage.as_slice(),
        )
    }

    /// Returns gap rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn gap_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, GapRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(self, ComparisonRowKind::Gap, self.rows.gap.as_slice())
    }

    /// Returns symmetric-difference rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn symmetric_difference_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, SymmetricDifferenceRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(
            self,
            ComparisonRowKind::SymmetricDifference,
            self.rows.symmetric_difference.as_slice(),
        )
    }

    /// Returns containment rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn containment_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, ContainmentRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(
            self,
            ComparisonRowKind::Containment,
            self.rows.containment.as_slice(),
        )
    }

    /// Returns lead/lag rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn lead_lag_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, LeadLagRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(
            self,
            ComparisonRowKind::LeadLag,
            self.rows.lead_lag.as_slice(),
        )
    }

    /// Returns as-of rows paired with their authoritative result metadata.
    ///
    /// Returns an error if the result's metadata count/kind layout is inconsistent.
    pub fn as_of_rows_with_finality(
        &self,
    ) -> Result<
        impl ExactSizeIterator<Item = ComparisonRowWithFinality<'_, AsOfRow>>,
        ComparisonRowMetadataError,
    > {
        row_finality_pairs(self, ComparisonRowKind::AsOf, self.rows.as_of.as_slice())
    }

    fn row_family_layouts(&self) -> [(ComparisonRowKind, usize); 9] {
        [
            (ComparisonRowKind::Overlap, self.rows.overlap.len()),
            (ComparisonRowKind::Residual, self.rows.residual.len()),
            (ComparisonRowKind::Missing, self.rows.missing.len()),
            (ComparisonRowKind::Coverage, self.rows.coverage.len()),
            (ComparisonRowKind::Gap, self.rows.gap.len()),
            (
                ComparisonRowKind::SymmetricDifference,
                self.rows.symmetric_difference.len(),
            ),
            (ComparisonRowKind::Containment, self.rows.containment.len()),
            (ComparisonRowKind::LeadLag, self.rows.lead_lag.len()),
            (ComparisonRowKind::AsOf, self.rows.as_of.len()),
        ]
    }

    fn row_family_bounds(&self, kind: ComparisonRowKind) -> (usize, usize) {
        let mut start = 0;
        for (candidate, count) in self.row_family_layouts() {
            if candidate == kind {
                return (start, count);
            }
            start += count;
        }
        unreachable!("all comparison row kinds have a canonical layout")
    }

    fn validate_row_metadata_layout(&self) -> Result<(), ComparisonRowMetadataError> {
        let layouts = self.row_family_layouts();
        let expected_total = layouts.iter().map(|(_, count)| count).sum::<usize>();
        let actual_total = self.row_finalities.len();

        let mut start = 0;
        for (kind, count) in layouts {
            let end = start + count;
            let available_end = end.min(actual_total);
            let metadata = &self.row_finalities[start.min(actual_total)..available_end];
            if let Some((relative_index, actual)) = metadata
                .iter()
                .enumerate()
                .find(|(_, item)| item.row_kind().ok() != Some(kind))
            {
                return Err(ComparisonRowMetadataError {
                    family: kind,
                    metadata_index: start + relative_index,
                    expected_count: count,
                    actual_count: metadata
                        .iter()
                        .filter(|item| item.row_kind().ok() == Some(kind))
                        .count(),
                    expected_kind: kind,
                    actual_kind: Some(actual.row_type.clone()),
                });
            }

            if available_end < end {
                return Err(ComparisonRowMetadataError {
                    family: kind,
                    metadata_index: available_end,
                    expected_count: count,
                    actual_count: available_end.saturating_sub(start),
                    expected_kind: kind,
                    actual_kind: None,
                });
            }
            start = end;
        }

        if actual_total > expected_total {
            let (family, expected_count) = layouts[layouts.len() - 1];
            let family_start = expected_total - expected_count;
            return Err(ComparisonRowMetadataError {
                family,
                metadata_index: expected_total,
                expected_count,
                actual_count: actual_total - family_start,
                expected_kind: family,
                actual_kind: self
                    .row_finalities
                    .get(expected_total)
                    .map(|metadata| metadata.row_type.clone()),
            });
        }

        Ok(())
    }
}

fn row_finality_pairs<'a, R>(
    result: &'a ComparisonResult,
    kind: ComparisonRowKind,
    rows: &'a [R],
) -> Result<
    impl ExactSizeIterator<Item = ComparisonRowWithFinality<'a, R>> + 'a,
    ComparisonRowMetadataError,
> {
    result.validate_row_metadata_layout()?;
    let (start, count) = result.row_family_bounds(kind);
    debug_assert_eq!(rows.len(), count);
    let metadata = &result.row_finalities[start..start + count];
    Ok(rows
        .iter()
        .zip(metadata)
        .map(|(row, metadata)| ComparisonRowWithFinality { row, metadata }))
}

#[cfg(test)]
mod tests {
    use super::ComparisonRowKind;

    #[test]
    fn row_kinds_parse_canonical_labels_and_rust_0_1_0_aliases() {
        let cases = [
            ("overlap", ComparisonRowKind::Overlap, "overlap"),
            ("residual", ComparisonRowKind::Residual, "residual"),
            ("missing", ComparisonRowKind::Missing, "missing"),
            ("coverage", ComparisonRowKind::Coverage, "coverage"),
            ("gap", ComparisonRowKind::Gap, "gap"),
            (
                "symmetricDifference",
                ComparisonRowKind::SymmetricDifference,
                "symmetricDifference",
            ),
            (
                "symmetric-difference",
                ComparisonRowKind::SymmetricDifference,
                "symmetricDifference",
            ),
            ("containment", ComparisonRowKind::Containment, "containment"),
            ("leadLag", ComparisonRowKind::LeadLag, "leadLag"),
            ("lead-lag", ComparisonRowKind::LeadLag, "leadLag"),
            ("asOf", ComparisonRowKind::AsOf, "asOf"),
            ("asof", ComparisonRowKind::AsOf, "asOf"),
        ];

        for (label, expected_kind, canonical_label) in cases {
            let kind = label.parse::<ComparisonRowKind>().expect("known row kind");
            assert_eq!(kind, expected_kind, "label {label}");
            assert_eq!(kind.as_str(), canonical_label, "label {label}");
        }

        assert!("lead_lag".parse::<ComparisonRowKind>().is_err());
    }
}