mediation 0.3.0

Telecom usage mediation as a pure Rust library: normalise, deduplicate, correlate and reconcile CDRs from mobile and fixed-line networks, with a conservation law that proves no record was lost. Zero I/O, no clock, integer quantities.
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
//! Partial records: reconstruct, never invent.
//!
//! A long session does not produce one record. It produces a run of **partial**
//! records, closed and reopened whenever a volume limit, a time limit or a
//! tariff boundary is reached. Billing the session means putting the run back
//! together.
//!
//! # This crate reassembles; it does not estimate
//!
//! Some metering domains are authorised to substitute a value for a reading
//! that was lost. Telecoms has no such authorisation: usage that cannot be
//! evidenced is not billable. So this module puts together the partials that
//! genuinely arrived and **reports** the ones that did not. It never synthesises
//! a quantity, and there is no API here that would let it.
//!
//! # FQPC and RPC are not interchangeable
//!
//! 3GPP defines two partial-record formats:
//!
//! - a **Fully Qualified Partial CDR** carries the complete set of fields and
//!   can be read standalone;
//! - a **Reduced Partial CDR** carries only mandatory fields plus what *changed*
//!   since the previous partial.
//!
//! **The first partial of a run must be fully qualified** (TS 32.240), and it is
//! the basis from which the reduced ones that follow are completed. Reading an
//! RPC's absent field as zero is therefore a correctness bug: the field did not
//! become zero, it did not change. An RPC whose predecessor is missing is
//! **unresolvable** — held, not billed, not silently zero-filled.
//!
//! ```rust
//! use mediation::{PartialFormat, PartialInfo, PartialRun, RecordClosureCause, Reassembly};
//!
//! // A run whose first record is reduced cannot be completed from itself.
//! let orphaned = PartialRun::from_parts(vec![
//!     PartialInfo::new(1, PartialFormat::Reduced, RecordClosureCause::VolumeLimit),
//! ]);
//! assert!(matches!(orphaned.assess(), Reassembly::Orphan { .. }));
//!
//! // A run whose first record has not arrived yet is waiting, not broken.
//! let waiting = PartialRun::from_parts(vec![
//!     PartialInfo::new(2, PartialFormat::Reduced, RecordClosureCause::VolumeLimit),
//! ]);
//! assert_eq!(waiting.assess().missing(), &[1]);
//! assert!(waiting.assess().may_still_complete());
//! ```
//!
//! # Why a partial exists tells you whether it is suspicious
//!
//! The closure cause (3GPP TS 32.298 `CauseForRecClosing`) distinguishes a run
//! that ended because the session ended from one that ended because a node
//! fell over:
//!
//! | Cause | Meaning | Missing successor is… |
//! |---|---|---|
//! | volume / time limit | container filled or aged out | unexplained |
//! | tariff time change | crossed a priced boundary | unexplained |
//! | serving node / RAT change | serving conditions moved | unexplained |
//! | max change conditions | reporting limit reached | unexplained |
//! | **abnormal release** | node failure, forced close | **expected** |
//! | management intervention | operator action | **expected** |
//!
//! A run ending on an abnormal release with no last record reconciles cleanly;
//! the same hole after a routine trigger is unexplained loss and must be
//! escalated. Modelling the cause is what makes that distinction available at
//! all — see [`PartialRun::is_explained_loss`].

use std::fmt;
use std::str::FromStr;

use crate::error::ParseError;

// ── RecordClosureCause ───────────────────────────────────────────────────────

/// Why a record was closed (3GPP TS 32.298 `CauseForRecClosing`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RecordClosureCause {
    /// The session ended normally.
    NormalRelease,
    /// A partial record: the session continued past this one.
    PartialRecord,
    /// The session was torn down abnormally — a node failed or forced a close.
    AbnormalRelease,
    /// The reported volume limit was reached.
    VolumeLimit,
    /// The reported time limit was reached.
    TimeLimit,
    /// A tariff period boundary was crossed.
    TariffTimeChange,
    /// The serving node changed.
    ServingNodeChange,
    /// The maximum number of change conditions was reached.
    MaxChangeConditions,
    /// An operator intervened.
    ManagementIntervention,
    /// The radio access technology changed.
    RatChange,
    /// The subscriber's reported time zone changed.
    TimeZoneChange,
    /// The serving network changed.
    ServingPlmnChange,
}

impl RecordClosureCause {
    /// Every variant, for exhaustive iteration in tests and mappings.
    pub const ALL: &'static [Self] = &[
        Self::NormalRelease,
        Self::PartialRecord,
        Self::AbnormalRelease,
        Self::VolumeLimit,
        Self::TimeLimit,
        Self::TariffTimeChange,
        Self::ServingNodeChange,
        Self::MaxChangeConditions,
        Self::ManagementIntervention,
        Self::RatChange,
        Self::TimeZoneChange,
        Self::ServingPlmnChange,
    ];

    /// The canonical codes, in the order of [`ALL`](Self::ALL).
    pub const CODES: &'static [&'static str] = &[
        "NORMAL_RELEASE",
        "PARTIAL_RECORD",
        "ABNORMAL_RELEASE",
        "VOLUME_LIMIT",
        "TIME_LIMIT",
        "TARIFF_TIME_CHANGE",
        "SERVING_NODE_CHANGE",
        "MAX_CHANGE_CONDITIONS",
        "MANAGEMENT_INTERVENTION",
        "RAT_CHANGE",
        "TIME_ZONE_CHANGE",
        "SERVING_PLMN_CHANGE",
    ];

    /// `true` when the closure ends the session rather than continuing it.
    #[must_use]
    pub const fn is_terminal(self) -> bool {
        matches!(
            self,
            Self::NormalRelease | Self::AbnormalRelease | Self::ManagementIntervention
        )
    }

    /// `true` when a run ending here **without** a last record is explained.
    ///
    /// A node that failed or was intervened in cannot be expected to have
    /// emitted its successor; a volume limit can. The distinction decides
    /// whether a hole reconciles cleanly or escalates.
    #[must_use]
    pub const fn explains_a_missing_successor(self) -> bool {
        matches!(self, Self::AbnormalRelease | Self::ManagementIntervention)
    }

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::NormalRelease => "NORMAL_RELEASE",
            Self::PartialRecord => "PARTIAL_RECORD",
            Self::AbnormalRelease => "ABNORMAL_RELEASE",
            Self::VolumeLimit => "VOLUME_LIMIT",
            Self::TimeLimit => "TIME_LIMIT",
            Self::TariffTimeChange => "TARIFF_TIME_CHANGE",
            Self::ServingNodeChange => "SERVING_NODE_CHANGE",
            Self::MaxChangeConditions => "MAX_CHANGE_CONDITIONS",
            Self::ManagementIntervention => "MANAGEMENT_INTERVENTION",
            Self::RatChange => "RAT_CHANGE",
            Self::TimeZoneChange => "TIME_ZONE_CHANGE",
            Self::ServingPlmnChange => "SERVING_PLMN_CHANGE",
        }
    }
}

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

impl FromStr for RecordClosureCause {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let t = s.trim().to_ascii_uppercase();
        Self::ALL
            .iter()
            .find(|c| c.as_str() == t)
            .copied()
            .ok_or_else(|| ParseError::one_of("RecordClosureCause", s, Self::CODES))
    }
}

// ── PartialFormat ────────────────────────────────────────────────────────────

/// Whether a partial record can be read on its own.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PartialFormat {
    /// A Fully Qualified Partial CDR — complete, readable standalone.
    ///
    /// The first partial of a run is always this (3GPP TS 32.240), and it is
    /// what the reduced records that follow are completed against.
    FullyQualified,
    /// A Reduced Partial CDR — mandatory fields plus **only what changed**.
    ///
    /// Unusable on its own. An absent field here means *unchanged*, never zero,
    /// and reading it as zero is the classic reassembly bug.
    Reduced,
}

impl PartialFormat {
    /// Every variant, for exhaustive iteration in tests and mappings.
    pub const ALL: &'static [Self] = &[Self::FullyQualified, Self::Reduced];

    /// The canonical codes, in the order of [`ALL`](Self::ALL).
    pub const CODES: &'static [&'static str] = &["FULLY_QUALIFIED", "REDUCED"];

    /// `true` when the record can be interpreted without its predecessor.
    #[must_use]
    pub const fn is_self_contained(self) -> bool {
        matches!(self, Self::FullyQualified)
    }

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::FullyQualified => "FULLY_QUALIFIED",
            Self::Reduced => "REDUCED",
        }
    }
}

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

impl FromStr for PartialFormat {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let t = s.trim().to_ascii_uppercase();
        Self::ALL
            .iter()
            .find(|c| c.as_str() == t)
            .copied()
            .ok_or_else(|| ParseError::one_of("PartialFormat", s, Self::CODES))
    }
}

// ── PartialInfo ──────────────────────────────────────────────────────────────

/// What one record says about its place in a run of partials.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PartialInfo {
    /// The record's position in the run.
    ///
    /// 3GPP numbers these from 1 and increments per partial of one session.
    pub sequence: u32,
    /// Whether the record can be read without its predecessor.
    pub format: PartialFormat,
    /// Why this record was closed.
    pub cause: RecordClosureCause,
}

impl PartialInfo {
    /// A record's place in a run.
    #[must_use]
    pub const fn new(sequence: u32, format: PartialFormat, cause: RecordClosureCause) -> Self {
        Self {
            sequence,
            format,
            cause,
        }
    }

    /// `true` when this record closes the run.
    #[must_use]
    pub const fn is_last(self) -> bool {
        self.cause.is_terminal()
    }

    /// `true` when this record is the first of a run.
    ///
    /// Positional, not structural: a run's first record must also be fully
    /// qualified, which [`PartialRun::assess`] checks separately so the two
    /// failures are reported apart.
    #[must_use]
    pub const fn is_first(self) -> bool {
        self.sequence == 1
    }
}

impl fmt::Display for PartialInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "#{} {} {}", self.sequence, self.format, self.cause)
    }
}

// ── Reassembly ───────────────────────────────────────────────────────────────

/// What a run of partials adds up to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reassembly {
    /// Every partial from the first to a terminal one is present.
    Complete {
        /// How many records the run holds.
        count: usize,
    },
    /// The run has holes — its first record among them, possibly — or has
    /// not ended.
    Incomplete {
        /// Sequence numbers present in the run, ascending.
        have: Vec<u32>,
        /// Sequence numbers missing between the ones present.
        ///
        /// Truncated to [`PartialRun::MAX_REPORTED_MISSING`]; the exact figure
        /// is always `missing_count`.
        missing: Vec<u32>,
        /// How many sequence numbers are missing in total.
        ///
        /// Separate from `missing.len()` because the list is bounded and this
        /// is not: one corrupt sequence field claiming partial 4 294 967 295
        /// would otherwise ask for a sixteen-gigabyte list of holes, and a
        /// decoder that allocates on a number an attacker chose is a denial of
        /// service with a domain name on it.
        missing_count: u64,
        /// `true` when a terminal record has arrived.
        ///
        /// A run with no holes and no terminal record is not broken — it is
        /// still open, and more partials are expected.
        terminated: bool,
    },
    /// The run can never be completed, however long it waits.
    ///
    /// Its first record is reduced and therefore has no basis to be completed
    /// against, or a partial carries sequence 0, which no run contains.
    Orphan {
        /// Why the run cannot be reassembled.
        reason: OrphanReason,
        /// Sequence numbers present in the run, ascending.
        have: Vec<u32>,
    },
    /// No partials at all.
    Empty,
}

impl Reassembly {
    /// `true` when the run is whole and may be billed.
    #[must_use]
    pub const fn is_complete(&self) -> bool {
        matches!(self, Self::Complete { .. })
    }

    /// `true` when more records may still arrive to finish the run.
    ///
    /// An orphan cannot be finished by waiting. A run whose first record has
    /// not arrived can: it is [`Incomplete`](Self::Incomplete), with `1` among
    /// its missing sequence numbers.
    #[must_use]
    pub const fn may_still_complete(&self) -> bool {
        matches!(self, Self::Incomplete { .. } | Self::Empty)
    }

    /// The sequence numbers still outstanding, up to
    /// [`PartialRun::MAX_REPORTED_MISSING`] of them.
    #[must_use]
    pub fn missing(&self) -> &[u32] {
        match self {
            Self::Incomplete { missing, .. } => missing,
            _ => &[],
        }
    }

    /// How many sequence numbers are outstanding, exactly.
    ///
    /// Always the true figure, even where [`missing`](Self::missing) was
    /// truncated.
    #[must_use]
    pub const fn missing_count(&self) -> u64 {
        match self {
            Self::Incomplete { missing_count, .. } => *missing_count,
            _ => 0,
        }
    }

    /// `true` when [`missing`](Self::missing) lists fewer holes than there are.
    #[must_use]
    pub fn missing_is_truncated(&self) -> bool {
        self.missing_count() > self.missing().len() as u64
    }
}

impl fmt::Display for Reassembly {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Complete { count } => write!(f, "COMPLETE ({count} partials)"),
            Self::Incomplete {
                have,
                missing_count,
                terminated,
                ..
            } => write!(
                f,
                "INCOMPLETE ({} present, {missing_count} missing, {})",
                have.len(),
                if *terminated { "terminated" } else { "open" }
            ),
            Self::Orphan { reason, have } => {
                write!(f, "ORPHAN:{reason} ({} present)", have.len())
            }
            Self::Empty => f.write_str("EMPTY"),
        }
    }
}

/// Why a run of partials cannot be reassembled at all.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum OrphanReason {
    /// The run's sequence 1 is present only as a reduced record.
    ///
    /// The first partial must be fully qualified. A reduced one has no basis
    /// to complete itself from, and treating its absent fields as zero would
    /// invent the session's parameters.
    FirstRecordNotFullyQualified,
    /// A partial carries sequence 0.
    ///
    /// 3GPP numbers partials from 1, so no run can place it.
    InvalidSequence,
}

impl OrphanReason {
    /// Every variant, for exhaustive iteration in tests and mappings.
    pub const ALL: &'static [Self] = &[Self::FirstRecordNotFullyQualified, Self::InvalidSequence];

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::FirstRecordNotFullyQualified => "FIRST_RECORD_NOT_FULLY_QUALIFIED",
            Self::InvalidSequence => "INVALID_SEQUENCE",
        }
    }
}

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

// ── PartialRun ───────────────────────────────────────────────────────────────

/// The partials of one session, gathered so they can be assessed together.
///
/// Order-independent: partials are sorted by sequence when assessed, so a run
/// delivered out of order is not mistaken for a run with holes.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PartialRun(Vec<PartialInfo>);

impl PartialRun {
    /// How many missing sequence numbers [`assess`](Self::assess) will list.
    ///
    /// The count is always exact; only the identities are capped. A partial's
    /// sequence number arrives from the wire, so the size of the hole it
    /// implies is attacker-chosen — the same reasoning as
    /// [`SequenceTracker::observe`](crate::SequenceTracker::observe)'s bound.
    pub const MAX_REPORTED_MISSING: usize = 10_000;

    /// An empty run.
    #[must_use]
    pub const fn new() -> Self {
        Self(Vec::new())
    }

    /// A run from the partials that have arrived, in any order.
    #[must_use]
    pub fn from_parts(parts: Vec<PartialInfo>) -> Self {
        Self(parts)
    }

    /// Add a partial to the run.
    pub fn push(&mut self, part: PartialInfo) {
        self.0.push(part);
    }

    /// How many partials have arrived.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// `true` when nothing has arrived.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// The partials, as supplied.
    #[must_use]
    pub fn as_slice(&self) -> &[PartialInfo] {
        &self.0
    }

    /// The closure cause of the highest-numbered partial present.
    ///
    /// What [`PartialRun::is_explained_loss`] needs in order to decide whether
    /// a run that simply stops is accounted for.
    #[must_use]
    pub fn last_cause(&self) -> Option<RecordClosureCause> {
        self.0.iter().max_by_key(|p| p.sequence).map(|p| p.cause)
    }

    /// `true` when the run simply stops, and how it closed accounts for that.
    ///
    /// A run whose highest partial reports an abnormal release or a management
    /// intervention has an explained ending: the node could not be expected to
    /// emit a successor. The same shape after a routine trigger — a volume or
    /// time limit — is unexplained loss and must be escalated.
    ///
    /// A **hole** is never explained away: an abnormal ending accounts for a
    /// missing successor, not for a missing middle.
    ///
    /// A method on the run rather than on [`Reassembly`], because the closure
    /// cause lives here and threading it through the assessment made every
    /// caller carry both halves of one question.
    #[must_use]
    pub fn is_explained_loss(&self) -> bool {
        match self.assess() {
            Reassembly::Incomplete {
                missing_count,
                terminated,
                ..
            } => {
                missing_count == 0
                    && !terminated
                    && self
                        .last_cause()
                        .is_some_and(RecordClosureCause::explains_a_missing_successor)
            }
            _ => false,
        }
    }

    /// Assess what the run adds up to.
    ///
    /// Duplicated sequence numbers are collapsed: two copies of partial 3 are
    /// one partial 3, and recognising the retransmission is
    /// [`crate::dedup`]'s job rather than this one's.
    ///
    /// A run whose sequence 1 has not arrived is
    /// [`Incomplete`](Reassembly::Incomplete) with `1` missing: the first
    /// record may still come. Where two copies of sequence 1 differ in format,
    /// the fully qualified one is the run's basis, whatever order they came in.
    #[must_use]
    pub fn assess(&self) -> Reassembly {
        if self.0.is_empty() {
            return Reassembly::Empty;
        }

        let mut have: Vec<u32> = self.0.iter().map(|p| p.sequence).collect();
        have.sort_unstable();
        have.dedup();

        if have.first() == Some(&0) {
            return Reassembly::Orphan {
                reason: OrphanReason::InvalidSequence,
                have,
            };
        }
        let mut firsts = self.0.iter().filter(|p| p.is_first()).peekable();
        if firsts.peek().is_some() && !firsts.any(|p| p.format.is_self_contained()) {
            return Reassembly::Orphan {
                reason: OrphanReason::FirstRecordNotFullyQualified,
                have,
            };
        }

        let Some(&highest) = have.last() else {
            return Reassembly::Empty;
        };
        // The count is exact; the *list* is bounded. `have` is sorted and
        // deduplicated, so a hole is the distance between neighbours and no
        // scan of the whole range is needed — which matters because `highest`
        // comes off the wire and may be four billion.
        let missing_count = u64::from(highest) - have.len() as u64;
        let mut missing: Vec<u32> = Vec::new();
        if missing_count > 0 {
            let mut expected = 1u32;
            for present in &have {
                while expected < *present && missing.len() < Self::MAX_REPORTED_MISSING {
                    missing.push(expected);
                    expected += 1;
                }
                if missing.len() >= Self::MAX_REPORTED_MISSING {
                    break;
                }
                expected = present.saturating_add(1);
            }
        }
        let terminated = self.0.iter().any(|p| p.is_last());

        if missing_count == 0 && terminated {
            return Reassembly::Complete { count: have.len() };
        }
        Reassembly::Incomplete {
            have,
            missing,
            missing_count,
            terminated,
        }
    }
}

impl FromIterator<PartialInfo> for PartialRun {
    fn from_iter<I: IntoIterator<Item = PartialInfo>>(iter: I) -> Self {
        Self(iter.into_iter().collect())
    }
}

// ── Serde ────────────────────────────────────────────────────────────────────

#[cfg(feature = "serde")]
mod serde_impl {
    use super::{OrphanReason, PartialFormat, PartialInfo, RecordClosureCause};
    use serde::de::{self, Visitor};
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::fmt;

    macro_rules! string_serde {
        ($ty:ty, $expecting:literal) => {
            impl Serialize for $ty {
                fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                    serializer.serialize_str(self.as_str())
                }
            }

            impl<'de> Deserialize<'de> for $ty {
                fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                    struct V;
                    impl Visitor<'_> for V {
                        type Value = $ty;
                        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                            f.write_str($expecting)
                        }
                        fn visit_str<E: de::Error>(self, v: &str) -> Result<$ty, E> {
                            v.parse().map_err(de::Error::custom)
                        }
                    }
                    deserializer.deserialize_str(V)
                }
            }
        };
    }

    string_serde!(RecordClosureCause, "a record closure cause code");
    string_serde!(PartialFormat, "a partial record format code");

    impl Serialize for OrphanReason {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.serialize_str(self.as_str())
        }
    }

    impl<'de> Deserialize<'de> for OrphanReason {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            struct V;
            impl Visitor<'_> for V {
                type Value = OrphanReason;
                fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    f.write_str("an orphan reason code")
                }
                fn visit_str<E: de::Error>(self, v: &str) -> Result<OrphanReason, E> {
                    OrphanReason::ALL
                        .iter()
                        .find(|r| r.as_str() == v)
                        .copied()
                        .ok_or_else(|| de::Error::custom(format!("unknown reason {v:?}")))
                }
            }
            deserializer.deserialize_str(V)
        }
    }

    /// Field names are part of the public representation and covered by semver.
    #[derive(Serialize, Deserialize)]
    #[serde(rename = "PartialInfo")]
    struct Repr {
        sequence: u32,
        format: PartialFormat,
        cause: RecordClosureCause,
    }

    impl Serialize for PartialInfo {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            Repr {
                sequence: self.sequence,
                format: self.format,
                cause: self.cause,
            }
            .serialize(serializer)
        }
    }

    impl<'de> Deserialize<'de> for PartialInfo {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            let r = Repr::deserialize(deserializer)?;
            Ok(PartialInfo::new(r.sequence, r.format, r.cause))
        }
    }
}

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

    fn fqpc(sequence: u32, cause: RecordClosureCause) -> PartialInfo {
        PartialInfo::new(sequence, PartialFormat::FullyQualified, cause)
    }

    fn rpc(sequence: u32, cause: RecordClosureCause) -> PartialInfo {
        PartialInfo::new(sequence, PartialFormat::Reduced, cause)
    }

    #[test]
    fn a_whole_run_reassembles() {
        let run = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::VolumeLimit),
            rpc(3, RecordClosureCause::NormalRelease),
        ]);
        assert_eq!(run.assess(), Reassembly::Complete { count: 3 });
        assert!(run.assess().is_complete());
    }

    /// Order of arrival is not order of sequence, and a shuffled run must not
    /// look like a run with holes.
    #[test]
    fn assessment_is_order_independent() {
        let ordered = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::VolumeLimit),
            rpc(3, RecordClosureCause::NormalRelease),
        ]);
        let shuffled = PartialRun::from_parts(vec![
            rpc(3, RecordClosureCause::NormalRelease),
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::VolumeLimit),
        ]);
        assert_eq!(ordered.assess(), shuffled.assess());
    }

    /// The rule from TS 32.240: the first partial must be fully qualified, or
    /// there is nothing to complete the reduced ones against.
    #[test]
    fn a_run_beginning_with_a_reduced_record_is_an_orphan() {
        let run = PartialRun::from_parts(vec![
            rpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::NormalRelease),
        ]);
        assert_eq!(
            run.assess(),
            Reassembly::Orphan {
                reason: OrphanReason::FirstRecordNotFullyQualified,
                have: vec![1, 2],
            }
        );
        assert!(!run.assess().may_still_complete(), "waiting cannot fix it");
    }

    /// A run that does not start at 1 is waiting for its opening record.
    #[test]
    fn a_run_that_does_not_begin_at_one_is_waiting_for_it() {
        let run = PartialRun::from_parts(vec![
            rpc(2, RecordClosureCause::VolumeLimit),
            rpc(3, RecordClosureCause::NormalRelease),
        ]);
        assert_eq!(
            run.assess(),
            Reassembly::Incomplete {
                have: vec![2, 3],
                missing: vec![1],
                missing_count: 1,
                terminated: true,
            }
        );
        assert!(run.assess().may_still_complete());
    }

    /// Sequence 0 belongs to no run.
    #[test]
    fn a_partial_numbered_zero_is_an_orphan() {
        let run = PartialRun::from_parts(vec![
            rpc(0, RecordClosureCause::VolumeLimit),
            fqpc(1, RecordClosureCause::NormalRelease),
        ]);
        assert!(matches!(
            run.assess(),
            Reassembly::Orphan {
                reason: OrphanReason::InvalidSequence,
                ..
            }
        ));
    }

    #[test]
    fn a_hole_in_the_middle_is_named() {
        let run = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(4, RecordClosureCause::NormalRelease),
        ]);
        assert_eq!(
            run.assess(),
            Reassembly::Incomplete {
                have: vec![1, 4],
                missing: vec![2, 3],
                missing_count: 2,
                terminated: true,
            }
        );
        assert_eq!(run.assess().missing(), &[2, 3]);
        assert!(run.assess().may_still_complete());
    }

    /// A run with no holes and no terminal record is still open, not broken.
    #[test]
    fn an_open_run_is_incomplete_rather_than_faulty() {
        let run = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::VolumeLimit),
        ]);
        let assessment = run.assess();
        assert_eq!(
            assessment,
            Reassembly::Incomplete {
                have: vec![1, 2],
                missing: vec![],
                missing_count: 0,
                terminated: false,
            }
        );
        assert!(assessment.may_still_complete());
        assert!(assessment.missing().is_empty());
    }

    /// The distinction the closure cause exists for: a run that stops after a
    /// node failed is explained; the same shape after a volume limit is not.
    #[test]
    fn an_abnormal_ending_explains_a_missing_successor() {
        let crashed = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::AbnormalRelease),
        ]);
        // An abnormal release is terminal, so the run is actually complete.
        assert!(crashed.assess().is_complete());

        // A run that simply stops mid-flight after a node failed is explained.
        let crashed_mid_flight = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::AbnormalRelease),
        ]);
        // …though an abnormal release is itself terminal, so this run is whole.
        assert!(crashed_mid_flight.assess().is_complete());
        assert!(!crashed_mid_flight.is_explained_loss());

        // A run that stops on a routine trigger explains nothing.
        let stopped = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::VolumeLimit),
        ]);
        assert!(
            !stopped.is_explained_loss(),
            "a volume limit explains nothing"
        );

        // The same run whose highest partial reports an operator intervention
        // is accounted for.
        let intervened = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::ManagementIntervention),
        ]);
        assert!(intervened.assess().is_complete());
    }

    /// The distinction, isolated: a run that stops without a terminal record.
    #[test]
    fn a_run_that_stops_without_terminating_is_explained_only_by_an_abnormal_cause() {
        let routine = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::TimeLimit),
            rpc(2, RecordClosureCause::TimeLimit),
        ]);
        assert!(!routine.is_explained_loss());

        // The highest partial decides, not the first.
        let mixed = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::TimeLimit),
            rpc(2, RecordClosureCause::PartialRecord),
        ]);
        assert!(!mixed.is_explained_loss());

        let empty = PartialRun::new();
        assert!(!empty.is_explained_loss());
    }

    /// A hole is never explained away: an abnormal ending accounts for a
    /// missing *successor*, not for a missing middle.
    #[test]
    fn an_abnormal_ending_does_not_excuse_a_hole() {
        let holed = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(3, RecordClosureCause::AbnormalRelease),
        ]);
        assert!(
            !holed.is_explained_loss(),
            "a hole is not a missing successor"
        );
        assert_eq!(holed.assess().missing(), &[2]);
    }

    /// The membership test is a binary search, so a long run does not degrade
    /// quadratically. Exercised at a length a real data session reaches.
    #[test]
    fn a_long_run_assesses_without_quadratic_membership_tests() {
        let mut parts = vec![fqpc(1, RecordClosureCause::VolumeLimit)];
        parts.extend((2..5_000).map(|n| rpc(n, RecordClosureCause::VolumeLimit)));
        parts.push(rpc(5_000, RecordClosureCause::NormalRelease));

        let run = PartialRun::from_parts(parts);
        assert_eq!(run.assess(), Reassembly::Complete { count: 5_000 });
    }

    /// Two copies of one partial are one partial; recognising the retransmission
    /// belongs to deduplication, not here.
    #[test]
    fn a_repeated_partial_does_not_inflate_the_run() {
        let run = PartialRun::from_parts(vec![
            fqpc(1, RecordClosureCause::VolumeLimit),
            rpc(2, RecordClosureCause::NormalRelease),
            rpc(2, RecordClosureCause::NormalRelease),
        ]);
        assert_eq!(run.assess(), Reassembly::Complete { count: 2 });
        assert_eq!(run.len(), 3, "the run still holds what arrived");
    }

    #[test]
    fn an_empty_run_is_empty_and_may_still_complete() {
        let run = PartialRun::new();
        assert_eq!(run.assess(), Reassembly::Empty);
        assert!(run.assess().may_still_complete());
        assert_eq!(run.last_cause(), None);
    }

    #[test]
    fn the_last_cause_comes_from_the_highest_sequence_not_the_last_arrival() {
        let run = PartialRun::from_parts(vec![
            rpc(3, RecordClosureCause::NormalRelease),
            fqpc(1, RecordClosureCause::VolumeLimit),
        ]);
        assert_eq!(run.last_cause(), Some(RecordClosureCause::NormalRelease));
    }

    #[test]
    fn terminal_and_explanatory_causes_are_distinct_questions() {
        assert!(RecordClosureCause::NormalRelease.is_terminal());
        assert!(!RecordClosureCause::NormalRelease.explains_a_missing_successor());

        assert!(RecordClosureCause::AbnormalRelease.is_terminal());
        assert!(RecordClosureCause::AbnormalRelease.explains_a_missing_successor());

        assert!(!RecordClosureCause::VolumeLimit.is_terminal());
        assert!(!RecordClosureCause::VolumeLimit.explains_a_missing_successor());
    }

    #[test]
    fn closure_cause_and_format_codes_round_trip() {
        assert_eq!(
            RecordClosureCause::ALL.len(),
            RecordClosureCause::CODES.len()
        );
        for (cause, code) in RecordClosureCause::ALL
            .iter()
            .zip(RecordClosureCause::CODES)
        {
            assert_eq!(cause.as_str(), *code);
            assert_eq!(code.parse::<RecordClosureCause>().unwrap(), *cause);
        }
        for (format, code) in PartialFormat::ALL.iter().zip(PartialFormat::CODES) {
            assert_eq!(format.as_str(), *code);
            assert_eq!(code.parse::<PartialFormat>().unwrap(), *format);
        }
        assert!("NOPE".parse::<RecordClosureCause>().is_err());
    }
}