π‘ mediation
The arithmetic between a mobile network and a billing system, as a domain library: normalising, validating, deduplicating and correlating usage records β and proving it did not lose any.
π§ Zero I/O Β· β±οΈ no async Β· π°οΈ no clock Β· π’ exact integer quantities Β· π no
unsafe
It computes usage, not money. There is no currency, tariff, price or rate type anywhere in the crate. What leaves here is octets, seconds and event counts, attributed to a charged party, a network and a service β which a rating layer then prices.
π Full documentation β Β· π¦ API reference β
π¦ Install
serde is the only feature flag, off by default; it adds
Serialize/Deserialize on every public type in its canonical string form.
MSRV Rust 1.94 (edition 2024), pinned in
rust-toolchain.toml and verified by its own CI lane.
API stability pre-1.0, so breaking changes ship as minor releases.
βοΈ The claim
A pipeline moves records. A mediation layer can prove it did not lose any:
carried_in + ingested == billable + suppressed + duplicate + rejected + held
That equation is a method β BatchReconciliation::balances() β and the crate is
built so it holds by construction. A batch that fails it has unknown
completeness, and billing over unknown completeness invoices an amount nobody
can prove is right.
use ;
let mut counts = EMPTY;
counts.observe;
counts.observe;
counts.observe;
assert_eq!;
assert_eq!;
assert!;
Held records come back, so PeriodLedger chains batches and the law telescopes
across a whole billing period rather than holding only inside each one.
π One pass over a batch
Every piece works on its own. process is the assembly, and it exists because
the order of the pieces is itself a decision that is easy to get wrong
quietly β deduplicating after rejecting reports one fault twice; holding a
partial run after counting it billable double-bills the session when its
successor arrives.
use ;
use ;
# use ;
# use Ipv4Addr;
#
let records: = .map.collect;
let mut state = new;
let outcome = process;
assert!;
assert_eq!;
assert!;
// The files to ask a partner to resend, by name rather than by count.
for gap in outcome.sequence_gaps
Deduplication and gap detection are inherently stateful, so ProcessState is
caller-owned and passed in. That keeps process a pure mapping from
(batch, config, as_of, state) to (outcome, stateβ²) β which is what makes a
replay reproducible in 2026 and in 2031. The state round-trips through serde,
because a restart that forgot it would re-bill a window of retransmissions and
declare every source complete.
The order, and why it is that one β
π― Four things it refuses to get wrong
π°οΈ Time is local, plus a quarter-hour offset
A charging record carries a local wall clock and a numeric offset to UTC (3GPP TS 32.298), and the offset counts quarter-hours because a great many zones are not on an hour boundary. Reading it as UTC books every evening event of a UTC+X network onto the wrong day β and unlike a fixed-zone mistake, no constant fixes it downstream, because the offset varies per record.
use CdrTimestamp;
let ts: CdrTimestamp = "2026-07-28T00:30:00+05:45".parse.unwrap;
assert_eq!; // the subscriber's day
assert_eq!; // β¦is not the UTC day
CdrTimestamp implements no ordering at all, because comparing two records from
different offsets by their local fields silently interleaves them.
More β
π’ Quantities are integers
A data volume is a count, so a float is not merely imprecise but categorically
wrong: above 2^53 octets β about 9.01 PB, which a national monthly aggregate
crosses β consecutive integers are no longer representable. Octets is a u64,
Seconds a u32, and CI greps the source for a float.
use ;
// Uplink and downlink never merge: asymmetric tariffs and interconnect
// settlement need the split, and it cannot be recovered once summed.
let volume = new;
assert_eq!;
// "MB" is ambiguous, so it is not a storage unit here β the scale is named.
assert_eq!;
assert_eq!;
assert_eq!;
Rounding up to whole chargeable units is priced behaviour, so it is offered as a named primitive and applied by nothing. More β
π Loss leaves exactly one trace
A lost record file leaves no trace in the surviving data β no short total, no
complaint, the usage simply was never billed. The only evidence is a hole in a
per-source sequence number, and the trap is the wrap at 99999 β 00001, where
three naive checks break once per cycle.
use ;
let last = new.unwrap;
let next = new.unwrap;
// RFC 1982: across the wrap the successor is the successor, not a 99 998-file gap.
assert!;
assert_eq!;
Holes are stored as runs, so a two-billion-file gap costs three numbers and the count stays exact β and a source that cannot account for itself has its records held, while every other source's release. More β
π‘οΈ Every input is untrusted
A mediation layer reads records that arrived over a network from a partner's
equipment, so no input may panic a parser or a decoder. Every FromStr and
every wire decoder returns a Result for arbitrary input, and a property suite
hammers all of them on every build. It has already caught three real defects: two
UTF-8 boundaries and a stack overflow on nested indefinite BER lengths.
Two consequences are visible in the API. Anything sized by the input is bounded β but never at the cost of the count, which stays exact. And nothing is silently skipped: a decoder that stepped over a field it could not read would produce a record missing exactly the fields it failed on, which is worse than no record. More β
ποΈ What is in it
| Module | Contents |
|---|---|
error |
ParseError, DecodeError β one per input kind, string and bytes |
digits |
DigitString β inline digits, plus the TBCD and plain-BCD codecs |
ber |
BerReader, BerElement, BerTag β the X.690 framing a CDR arrives in |
cdrtime |
CdrTimestamp, QuarterHourOffset, DstAdjustment, MsTimeZone, Seconds |
volume |
Octets, VolumePair, ByteScale β integer data volumes |
plmn |
Mcc, Mnc, PlmnId, TadigCode β network identity and its wire form |
identity |
Imsi, Msisdn, Imei, Imeisv, Iccid, Supi, Gpsi, Pei, Suci, AddressString |
party |
ChargedParty, SubscriberRef, TenantRef β who the usage is charged to |
jurisdiction |
usage_country(), customer_country_presumption() β two questions, two countries |
location |
UserLocation, Uli, Cgi, Sai, Rai, Lai, Ecgi, Ncgi, Tai |
numbering |
NumberRange, NumberRangeSet, Porting, terminating_plmn |
session |
Dnn, Snssai, RatType, ChargingCharacteristics |
correlation |
CorrelationKeys, ChargingId, Icid, correlate(), LegChain |
container |
ServiceDataContainer, ContainerSet, RatingGroup, ChangeCondition |
record |
UsageRecord, RecordType, ServiceDomain, SourceRef |
lateness |
LatenessPolicy, Lateness β one concept behind every window |
sequence |
SequenceSpace, SequenceTracker, SequenceSnapshot β wrap-aware gaps |
dedup |
RecordKey, DedupStore, DedupWindow β natural-key deduplication |
partial |
PartialInfo, PartialRun, Reassembly, RecordClosureCause |
pipeline |
process(), Batch, ProcessState, ProcessOutcome |
disposition |
Disposition, Outcome, BatchReconciliation, PeriodLedger |
validation |
validate_records(), Rule (V01βV21), Finding, Severity |
quality |
score_batch(), QualityGrade, ReleaseBlocker β the release gate |
aggregation |
aggregate_released(), UsageTotal β what the batch adds up to |
timeband |
split_session(), split_measured(), apportion() |
restatement |
Restatement, UsageDelta, RestatementLog β corrections as signed events |
roaming |
TapBatch, BatchTotals, ScaledAmount, RapReturn |
fairuse |
assess(), FairUseAssessment, FairUseAlert β EU 2016/2286 |
retention |
RetentionPolicy, RetentionSchedule, Deletion |
privacy |
Pseudonymiser, Reduction β narrowing an identity to the question |
fraud |
Signal, Indication, impossible_travel() β evidence, never verdicts |
π« What it is not
| Not this | Why | Where it belongs |
|---|---|---|
| A collector | GTPβ², SFTP pulls, Kafka consumers and Diameter peers are I/O | your ingest service |
| A rater | no currency, tariff plan, bundle, discount or balance type exists here | your charging engine |
| An online charging function | Ro/Nchf is a live, latency-bound conversation with a CHF | your CHF / OCS |
| A pipeline framework | no scheduler, no retry, no queue, no state store | your orchestrator |
| A rules GUI | rules are compiled functions with a cited clause, not a scripting runtime | β |
| An ASN.1 compiler | the framing is here; the generated TS 32.298 schema is not | a sibling crate |
Parsing is not I/O: decoding a record from a &[u8] you already hold is a
pure function, so codecs are in scope and transports are not.
π§ͺ Testing
Three properties get proptest attention because they are the crate's promises:
determinism (same input, same output, across shuffles), conservation
(nothing is created or destroyed), and totality (nothing panics, at any
input).
Every Rust block in this README and on the documentation site is compiled and
run as a doctest β see ReadmeDoctests and site_doctests in
src/lib.rs. Documentation that claims to be tested and is not is
worse than documentation that claims nothing, because a reader trusts it.
π€ Contributing
Issues and pull requests are welcome at github.com/hupe1980/mediation.
Please make sure just ci passes before opening a PR. New rules should cite the
clause they implement in a doc comment and come with a test derived from a
published example wherever one exists.
π License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
βΉοΈ This crate implements arithmetic and classification rules derived from published standards and regulations. It is not legal advice and carries no warranty of regulatory compliance β verify against the current text for your use case.