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
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
// @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/informationManagement`.
//!
//! 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, message_container as con, exchange_information as ex, 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 enumeration `inf:ManagementTypeEnum`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ManagementTypeEnum {
    /// `cancelled`
    Cancelled,
    /// `closed`
    Closed,
    /// A value outside the standard list.
    Extended(String),
}

impl ManagementTypeEnum {
    /// The value as written in a document.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Cancelled => "cancelled",
            Self::Closed => "closed",
            Self::Extended(v) => v,
        }
    }
}

impl Enumeration for ManagementTypeEnum {
    fn from_literal(literal: &str) -> Option<Self> {
        match literal {
            "cancelled" => Some(Self::Cancelled),
            "closed" => Some(Self::Closed),
            _ => None,
        }
    }

    fn extended(value: String) -> Self {
        Self::Extended(value)
    }
}

impl ManagementTypeEnum {
    /// The literal to write and, for a value outside the standard list, that value.
    #[allow(unreachable_patterns)]
    pub(crate) fn literal(&self) -> (&str, Option<&str>) {
        match self {
            Self::Extended(v) => ("_extended", Some(v)),
            other => (other.as_str(), None),
        }
    }
}

/// XML type `inf:ElementReference`.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ElementReference {
    /// XML element `inf:managementStatus`.
    pub management_status: Option<ManagementTypeEnum>,
    /// XML element `inf:reference`.
    pub reference: Option<Box<com::Reference>>,
    /// XML element `inf:versionedReference`.
    pub versioned_reference: Option<Box<com::VersionedReference>>,
    /// XML element `inf:_elementReferenceExtension`.
    pub element_reference_extension: Option<Box<Extension>>,
}

impl ElementReference {
    const FIELDS: &'static [u32] = &[0xc0932, 0xc0b01, 0xc0cb0, 0xc049e];

    pub(crate) fn read(r: &mut Xml<'_>) -> Result<Self, Error> {
        r.expect_type(0xc0099);
        r.attrs_done();
        let mut e0 = None;
        let mut e1 = None;
        let mut e2 = None;
        let mut e3 = None;
        while let Some(mut key) = r.next_child()? {
            loop {
                match key {
                    0xc0932 => { let v = r.enumeration::<ManagementTypeEnum>()?; r.set(&mut e0, v, 2354); }
                    0xc0b01 => { let v = Some(com::Reference::read(r)?); r.set(&mut e1, v.map(Box::new), 2817); }
                    0xc0cb0 => { let v = Some(com::VersionedReference::read(r)?); r.set(&mut e2, v.map(Box::new), 3248); }
                    0xc049e => { let v = r.raw()?; r.set(&mut e3, v.map(Box::new), 1182); }
                    _ => match r.fallback(key, Self::FIELDS) {
                        Some(k) => { key = k; continue; }
                        None => r.skip_unknown(key)?,
                    },
                }
                break;
            }
        }
        r.required(&e0, 2354);
        let v = Self {
            management_status: e0,
            reference: e1,
            versioned_reference: e2,
            element_reference_extension: e3,
        };
        r.close();
        Ok(v)
    }
}

impl ElementReference {
    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)?;
        match &self.management_status {
            Some(v) => { w.enumeration("inf:managementStatus", v.literal())?; }
            None => return Err(w.missing("inf:managementStatus")),
        }
        if let Some(v) = &self.reference { v.write(w, "inf:reference", "", "")?; }
        if let Some(v) = &self.versioned_reference { v.write(w, "inf:versionedReference", "", "")?; }
        if let Some(v) = &self.element_reference_extension { w.extension("inf:_elementReferenceExtension", v)?; }
        w.end(tag)
    }
}

impl ElementReference {
    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; 4];
        let mut f0 = None;
        let mut f1 = None;
        let mut f2 = None;
        let mut f3 = None;
        loop {
            let key = match pending.take() {
                Some(key) => key,
                None => match r.next_key()? {
                    Some(key) => key,
                    None => break,
                },
            };
            match key {
                1255 => if r.first(&mut seen[0])? { f0 = r.enumeration::<ManagementTypeEnum>()?; },
                1817 => if r.first(&mut seen[1])? { f1 = com::Reference::read_json(r)?.map(Box::new); },
                2581 => if r.first(&mut seen[2])? { f2 = com::VersionedReference::read_json(r)?.map(Box::new); },
                983 => if r.first(&mut seen[3])? { f3 = r.raw_object()?.map(Box::new); },
                _ => r.skip_unknown()?,
            }
        }
        r.required(&f0, 1255);
        Ok(Self {
            management_status: f0,
            reference: f1,
            versioned_reference: f2,
            element_reference_extension: f3,
        })
    }

    pub(crate) fn write_json(&self, w: &mut JsonWriter<'_>, target_class: Option<&'static str>) -> Result<(), Error> {
        w.begin_object(469)?;
        if let Some(class) = target_class {
            w.target_class(class)?;
        }
        match &self.management_status {
            Some(v) => {
                w.key(1255)?;
                w.enumeration(472, v.literal())?;
            }
            None => w.absent(1255, w.missing("managementStatus"))?,
        }
        if let Some(v) = &self.reference {
            w.key(1817)?;
            v.write_json(w, None)?;
        }
        if let Some(v) = &self.versioned_reference {
            w.key(2581)?;
            v.write_json(w, None)?;
        }
        if let Some(v) = &self.element_reference_extension {
            w.key(983)?;
            w.extension(v)?;
        }
        w.end_object()
    }
}

/// XML type `inf:InformationManagedResourceList`.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct InformationManagedResourceList {
    /// XML element `inf:elementReference`.
    pub element_reference: Vec<ElementReference>,
    /// XML element `inf:_informationManagedResourceListExtension`.
    pub information_managed_resource_list_extension: Option<Box<Extension>>,
}

impl InformationManagedResourceList {
    const FIELDS: &'static [u32] = &[0xc07ef, 0xc04e9];

    pub(crate) fn read(r: &mut Xml<'_>) -> Result<Self, Error> {
        r.expect_type(0xc00e6);
        r.attrs_done();
        let mut e0 = Vec::new();
        let mut e1 = None;
        while let Some(mut key) = r.next_child()? {
            loop {
                match key {
                    0xc07ef => { let v = Some(ElementReference::read(r)?); e0.extend(v); }
                    0xc04e9 => { let v = r.raw()?; r.set(&mut e1, v.map(Box::new), 1257); }
                    _ => match r.fallback(key, Self::FIELDS) {
                        Some(k) => { key = k; continue; }
                        None => r.skip_unknown(key)?,
                    },
                }
                break;
            }
        }
        let v = Self {
            element_reference: e0,
            information_managed_resource_list_extension: e1,
        };
        r.close();
        Ok(v)
    }
}

impl InformationManagedResourceList {
    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.count("inf:elementReference", self.element_reference.len(), 1, usize::MAX)?;
        for v in &self.element_reference { v.write(w, "inf:elementReference", "", "")?; }
        if let Some(v) = &self.information_managed_resource_list_extension { w.extension("inf:_informationManagedResourceListExtension", v)?; }
        w.end(tag)
    }
}

impl InformationManagedResourceList {
    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; 2];
        let mut f0 = Vec::new();
        let mut f1 = None;
        loop {
            let key = match pending.take() {
                Some(key) => key,
                None => match r.next_key()? {
                    Some(key) => key,
                    None => break,
                },
            };
            match key {
                651 => if r.first(&mut seen[0])? { let mut l = r.list()?; while r.item(&mut l)? { if let Some(v) = ElementReference::read_json(r)? { f0.push(v); } } },
                984 => if r.first(&mut seen[1])? { f1 = r.raw_object()?.map(Box::new); },
                _ => r.skip_unknown()?,
            }
        }
        Ok(Self {
            element_reference: f0,
            information_managed_resource_list_extension: f1,
        })
    }

    pub(crate) fn write_json(&self, w: &mut JsonWriter<'_>, target_class: Option<&'static str>) -> Result<(), Error> {
        w.begin_object(470)?;
        if let Some(class) = target_class {
            w.target_class(class)?;
        }
        w.count("elementReference", self.element_reference.len(), 1, usize::MAX)?;
        if !self.element_reference.is_empty() {
            w.key(651)?;
            w.begin_array()?;
            for v in &self.element_reference {
                w.item()?;
                v.write_json(w, None)?;
            }
            w.end_array()?;
        }
        if let Some(v) = &self.information_managed_resource_list_extension {
            w.key(984)?;
            w.extension(v)?;
        }
        w.end_object()
    }
}

/// XML type `inf:InformationManagement`.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct InformationManagement {
    /// XML element `inf:informationManagedResourceList`.
    pub information_managed_resource_list: Option<Box<InformationManagedResourceList>>,
    /// XML element `inf:_informationManagementExtension`.
    pub information_management_extension: Option<Box<Extension>>,
}

impl InformationManagement {
    const FIELDS: &'static [u32] = &[0xc08c0, 0xc04ea];

    pub(crate) fn read(r: &mut Xml<'_>) -> Result<Self, Error> {
        r.expect_type(0xc00e7);
        r.attrs_done();
        let mut e0 = None;
        let mut e1 = None;
        while let Some(mut key) = r.next_child()? {
            loop {
                match key {
                    0xc08c0 => { let v = Some(InformationManagedResourceList::read(r)?); r.set(&mut e0, v.map(Box::new), 2240); }
                    0xc04ea => { let v = r.raw()?; r.set(&mut e1, v.map(Box::new), 1258); }
                    _ => match r.fallback(key, Self::FIELDS) {
                        Some(k) => { key = k; continue; }
                        None => r.skip_unknown(key)?,
                    },
                }
                break;
            }
        }
        let v = Self {
            information_managed_resource_list: e0,
            information_management_extension: e1,
        };
        r.close();
        Ok(v)
    }
}

impl InformationManagement {
    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)?;
        if let Some(v) = &self.information_managed_resource_list { v.write(w, "inf:informationManagedResourceList", "", "")?; }
        if let Some(v) = &self.information_management_extension { w.extension("inf:_informationManagementExtension", v)?; }
        w.end(tag)
    }
}

impl InformationManagement {
    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; 2];
        let mut f0 = None;
        let mut f1 = None;
        loop {
            let key = match pending.take() {
                Some(key) => key,
                None => match r.next_key()? {
                    Some(key) => key,
                    None => break,
                },
            };
            match key {
                987 => if r.first(&mut seen[0])? { f0 = InformationManagedResourceList::read_json(r)?.map(Box::new); },
                985 => if r.first(&mut seen[1])? { f1 = r.raw_object()?.map(Box::new); },
                _ => r.skip_unknown()?,
            }
        }
        Ok(Self {
            information_managed_resource_list: f0,
            information_management_extension: f1,
        })
    }

    pub(crate) fn write_json(&self, w: &mut JsonWriter<'_>, target_class: Option<&'static str>) -> Result<(), Error> {
        w.begin_object(471)?;
        if let Some(class) = target_class {
            w.target_class(class)?;
        }
        if let Some(v) = &self.information_managed_resource_list {
            w.key(987)?;
            v.write_json(w, None)?;
        }
        if let Some(v) = &self.information_management_extension {
            w.key(985)?;
            w.extension(v)?;
        }
        w.end_object()
    }
}