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
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
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
//! The core usage record, and the vocabulary that classifies it.
//!
//! # Duration is not `closed − opened`
//!
//! The duration a record carries is the **chargeable** one, measured from
//! answer. A 45-second ring followed by 10 seconds of conversation is a
//! 10-second call across a 55-second wall-clock span. Deriving duration by
//! subtraction over-bills every answered call and invents duration for every
//! unanswered one, so [`UsageRecord::duration`] is a field and
//! [`UsageRecord::wall_clock_span`] is a separate, differently-named accessor.
//! Nothing falls back from one to the other.
//!
//! ```rust
//! # use mediation::{ChargedParty, RecordType, ServiceDomain, Seconds, SourceRef, UsageRecord};
//! # let party = ChargedParty::Subscriber(
//! #     mediation::SubscriberRef::new("imsi-262021234567890".parse().unwrap()));
//! # let source = SourceRef::new("msc-01".parse().unwrap(), 0);
//! let call = UsageRecord::new(
//!     RecordType::OriginatedCall,
//!     party,
//!     "2026-07-28T10:00:00+02:00".parse().unwrap(),
//!     source,
//! )
//! .with_closed("2026-07-28T10:00:55+02:00".parse().unwrap())
//! .with_duration(Seconds::new(10));
//!
//! assert_eq!(call.duration, Some(Seconds::new(10)));           // billed
//! assert_eq!(call.wall_clock_span(), Some(Seconds::new(55)));  // observed
//! assert_eq!(call.service(), ServiceDomain::Voice);            // derived
//! ```
//!
//! # The service domain is derived, not stored
//!
//! [`RecordType::service_domain`] is total, so a `service` field alongside the
//! record type would be a second copy of the same fact — and two copies of one
//! fact eventually disagree. Deriving it removes the disagreement rather than
//! adding a rule to detect it.
//!
//! # Provenance is mandatory
//!
//! [`SourceRef`] is not optional and not decorative. When a billing dispute
//! reaches an auditor two years later the question is always *which file, from
//! which node, at which offset produced this line* — and a record that cannot
//! answer it is not evidence.

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

use crate::cdrtime::{CdrTimestamp, Seconds};
use crate::container::ContainerSet;
use crate::correlation::{CallId, CorrelationKeys};
use crate::dialplan::Destination;
use crate::error::ParseError;
use crate::identity::{Iccid, Msisdn, Pei};
use crate::partial::PartialInfo;
use crate::party::ChargedParty;
use crate::plmn::PlmnId;
use crate::session::SessionContext;
use crate::volume::VolumePair;

// ── ServiceDomain ────────────────────────────────────────────────────────────

/// What kind of service produced a usage record.
///
/// Exhaustive, and deliberately not `#[non_exhaustive]`: a consumer mapping
/// this vocabulary onto their own storage codes should get a compile error when
/// a new domain appears, not a silent wildcard that files the usage under the
/// wrong service.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ServiceDomain {
    /// Telephony over any access — circuit-switched, VoLTE, VoNR or a fixed-line IMS platform.
    Voice,
    /// Short messages.
    Sms,
    /// Multimedia messages.
    Mms,
    /// Packet data — PDP contexts and PDU sessions.
    Data,
    /// IMS sessions other than telephony.
    Ims,
    /// Fixed broadband access — a line session measured by volume.
    ///
    /// Not fixed-line *telephony*: a call over a fixed line is [`Voice`](Self::Voice),
    /// measured by duration, like a call over any other access.
    FixedAccess,
    /// Machine-type communications: NB-IoT, LTE-M and their successors.
    Iot,
    /// Network API invocations exposed to third parties.
    NetworkApi,
    /// Network slice lifecycle and performance.
    Slice,
}

/// The quantity that principally measures a service.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PrimaryMeasure {
    /// Measured by elapsed chargeable time.
    Duration,
    /// Measured by transferred octets.
    Volume,
    /// Measured by a count of discrete events.
    Events,
}

impl ServiceDomain {
    /// Every variant, for exhaustive iteration in tests and mappings.
    pub const ALL: &'static [Self] = &[
        Self::Voice,
        Self::Sms,
        Self::Mms,
        Self::Data,
        Self::Ims,
        Self::FixedAccess,
        Self::Iot,
        Self::NetworkApi,
        Self::Slice,
    ];

    /// The canonical codes, in the order of [`ALL`](Self::ALL).
    pub const CODES: &'static [&'static str] = &[
        "VOICE",
        "SMS",
        "MMS",
        "DATA",
        "IMS",
        "FIXED_ACCESS",
        "IOT",
        "NETWORK_API",
        "SLICE",
    ];

    /// The quantity this service is principally measured by.
    ///
    /// A cross-check rather than a rule: a record whose populated quantity does
    /// not match its domain is worth a finding, because it usually means a
    /// field was mapped into the wrong column upstream.
    #[must_use]
    pub const fn primary_measure(self) -> PrimaryMeasure {
        match self {
            Self::Voice | Self::Ims | Self::Slice => PrimaryMeasure::Duration,
            Self::Data | Self::FixedAccess | Self::Iot => PrimaryMeasure::Volume,
            Self::Sms | Self::Mms | Self::NetworkApi => PrimaryMeasure::Events,
        }
    }

    /// `true` for services whose usage always belongs to a subscription.
    ///
    /// [`NetworkApi`](Self::NetworkApi) and [`Slice`](Self::Slice) are the two
    /// that need not — see [`crate::party`].
    #[must_use]
    pub const fn is_subscriber_bound(self) -> bool {
        !matches!(self, Self::NetworkApi | Self::Slice)
    }

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Voice => "VOICE",
            Self::Sms => "SMS",
            Self::Mms => "MMS",
            Self::Data => "DATA",
            Self::Ims => "IMS",
            Self::FixedAccess => "FIXED_ACCESS",
            Self::Iot => "IOT",
            Self::NetworkApi => "NETWORK_API",
            Self::Slice => "SLICE",
        }
    }
}

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

impl FromStr for ServiceDomain {
    type Err = ParseError;

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

// ── RecordType ───────────────────────────────────────────────────────────────

/// The specific kind of record, within a [`ServiceDomain`].
///
/// Exhaustive for the same reason [`ServiceDomain`] is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RecordType {
    /// A call placed by the subscriber, over any access (TS 32.250, TS 32.260).
    OriginatedCall,
    /// A call received by the subscriber.
    TerminatedCall,
    /// A call forwarded on the subscriber's behalf.
    ForwardedCall,
    /// A short message sent by the subscriber.
    SmsOriginated,
    /// A short message delivered to the subscriber.
    SmsTerminated,
    /// A multimedia message sent by the subscriber.
    MmsOriginated,
    /// A multimedia message retrieved by the subscriber.
    MmsTerminated,
    /// A 5G packet data unit session.
    PduSession,
    /// A pre-5G packet data protocol context.
    PdpContext,
    /// An IMS session other than telephony.
    ImsSession,
    /// A fixed broadband access session.
    FixedAccessSession,
    /// A single invocation of an exposed network API.
    NetworkApiInvocation,
    /// A network slice lifecycle event.
    SliceLifecycle,
    /// A registration or mobility event, charged in its own right.
    Registration,
}

impl RecordType {
    /// Every variant, for exhaustive iteration in tests and mappings.
    pub const ALL: &'static [Self] = &[
        Self::OriginatedCall,
        Self::TerminatedCall,
        Self::ForwardedCall,
        Self::SmsOriginated,
        Self::SmsTerminated,
        Self::MmsOriginated,
        Self::MmsTerminated,
        Self::PduSession,
        Self::PdpContext,
        Self::ImsSession,
        Self::FixedAccessSession,
        Self::NetworkApiInvocation,
        Self::SliceLifecycle,
        Self::Registration,
    ];

    /// The canonical codes, in the order of [`ALL`](Self::ALL).
    pub const CODES: &'static [&'static str] = &[
        "ORIGINATED_CALL",
        "TERMINATED_CALL",
        "FORWARDED_CALL",
        "SMS_MO",
        "SMS_MT",
        "MMS_MO",
        "MMS_MT",
        "PDU_SESSION",
        "PDP_CONTEXT",
        "IMS_SESSION",
        "FIXED_ACCESS_SESSION",
        "NETWORK_API_INVOCATION",
        "SLICE_LIFECYCLE",
        "REGISTRATION",
    ];

    /// The service domain this record type belongs to.
    ///
    /// Lets a decoder derive the domain rather than carry it twice, and lets a
    /// validator catch a record whose two fields disagree.
    #[must_use]
    pub const fn service_domain(self) -> ServiceDomain {
        match self {
            Self::OriginatedCall | Self::TerminatedCall | Self::ForwardedCall => {
                ServiceDomain::Voice
            }
            Self::SmsOriginated | Self::SmsTerminated => ServiceDomain::Sms,
            Self::MmsOriginated | Self::MmsTerminated => ServiceDomain::Mms,
            Self::PduSession | Self::PdpContext | Self::Registration => ServiceDomain::Data,
            Self::ImsSession => ServiceDomain::Ims,
            Self::FixedAccessSession => ServiceDomain::FixedAccess,
            Self::NetworkApiInvocation => ServiceDomain::NetworkApi,
            Self::SliceLifecycle => ServiceDomain::Slice,
        }
    }

    /// `true` for record types that describe a session, and can therefore be
    /// split across partial records.
    ///
    /// Event-shaped records — a short message, an API invocation — are produced
    /// whole, so an incomplete one is a different kind of problem.
    #[must_use]
    pub const fn is_session(self) -> bool {
        matches!(
            self,
            Self::OriginatedCall
                | Self::TerminatedCall
                | Self::ForwardedCall
                | Self::PduSession
                | Self::PdpContext
                | Self::ImsSession
                | Self::FixedAccessSession
                | Self::SliceLifecycle
        )
    }

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::OriginatedCall => "ORIGINATED_CALL",
            Self::TerminatedCall => "TERMINATED_CALL",
            Self::ForwardedCall => "FORWARDED_CALL",
            Self::SmsOriginated => "SMS_MO",
            Self::SmsTerminated => "SMS_MT",
            Self::MmsOriginated => "MMS_MO",
            Self::MmsTerminated => "MMS_MT",
            Self::PduSession => "PDU_SESSION",
            Self::PdpContext => "PDP_CONTEXT",
            Self::ImsSession => "IMS_SESSION",
            Self::FixedAccessSession => "FIXED_ACCESS_SESSION",
            Self::NetworkApiInvocation => "NETWORK_API_INVOCATION",
            Self::SliceLifecycle => "SLICE_LIFECYCLE",
            Self::Registration => "REGISTRATION",
        }
    }
}

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

impl FromStr for RecordType {
    type Err = ParseError;

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

// ── Provenance ───────────────────────────────────────────────────────────────

/// The network element that produced a record file.
///
/// Printable ASCII without whitespace, 1–64 characters — the same shape a
/// 3GPP TS 32.297 file name carries as its `NodeID`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NodeId(String);

/// The shape [`NodeId`] accepts, as rendered in a [`ParseError`].
const NODE_ID_FORMAT: &str =
    "1 to 64 printable ASCII characters without whitespace, such as pgw-fra-01";

impl NodeId {
    /// The identifier as written.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

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

impl FromStr for NodeId {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let t = s.trim();
        let ok = !t.is_empty()
            && t.len() <= 64
            && t.bytes()
                .all(|b| b.is_ascii_graphic() && !b.is_ascii_whitespace());
        if ok {
            Ok(Self(t.to_owned()))
        } else {
            Err(ParseError::format("NodeId", s, NODE_ID_FORMAT))
        }
    }
}

/// Where a record came from: which node, which file, which position.
///
/// Mandatory on every [`UsageRecord`]. The file sequence is what makes silent
/// loss detectable at all — a lost file leaves no trace in the surviving data,
/// and the only evidence is a hole in this number.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SourceRef {
    /// The network element that produced the file.
    pub node_id: NodeId,
    /// The file's name, where the transport supplied one.
    pub file_name: Option<String>,
    /// The file's sequence number within its source, where it carried one.
    ///
    /// `None` means completeness cannot be established for this record's file —
    /// which is a finding, not a neutral absence.
    pub file_sequence: Option<u32>,
    /// The record's zero-based position within its file.
    pub record_index: u32,
}

impl SourceRef {
    /// Provenance for a record at a known position in an unnamed stream.
    #[must_use]
    pub const fn new(node_id: NodeId, record_index: u32) -> Self {
        Self {
            node_id,
            file_name: None,
            file_sequence: None,
            record_index,
        }
    }

    /// The same provenance with the file's name recorded.
    #[must_use]
    pub fn with_file_name(mut self, name: impl Into<String>) -> Self {
        self.file_name = Some(name.into());
        self
    }

    /// The same provenance with the file's sequence number recorded.
    #[must_use]
    pub const fn with_file_sequence(mut self, sequence: u32) -> Self {
        self.file_sequence = Some(sequence);
        self
    }

    /// `true` when this record's file can take part in gap detection.
    #[must_use]
    pub const fn supports_completeness_check(&self) -> bool {
        self.file_sequence.is_some()
    }
}

impl fmt::Display for SourceRef {
    /// Writes `node[/file][#sequence]:index`, compact enough for a log line.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.node_id)?;
        if let Some(name) = &self.file_name {
            write!(f, "/{name}")?;
        }
        if let Some(sequence) = self.file_sequence {
            write!(f, "#{sequence}")?;
        }
        write!(f, ":{}", self.record_index)
    }
}

// ── CallDirection ────────────────────────────────────────────────────────────

/// Which way a call went, from the served subscriber's side.
///
/// Access-neutral: a call over a fixed line, VoLTE or a circuit-switched network
/// is the same kind of usage, measured by chargeable duration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CallDirection {
    /// The subscriber placed the call.
    Originated,
    /// The subscriber received the call.
    Terminated,
    /// The call was forwarded on the subscriber's behalf.
    Forwarded,
}

impl CallDirection {
    /// Every variant, for exhaustive iteration in tests and mappings.
    pub const ALL: &'static [Self] = &[Self::Originated, Self::Terminated, Self::Forwarded];

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

    /// The record type for a call in this direction.
    #[must_use]
    pub const fn record_type(self) -> RecordType {
        match self {
            Self::Originated => RecordType::OriginatedCall,
            Self::Terminated => RecordType::TerminatedCall,
            Self::Forwarded => RecordType::ForwardedCall,
        }
    }

    /// The canonical string form.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Originated => "ORIGINATED",
            Self::Terminated => "TERMINATED",
            Self::Forwarded => "FORWARDED",
        }
    }
}

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

impl FromStr for CallDirection {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::ALL
            .iter()
            .find(|d| d.as_str().eq_ignore_ascii_case(s.trim()))
            .copied()
            .ok_or_else(|| ParseError::one_of("CallDirection", s, Self::CODES))
    }
}

// ── UsageRecord ──────────────────────────────────────────────────────────────

/// One unit of usage, normalised out of whatever format carried it.
///
/// The canonical internal form. Decoders for 3GPP CDRs, TAP batches, RADIUS
/// accounting and the 5G charging service all target this type, so that no
/// wire format's idiosyncrasies reach the rules that operate on it.
///
/// Immutable by convention: a correction is a new record that supersedes an old
/// one, never an edit. Once a batch's counts have been reported, editing one of
/// its records invalidates the report with no way left to reconcile what was
/// released against what is now stored.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UsageRecord {
    /// The specific kind of record, from which the service domain follows.
    pub record_type: RecordType,
    /// Who the usage is charged to.
    pub party: ChargedParty,
    /// The equipment involved, where the record identified it.
    pub equipment: Option<Pei>,
    /// The SIM the subscription was served on, where the record carried it.
    ///
    /// Distinct from the subscription itself: a subscription outlives the cards
    /// it has been issued on, so counting cards and counting subscriptions give
    /// different answers and both are sometimes wanted.
    pub iccid: Option<Iccid>,
    /// The network where the usage happened.
    ///
    /// `None` for management-plane usage that no access network served.
    pub serving_plmn: Option<PlmnId>,
    /// The network that owns the subscription.
    pub home_plmn: Option<PlmnId>,
    /// When the usage began, in local time with its offset.
    pub opened: CdrTimestamp,
    /// When the usage ended, where it has ended.
    pub closed: Option<CdrTimestamp>,
    /// The **chargeable** duration the record carried.
    ///
    /// Never derived from [`opened`](Self::opened) and [`closed`](Self::closed)
    /// — see [`wall_clock_span`](Self::wall_clock_span).
    pub duration: Option<Seconds>,
    /// Transferred volume, uplink and downlink kept apart.
    pub volume: Option<VolumePair>,
    /// A count of discrete events, for event-shaped services.
    pub events: Option<u32>,
    /// Per-rating-group usage, where the record broke its total down.
    ///
    /// Kept alongside the record-level total rather than replacing it: the two
    /// are separate statements of one fact and can disagree, which is worth
    /// reporting rather than resolving by fiat.
    pub containers: ContainerSet,
    /// This record's place in a run of partials, when it is one.
    pub partial: Option<PartialInfo>,
    /// The data network, slice and access technology the session ran on.
    pub session: SessionContext,
    /// The identifiers that tie this record to its session and its siblings.
    pub correlation: CorrelationKeys,
    /// The calling party's number (the A-number), where the record carried one.
    pub calling: Option<Msisdn>,
    /// Where the call went (the B-number), normalised — see
    /// [`DialContext::normalise`](crate::DialContext::normalise).
    pub called: Option<Destination>,
    /// Which file, from which node, at which position produced this record.
    pub provenance: SourceRef,
}

impl UsageRecord {
    /// A record with only the fields every record must have.
    ///
    /// Quantities are attached with the `with_*` methods, so a record can never
    /// be constructed with a quantity and no provenance, or with a close time
    /// and no open time.
    #[must_use]
    pub const fn new(
        record_type: RecordType,
        party: ChargedParty,
        opened: CdrTimestamp,
        provenance: SourceRef,
    ) -> Self {
        Self {
            record_type,
            party,
            equipment: None,
            iccid: None,
            serving_plmn: None,
            home_plmn: None,
            opened,
            closed: None,
            duration: None,
            volume: None,
            events: None,
            containers: ContainerSet::empty(),
            partial: None,
            session: SessionContext::none(),
            correlation: CorrelationKeys::none(),
            calling: None,
            called: None,
            provenance,
        }
    }

    /// A call record: who is charged, when it was answered, and how long it was
    /// charged for.
    ///
    /// The one-call way to bring a voice platform's row into the crate. The close
    /// time is `answered + duration`, so the record splits at tariff boundaries
    /// ([`split_session`](crate::split_session)) without a second field.
    /// `answered` is the **answer** time: a platform that reports seizure time
    /// must convert, or the split counts ringing as talking.
    ///
    /// ```rust
    /// use mediation::{
    ///     CallDirection, ChargedParty, Destination, RecordKey, SourceRef, Seconds,
    ///     SubscriberRef, UsageRecord, CdrTimestamp, QuarterHourOffset,
    /// };
    /// use time::macros::datetime;
    ///
    /// let berlin = QuarterHourOffset::from_minutes(60).unwrap();
    /// let party = ChargedParty::Subscriber(SubscriberRef::new("nai-line-4711@fibre.example".parse()?));
    /// let call = UsageRecord::call(
    ///     CallDirection::Originated,
    ///     party,
    ///     CdrTimestamp::new(datetime!(2026-07-28 18:00), berlin).unwrap(),
    ///     Seconds::new(95),
    ///     SourceRef::new("sbc-ber-1".parse()?, 0),
    /// )
    /// .with_calling("+49301234567".parse()?)
    /// .with_called("+491511234567".parse::<Destination>()?)
    /// .with_call_id("sbc-ber-1".parse()?, "a84b4c76e66710@sbc-ber-1".parse()?);
    ///
    /// assert!(RecordKey::of(&call).is_ok());
    /// assert_eq!(call.wall_clock_span(), Some(Seconds::new(95)));
    /// # Ok::<(), mediation::ParseError>(())
    /// ```
    #[must_use]
    pub fn call(
        direction: CallDirection,
        party: ChargedParty,
        answered: CdrTimestamp,
        duration: Seconds,
        provenance: SourceRef,
    ) -> Self {
        let closed = answered
            .to_utc()
            .checked_add(time::Duration::seconds(i64::from(duration.get())))
            .and_then(|end| CdrTimestamp::from_utc(end, answered.offset()));
        let mut record = Self::new(direction.record_type(), party, answered, provenance);
        record.closed = closed;
        record.duration = Some(duration);
        record
    }

    /// The same record with the calling number (A-number).
    #[must_use]
    pub const fn with_calling(mut self, calling: Msisdn) -> Self {
        self.calling = Some(calling);
        self
    }

    /// The same record with the called destination (B-number).
    #[must_use]
    pub const fn with_called(mut self, called: Destination) -> Self {
        self.called = Some(called);
        self
    }

    /// The same record keyed by a SIP Call-ID and the entity that recorded it.
    ///
    /// Both, because a back-to-back user agent regenerates the Call-ID per leg —
    /// see [`RecordKey::Call`](crate::RecordKey::Call).
    #[must_use]
    pub fn with_call_id(mut self, entity: NodeId, call_id: CallId) -> Self {
        self.correlation.recording_entity = Some(entity);
        self.correlation.call_id = Some(call_id);
        self
    }

    /// The service domain, derived from the record type.
    ///
    /// Not a stored field: a second copy of the same fact eventually disagrees
    /// with the first, and there is no correct way to resolve the disagreement
    /// after the fact.
    #[must_use]
    pub const fn service(&self) -> ServiceDomain {
        self.record_type.service_domain()
    }

    /// The same record with its session correlators.
    #[must_use]
    pub fn with_correlation(mut self, correlation: CorrelationKeys) -> Self {
        self.correlation = correlation;
        self
    }

    /// The same record with its per-rating-group breakdown.
    #[must_use]
    pub fn with_containers(mut self, containers: ContainerSet) -> Self {
        self.containers = containers;
        self
    }

    /// The same record with the network, slice and access it ran on.
    #[must_use]
    pub fn with_session(mut self, session: SessionContext) -> Self {
        self.session = session;
        self
    }

    /// The same record marked as one partial of a run.
    #[must_use]
    pub const fn with_partial(mut self, partial: PartialInfo) -> Self {
        self.partial = Some(partial);
        self
    }

    /// `true` when this record is one of a run rather than a whole session.
    #[must_use]
    pub const fn is_partial(&self) -> bool {
        self.partial.is_some()
    }

    /// The volume to bill: the record's own total, or the container sum when
    /// it has none.
    ///
    /// A record that carries only a breakdown still described real usage, and
    /// dropping it for want of a record-level field would lose it. Where both
    /// are present the record's own total wins — and
    /// [`containers_agree`](Self::containers_agree) is how a caller learns they
    /// disagree before trusting either.
    #[must_use]
    pub fn billable_volume(&self) -> Option<VolumePair> {
        self.volume.or_else(|| {
            if self.containers.is_empty() {
                None
            } else {
                self.containers.checked_total()
            }
        })
    }

    /// Whether the record-level volume agrees with its container breakdown.
    ///
    /// `None` when there is nothing to compare — no containers, no total, or a
    /// sum that overflows. See [`ContainerSet::discrepancy`] for how far out a
    /// disagreement is.
    #[must_use]
    pub fn containers_agree(&self) -> Option<bool> {
        if self.containers.is_empty() {
            return None;
        }
        self.containers.agrees_with(self.volume)
    }

    /// The same record with a close time.
    #[must_use]
    pub const fn with_closed(mut self, closed: CdrTimestamp) -> Self {
        self.closed = Some(closed);
        self
    }

    /// The same record with a chargeable duration.
    #[must_use]
    pub const fn with_duration(mut self, duration: Seconds) -> Self {
        self.duration = Some(duration);
        self
    }

    /// The same record with a transferred volume.
    #[must_use]
    pub const fn with_volume(mut self, volume: VolumePair) -> Self {
        self.volume = Some(volume);
        self
    }

    /// The same record with an event count.
    #[must_use]
    pub const fn with_events(mut self, events: u32) -> Self {
        self.events = Some(events);
        self
    }

    /// The same record with the equipment identified.
    #[must_use]
    pub const fn with_equipment(mut self, equipment: Pei) -> Self {
        self.equipment = Some(equipment);
        self
    }

    /// The same record with the SIM identified.
    #[must_use]
    pub const fn with_iccid(mut self, iccid: Iccid) -> Self {
        self.iccid = Some(iccid);
        self
    }

    /// The same record with its serving and home networks.
    #[must_use]
    pub const fn with_networks(mut self, serving: PlmnId, home: PlmnId) -> Self {
        self.serving_plmn = Some(serving);
        self.home_plmn = Some(home);
        self
    }

    /// The elapsed time between the open and close timestamps.
    ///
    /// **Not** the chargeable duration, and not a substitute for it. Useful for
    /// plausibility checks — a chargeable duration exceeding the wall-clock span
    /// is impossible — and for nothing that reaches an invoice.
    ///
    /// `None` when the record has not closed, or when it closed before it
    /// opened, which is a broken record rather than a negative duration.
    #[must_use]
    pub fn wall_clock_span(&self) -> Option<Seconds> {
        let closed = self.closed?;
        let elapsed = (closed.to_utc() - self.opened.to_utc()).whole_seconds();
        u32::try_from(elapsed).ok().map(Seconds::new)
    }

    /// Whether the usage happened outside the subscription's home network.
    ///
    /// `None` when either network is unknown: a record that cannot establish
    /// this is not a domestic record, and defaulting it to `false` understates
    /// roaming in exactly the direction that costs money.
    #[must_use]
    pub fn is_roaming(&self) -> Option<bool> {
        let (serving, home) = (self.serving_plmn?, self.home_plmn?);
        Some(!serving.is_same_network(home))
    }

    /// The local calendar date the usage began on — the day a subscriber would
    /// recognise on an itemised bill.
    #[must_use]
    pub fn local_date(&self) -> time::Date {
        self.opened.local_date()
    }

    /// `true` when the record carries no quantity at all.
    ///
    /// Zero volume, zero duration and zero events together mean the record
    /// describes nothing; whether that is chargeable is a policy question, but
    /// it is always worth surfacing.
    ///
    /// The volume is [`billable_volume`](Self::billable_volume), not the
    /// record-level field: a packet-data record commonly carries its volume
    /// **only** in its containers, and reading the absent record total as zero
    /// would call a five-gigabyte session empty — which a suppression policy
    /// then deliberately does not bill.
    #[must_use]
    pub fn is_zero_usage(&self) -> bool {
        self.duration.is_none_or(Seconds::is_zero)
            && self.billable_volume().is_none_or(VolumePair::is_zero)
            && self.events.is_none_or(|e| e == 0)
    }
}

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

#[cfg(feature = "serde")]
mod serde_impl {
    use super::{CallDirection, NodeId, RecordType, ServiceDomain, SourceRef, UsageRecord};
    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.collect_str(self)
                }
            }

            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!(ServiceDomain, "a service domain code such as \"DATA\"");
    string_serde!(RecordType, "a record type code such as \"PDU_SESSION\"");
    string_serde!(NodeId, "a node identifier such as \"pgw-fra-01\"");
    string_serde!(CallDirection, "a call direction such as \"ORIGINATED\"");

    /// Field names are part of the public representation and covered by semver.
    #[derive(Serialize, Deserialize)]
    #[serde(rename = "SourceRef")]
    struct SourceRefRepr {
        node_id: NodeId,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        file_name: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        file_sequence: Option<u32>,
        record_index: u32,
    }

    impl Serialize for SourceRef {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            SourceRefRepr {
                node_id: self.node_id.clone(),
                file_name: self.file_name.clone(),
                file_sequence: self.file_sequence,
                record_index: self.record_index,
            }
            .serialize(serializer)
        }
    }

    impl<'de> Deserialize<'de> for SourceRef {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            let r = SourceRefRepr::deserialize(deserializer)?;
            Ok(SourceRef {
                node_id: r.node_id,
                file_name: r.file_name,
                file_sequence: r.file_sequence,
                record_index: r.record_index,
            })
        }
    }

    #[derive(Serialize, Deserialize)]
    #[serde(rename = "UsageRecord")]
    struct UsageRecordRepr {
        record_type: RecordType,
        party: crate::party::ChargedParty,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        equipment: Option<crate::identity::Pei>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        iccid: Option<crate::identity::Iccid>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        serving_plmn: Option<crate::plmn::PlmnId>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        home_plmn: Option<crate::plmn::PlmnId>,
        opened: crate::cdrtime::CdrTimestamp,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        closed: Option<crate::cdrtime::CdrTimestamp>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        duration_s: Option<crate::cdrtime::Seconds>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        volume: Option<crate::volume::VolumePair>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        events: Option<u32>,
        #[serde(
            default,
            skip_serializing_if = "crate::container::ContainerSet::is_empty"
        )]
        containers: crate::container::ContainerSet,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        partial: Option<crate::partial::PartialInfo>,
        #[serde(
            default,
            skip_serializing_if = "crate::session::SessionContext::is_empty"
        )]
        session: crate::session::SessionContext,
        #[serde(
            default,
            skip_serializing_if = "crate::correlation::CorrelationKeys::is_empty"
        )]
        correlation: crate::correlation::CorrelationKeys,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        calling: Option<crate::identity::Msisdn>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        called: Option<crate::dialplan::Destination>,
        provenance: SourceRef,
    }

    impl Serialize for UsageRecord {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            UsageRecordRepr {
                record_type: self.record_type,
                party: self.party.clone(),
                equipment: self.equipment,
                iccid: self.iccid,
                serving_plmn: self.serving_plmn,
                home_plmn: self.home_plmn,
                opened: self.opened,
                closed: self.closed,
                duration_s: self.duration,
                volume: self.volume,
                events: self.events,
                containers: self.containers.clone(),
                partial: self.partial,
                session: self.session.clone(),
                correlation: self.correlation.clone(),
                calling: self.calling,
                called: self.called,
                provenance: self.provenance.clone(),
            }
            .serialize(serializer)
        }
    }

    impl<'de> Deserialize<'de> for UsageRecord {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            let r = UsageRecordRepr::deserialize(deserializer)?;
            Ok(UsageRecord {
                record_type: r.record_type,
                party: r.party,
                equipment: r.equipment,
                iccid: r.iccid,
                serving_plmn: r.serving_plmn,
                home_plmn: r.home_plmn,
                opened: r.opened,
                closed: r.closed,
                duration: r.duration_s,
                volume: r.volume,
                events: r.events,
                containers: r.containers,
                partial: r.partial,
                session: r.session,
                correlation: r.correlation,
                calling: r.calling,
                called: r.called,
                provenance: r.provenance,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::party::SubscriberRef;
    use crate::volume::Octets;

    fn party() -> ChargedParty {
        ChargedParty::Subscriber(SubscriberRef::new("imsi-262021234567890".parse().unwrap()))
    }

    fn source() -> SourceRef {
        SourceRef::new("pgw-fra-01".parse().unwrap(), 0)
    }

    fn call() -> UsageRecord {
        UsageRecord::new(
            RecordType::OriginatedCall,
            party(),
            "2026-07-28T10:00:00+02:00".parse().unwrap(),
            source(),
        )
    }

    /// The bug this API shape exists to prevent: chargeable duration is not the
    /// wall-clock span, and no accessor may quietly substitute one for the other.
    #[test]
    fn chargeable_duration_and_wall_clock_span_are_different_numbers() {
        let record = call()
            .with_closed("2026-07-28T10:00:55+02:00".parse().unwrap())
            .with_duration(Seconds::new(10));

        assert_eq!(record.duration, Some(Seconds::new(10)));
        assert_eq!(record.wall_clock_span(), Some(Seconds::new(55)));

        // An unanswered call has a span but no chargeable duration, and nothing
        // fills the gap.
        let unanswered = call().with_closed("2026-07-28T10:00:45+02:00".parse().unwrap());
        assert_eq!(unanswered.duration, None);
        assert_eq!(unanswered.wall_clock_span(), Some(Seconds::new(45)));
    }

    #[test]
    fn a_record_that_closed_before_it_opened_has_no_span() {
        let broken = call().with_closed("2026-07-28T09:59:00+02:00".parse().unwrap());
        assert_eq!(broken.wall_clock_span(), None);
        // An open record has no span either — for a different reason.
        assert_eq!(call().wall_clock_span(), None);
    }

    /// The span is computed on instants, so a session that crossed a DST
    /// transition measures correctly even though the wall clock jumped.
    #[test]
    fn a_span_across_a_dst_transition_measures_the_real_elapsed_time() {
        // 02:00+01:00 to 03:30+02:00 — the clock advanced 90 minutes on the
        // wall but only 30 minutes actually elapsed.
        let record = UsageRecord::new(
            RecordType::PduSession,
            party(),
            "2026-03-29T01:50:00+01:00".parse().unwrap(),
            source(),
        )
        .with_closed("2026-03-29T03:20:00+02:00".parse().unwrap());

        assert_eq!(record.wall_clock_span(), Some(Seconds::new(30 * 60)));
    }

    #[test]
    fn roaming_is_unknown_rather_than_false_when_a_network_is_missing() {
        let home: PlmnId = "262-02".parse().unwrap();
        let visited: PlmnId = "310-260".parse().unwrap();

        assert_eq!(call().is_roaming(), None);
        assert_eq!(call().with_networks(home, home).is_roaming(), Some(false));
        assert_eq!(call().with_networks(visited, home).is_roaming(), Some(true));
    }

    #[test]
    fn zero_usage_is_detected_across_all_three_quantities() {
        assert!(call().is_zero_usage());
        assert!(call().with_duration(Seconds::ZERO).is_zero_usage());
        assert!(!call().with_duration(Seconds::new(1)).is_zero_usage());
        assert!(
            !call()
                .with_volume(VolumePair::new(Octets::ZERO, Octets::new(1)))
                .is_zero_usage()
        );
        assert!(!call().with_events(1).is_zero_usage());
    }

    /// The service domain is derived, so a record cannot disagree with itself
    /// about which service produced it.
    #[test]
    fn the_service_domain_follows_from_the_record_type() {
        assert_eq!(call().service(), ServiceDomain::Voice);

        let session = UsageRecord::new(
            RecordType::PduSession,
            party(),
            "2026-07-28T10:00:00+02:00".parse().unwrap(),
            source(),
        );
        assert_eq!(session.service(), ServiceDomain::Data);
        assert_eq!(session.service(), session.record_type.service_domain());
    }

    /// Correlators default to absent, and attaching them does not disturb the
    /// quantities.
    #[test]
    fn correlators_are_optional_and_additive() {
        use crate::correlation::{ChargingId, CorrelationKeys};
        use std::net::Ipv4Addr;

        assert!(call().correlation.packet_session().is_none());

        let keyed = call().with_correlation(
            CorrelationKeys::default()
                .with_charging_id(ChargingId::new(7))
                .with_gateway(Ipv4Addr::new(10, 0, 0, 1).into()),
        );
        assert!(keyed.correlation.packet_session().is_some());
        assert_eq!(keyed.duration, call().duration);
    }

    #[test]
    fn every_record_type_maps_to_a_domain_and_every_code_round_trips() {
        assert_eq!(RecordType::ALL.len(), RecordType::CODES.len());
        for (r, code) in RecordType::ALL.iter().zip(RecordType::CODES) {
            assert_eq!(r.as_str(), *code);
            assert_eq!(code.parse::<RecordType>().unwrap(), *r);
            assert!(ServiceDomain::ALL.contains(&r.service_domain()));
        }

        assert_eq!(ServiceDomain::ALL.len(), ServiceDomain::CODES.len());
        for (d, code) in ServiceDomain::ALL.iter().zip(ServiceDomain::CODES) {
            assert_eq!(d.as_str(), *code);
            assert_eq!(code.parse::<ServiceDomain>().unwrap(), *d);
        }
    }

    /// The two domains that need not belong to a subscription are exactly the
    /// two that made the charged party a sum type.
    #[test]
    fn only_network_api_and_slice_are_subscriber_free() {
        for d in ServiceDomain::ALL {
            let expected = !matches!(d, ServiceDomain::NetworkApi | ServiceDomain::Slice);
            assert_eq!(d.is_subscriber_bound(), expected, "{d}");
        }
    }

    /// Completeness cannot be established without a sequence number, and the
    /// record says so rather than implying it is fine.
    #[test]
    fn provenance_reports_whether_gaps_are_detectable() {
        let bare = source();
        assert!(!bare.supports_completeness_check());
        assert_eq!(bare.to_string(), "pgw-fra-01:0");

        let full = source()
            .with_file_name("CDR-20260728-0001")
            .with_file_sequence(1);
        assert!(full.supports_completeness_check());
        assert_eq!(full.to_string(), "pgw-fra-01/CDR-20260728-0001#1:0");
    }

    #[test]
    fn node_identifiers_reject_whitespace_and_overlong_input() {
        assert!("pgw-fra-01".parse::<NodeId>().is_ok());
        for s in ["", "has space", &"x".repeat(65)] {
            assert!(s.parse::<NodeId>().is_err(), "{s:?} must not parse");
        }
    }

    #[test]
    fn sessions_and_events_are_distinguished() {
        assert!(RecordType::PduSession.is_session());
        assert!(RecordType::OriginatedCall.is_session());
        assert!(!RecordType::SmsOriginated.is_session());
        assert!(!RecordType::NetworkApiInvocation.is_session());
    }

    #[test]
    fn the_primary_measure_matches_the_domain() {
        assert_eq!(
            ServiceDomain::Voice.primary_measure(),
            PrimaryMeasure::Duration
        );
        assert_eq!(
            ServiceDomain::Data.primary_measure(),
            PrimaryMeasure::Volume
        );
        assert_eq!(ServiceDomain::Sms.primary_measure(), PrimaryMeasure::Events);
        assert_eq!(
            ServiceDomain::NetworkApi.primary_measure(),
            PrimaryMeasure::Events
        );
    }
}