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
410
411
412
413
//! XML support for deser.
//!
//! ```rust
//! use deser::{Deserialize, Serialize};
//!
//! #[derive(Debug, Deserialize, Serialize, PartialEq)]
//! struct Link {
//! #[deser(rename = "@href")]
//! href: String,
//! #[deser(rename = "@rel")]
//! rel: Option<String>,
//! }
//!
//! #[derive(Debug, Deserialize, Serialize, PartialEq)]
//! #[deser(rename = "feed")]
//! struct Feed {
//! title: String,
//! link: Vec<Link>,
//! count: u32,
//! }
//!
//! let feed: Feed = deser_xml::from_str(r#"
//! <feed>
//! <title>Example</title>
//! <link href="/a"/>
//! <count>3</count>
//! <link href="/b" rel="self"/>
//! </feed>
//! "#).unwrap();
//! assert_eq!(feed.title, "Example");
//! assert_eq!(feed.link.len(), 2);
//! assert_eq!(feed.link[1].rel.as_deref(), Some("self"));
//!
//! assert_eq!(
//! deser_xml::to_string(&feed).unwrap(),
//! "<feed><title>Example</title><link href=\"/a\"/>\
//! <link href=\"/b\" rel=\"self\"/><count>3</count></feed>"
//! );
//! ```
//!
//! # Data Model
//!
//! The document is the value of its root element (the name of the root
//! element is not checked, see [`Root`]). An element is:
//!
//! * **Its text** if it has neither attributes nor child elements:
//! `<count>3</count>` is `"3"` and `<empty/>` is `""`. Like the values of
//! query strings, text is passed on as
//! [lexical atom](deser_core::Atom::Lexical) that the type it's
//! deserialized into parses. An empty element is `None` for optionals of
//! types that do not accept the empty text.
//! * **A map** otherwise. Attributes are entries whose key has the
//! [attribute prefix](DeserializerConfig::set_attribute_prefix) (`@href`),
//! child elements are entries with their name and text is an entry with
//! the [text key](DeserializerConfig::set_text_key) (`$text`). Whitespace
//! between child elements is not text.
//!
//! | XML | deser |
//! |---------------------------------------|-----------------------------------------|
//! | `<a>1</a>` | `"1"` |
//! | `<a/>` | `""` |
//! | `<a href="x"/>` | `{"@href": "x"}` |
//! | `<a href="x">y</a>` | `{"@href": "x", "$text": "y"}` |
//! | `<a><b>1</b><c>2</c></a>` | `{"b": "1", "c": "2"}` |
//! | `<a><b>1</b><c/><b>2</b></a>` | `{"b": "1", "c": "", "b": "2"}` |
//! | `<p>x <b>y</b> z</p>` | `{"$text": "x ", "b": "y", "$text": " z"}` |
//!
//! Maps are [multimaps](deser_core::ContainerShape::set_multimap) whose
//! order is [significant](deser_core::Order::Significant): an element can
//! have more than one child with the same name. Fields and map values
//! that are collections (like `Vec<T>`) collect all of them, also if other
//! elements are between them. A single child element is a collection of
//! one value and a missing one an empty collection. For other types
//! the [`DuplicateKeys`](deser_core::de::DuplicateKeys) policy of the
//! [`Context`](deser_core::Context) decides (repeated elements are an error
//! by default).
//!
//! Whether an element is text or a map depends on the document, the type
//! it's deserialized into decides what it wants (the text key is the
//! [key of the content](deser_core::de::ContentKey)):
//!
//! * An element with attributes for a type that expects text (like a
//! `u32`) is its text, the attributes are skipped:
//! `<count unit="m">3</count>` is `3` for a `u32`.
//! * An element that is only text for a type that expects a map (like a
//! struct) is a map with the text under the text key: `<price>3</price>`
//! is `{"$text": "3"}` for a struct and an empty element an empty map.
//!
//! ```rust
//! #[derive(deser::Deserialize)]
//! struct Price {
//! #[deser(rename = "@currency")]
//! currency: Option<String>,
//! #[deser(rename = "$text")]
//! amount: f64,
//! }
//!
//! #[derive(deser::Deserialize)]
//! struct Item {
//! price: Vec<Price>,
//! weight: f64,
//! }
//!
//! let item: Item = deser_xml::from_str(r#"
//! <item>
//! <price currency="EUR">3</price>
//! <price>4</price>
//! <weight unit="kg">1.5</weight>
//! </item>
//! "#).unwrap();
//! assert_eq!(item.price[0].currency.as_deref(), Some("EUR"));
//! assert_eq!(item.price[1].amount, 4.0);
//! assert_eq!(item.weight, 1.5);
//! ```
//!
//! The order of text and child elements is kept by [`Mixed`], which is
//! for mixed content (`<p>x <b>y</b> z</p>`): every text and child
//! element is a value, typically of an enum whose variants are named after
//! the elements:
//!
//! ```rust
//! use deser::Deserialize;
//! use deser_xml::Mixed;
//!
//! #[derive(Debug, Deserialize, PartialEq)]
//! enum Inline {
//! #[deser(rename = "$text")]
//! Text(String),
//! #[deser(rename = "b")]
//! Bold(String),
//! }
//!
//! #[derive(Deserialize)]
//! struct Paragraph {
//! #[deser(rename = "@class")]
//! class: Option<String>,
//! #[deser(flatten)]
//! content: Mixed<Inline>,
//! }
//!
//! let p: Paragraph =
//! deser_xml::from_str(r#"<p class="x">x <b>y</b> z</p>"#).unwrap();
//! assert_eq!(p.content.0, [
//! Inline::Text("x ".into()),
//! Inline::Bold("y".into()),
//! Inline::Text(" z".into()),
//! ]);
//! ```
//!
//! Enums work like elsewhere: an externally tagged enum is an element with
//! one child (`<shape><circle r="1"/></shape>`), unit variants are text,
//! internally tagged enums can use an attribute as tag
//! (`#[deser(tag = "@type")]`).
//!
//! Names are passed on as written (`atom:link`), namespace declarations
//! (`xmlns` attributes) are not data. The name of the root element and
//! the namespaces declared on it are not part of the value either, they are
//! captured by [`Root`]. Values that capture event data (such as
//! [`Recording`](deser_core::de::Recording)) keep the root element and the
//! namespace declarations of all elements, so they are written again where
//! they were. Namespaces can be given prefixes
//! that are used regardless of the prefixes of the document (see
//! [`DeserializerConfig::namespaces`]) or be
//! [resolved](DeserializerConfig::set_resolve_namespaces) into names like
//! `{http://www.w3.org/2005/Atom}title` (see [`qname!`] and
//! [`namespace!`]), which the serializer writes with prefixes.
//! CDATA sections are text, the
//! predefined entities (`&`, ...) and character references are
//! resolved. Entities of document types are never expanded.
//!
//! Serializing works the other way around (see [`SerializerConfig`]).
//!
//! # Pretty Printing
//!
//! By default the output is a single line. [`SerializerConfig::set_pretty`]
//! (or [`set_indent`](SerializerConfig::set_indent)) writes child elements on
//! lines of their own, but only where the whitespace is not text: the text
//! of elements is kept as it is and mixed content stays on a single line.
//!
//! ```rust
//! use deser_xml::{Indent, SerializerConfig};
//!
//! #[derive(deser::Serialize)]
//! #[deser(rename = "item")]
//! struct Item {
//! name: &'static str,
//! tag: Vec<&'static str>,
//! }
//!
//! const PRETTY: SerializerConfig =
//! SerializerConfig::builder().pretty(Indent::Spaces(2)).build();
//! let item = Item { name: "x", tag: vec!["a", "b"] };
//! assert_eq!(
//! PRETTY.to_string(&item).unwrap(),
//! "<item>\n <name>x</name>\n <tag>a</tag>\n <tag>b</tag>\n</item>"
//! );
//! ```
//!
//! # Streams
//!
//! Documents are read from a [`Read`](std::io::Read) with
//! [`from_reader`] and written to a [`Write`](std::io::Write) with
//! [`to_writer`]. The configurations also create readers and writers of
//! [`deser::io`](deser_core::io) ([`DeserializerConfig::reader`] and
//! [`SerializerConfig::writer`]), the stream serializer ([`Serializer`])
//! and deserializer ([`StreamDeserializer`]) work with other kinds of IO
//! too (for instance async runtimes with `deser-tokio`). A stream holds a
//! single document. The reader is read to
//! the end before the document is parsed. The output is written while the
//! value is serialized: an element is only held back until no more
//! attributes can come for it, which for structs is known from their fields
//! and for maps is their end.
//!
//! ```rust
//! # #[cfg(feature = "io")] {
//! #[derive(deser::Serialize, deser::Deserialize)]
//! #[deser(rename = "feed")]
//! struct Feed {
//! entry: Vec<String>,
//! }
//!
//! let feed = Feed { entry: (0..100).map(|x| x.to_string()).collect() };
//! let mut out = Vec::new();
//! deser_xml::to_writer(&mut out, &feed).unwrap();
//! let read: Feed = deser_xml::from_reader(&out[..]).unwrap();
//! assert_eq!(read.entry.len(), 100);
//! # }
//! ```
//!
//! # Features
//!
//! * `io` (enabled by default): reading and writing streams of the
//! standard library, see [streams](#streams).
//! * `speedups` (enabled by default): has no effect yet, it exists so
//! that all formats have it.
//!
//! # Limitations
//!
//! This crate is an early version. The input has to be UTF-8.
pub use ;
pub use ;
pub use Root;
pub use to_writer;
pub use ;
pub use StreamDeserializer;
pub use from_reader;
/// Writes a name in a namespace as `{uri}local`.
///
/// This is the notation for names in namespaces (see
/// [`DeserializerConfig::set_resolve_namespaces`]). With `@` in front it's
/// the name of an attribute with the default
/// [attribute prefix](DeserializerConfig::set_attribute_prefix). The name is
/// a literal, so it can be used for `rename`:
///
/// ```
/// use deser_xml::qname;
///
/// #[derive(deser::Deserialize)]
/// struct Link {
/// #[deser(rename = qname!(@ "http://www.w3.org/1999/xlink", "href"))]
/// href: String,
/// }
///
/// assert_eq!(qname!("urn:x", "a"), "{urn:x}a");
/// assert_eq!(qname!(@ "urn:x", "a"), "@{urn:x}a");
/// ```
/// Defines a macro that writes names in a namespace.
///
/// `namespace!(atom = "http://www.w3.org/2005/Atom")` defines `atom!` so
/// that `atom!("title")` is [`qname!("http://www.w3.org/2005/Atom",
/// "title")`](qname), `atom!(@ "href")` the attribute and `atom!()` the
/// URI. The names of the macros are the prefixes of the namespaces in
/// [`prefixes!`]. Like all macros defined by macros, they can be used
/// after the invocation in the same module and its children.
///
/// ```
/// deser_xml::namespace!(atom = "http://www.w3.org/2005/Atom");
///
/// #[derive(deser::Deserialize)]
/// struct Entry {
/// #[deser(rename = atom!("title"))]
/// title: String,
/// #[deser(rename = atom!(@ "lang"))]
/// lang: Option<String>,
/// }
///
/// assert_eq!(atom!("title"), "{http://www.w3.org/2005/Atom}title");
/// assert_eq!(atom!(), "http://www.w3.org/2005/Atom");
/// ```
// rustfmt indents the inner macro further on every run
/// Writes the prefixes of namespaces defined by [`namespace!`].
///
/// Every namespace has the name of its macro as prefix unless another one
/// is given with `as`. The result is the table for
/// [`SerializerConfig::namespaces`] and
/// [`DeserializerConfig::namespaces`]:
///
/// ```
/// use deser_xml::{DeserializerConfig, SerializerConfig, prefixes};
///
/// deser_xml::namespace!(
/// atom = "http://www.w3.org/2005/Atom",
/// dc = "http://purl.org/dc/elements/1.1/",
/// xlink = "http://www.w3.org/1999/xlink",
/// );
///
/// const PREFIXES: &[(&str, &str)] =
/// prefixes![atom as "", dc, xlink as "xl"];
/// assert_eq!(PREFIXES, [
/// ("", "http://www.w3.org/2005/Atom"),
/// ("dc", "http://purl.org/dc/elements/1.1/"),
/// ("xl", "http://www.w3.org/1999/xlink"),
/// ]);
///
/// const WRITE: SerializerConfig =
/// SerializerConfig::new().namespaces(PREFIXES);
/// const READ: DeserializerConfig =
/// DeserializerConfig::new().namespaces(PREFIXES);
/// ```
;
}
/// The names of the special keys and the prefixes of namespaces.
///
/// The deserializer publishes them in the state for [`Mixed`].
pub