DATEX II v3 · XML and JSON · read · stream · write

European road traffic data, as Rust types.

datex2 reads, streams and writes DATEX II, the European standard for road traffic and travel information. One call turns a national feed, XML or JSON, into typed data. The reader tolerates what real feeds send and tells you what it tolerated; the writer produces only documents the official schemas accept, including AFIR charging data for Germany's Mobilithek.

cargo add datex2
use datex2::v3::{self, Publication};
use std::{fs::File, io::BufReader};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = BufReader::new(File::open("incidents.xml")?);
    let doc = v3::read(file)?;

    for publication in &doc.publications {
        if let Publication::Situation(p) = publication {
            println!("{} situations", p.situation.len());
        }
    }
    for warning in &doc.warnings {
        eprintln!("{warning}"); // what was tolerated, and where
    }
    Ok(())
}
19
DATEX II 3.7 namespaces
158 MB/s
read on one core
8 MB
to stream a 189 MB feed
96
streamable record types
0
lines of unsafe

Four rules it keeps

Road operators, access points and charging operators across Europe publish DATEX II. The standard is large and real feeds bend it. These rules are what makes the data usable anyway.

01

Tolerant in

Namespaces under http://levelC/…, a payload without xsi:type, required elements left out, a decimal comma in a number: the reader accepts the deviations national feeds really have, and reads the rest of the document.

02

Nothing silent

Every deviation is a warning with its kind, the element path and the byte offset. Unknown elements are reported, extension content is kept as it was written, and no value is ever made up.

03

Strict out

The writer checks required content, list sizes and every value rule of the schema. When it succeeds, the official 3.7 schemas, or the Mobilithek profile's, accept the document; when it cannot, it names the place, the rule and the schema type. It never rounds.

04

Safe on untrusted input

Nesting, attributes and token length are bounded while reading, document type declarations are refused, every text in an error is made safe to log, and the reader is fuzzed. There is no unsafe.

No setup

You do not state a version, a namespace, a schema or an envelope. The crate works on the bytes you give it and opens no files or sockets of its own.

  • Any document shape. A payload, a messageContainer with several payloads, a root named after the publication, and SOAP around any of them.
  • Any 3.x release. The types are generated from the official 3.7 schemas and read documents of every 3.x release.
  • Names from the standard. SituationRecord is SituationRecord, so the official documentation applies to the Rust types.
  • Shared fields without matching. record.validity() works for an accident, roadworks and every other kind of situation record.

How reading works →

You want toCall
Read a documentv3::read(input)
Process a large feed record by recordv3::stream::<T>(input)
Publish a publication or a message containerv3::write(sink, &publication), v3::write(sink, &container)
Read or write JSONv3::json::read(input), v3::json::write(…)
Publish charging data for the Mobilithekmobilithek::write(sink, &table), mobilithek::snapshot(status, at)
See what a file containscargo run --example inspect

What is implemented

Every publication of DATEX II 3.7, in XML and JSON.

AreaState
Situations (EN 16157-3)Accidents, roadworks, closures, speed management and every other situation record
Traffic data (EN 16157-5)Measured data, elaborated data, measurement site tables
Variable message signs (EN 16157-4)VMS publications and tables
Parking (CEN/TS 16157-6)Parking tables and status
Energy infrastructure (CEN/TS 16157-10)Charging and refuelling points: the 2022 namespaces and the AFIR namespaces of 3.7
Traffic regulations and controlled zones (CEN/TS 16157-11)Regulation orders, low-emission and access zones
Traffic management, facilities, devices and faultsTraffic management plans, rerouting, facilities and rates, device and fault publications
Location referencing (EN 16157-2)Coordinates, linear and area locations, Alert-C, TPEG-Loc and OpenLR data, as typed values
JSONThe official 3.7 mapping, and the AFIR Recharging profile the Mobilithek requires, read and written with exact numbers
Reading, streaming, writingWhole documents, one record at a time in bounded memory, and schema-valid output

Read the documentation →

And what is not

Named, so that you do not have to find out.

DATEX II version 2

This release handles version 3. The version 2 model, which several countries still publish, is not read yet.

Exchange and transport

No HTTP client, no SOAP services, no Exchange 2020 protocol. A messageContainer yields its payloads and its exchange information, such as snapshot or delta, and nothing more.

Older shapes of draft namespaces

Parking, controlled-zone and traffic-regulation documents written against releases before 3.6 lose the elements that 3.7 renamed; each is reported.

All limits →