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
//! DATEX II version 3.
//!
//! [`read`] takes any DATEX II v3 XML document and returns a [`Document`]. The types are generated
//! from the official 3.7 schemas, one module per namespace; they read documents of every 3.x
//! release, because a document does not say which minor release it follows and stable namespaces
//! only grow. [`json`] reads and writes the same types as JSON.
use BufRead;
use crate;
use crateLimits;
pub use *;
pub use Record;
pub use Stream;
/// A publication of any kind: the payload of a DATEX II v3 document.
pub use PayloadPublication as Publication;
/// What a writer puts at the top of a document: a publication, written as a `payload`, or a
/// message container, written as a `messageContainer`.
///
/// Every writer takes `impl Into<Root>`, so you pass `&publication` or `&container` and never
/// name this type:
///
/// ```
/// use datex2::v3::{self, json, Publication, message_container::MessageContainer};
///
/// fn both(publication: &Publication, container: &MessageContainer) -> Result<(), datex2::Error> {
/// let xml = v3::to_string(publication)?;
/// let json = json::to_string(container)?;
/// # let _ = (xml, json);
/// Ok(())
/// }
/// ```
///
/// Nothing else is a root:
///
/// ```compile_fail
/// let site = datex2::v3::energy_infrastructure::EnergyInfrastructureSite::default();
/// datex2::v3::to_string(&site);
/// ```
/// The namespaces of a message container and its exchange parts. Each payload declares its own.
const CONTAINER_NAMESPACES: &str = concat!;
/// The outermost DATEX II element of a document.
/// The result of reading one document.
/// Reads a DATEX II v3 XML document.
///
/// `input` is anything buffered: a byte slice, or a file in a [`BufReader`](std::io::BufReader).
/// No version, namespace or envelope has to be stated.
///
/// ```no_run
/// 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::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// Fails when the input is not well-formed XML, is not DATEX II v3, lacks required content,
/// holds a value that cannot be read, or exceeds a default [`Limits`] value. Deviations the reader
/// tolerates are not errors; they are listed in [`Document::warnings`].
/// Reads a DATEX II v3 XML document with the given [`Limits`].
///
/// ```
/// let mut limits = datex2::Limits::default();
/// limits.max_depth = 20;
/// let result = datex2::v3::read_with(&b"<a/>"[..], &limits);
/// assert!(result.is_err()); // not DATEX II
/// ```
///
/// # Errors
///
/// As for [`read`], with the limits given.
/// Reads the records of a DATEX II v3 XML document one at a time.
///
/// Name the record type you want; the [`Stream`] delivers every object of that type the
/// publication holds, in document order, and keeps only one in memory. Any type that implements
/// [`Record`] can be asked for.
///
/// ```no_run
/// use datex2::v3::{self, situation::Situation};
///
/// let file = std::io::BufReader::new(std::fs::File::open("roadworks.xml")?);
/// for situation in v3::stream::<Situation>(file)? {
/// let situation = situation?;
/// println!("{:?}: {} records", situation.id, situation.situation_record.len());
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// To see what the reader tolerated, keep the stream and take its warnings:
///
/// ```
/// # let xml = include_str!("../../tests/fixtures/situation.xml");
/// use datex2::v3::{self, situation::SituationRecord};
///
/// let mut records = v3::stream::<SituationRecord>(xml.as_bytes())?;
/// for record in records.by_ref() {
/// println!("{:?}", record?.id());
/// }
/// assert!(records.take_warnings().is_empty());
/// # Ok::<(), datex2::Error>(())
/// ```
///
/// # Errors
///
/// Fails at once when the input is not DATEX II v3, or when its publication cannot hold records
/// of type `T` (situations asked of a VMS publication). Failures further into the document arrive
/// as an item of the stream, after the records before them; the stream then ends.
/// Like [`stream`], with the given [`Limits`].
///
/// # Errors
///
/// As for [`stream`].
/// Writes a publication or a message container as a DATEX II v3 XML document.
///
/// A publication becomes a `d2:payload` element, a [`MessageContainer`](message_container::MessageContainer)
/// a `con:messageContainer` with exchange information and a `con:payload` per publication, each
/// with the namespace declarations, type attributes and element order the standard requires. When
/// the write succeeds the official schemas (3.7, and Exchange 2020 for a container) accept the
/// output, except for their uniqueness constraints on identifiers, which are not checked.
///
/// ```
/// use datex2::v3::{self, common, situation::SituationPublication, Publication};
///
/// let publication = Publication::Situation(Box::new(SituationPublication {
/// lang: Some("en".into()),
/// publication_time: "2026-11-19T12:00:00Z".parse().ok(),
/// publication_creator: Some(Box::new(common::InternationalIdentifier {
/// country: Some("NL".into()),
/// national_identifier: Some("EXAMPLE".into()),
/// ..Default::default()
/// })),
/// ..Default::default()
/// }));
/// let xml = v3::to_string(&publication)?;
/// assert!(xml.contains("<com:country>NL</com:country>"));
/// # Ok::<(), datex2::Error>(())
/// ```
///
/// # Errors
///
/// The writer is strict. It fails, naming the element and the rule, when content the schema
/// requires is absent, when a list has too few or too many entries, when a value breaks a length,
/// range, digit or pattern rule, when a text holds a character XML cannot carry, when extension
/// content is not well-formed, or when the sink fails. Bytes already written stay in the sink.
/// Writes a publication or a message container as a DATEX II v3 XML document in memory. See
/// [`write()`].
///
/// # Errors
///
/// As for [`write()`].