datex2 0.3.0

Unofficial DATEX II implementation in Rust: read, stream and write road traffic and travel information (v3 XML and JSON, including the Mobilithek AFIR profile) as typed data.
Documentation
// @generated by `cargo xtask codegen` from the DATEX II 3.7 schemas. Do not edit.
//! Types of the DATEX II namespace `http://datex2.eu/schema/3/messageContainer`.
//!
//! Names follow the standard; see <https://docs.datex2.eu/> for what each one means.

#[allow(unused_imports)]
use super::{common as com, common_extension as comx, location_referencing as loc, location_extension as locx, situation as sit, road_traffic_data as roa, vms, parking as prk, energy_infrastructure as egi, afir_energy_infrastructure as aegi, facilities as fac, afir_facilities as afac, traffic_regulation as tro, controlled_zone as cz, traffic_management_plan as tmp, rerouting_management_enhanced as rer, urban_extensions as ubx, fault_and_status as fst, exchange_information as ex, information_management as inf, cis_information as cis};
#[allow(unused_imports)]
use crate::out::{Out, Pattern, Rules};
#[allow(unused_imports)]
use crate::json::{JsonReader, JsonWriter};
#[allow(unused_imports)]
use crate::{ErrorKind, WarningKind};
#[allow(unused_imports)]
use crate::xml::{Enumeration, Xml};
#[allow(unused_imports)]
use crate::{Date, DateTime, Decimal, Error, Extension, MultilingualString, Time};

/// XML type `con:MessageContainer`.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct MessageContainer {
    /// XML attribute `extensionName`.
    pub extension_name: Option<String>,
    /// XML attribute `extensionVersion`.
    pub extension_version: Option<String>,
    /// XML attribute `profileName`.
    pub profile_name: Option<String>,
    /// XML attribute `profileVersion`.
    pub profile_version: Option<String>,
    /// XML element `con:payload`.
    pub payload: Vec<com::PayloadPublication>,
    /// XML element `con:cisInformation`.
    pub cis_information: Option<Box<cis::CISInformation>>,
    /// XML element `con:exchangeInformation`.
    pub exchange_information: Option<Box<ex::ExchangeInformation>>,
    /// XML element `con:informationManagement`.
    pub information_management: Option<Box<inf::InformationManagement>>,
    /// XML element `con:_messageContainerExtension`.
    pub message_container_extension: Option<Box<Extension>>,
}

impl MessageContainer {
    const FIELDS: &'static [u32] = &[0xf0a55, 0xf0742, 0xf082a, 0xf08c1, 0xf0518];

    pub(crate) fn read(r: &mut Xml<'_>) -> Result<Self, Error> {
        r.expect_type(0xf0115);
        let _ = r.attr(2439);
        let a1 = r.attr(2104);
        let a2 = r.attr(2105);
        let a3 = r.attr(2760);
        let a4 = r.attr(2761);
        r.attrs_done();
        let mut e0 = Vec::new();
        let mut e1 = None;
        let mut e2 = None;
        let mut e3 = None;
        let mut e4 = None;
        while let Some(mut key) = r.next_child()? {
            loop {
                match key {
                    0xf0a55 => { let v = com::PayloadPublication::read(r)?; e0.extend(v); }
                    0xf0742 => { let v = Some(cis::CISInformation::read(r)?); r.set(&mut e1, v.map(Box::new), 1858); }
                    0xf082a => { let v = Some(ex::ExchangeInformation::read(r)?); r.set(&mut e2, v.map(Box::new), 2090); }
                    0xf08c1 => { let v = Some(inf::InformationManagement::read(r)?); r.set(&mut e3, v.map(Box::new), 2241); }
                    0xf0518 => { let v = r.raw()?; r.set(&mut e4, v.map(Box::new), 1304); }
                    _ => match r.fallback(key, Self::FIELDS) {
                        Some(k) => { key = k; continue; }
                        None => r.skip_unknown(key)?,
                    },
                }
                break;
            }
        }
        r.required(&e2, 2090);
        let v = Self {
            extension_name: a1,
            extension_version: a2,
            profile_name: a3,
            profile_version: a4,
            payload: e0,
            cis_information: e1,
            exchange_information: e2,
            information_management: e3,
            message_container_extension: e4,
        };
        r.close();
        Ok(v)
    }
}

impl MessageContainer {
    pub(crate) fn write(&self, w: &mut Out<'_>, tag: &'static str, kind: &'static str, fixed: &'static str) -> Result<(), Error> {
        w.begin(tag)?;
        w.constant_attrs(kind)?;
        w.constant_attrs(fixed)?;
        w.constant_attrs(" modelBaseVersion=\"3\"")?;
        if let Some(v) = &self.extension_name { w.attr("extensionName", v)?; }
        if let Some(v) = &self.extension_version { w.attr("extensionVersion", v)?; }
        if let Some(v) = &self.profile_name { w.attr("profileName", v)?; }
        if let Some(v) = &self.profile_version { w.attr("profileVersion", v)?; }
        for v in &self.payload { v.write(w, "con:payload")?; }
        if let Some(v) = &self.cis_information { v.write(w, "con:cisInformation", "", "")?; }
        match &self.exchange_information {
            Some(v) => { v.write(w, "con:exchangeInformation", "", "")?; }
            None => return Err(w.missing("con:exchangeInformation")),
        }
        if let Some(v) = &self.information_management { v.write(w, "con:informationManagement", "", "")?; }
        if let Some(v) = &self.message_container_extension { w.extension("con:_messageContainerExtension", v)?; }
        w.end(tag)
    }
}

impl MessageContainer {
    pub(crate) fn read_json(r: &mut JsonReader<'_>) -> Result<Option<Self>, Error> {
        if !r.begin_object()? {
            return Ok(None);
        }
        Self::read_json_members(r, None).map(Some)
    }

    /// Reads the members of an object that is already open; `pending` is a member key read
    /// already.
    pub(crate) fn read_json_members(r: &mut JsonReader<'_>, mut pending: Option<u32>) -> Result<Self, Error> {
        let mut seen = [false; 9];
        let mut f0 = None;
        let mut f1 = None;
        let mut f2 = None;
        let mut f3 = None;
        let mut f4 = Vec::new();
        let mut f5 = None;
        let mut f6 = None;
        let mut f7 = None;
        let mut f8 = None;
        loop {
            let key = match pending.take() {
                Some(key) => key,
                None => match r.next_key()? {
                    Some(key) => key,
                    None => break,
                },
            };
            match key {
                1544 => if r.first(&mut seen[4])? { let mut l = r.list()?; while r.item(&mut l)? { if let Some(v) = com::PayloadPublication::read_json(r)? { f4.push(v); } } },
                333 => if r.first(&mut seen[5])? { f5 = cis::CISInformation::read_json(r)?.map(Box::new); },
                716 => if r.first(&mut seen[6])? { f6 = ex::ExchangeInformation::read_json(r)?.map(Box::new); },
                988 => if r.first(&mut seen[7])? { f7 = inf::InformationManagement::read_json(r)?.map(Box::new); },
                444 => if r.first(&mut seen[8])? { f8 = r.raw_object()?.map(Box::new); },
                _ => r.skip_unknown()?,
            }
        }
        r.required(&f6, 716);
        Ok(Self {
            extension_name: f0,
            extension_version: f1,
            profile_name: f2,
            profile_version: f3,
            payload: f4,
            cis_information: f5,
            exchange_information: f6,
            information_management: f7,
            message_container_extension: f8,
        })
    }

    pub(crate) fn write_json(&self, w: &mut JsonWriter<'_>, target_class: Option<&'static str>) -> Result<(), Error> {
        w.begin_object(623)?;
        if let Some(class) = target_class {
            w.target_class(class)?;
        }
        if self.extension_name.is_some() {
            return Err(w.no_place("extensionName"));
        }
        if self.extension_version.is_some() {
            return Err(w.no_place("extensionVersion"));
        }
        if self.profile_name.is_some() {
            return Err(w.no_place("profileName"));
        }
        if self.profile_version.is_some() {
            return Err(w.no_place("profileVersion"));
        }
        if !self.payload.is_empty() {
            w.key(1544)?;
            w.begin_array()?;
            for v in &self.payload {
                w.item()?;
                v.write_json(w)?;
            }
            w.end_array()?;
        }
        if let Some(v) = &self.cis_information {
            w.key(333)?;
            v.write_json(w, None)?;
        }
        match &self.exchange_information {
            Some(v) => {
                w.key(716)?;
                v.write_json(w, None)?;
            }
            None => w.absent(716, w.missing("exchangeInformation"))?,
        }
        if let Some(v) = &self.information_management {
            w.key(988)?;
            v.write_json(w, None)?;
        }
        if let Some(v) = &self.message_container_extension {
            w.key(444)?;
            w.extension(v)?;
        }
        w.end_object()
    }
}