mediation 0.2.0

Telecom usage mediation domain library — one-pass batch processing with a conservation law that proves no record was lost, 3GPP CDR local-time-plus-offset arithmetic (quarter-hour offsets, DST-adjusted), integer octet volumes, validated identifiers (IMSI/MSISDN/IMEI/ICCID/SUPI/GPSI/PEI/SUCI, PLMN, TADIG), natural-key deduplication, RFC 1982 wrap-aware sequence-gap detection, partial-record reassembly, cross-node correlation, signed restatement, a numbered validation catalogue, wire codecs for TBCD/BCD, ISDN-AddressString, the PLMN identity and the full User Location Information element, a zero-copy BER reader, usage aggregation, roaming-batch reconciliation, longest-prefix number ranges with portability, conserving tariff-band apportionment, two-question jurisdiction determination and an EU/German regulatory layer. Zero I/O, no async, no clock, exact integer quantities, and no input can panic a parser.
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
//! Data volumes as **integer octets**, and the unit arithmetic around them.
//!
//! # Why not a float
//!
//! A data volume is inherently an integer count of octets, so a floating-point
//! representation is not merely imprecise — it is categorically wrong. Above
//! 2^53 octets (about 9.01 PB) a 64-bit float cannot represent consecutive
//! integers, and a national monthly aggregate crosses that threshold. Counts
//! would then be dropped silently, in the one direction nobody audits.
//!
//! Nothing in this crate converts a volume to a float, not even transiently.
//! A CI lane greps the source for float types to keep it that way.
//!
//! # Why not a total
//!
//! [`VolumePair`] keeps uplink and downlink apart and offers no constructor
//! that takes only a total. Asymmetric tariffs, zero-rated uplink and
//! interconnect settlement all need the two directions separately, and once
//! they are summed the split cannot be recovered. Pre-summing at ingest is the
//! kind of lossy normalisation that looks tidy for a year and then cannot be
//! undone.
//!
//! # Why the unit names are strict
//!
//! Operators bill in both powers of ten and powers of two, sometimes in the
//! same tariff sheet, and "MB" is read as either. This module follows
//! **IEC 80000-13** without exception: `kB` is 1 000 octets and `KiB` is 1 024.
//! Exactly one unit — the octet — reaches storage, and conversion happens only
//! at the boundary, explicitly:
//!
//! ```rust
//! use mediation::{ByteScale, Octets};
//!
//! assert_eq!(Octets::from_scaled(5, ByteScale::Mega).unwrap().get(), 5_000_000);
//! assert_eq!(Octets::from_scaled(5, ByteScale::Mebi).unwrap().get(), 5_242_880);
//! ```
//!
//! A contract that says "5 MB" without saying which is ambiguous in the
//! contract, not in this crate. Resolve it where the ambiguity lives.
//!
//! # Rounding is a rating decision
//!
//! Rounding a volume up to a whole chargeable unit is priced behaviour, so it
//! is offered here as a named primitive and applied by nothing. No aggregation,
//! reassembly or validation path in this crate rounds a quantity. What leaves
//! is the measured value.

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

use crate::error::ParseError;

// ── ByteScale ────────────────────────────────────────────────────────────────

/// A multiple of the octet, per IEC 80000-13.
///
/// The decimal prefixes (`kB`, `MB`, …) are powers of 1 000 and the binary ones
/// (`KiB`, `MiB`, …) powers of 1 024. Both are offered because both appear in
/// real tariffs; neither is inferred from the other.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ByteScale {
    /// A single octet.
    Byte,
    /// 10^3 octets.
    Kilo,
    /// 2^10 octets.
    Kibi,
    /// 10^6 octets.
    Mega,
    /// 2^20 octets.
    Mebi,
    /// 10^9 octets.
    Giga,
    /// 2^30 octets.
    Gibi,
    /// 10^12 octets.
    Tera,
    /// 2^40 octets.
    Tebi,
}

impl ByteScale {
    /// Every variant, coarsest last.
    pub const ALL: &'static [Self] = &[
        Self::Byte,
        Self::Kilo,
        Self::Kibi,
        Self::Mega,
        Self::Mebi,
        Self::Giga,
        Self::Gibi,
        Self::Tera,
        Self::Tebi,
    ];

    /// The canonical symbols, in the order of [`ALL`](Self::ALL).
    pub const CODES: &'static [&'static str] =
        &["B", "kB", "KiB", "MB", "MiB", "GB", "GiB", "TB", "TiB"];

    /// How many octets one unit of this scale is.
    #[must_use]
    pub const fn factor(self) -> u64 {
        match self {
            Self::Byte => 1,
            Self::Kilo => 1_000,
            Self::Kibi => 1_024,
            Self::Mega => 1_000_000,
            Self::Mebi => 1_048_576,
            Self::Giga => 1_000_000_000,
            Self::Gibi => 1_073_741_824,
            Self::Tera => 1_000_000_000_000,
            Self::Tebi => 1_099_511_627_776,
        }
    }

    /// `true` for the binary prefixes (`KiB`, `MiB`, `GiB`, `TiB`).
    #[must_use]
    pub const fn is_binary(self) -> bool {
        matches!(self, Self::Kibi | Self::Mebi | Self::Gibi | Self::Tebi)
    }

    /// The canonical symbol.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Byte => "B",
            Self::Kilo => "kB",
            Self::Kibi => "KiB",
            Self::Mega => "MB",
            Self::Mebi => "MiB",
            Self::Giga => "GB",
            Self::Gibi => "GiB",
            Self::Tera => "TB",
            Self::Tebi => "TiB",
        }
    }
}

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

impl FromStr for ByteScale {
    type Err = ParseError;

    /// Parses the IEC 80000-13 symbols.
    ///
    /// # A lower-case `b` is a **bit**
    ///
    /// IEC 80000-13 gives `B` to the byte and `b` to the bit, so `kb` is a
    /// kilobit and `kB` a kilobyte. Accepting `kb` as a kilobyte is a
    /// factor-of-eight error in exactly the direction this module exists to
    /// prevent — a silent unit confusion that produces a plausible number — so
    /// the bit spellings are **rejected** rather than helpfully coerced. A
    /// tariff sheet that says "kb" and means kilobytes is ambiguous in the
    /// tariff sheet; resolve it there.
    ///
    /// `KB` is accepted as `kB`, because that capitalisation is ubiquitous and
    /// is not a bit spelling — the binary kilobyte is `KiB`, which differs by
    /// more than case. The binary prefixes are matched case-insensitively for
    /// the same reason: the `i` disambiguates them on its own.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let t = s.trim();
        match t {
            "B" | "octet" | "octets" | "byte" | "bytes" => return Ok(Self::Byte),
            "kB" | "KB" => return Ok(Self::Kilo),
            "MB" => return Ok(Self::Mega),
            "GB" => return Ok(Self::Giga),
            "TB" => return Ok(Self::Tera),
            _ => {}
        }
        for scale in Self::ALL.iter().filter(|s| s.is_binary()) {
            if t.eq_ignore_ascii_case(scale.as_str()) {
                return Ok(*scale);
            }
        }
        Err(ParseError::one_of("ByteScale", s, Self::CODES))
    }
}

// ── Octets ───────────────────────────────────────────────────────────────────

/// A data volume, counted in octets.
///
/// Deliberately implements no [`Add`](std::ops::Add): every sum is checked,
/// because summing partial records and per-container volumes is precisely where
/// an overflow would arise, and a wrapped total is a plausible-looking wrong
/// number that reaches an invoice.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Octets(u64);

impl Octets {
    /// No volume at all.
    pub const ZERO: Self = Self(0);

    /// A volume from a raw octet count.
    #[must_use]
    pub const fn new(octets: u64) -> Self {
        Self(octets)
    }

    /// The raw octet count.
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }

    /// `true` for a zero volume.
    #[must_use]
    pub const fn is_zero(self) -> bool {
        self.0 == 0
    }

    /// A volume from a count of larger units, e.g. 5 [`Mega`](ByteScale::Mega).
    ///
    /// `None` on overflow, so a corrupt unit count cannot wrap into a small
    /// plausible volume.
    #[must_use]
    pub const fn from_scaled(count: u64, scale: ByteScale) -> Option<Self> {
        match count.checked_mul(scale.factor()) {
            Some(v) => Some(Self(v)),
            None => None,
        }
    }

    /// Sum, `None` on overflow.
    #[must_use]
    pub const fn checked_add(self, other: Self) -> Option<Self> {
        match self.0.checked_add(other.0) {
            Some(v) => Some(Self(v)),
            None => None,
        }
    }

    /// Sum, saturating at [`u64::MAX`].
    ///
    /// For the few places a running total must keep going rather than stop —
    /// a regulatory ratio, a diagnostic counter. Saturating is the lesser evil
    /// there: `u64::MAX` octets is 18 exabytes and visibly wrong, while
    /// discarding the operand silently loses a record from a comparison that
    /// decides something.
    ///
    /// Anything that reaches an invoice uses [`checked_add`](Self::checked_add)
    /// instead, and stops.
    #[must_use]
    pub const fn saturating_add(self, other: Self) -> Self {
        Self(self.0.saturating_add(other.0))
    }

    /// Difference, `None` when it would go below zero.
    ///
    /// A negative volume is not a small volume; it is a broken counter, and the
    /// caller should say so rather than receive a saturated zero.
    #[must_use]
    pub const fn checked_sub(self, other: Self) -> Option<Self> {
        match self.0.checked_sub(other.0) {
            Some(v) => Some(Self(v)),
            None => None,
        }
    }

    /// The number of whole chargeable units this volume occupies, rounding up.
    ///
    /// A rating primitive. Nothing in this crate calls it — see the module
    /// documentation.
    ///
    /// ```rust
    /// use mediation::{ByteScale, Octets};
    ///
    /// // One octet over a kilobyte is two chargeable kilobytes.
    /// assert_eq!(Octets::new(1_001).units_ceil(ByteScale::Kilo), 2);
    /// assert_eq!(Octets::new(1_000).units_ceil(ByteScale::Kilo), 1);
    /// assert_eq!(Octets::ZERO.units_ceil(ByteScale::Kilo), 0);
    /// ```
    #[must_use]
    pub const fn units_ceil(self, scale: ByteScale) -> u64 {
        let factor = scale.factor();
        self.0.div_ceil(factor)
    }

    /// The number of whole units this volume contains, discarding a remainder.
    #[must_use]
    pub const fn units_floor(self, scale: ByteScale) -> u64 {
        self.0 / scale.factor()
    }

    /// This volume rounded up to a whole multiple of `scale`.
    ///
    /// A rating primitive; `None` on overflow.
    #[must_use]
    pub const fn round_up_to(self, scale: ByteScale) -> Option<Self> {
        match self.units_ceil(scale).checked_mul(scale.factor()) {
            Some(v) => Some(Self(v)),
            None => None,
        }
    }
}

impl fmt::Display for Octets {
    /// Writes the plain octet count, with no unit suffix.
    ///
    /// A suffix would imply that some other unit is also canonical. Exactly one
    /// is.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for Octets {
    type Err = ParseError;

    /// Parses a plain octet count.
    ///
    /// Deliberately does **not** accept a unit suffix: `"5MB"` would have to
    /// guess between 5 000 000 and 5 242 880. Parse the number and the
    /// [`ByteScale`] separately, then call [`from_scaled`](Self::from_scaled).
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.trim().parse::<u64>().map(Self).map_err(|_| {
            ParseError::format(
                "Octets",
                s,
                "a non-negative whole number of octets, with no unit suffix",
            )
        })
    }
}

impl From<Octets> for u64 {
    fn from(o: Octets) -> Self {
        o.0
    }
}

impl From<u64> for Octets {
    fn from(o: u64) -> Self {
        Self(o)
    }
}

// ── VolumePair ───────────────────────────────────────────────────────────────

/// Uplink and downlink volumes, kept apart.
///
/// There is no constructor taking a total. See the module documentation for
/// why: the split is not recoverable once lost, and several downstream
/// decisions need it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct VolumePair {
    uplink: Octets,
    downlink: Octets,
}

impl VolumePair {
    /// Both directions zero.
    pub const ZERO: Self = Self {
        uplink: Octets::ZERO,
        downlink: Octets::ZERO,
    };

    /// A pair from its two directions.
    #[must_use]
    pub const fn new(uplink: Octets, downlink: Octets) -> Self {
        Self { uplink, downlink }
    }

    /// Volume sent by the subscriber.
    #[must_use]
    pub const fn uplink(self) -> Octets {
        self.uplink
    }

    /// Volume received by the subscriber.
    #[must_use]
    pub const fn downlink(self) -> Octets {
        self.downlink
    }

    /// Both directions summed, `None` on overflow.
    ///
    /// Checked, and a method rather than a field, so that a total is always
    /// derived at the point of use and never stored in place of the pair.
    #[must_use]
    pub const fn checked_total(self) -> Option<Octets> {
        self.uplink.checked_add(self.downlink)
    }

    /// `true` when both directions are zero.
    #[must_use]
    pub const fn is_zero(self) -> bool {
        self.uplink.is_zero() && self.downlink.is_zero()
    }

    /// Element-wise sum, `None` on overflow in either direction.
    ///
    /// Used to fold the containers of a record, or the partials of a session,
    /// without ever collapsing the two directions.
    #[must_use]
    pub const fn checked_add(self, other: Self) -> Option<Self> {
        match (
            self.uplink.checked_add(other.uplink),
            self.downlink.checked_add(other.downlink),
        ) {
            (Some(up), Some(down)) => Some(Self::new(up, down)),
            _ => None,
        }
    }
}

impl fmt::Display for VolumePair {
    /// Writes `uplink/downlink` in octets — one string, both numbers, no total.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.uplink, self.downlink)
    }
}

/// The shape [`VolumePair`] accepts, as rendered in a [`ParseError`].
const VOLUME_PAIR_FORMAT: &str = "two octet counts separated by a slash, such as 1024/8192";

impl FromStr for VolumePair {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let err = || ParseError::format("VolumePair", s, VOLUME_PAIR_FORMAT);
        let (up, down) = s.trim().split_once('/').ok_or_else(err)?;
        Ok(Self::new(
            up.parse().map_err(|_| err())?,
            down.parse().map_err(|_| err())?,
        ))
    }
}

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

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

    impl Serialize for Octets {
        /// A JSON number — a volume is a quantity, not a label.
        ///
        /// JSON numbers are read as doubles by many parsers, which is the very
        /// failure this type exists to avoid; a consumer handling volumes above
        /// 2^53 octets should decode into an integer type explicitly.
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.serialize_u64(self.get())
        }
    }

    impl<'de> Deserialize<'de> for Octets {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            u64::deserialize(deserializer).map(Octets::new)
        }
    }

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

    impl<'de> Deserialize<'de> for ByteScale {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            struct V;
            impl Visitor<'_> for V {
                type Value = ByteScale;
                fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    f.write_str("an IEC 80000-13 byte unit such as \"MB\" or \"MiB\"")
                }
                fn visit_str<E: de::Error>(self, v: &str) -> Result<ByteScale, E> {
                    v.parse().map_err(de::Error::custom)
                }
            }
            deserializer.deserialize_str(V)
        }
    }

    /// A struct with two named octet fields, so neither direction can be read
    /// as the other and neither is optional.
    #[derive(Serialize, Deserialize)]
    #[serde(rename = "VolumePair")]
    struct VolumePairRepr {
        uplink_octets: Octets,
        downlink_octets: Octets,
    }

    impl Serialize for VolumePair {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            VolumePairRepr {
                uplink_octets: self.uplink(),
                downlink_octets: self.downlink(),
            }
            .serialize(serializer)
        }
    }

    impl<'de> Deserialize<'de> for VolumePair {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            let r = VolumePairRepr::deserialize(deserializer)?;
            Ok(VolumePair::new(r.uplink_octets, r.downlink_octets))
        }
    }
}

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

    #[test]
    fn decimal_and_binary_prefixes_are_different_numbers() {
        assert_eq!(
            Octets::from_scaled(5, ByteScale::Mega).unwrap().get(),
            5_000_000
        );
        assert_eq!(
            Octets::from_scaled(5, ByteScale::Mebi).unwrap().get(),
            5_242_880
        );
        assert!(ByteScale::Mebi.is_binary() && !ByteScale::Mega.is_binary());
    }

    /// The volume this type exists for: past 2^53 octets a float loses counts,
    /// while the integer stays exact.
    #[test]
    fn volumes_beyond_the_float_horizon_stay_exact() {
        const TWO_POW_53: u64 = 9_007_199_254_740_992;
        let big = Octets::new(TWO_POW_53);
        let one_more = big.checked_add(Octets::new(1)).unwrap();
        assert_eq!(one_more.get(), TWO_POW_53 + 1);
        assert_ne!(one_more, big);
    }

    #[test]
    fn arithmetic_is_checked_in_both_directions() {
        assert_eq!(Octets::new(u64::MAX).checked_add(Octets::new(1)), None);
        assert_eq!(Octets::new(1).checked_sub(Octets::new(2)), None);
        assert_eq!(
            Octets::new(3).checked_sub(Octets::new(2)),
            Some(Octets::new(1))
        );
        assert_eq!(Octets::from_scaled(u64::MAX, ByteScale::Tera), None);
    }

    /// The two sums differ deliberately: one stops, one keeps going visibly.
    #[test]
    fn saturating_and_checked_sums_fail_differently() {
        let huge = Octets::new(u64::MAX);
        assert_eq!(huge.checked_add(Octets::new(1)), None);
        assert_eq!(huge.saturating_add(Octets::new(1)), huge);
        assert_eq!(
            Octets::new(2).saturating_add(Octets::new(3)),
            Octets::new(5)
        );
    }

    #[test]
    fn chargeable_units_round_up_and_floor_down() {
        let v = Octets::new(1_001);
        assert_eq!(v.units_ceil(ByteScale::Kilo), 2);
        assert_eq!(v.units_floor(ByteScale::Kilo), 1);
        assert_eq!(v.round_up_to(ByteScale::Kilo), Some(Octets::new(2_000)));

        assert_eq!(Octets::ZERO.units_ceil(ByteScale::Kilo), 0);
        assert_eq!(Octets::new(1_000).units_ceil(ByteScale::Kilo), 1);
    }

    /// The pair keeps two numbers; a total is derived, never stored.
    #[test]
    fn uplink_and_downlink_stay_apart() {
        let v = VolumePair::new(Octets::new(100), Octets::new(900));
        assert_eq!(v.uplink().get(), 100);
        assert_eq!(v.downlink().get(), 900);
        assert_eq!(v.checked_total(), Some(Octets::new(1_000)));
        assert_eq!(v.to_string(), "100/900");
        assert_eq!("100/900".parse::<VolumePair>().unwrap(), v);
    }

    #[test]
    fn pair_addition_folds_each_direction_independently() {
        let a = VolumePair::new(Octets::new(1), Octets::new(2));
        let b = VolumePair::new(Octets::new(10), Octets::new(20));
        assert_eq!(
            a.checked_add(b),
            Some(VolumePair::new(Octets::new(11), Octets::new(22)))
        );

        let huge = VolumePair::new(Octets::new(u64::MAX), Octets::ZERO);
        assert_eq!(huge.checked_add(a), None);
    }

    /// A unit suffix on a bare number would have to guess a scale, so it is
    /// rejected rather than assumed.
    #[test]
    fn octets_reject_a_unit_suffix() {
        assert_eq!("5000".parse::<Octets>().unwrap(), Octets::new(5_000));
        for s in ["5MB", "5 MB", "5.0", "-1", ""] {
            assert!(s.parse::<Octets>().is_err(), "{s:?} must not parse");
        }
    }

    #[test]
    fn byte_scale_symbols_round_trip() {
        for scale in ByteScale::ALL {
            assert_eq!(scale.as_str().parse::<ByteScale>().unwrap(), *scale);
        }
        // The capitalised decimal kilobyte is unambiguous under IEC and accepted.
        assert_eq!("KB".parse::<ByteScale>().unwrap(), ByteScale::Kilo);
        assert_eq!("KiB".parse::<ByteScale>().unwrap(), ByteScale::Kibi);
        assert!("MBps".parse::<ByteScale>().is_err());
    }

    #[test]
    fn every_scale_code_matches_its_variant() {
        assert_eq!(ByteScale::ALL.len(), ByteScale::CODES.len());
        for (scale, code) in ByteScale::ALL.iter().zip(ByteScale::CODES) {
            assert_eq!(scale.as_str(), *code);
        }
    }
}