mediation 0.1.0

Telecom usage mediation domain library — 3GPP CDR local-time-plus-offset arithmetic (quarter-hour offsets, DST-adjusted), integer octet volumes, validated identifiers (IMSI/MSISDN/IMEI/ICCID/SUPI/GPSI/PEI, PLMN, TADIG), natural-key deduplication, RFC 1982 wrap-aware sequence-gap detection, partial-record reassembly, cross-node correlation, signed restatement, a numbered validation catalogue, a conservation law that spans batches, TBCD/BCD wire codecs, a zero-copy BER reader, usage aggregation, roaming-batch reconciliation, longest-prefix number ranges with portability, User Location Information (CGI/SAI/ECGI/NCGI/TAI), conserving tariff-band apportionment, two-question jurisdiction determination and an EU/German regulatory layer. Zero I/O, no async, no clock, exact 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
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
1193
1194
1195
1196
1197
1198
//! Where a record was produced: cells, service areas and tracking areas.
//!
//! Every charging record carries some form of User Location Information, and
//! which form depends on the access generation. They are not interchangeable,
//! and the crate keeps them as distinct types so a 2G Cell Global Identity can
//! never be compared against a 5G one and found equal by accident.
//!
//! # A cell identity cannot be decomposed without the operator's configuration
//!
//! This is the same trap as the two-versus-three-digit MNC, in a new place. The
//! E-UTRAN Cell Identity is 28 bits and the NR Cell Identity is 36, and both are
//! split internally between a base-station identifier and a cell identifier —
//! but **where the split falls is a per-operator deployment choice**, not
//! something the value carries:
//!
//! | Identity | Total | Base station | Notes |
//! |---|---|---|---|
//! | [`Eci`] | 28 bits | 20 (macro) or 28 (home) | a home eNB consumes the whole value |
//! | [`Nci`] | 36 bits | 22 to 32, configured | the remainder is the cell |
//!
//! So [`Eci::enb_id`] takes an [`EnbKind`] and [`Nci::gnb_id`] takes a
//! [`GnbIdLength`]. Neither can be guessed, and a crate that guessed would
//! attribute a cell to the wrong base station silently — which is worse than
//! not answering, because a coverage or fraud report built on it looks fine.
//!
//! ```rust
//! use mediation::{EnbKind, Eci, GnbIdLength, Nci};
//!
//! // The same 28 bits mean two different things depending on the deployment.
//! let eci = Eci::new(0x00A1_B2C3).unwrap();
//! assert_eq!(eci.enb_id(EnbKind::Macro), 0x0000_A1B2);
//! assert_eq!(eci.cell_id(EnbKind::Macro), Some(0xC3));
//! assert_eq!(eci.enb_id(EnbKind::Home), 0x00A1_B2C3);
//! assert_eq!(eci.cell_id(EnbKind::Home), None, "a home eNB has no cell part");
//!
//! // And an NR cell identity needs the operator's configured gNB-ID length.
//! let nci = Nci::new(0x0_1234_5678).unwrap();
//! assert_ne!(
//!     nci.gnb_id(GnbIdLength::new(22).unwrap()),
//!     nci.gnb_id(GnbIdLength::new(24).unwrap()),
//! );
//! ```
//!
//! # A tracking area code carries its width
//!
//! A tracking area code is 16 bits in EPS and 24 bits in 5GS, and an EPS code
//! appears in 5GS **zero-padded** to 24 bits. Storing the number alone would
//! make `0x0001` from a 4G record and `0x000001` from a 5G one look like
//! different values in one direction and equal in the other, depending on which
//! way a comparison was written. [`TrackingAreaCode`] therefore carries its
//! width the way [`Mnc`](crate::Mnc) carries its digit count, and
//! [`widened`](TrackingAreaCode::widened) is the only way across.

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

use crate::error::ParseError;
use crate::plmn::{Mcc, PlmnId};

// ── The reserved "no valid area" codes ───────────────────────────────────────

/// `true` when `value` is one of the two codes reserved to mean *no valid
/// area*: all zeros, or all ones less one.
///
/// The same rule at every width — `0000`/`FFFE` at 16 bits, `000000`/`FFFFFE`
/// at 24 — so it is written once. A location area, an EPS tracking area and a
/// 5GS tracking area all use it, and three copies of one rule eventually
/// disagree at one width.
const fn is_reserved_area(value: u32, bits: u8) -> bool {
    value == 0 || value == (1u32 << bits) - 2
}

// ── Location area, cell and service area (2G/3G) ─────────────────────────────

/// A Location Area Code: 16 bits, excluding the two reserved values.
///
/// `0x0000` and `0xFFFE` are reserved to mean *no valid location area* — a
/// handset that has never attached, or one whose stored area was deleted. They
/// reach a charging record when an element copies its internal "unknown"
/// marker into the field, and reading them as ordinary areas puts real usage in
/// a location area that does not exist.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Lac(u16);

impl Lac {
    /// `true` when the value is reserved to mean *no valid location area*.
    #[must_use]
    pub const fn is_reserved(value: u16) -> bool {
        is_reserved_area(value as u32, 16)
    }

    /// A location area code, or `None` for a reserved value.
    #[must_use]
    pub const fn new(value: u16) -> Option<Self> {
        if Self::is_reserved(value) {
            None
        } else {
            Some(Self(value))
        }
    }

    /// The numeric value.
    #[must_use]
    pub const fn get(self) -> u16 {
        self.0
    }
}

impl fmt::Display for Lac {
    /// Four uppercase hex digits, the form every element logs.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:04X}", self.0)
    }
}

/// A Cell Identity: two octets, within a location area.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CellId(u16);

impl CellId {
    /// A cell identity. Every 16-bit value is valid.
    #[must_use]
    pub const fn new(value: u16) -> Self {
        Self(value)
    }

    /// The numeric value.
    #[must_use]
    pub const fn get(self) -> u16 {
        self.0
    }
}

impl fmt::Display for CellId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:04X}", self.0)
    }
}

/// A Service Area Code: two octets, within a location area.
///
/// A service area is a UTRAN grouping of cells, not a cell — which is why it is
/// a separate type from [`CellId`] rather than the same number under two names.
/// A record reporting an SAI locates the subscriber less precisely than one
/// reporting a CGI, and a report that mixed them would overstate its precision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ServiceAreaCode(u16);

impl ServiceAreaCode {
    /// A service area code. Every 16-bit value is valid.
    #[must_use]
    pub const fn new(value: u16) -> Self {
        Self(value)
    }

    /// The numeric value.
    #[must_use]
    pub const fn get(self) -> u16 {
        self.0
    }
}

impl fmt::Display for ServiceAreaCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:04X}", self.0)
    }
}

/// A Cell Global Identity: network, location area and cell.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Cgi {
    plmn: PlmnId,
    lac: Lac,
    cell: CellId,
}

impl Cgi {
    /// A cell global identity from its parts.
    #[must_use]
    pub const fn new(plmn: PlmnId, lac: Lac, cell: CellId) -> Self {
        Self { plmn, lac, cell }
    }

    /// The network.
    #[must_use]
    pub const fn plmn(self) -> PlmnId {
        self.plmn
    }

    /// The location area.
    #[must_use]
    pub const fn lac(self) -> Lac {
        self.lac
    }

    /// The cell.
    #[must_use]
    pub const fn cell(self) -> CellId {
        self.cell
    }
}

impl fmt::Display for Cgi {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}-{}-{}", self.plmn, self.lac, self.cell)
    }
}

/// A Service Area Identity: network, location area and service area.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Sai {
    plmn: PlmnId,
    lac: Lac,
    sac: ServiceAreaCode,
}

impl Sai {
    /// A service area identity from its parts.
    #[must_use]
    pub const fn new(plmn: PlmnId, lac: Lac, sac: ServiceAreaCode) -> Self {
        Self { plmn, lac, sac }
    }

    /// The network.
    #[must_use]
    pub const fn plmn(self) -> PlmnId {
        self.plmn
    }

    /// The location area.
    #[must_use]
    pub const fn lac(self) -> Lac {
        self.lac
    }

    /// The service area.
    #[must_use]
    pub const fn sac(self) -> ServiceAreaCode {
        self.sac
    }
}

impl fmt::Display for Sai {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}-{}-{}", self.plmn, self.lac, self.sac)
    }
}

// ── E-UTRAN (4G) ─────────────────────────────────────────────────────────────

/// Which kind of eNodeB an [`Eci`] belongs to, and therefore where it splits.
///
/// Not derivable from the value: the same 28 bits are a macro eNB with a cell
/// beneath it, or one home eNB and nothing else. A caller knows which from the
/// deployment; the number does not say.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EnbKind {
    /// A macro eNodeB: a 20-bit identifier and an 8-bit cell beneath it.
    Macro,
    /// A home eNodeB: a 28-bit identifier occupying the whole value.
    Home,
}

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

    /// The canonical code of each variant, in [`ALL`](Self::ALL) order.
    pub const CODES: &'static [&'static str] = &["MACRO", "HOME"];

    /// How many of the 28 bits the base-station identifier occupies.
    #[must_use]
    pub const fn id_bits(self) -> u8 {
        match self {
            Self::Macro => 20,
            Self::Home => 28,
        }
    }

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Macro => "MACRO",
            Self::Home => "HOME",
        }
    }
}

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

impl FromStr for EnbKind {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::ALL
            .iter()
            .find(|k| k.as_str().eq_ignore_ascii_case(s.trim()))
            .copied()
            .ok_or_else(|| ParseError::format("EnbKind", s, "MACRO or HOME"))
    }
}

/// An E-UTRAN Cell Identity: 28 bits.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Eci(u32);

impl Eci {
    /// How many bits the identity occupies.
    pub const BITS: u32 = 28;

    /// One more than the largest valid value.
    const LIMIT: u32 = 1 << Self::BITS;

    /// An identity, or `None` when the value does not fit in 28 bits.
    #[must_use]
    pub const fn new(value: u32) -> Option<Self> {
        if value < Self::LIMIT {
            Some(Self(value))
        } else {
            None
        }
    }

    /// The whole 28-bit value.
    #[must_use]
    pub const fn get(self) -> u32 {
        self.0
    }

    /// The base-station identifier, given the deployment kind.
    #[must_use]
    pub const fn enb_id(self, kind: EnbKind) -> u32 {
        match kind {
            EnbKind::Macro => self.0 >> 8,
            EnbKind::Home => self.0,
        }
    }

    /// The cell beneath the base station, or `None` for a home eNodeB.
    ///
    /// A home eNodeB identifier fills all 28 bits, so there is no cell part to
    /// return. Returning `0` there would be a plausible-looking value for a
    /// cell that does not exist.
    #[must_use]
    pub const fn cell_id(self, kind: EnbKind) -> Option<u8> {
        match kind {
            EnbKind::Macro => Some((self.0 & 0xFF) as u8),
            EnbKind::Home => None,
        }
    }
}

impl fmt::Display for Eci {
    /// Seven uppercase hex digits — 28 bits, never truncated.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:07X}", self.0)
    }
}

/// An E-UTRAN Cell Global Identity: network plus [`Eci`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ecgi {
    plmn: PlmnId,
    eci: Eci,
}

impl Ecgi {
    /// An E-UTRAN cell global identity from its parts.
    #[must_use]
    pub const fn new(plmn: PlmnId, eci: Eci) -> Self {
        Self { plmn, eci }
    }

    /// The network.
    #[must_use]
    pub const fn plmn(self) -> PlmnId {
        self.plmn
    }

    /// The cell identity.
    #[must_use]
    pub const fn eci(self) -> Eci {
        self.eci
    }
}

impl fmt::Display for Ecgi {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}-{}", self.plmn, self.eci)
    }
}

// ── NR (5G) ──────────────────────────────────────────────────────────────────

/// How many of an [`Nci`]'s 36 bits the gNodeB identifier occupies.
///
/// Between 22 and 32, chosen per deployment. The value is not recoverable from
/// a cell identity, so it travels alongside — the same shape as
/// [`MncWidth`](crate::MncWidth).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GnbIdLength(u8);

impl GnbIdLength {
    /// The shortest permitted gNodeB identifier.
    pub const MIN: u8 = 22;
    /// The longest permitted gNodeB identifier.
    pub const MAX: u8 = 32;

    /// A length, or `None` outside 22 to 32 inclusive.
    #[must_use]
    pub const fn new(bits: u8) -> Option<Self> {
        if bits >= Self::MIN && bits <= Self::MAX {
            Some(Self(bits))
        } else {
            None
        }
    }

    /// The number of bits.
    #[must_use]
    pub const fn get(self) -> u8 {
        self.0
    }

    /// How many bits are left for the cell beneath the gNodeB.
    #[must_use]
    pub const fn cell_bits(self) -> u8 {
        Nci::BITS as u8 - self.0
    }
}

impl fmt::Display for GnbIdLength {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for GnbIdLength {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let err = || ParseError::format("GnbIdLength", s, "a bit count from 22 to 32");
        Self::new(s.trim().parse::<u8>().map_err(|_| err())?).ok_or_else(err)
    }
}

/// An NR Cell Identity: 36 bits.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Nci(u64);

impl Nci {
    /// How many bits the identity occupies.
    pub const BITS: u32 = 36;

    /// One more than the largest valid value.
    const LIMIT: u64 = 1 << Self::BITS;

    /// An identity, or `None` when the value does not fit in 36 bits.
    #[must_use]
    pub const fn new(value: u64) -> Option<Self> {
        if value < Self::LIMIT {
            Some(Self(value))
        } else {
            None
        }
    }

    /// The whole 36-bit value.
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }

    /// The gNodeB identifier, given the deployment's configured length.
    #[must_use]
    pub const fn gnb_id(self, length: GnbIdLength) -> u64 {
        self.0 >> length.cell_bits()
    }

    /// The cell beneath the gNodeB, given the deployment's configured length.
    ///
    /// A 32-bit gNodeB identifier leaves four bits, and a 22-bit one leaves
    /// fourteen — so unlike [`Eci::cell_id`] there is always a cell part.
    #[must_use]
    pub const fn cell_id(self, length: GnbIdLength) -> u64 {
        self.0 & ((1u64 << length.cell_bits()) - 1)
    }
}

impl fmt::Display for Nci {
    /// Nine uppercase hex digits — 36 bits, never truncated.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:09X}", self.0)
    }
}

/// An NR Cell Global Identity: network plus [`Nci`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ncgi {
    plmn: PlmnId,
    nci: Nci,
}

impl Ncgi {
    /// An NR cell global identity from its parts.
    #[must_use]
    pub const fn new(plmn: PlmnId, nci: Nci) -> Self {
        Self { plmn, nci }
    }

    /// The network.
    #[must_use]
    pub const fn plmn(self) -> PlmnId {
        self.plmn
    }

    /// The cell identity.
    #[must_use]
    pub const fn nci(self) -> Nci {
        self.nci
    }
}

impl fmt::Display for Ncgi {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}-{}", self.plmn, self.nci)
    }
}

// ── Tracking area ────────────────────────────────────────────────────────────

/// Which system a [`TrackingAreaCode`] was assigned in, and therefore how wide.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TacWidth {
    /// EPS: 16 bits.
    Eps,
    /// 5GS: 24 bits.
    FiveGs,
}

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

    /// The canonical code of each variant, in [`ALL`](Self::ALL) order.
    pub const CODES: &'static [&'static str] = &["EPS", "5GS"];

    /// How many bits the code occupies.
    #[must_use]
    pub const fn bits(self) -> u8 {
        match self {
            Self::Eps => 16,
            Self::FiveGs => 24,
        }
    }

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Eps => "EPS",
            Self::FiveGs => "5GS",
        }
    }
}

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

impl FromStr for TacWidth {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::ALL
            .iter()
            .find(|w| w.as_str().eq_ignore_ascii_case(s.trim()))
            .copied()
            .ok_or_else(|| ParseError::format("TacWidth", s, "EPS or 5GS"))
    }
}

/// A Tracking Area Code, carrying the width it was assigned at.
///
/// Not to be confused with [`Tac`](crate::Tac), which is the Type Allocation
/// Code inside an IMEI. The two are unrelated and the industry reuses the
/// abbreviation for both, which is why this type is spelled out.
///
/// `0x0000` and the all-ones-less-one value are reserved to mean *no valid
/// tracking area*, exactly as for [`Lac`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TrackingAreaCode {
    value: u32,
    width: TacWidth,
}

impl TrackingAreaCode {
    /// An EPS tracking area code, or `None` for a reserved value.
    #[must_use]
    pub const fn eps(value: u16) -> Option<Self> {
        if is_reserved_area(value as u32, TacWidth::Eps.bits()) {
            None
        } else {
            Some(Self {
                value: value as u32,
                width: TacWidth::Eps,
            })
        }
    }

    /// A 5GS tracking area code, or `None` for a reserved or oversized value.
    #[must_use]
    pub const fn five_gs(value: u32) -> Option<Self> {
        if value > 0x00FF_FFFF || is_reserved_area(value, TacWidth::FiveGs.bits()) {
            None
        } else {
            Some(Self {
                value,
                width: TacWidth::FiveGs,
            })
        }
    }

    /// The numeric value.
    #[must_use]
    pub const fn get(self) -> u32 {
        self.value
    }

    /// The width the code was assigned at.
    #[must_use]
    pub const fn width(self) -> TacWidth {
        self.width
    }

    /// The same area as a 5GS code, zero-padded if it was an EPS one.
    ///
    /// The only way to compare across systems, and it is deliberately explicit:
    /// an EPS code and the 5GS code with the same number are the same tracking
    /// area, but `==` says they are not, because the values carry different
    /// widths and silently treating them as equal is how a 4G area and a 5G one
    /// merge in a report that meant to keep them apart.
    #[must_use]
    pub const fn widened(self) -> Self {
        Self {
            value: self.value,
            width: TacWidth::FiveGs,
        }
    }

    /// `true` when both name the same area, ignoring the width they were
    /// written at.
    #[must_use]
    pub const fn is_same_area(self, other: Self) -> bool {
        self.value == other.value
    }
}

impl fmt::Display for TrackingAreaCode {
    /// Hex, at the width the code was assigned: four digits or six.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.width {
            TacWidth::Eps => write!(f, "{:04X}", self.value),
            TacWidth::FiveGs => write!(f, "{:06X}", self.value),
        }
    }
}

/// A Tracking Area Identity: network plus [`TrackingAreaCode`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Tai {
    plmn: PlmnId,
    tac: TrackingAreaCode,
}

impl Tai {
    /// A tracking area identity from its parts.
    #[must_use]
    pub const fn new(plmn: PlmnId, tac: TrackingAreaCode) -> Self {
        Self { plmn, tac }
    }

    /// The network.
    #[must_use]
    pub const fn plmn(self) -> PlmnId {
        self.plmn
    }

    /// The tracking area.
    #[must_use]
    pub const fn tac(self) -> TrackingAreaCode {
        self.tac
    }
}

impl fmt::Display for Tai {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}-{}", self.plmn, self.tac)
    }
}

// ── User location ────────────────────────────────────────────────────────────

/// Where a record says the subscriber was.
///
/// A sum type rather than a struct of options, because a record reports **one**
/// of these — and a struct with five optional fields invites a reader to check
/// them in an order that silently prefers the least precise.
///
/// Deliberately not `#[non_exhaustive]`: a consumer mapping these onto their own
/// location model wants a compile error when a new access generation appears,
/// not a wildcard arm that files 6G usage under "unknown".
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UserLocation {
    /// A 2G cell.
    Cgi(Cgi),
    /// A 3G service area.
    Sai(Sai),
    /// A 4G cell.
    Ecgi(Ecgi),
    /// A 5G cell.
    Ncgi(Ncgi),
    /// A tracking area only, with no cell.
    Tai(Tai),
}

impl UserLocation {
    /// The network the location belongs to.
    #[must_use]
    pub const fn plmn(self) -> PlmnId {
        match self {
            Self::Cgi(v) => v.plmn(),
            Self::Sai(v) => v.plmn(),
            Self::Ecgi(v) => v.plmn(),
            Self::Ncgi(v) => v.plmn(),
            Self::Tai(v) => v.plmn(),
        }
    }

    /// The country the location is in.
    ///
    /// The one question every form answers, and the only one this crate uses:
    /// a country is what regulation, settlement and roaming turn on, and it is
    /// recoverable without any cell database at all.
    #[must_use]
    pub const fn mcc(self) -> Mcc {
        self.plmn().mcc()
    }

    /// `true` when the location names an individual cell.
    ///
    /// A tracking area is an area, not a cell — an assessment that treated it
    /// as one would claim a precision the record does not carry.
    #[must_use]
    pub const fn is_cell(self) -> bool {
        !matches!(self, Self::Tai(_))
    }

    /// The canonical string form of the variant.
    #[must_use]
    pub const fn kind(self) -> &'static str {
        match self {
            Self::Cgi(_) => "CGI",
            Self::Sai(_) => "SAI",
            Self::Ecgi(_) => "ECGI",
            Self::Ncgi(_) => "NCGI",
            Self::Tai(_) => "TAI",
        }
    }
}

impl fmt::Display for UserLocation {
    /// The kind and the value, so two forms never render alike.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Cgi(v) => write!(f, "cgi:{v}"),
            Self::Sai(v) => write!(f, "sai:{v}"),
            Self::Ecgi(v) => write!(f, "ecgi:{v}"),
            Self::Ncgi(v) => write!(f, "ncgi:{v}"),
            Self::Tai(v) => write!(f, "tai:{v}"),
        }
    }
}

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

#[cfg(feature = "serde")]
mod serde_impl {
    use super::{
        CellId, Cgi, Ecgi, Eci, EnbKind, GnbIdLength, Lac, Ncgi, Nci, Sai, ServiceAreaCode,
        TacWidth, Tai, TrackingAreaCode, UserLocation,
    };
    use crate::plmn::PlmnId;
    use serde::de::{self, Visitor};
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::fmt;

    /// Serialises as its canonical string, deserialises through `FromStr`.
    macro_rules! string_enum {
        ($t:ty, $expecting:literal) => {
            impl Serialize for $t {
                fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                    s.serialize_str(self.as_str())
                }
            }

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

    string_enum!(EnbKind, "MACRO or HOME");
    string_enum!(TacWidth, "EPS or 5GS");

    /// Serialises as a number, rejecting out-of-range values on the way back in
    /// rather than truncating them.
    macro_rules! checked_number {
        ($t:ty, $raw:ty, $ctor:expr, $expecting:literal) => {
            impl Serialize for $t {
                fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                    self.get().serialize(s)
                }
            }

            impl<'de> Deserialize<'de> for $t {
                fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                    let raw = <$raw>::deserialize(d)?;
                    #[allow(clippy::redundant_closure_call)]
                    ($ctor)(raw)
                        .ok_or_else(|| de::Error::custom(format!("{raw} is not {}", $expecting)))
                }
            }
        };
    }

    checked_number!(
        Lac,
        u16,
        Lac::new,
        "a location area code (0000 and FFFE are reserved)"
    );
    checked_number!(Eci, u32, Eci::new, "a 28-bit E-UTRAN cell identity");
    checked_number!(Nci, u64, Nci::new, "a 36-bit NR cell identity");
    checked_number!(
        GnbIdLength,
        u8,
        GnbIdLength::new,
        "a gNB-ID length of 22 to 32 bits"
    );

    /// Serialises as a number; every value of the underlying width is valid.
    macro_rules! plain_number {
        ($t:ty, $raw:ty, $ctor:expr) => {
            impl Serialize for $t {
                fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                    self.get().serialize(s)
                }
            }

            impl<'de> Deserialize<'de> for $t {
                fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                    #[allow(clippy::redundant_closure_call)]
                    Ok(($ctor)(<$raw>::deserialize(d)?))
                }
            }
        };
    }

    plain_number!(CellId, u16, CellId::new);
    plain_number!(ServiceAreaCode, u16, ServiceAreaCode::new);

    #[derive(Serialize, Deserialize)]
    #[serde(rename = "TrackingAreaCode")]
    struct TacRepr {
        value: u32,
        width: TacWidth,
    }

    impl Serialize for TrackingAreaCode {
        fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
            TacRepr {
                value: self.get(),
                width: self.width(),
            }
            .serialize(s)
        }
    }

    impl<'de> Deserialize<'de> for TrackingAreaCode {
        fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
            let r = TacRepr::deserialize(d)?;
            let built = match r.width {
                TacWidth::Eps => u16::try_from(r.value).ok().and_then(TrackingAreaCode::eps),
                TacWidth::FiveGs => TrackingAreaCode::five_gs(r.value),
            };
            built.ok_or_else(|| {
                de::Error::custom(format!(
                    "{} is not a valid {} tracking area code",
                    r.value, r.width
                ))
            })
        }
    }

    /// Every composite names its parts, so a stored location stays readable.
    macro_rules! composite {
        ($t:ty, $name:literal, { $($field:ident : $ty:ty),+ $(,)? }) => {
            #[derive(Serialize, Deserialize)]
            #[serde(rename = $name)]
            struct Repr {
                $($field: $ty),+
            }

            impl Serialize for $t {
                fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                    Repr { $($field: self.$field()),+ }.serialize(s)
                }
            }

            impl<'de> Deserialize<'de> for $t {
                fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                    let r = Repr::deserialize(d)?;
                    Ok(<$t>::new($(r.$field),+))
                }
            }
        };
    }

    mod cgi {
        use super::*;
        composite!(Cgi, "Cgi", { plmn: PlmnId, lac: Lac, cell: CellId });
    }
    mod sai {
        use super::*;
        composite!(Sai, "Sai", { plmn: PlmnId, lac: Lac, sac: ServiceAreaCode });
    }
    mod ecgi {
        use super::*;
        composite!(Ecgi, "Ecgi", { plmn: PlmnId, eci: Eci });
    }
    mod ncgi {
        use super::*;
        composite!(Ncgi, "Ncgi", { plmn: PlmnId, nci: Nci });
    }
    mod tai {
        use super::*;
        composite!(Tai, "Tai", { plmn: PlmnId, tac: TrackingAreaCode });
    }

    /// Internally tagged on `kind`, so adding an access generation does not
    /// renumber the others.
    #[derive(Serialize, Deserialize)]
    #[serde(tag = "kind", rename = "UserLocation")]
    enum LocationRepr {
        #[serde(rename = "CGI")]
        Cgi(Cgi),
        #[serde(rename = "SAI")]
        Sai(Sai),
        #[serde(rename = "ECGI")]
        Ecgi(Ecgi),
        #[serde(rename = "NCGI")]
        Ncgi(Ncgi),
        #[serde(rename = "TAI")]
        Tai(Tai),
    }

    impl Serialize for UserLocation {
        fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
            match *self {
                UserLocation::Cgi(v) => LocationRepr::Cgi(v),
                UserLocation::Sai(v) => LocationRepr::Sai(v),
                UserLocation::Ecgi(v) => LocationRepr::Ecgi(v),
                UserLocation::Ncgi(v) => LocationRepr::Ncgi(v),
                UserLocation::Tai(v) => LocationRepr::Tai(v),
            }
            .serialize(s)
        }
    }

    impl<'de> Deserialize<'de> for UserLocation {
        fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
            Ok(match LocationRepr::deserialize(d)? {
                LocationRepr::Cgi(v) => UserLocation::Cgi(v),
                LocationRepr::Sai(v) => UserLocation::Sai(v),
                LocationRepr::Ecgi(v) => UserLocation::Ecgi(v),
                LocationRepr::Ncgi(v) => UserLocation::Ncgi(v),
                LocationRepr::Tai(v) => UserLocation::Tai(v),
            })
        }
    }
}

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

    fn plmn() -> PlmnId {
        "262-02".parse().expect("a valid network")
    }

    // ── The reserved location areas ──────────────────────────────────────────

    /// A reserved code means *no valid area*, and reading it as an ordinary one
    /// files real usage in a location area that does not exist.
    #[test]
    fn the_two_reserved_location_areas_are_rejected() {
        assert!(Lac::new(0x0000).is_none());
        assert!(Lac::new(0xFFFE).is_none());
        assert!(Lac::new(0x0001).is_some());
        assert!(Lac::new(0xFFFF).is_some(), "only the two are reserved");
        assert!(Lac::is_reserved(0xFFFE));
        assert!(!Lac::is_reserved(0xFFFF));

        // The same rule holds at both widths, which is why it is written once.
        assert_eq!(
            (0u32..=0xFFFF)
                .filter(|v| super::is_reserved_area(*v, 16))
                .count(),
            2
        );
        assert!(super::is_reserved_area(0x00FF_FFFE, 24));
        assert!(!super::is_reserved_area(0x0000_FFFE, 24));
        assert_eq!(Lac::new(0x00AB).unwrap().to_string(), "00AB");
    }

    #[test]
    fn the_same_two_values_are_reserved_for_tracking_areas() {
        assert!(TrackingAreaCode::eps(0x0000).is_none());
        assert!(TrackingAreaCode::eps(0xFFFE).is_none());
        assert!(TrackingAreaCode::five_gs(0x00_0000).is_none());
        assert!(TrackingAreaCode::five_gs(0xFF_FFFE).is_none());
        assert!(TrackingAreaCode::five_gs(0x01_0000).is_some());
        assert!(
            TrackingAreaCode::five_gs(0x0100_0000).is_none(),
            "more than 24 bits"
        );
    }

    // ── The split that cannot be guessed ─────────────────────────────────────

    /// The same 28 bits are two different base stations depending on the
    /// deployment, and only a caller knows which.
    #[test]
    fn a_cell_identity_splits_differently_for_a_home_enodeb() {
        let eci = Eci::new(0x00A1_B2C3).unwrap();

        assert_eq!(eci.enb_id(EnbKind::Macro), 0x0000_A1B2);
        assert_eq!(eci.cell_id(EnbKind::Macro), Some(0xC3));

        assert_eq!(eci.enb_id(EnbKind::Home), 0x00A1_B2C3);
        assert_eq!(
            eci.cell_id(EnbKind::Home),
            None,
            "a 28-bit home identifier leaves no cell part"
        );
        assert_eq!(EnbKind::Macro.id_bits() + 8, EnbKind::Home.id_bits());
    }

    /// An NR cell identity means nothing without the operator's configured
    /// gNodeB-identifier length.
    #[test]
    fn an_nr_cell_identity_needs_the_configured_length() {
        let nci = Nci::new(0x0_1234_5678).unwrap();

        let short = GnbIdLength::new(22).unwrap();
        let long = GnbIdLength::new(32).unwrap();

        assert_eq!(short.cell_bits(), 14);
        assert_eq!(long.cell_bits(), 4);
        assert_ne!(nci.gnb_id(short), nci.gnb_id(long));

        // Whatever the split, the two halves reconstruct the whole.
        for bits in GnbIdLength::MIN..=GnbIdLength::MAX {
            let length = GnbIdLength::new(bits).unwrap();
            let rebuilt = (nci.gnb_id(length) << length.cell_bits()) | nci.cell_id(length);
            assert_eq!(rebuilt, nci.get(), "at {bits} bits");
        }
    }

    #[test]
    fn identities_are_bounded_by_their_bit_widths() {
        assert!(Eci::new((1 << 28) - 1).is_some());
        assert!(Eci::new(1 << 28).is_none());
        assert!(Nci::new((1 << 36) - 1).is_some());
        assert!(Nci::new(1 << 36).is_none());

        assert!(GnbIdLength::new(21).is_none());
        assert!(GnbIdLength::new(33).is_none());
        assert_eq!(GnbIdLength::new(22).unwrap().get(), 22);
    }

    // ── Width is part of a tracking area code ────────────────────────────────

    /// An EPS code appears in 5GS zero-padded, so the same area written at two
    /// widths must not compare equal by accident — nor be impossible to
    /// compare at all.
    #[test]
    fn a_tracking_area_carries_the_width_it_was_written_at() {
        let eps = TrackingAreaCode::eps(0x1234).unwrap();
        let five_gs = TrackingAreaCode::five_gs(0x00_1234).unwrap();

        assert_ne!(eps, five_gs, "different widths are different values");
        assert_eq!(eps.widened(), five_gs, "and this is the way across");
        assert!(eps.is_same_area(five_gs));

        assert_eq!(eps.to_string(), "1234");
        assert_eq!(five_gs.to_string(), "001234");
        assert_eq!(eps.width(), TacWidth::Eps);
        assert_eq!(TacWidth::Eps.bits() + 8, TacWidth::FiveGs.bits());
    }

    // ── User location ────────────────────────────────────────────────────────

    /// Every form answers the country question, which is the only one this
    /// crate asks of a location.
    #[test]
    fn every_location_form_yields_a_country() {
        let lac = Lac::new(0x1234).unwrap();
        let forms = [
            UserLocation::Cgi(Cgi::new(plmn(), lac, CellId::new(0xABCD))),
            UserLocation::Sai(Sai::new(plmn(), lac, ServiceAreaCode::new(0xABCD))),
            UserLocation::Ecgi(Ecgi::new(plmn(), Eci::new(0x0012_3456).unwrap())),
            UserLocation::Ncgi(Ncgi::new(plmn(), Nci::new(0x0_1234_5678).unwrap())),
            UserLocation::Tai(Tai::new(
                plmn(),
                TrackingAreaCode::five_gs(0x00_1234).unwrap(),
            )),
        ];

        for form in forms {
            assert_eq!(form.mcc(), plmn().mcc());
            assert_eq!(form.plmn(), plmn());
        }

        // A tracking area is an area, not a cell.
        assert!(forms[0].is_cell());
        assert!(!forms[4].is_cell());
    }

    /// Two forms of the same numbers must never render alike, or a report
    /// keyed on the string silently merges a 2G cell with a 3G service area.
    #[test]
    fn the_forms_are_distinguishable_as_strings() {
        let lac = Lac::new(0x1234).unwrap();
        let cgi = UserLocation::Cgi(Cgi::new(plmn(), lac, CellId::new(0xABCD)));
        let sai = UserLocation::Sai(Sai::new(plmn(), lac, ServiceAreaCode::new(0xABCD)));

        assert_ne!(cgi.to_string(), sai.to_string());
        assert_eq!(cgi.to_string(), "cgi:262-02-1234-ABCD");
        assert_eq!(sai.to_string(), "sai:262-02-1234-ABCD");
        assert_eq!(cgi.kind(), "CGI");
    }

    /// A cell identity renders at its full width, so a leading zero is never
    /// dropped and two cells never collide as strings.
    #[test]
    fn identities_render_at_their_full_width() {
        assert_eq!(Eci::new(0x1).unwrap().to_string(), "0000001");
        assert_eq!(Nci::new(0x1).unwrap().to_string(), "000000001");
        assert_eq!(CellId::new(0x1).to_string(), "0001");
        assert_eq!(ServiceAreaCode::new(0x1).to_string(), "0001");
    }

    #[test]
    fn vocabulary_codes_round_trip() {
        for (kind, code) in EnbKind::ALL.iter().zip(EnbKind::CODES) {
            assert_eq!(kind.as_str(), *code);
            assert_eq!(code.parse::<EnbKind>().unwrap(), *kind);
        }
        for (width, code) in TacWidth::ALL.iter().zip(TacWidth::CODES) {
            assert_eq!(width.as_str(), *code);
            assert_eq!(code.parse::<TacWidth>().unwrap(), *width);
        }
        assert_eq!("24".parse::<GnbIdLength>().unwrap().get(), 24);
        assert!("21".parse::<GnbIdLength>().is_err());
        assert!("LTE".parse::<TacWidth>().is_err());
    }
}