Reading

Read a DATEX II v3 XML document into typed Rust data with datex2::v3::read: the Document, publications, envelopes, shared fields of polymorphic types and warnings.

One function

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = std::io::BufReader::new(std::fs::File::open("incidents.xml")?);
    let doc = datex2::v3::read(file)?;
    println!("{} publications, {} warnings", doc.publications.len(), doc.warnings.len());
    Ok(())
}

read takes anything that implements BufRead: a file in a BufReader, a byte slice, a decompressing reader. It returns a Document:

FieldContent
publicationsThe publications found, in document order. One for a payload; none, one or several for a messageContainer.
envelopeThe shape of the document: Payload, MessageContainer or Publication.
soapWhether the document was wrapped in a SOAP envelope.
warningsEvery deviation the reader tolerated.
suppressed_warningsHow many further warnings were counted but not kept.

read holds the whole result in memory, about 1.7 times the size of the XML. For large feeds use stream.

What it accepts

DocumentResult
Root payload with xsi:typeOne publication
Root payload without xsi:typeOne publication, its type inferred from its content, and a warning
Root messageContainerEvery payload in it, and its exchange information in exchange_information
A root named after the publication type (SituationPublication)One publication
SOAP 1.1 or 1.2 around a payload or message containerThe same as without SOAP
Anything elseAn error naming the root element and its namespace

Namespace prefixes do not matter; names are matched by namespace URI. A byte order mark, either line-end style and a missing XML declaration are all fine. The input must be UTF-8.

Publications

Publication is an enum with one variant per publication type. Match on the ones you handle:

use datex2::v3::Publication;

fn describe(publication: &Publication) -> String {
    match publication {
        Publication::Situation(p) => format!("{} situations", p.situation.len()),
        Publication::MeasuredData(p) => format!("{} site measurements", p.site_measurements.len()),
        Publication::Vms(p) => format!("{} sign controllers", p.vms_controller_status.len()),
        _ => "another kind of publication".to_owned(),
    }
}

What every publication has is available on the enum itself:

fn header(publication: &datex2::v3::Publication) {
    println!("{:?} {:?}", publication.lang(), publication.publication_time());
    if let Some(creator) = publication.publication_creator() {
        println!("{:?}/{:?}", creator.country, creator.national_identifier);
    }
}

Polymorphic types

Where the standard has an abstract class, the crate has an enum of its concrete kinds, chosen by the document's xsi:type. SituationRecord has 28 kinds, LocationReference 10. Each such enum has an accessor for every field its kinds share, so common questions need no match:

use datex2::v3::situation::SituationRecord;

fn is_active(record: &SituationRecord) -> bool {
    use datex2::v3::common::ValidityStatusEnum::Active;
    record.validity().and_then(|v| v.validity_status.as_ref()) == Some(&Active)
}

An accessor returns Option<&T> for a single value, Option<&str> for text and &[T] for a list.

Values

In the documentIn Rust
TextString
Multilingual textMultilingualString, with get("en") and first()
EnumerationAn enum; a value outside the standard list is Extended(String)
TimestampDateTime, kept exactly as written: nine fractional digits, the offset, or no offset at all
Decimal amount (prices)Decimal, the digits as written, never a float
Numbersf32, f64, i32, i64, u64; measure types are aliases such as KilometresPerHour = f32
Reference to another objectVersionedReference { id, version } or Reference { id }
Extension contentExtension, the content as written, with the namespace bindings it uses

A DateTime without a time zone has offset_minutes == None and no unix_timestamp(); the reader does not guess a zone.

Limits

read applies default limits to the input. To change one:

fn read_deep(xml: &[u8]) -> Result<datex2::v3::Document, datex2::Error> {
    let mut limits = datex2::Limits::default();
    limits.max_depth = 200;
    datex2::v3::read_with(xml, &limits)
}