Skip to main content

quick_xml/de/
mod.rs

1//! Serde `Deserializer` module.
2//!
3//! Due to the complexity of the XML standard and the fact that Serde was developed
4//! with JSON in mind, not all Serde concepts apply smoothly to XML. This leads to
5//! that fact that some XML concepts are inexpressible in terms of Serde derives
6//! and may require manual deserialization.
7//!
8//! The most notable restriction is the ability to distinguish between _elements_
9//! and _attributes_, as no other format used by serde has such a conception.
10//!
11//! Due to that the mapping is performed in a best effort manner.
12//!
13//!
14//!
15//! Table of Contents
16//! =================
17//! - [Mapping XML to Rust types](#mapping-xml-to-rust-types)
18//!   - [Basics](#basics)
19//!   - [Optional attributes and elements](#optional-attributes-and-elements)
20//!   - [Choices (`xs:choice` XML Schema type)](#choices-xschoice-xml-schema-type)
21//!   - [Sequences (`xs:all` and `xs:sequence` XML Schema types)](#sequences-xsall-and-xssequence-xml-schema-types)
22//! - [Mapping of `xsi:nil`](#mapping-of-xsinil)
23//! - [Generate Rust types from XML](#generate-rust-types-from-xml)
24//! - [Composition Rules](#composition-rules)
25//! - [Enum Representations](#enum-representations)
26//!   - [Normal enum variant](#normal-enum-variant)
27//!   - [`$text` enum variant](#text-enum-variant)
28//! - [`$text` and `$value` special names](#text-and-value-special-names)
29//!   - [`$text`](#text)
30//!   - [`$value`](#value)
31//!     - [Primitives and sequences of primitives](#primitives-and-sequences-of-primitives)
32//!     - [Structs and sequences of structs](#structs-and-sequences-of-structs)
33//!     - [Enums and sequences of enums](#enums-and-sequences-of-enums)
34//! - [Frequently Used Patterns](#frequently-used-patterns)
35//!   - [`<element>` lists](#element-lists)
36//!   - [Overlapped (Out-of-Order) Elements](#overlapped-out-of-order-elements)
37//!   - [Internally Tagged Enums](#internally-tagged-enums)
38//!
39//!
40//!
41//! Mapping XML to Rust types
42//! =========================
43//!
44//! Type names are never considered when deserializing, so you can name your
45//! types as you wish. Other general rules:
46//! - `struct` field name could be represented in XML only as an attribute name
47//!   or an element name;
48//! - `enum` variant name could be represented in XML only as an attribute name
49//!   or an element name;
50//! - the unit struct, unit type `()` and unit enum variant can be deserialized
51//!   from any valid XML content:
52//!   - attribute and element names;
53//!   - attribute and element values;
54//!   - text or CDATA content (including mixed text and CDATA content).
55//!
56//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
57//!
58//! NOTE: All tests are marked with an `ignore` option, even though they do
59//! compile. This is  because rustdoc marks such blocks with an information
60//! icon unlike `no_run` blocks.
61//!
62//! </div>
63//!
64//! <table>
65//! <thead>
66//! <tr><th colspan="2">
67//!
68//! ## Basics
69//!
70//! </th></tr>
71//! <tr><th>To parse all these XML's...</th><th>...use these Rust type(s)</th></tr>
72//! </thead>
73//! <tbody style="vertical-align:top;">
74//! <tr>
75//! <td>
76//! Content of attributes and text / CDATA content of elements (including mixed
77//! text and CDATA content):
78//!
79//! ```xml
80//! <... ...="content" />
81//! ```
82//! ```xml
83//! <...>content</...>
84//! ```
85//! ```xml
86//! <...><![CDATA[content]]></...>
87//! ```
88//! ```xml
89//! <...>text<![CDATA[cdata]]>text</...>
90//! ```
91//! Mixed text / CDATA content represents one logical string, `"textcdatatext"` in that case.
92//! </td>
93//! <td>
94//!
95//! You can use any type that can be deserialized from an `&str`, for example:
96//! - [`String`] and [`&str`]
97//! - [`Cow<str>`]
98//! - [`u32`], [`f32`] and other numeric types
99//! - `enum`s, like
100//!   ```
101//!   # use pretty_assertions::assert_eq;
102//!   # use serde::Deserialize;
103//!   # #[derive(Debug, PartialEq)]
104//!   #[derive(Deserialize)]
105//!   enum Language {
106//!     Rust,
107//!     Cpp,
108//!     #[serde(other)]
109//!     Other,
110//!   }
111//!   # #[derive(Debug, PartialEq, Deserialize)]
112//!   # struct X { #[serde(rename = "$text")] x: Language }
113//!   # assert_eq!(X { x: Language::Rust  }, quick_xml::de::from_str("<x>Rust</x>").unwrap());
114//!   # assert_eq!(X { x: Language::Cpp   }, quick_xml::de::from_str("<x>C<![CDATA[p]]>p</x>").unwrap());
115//!   # assert_eq!(X { x: Language::Other }, quick_xml::de::from_str("<x><![CDATA[other]]></x>").unwrap());
116//!   ```
117//!
118//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
119//!
120//! NOTE: deserialization to non-owned types (i.e. borrow from the input),
121//! such as `&str`, is possible only if you parse document in the UTF-8
122//! encoding and content does not contain entity references such as `&amp;`,
123//! or character references such as `&#xD;`, as well as text content represented
124//! by one piece of [text] or [CDATA] element.
125//! </div>
126//! <!-- TODO: document an error type returned -->
127//!
128//! [text]: Event::Text
129//! [CDATA]: Event::CData
130//! </td>
131//! </tr>
132//! <!-- 2 ===================================================================================== -->
133//! <tr>
134//! <td>
135//!
136//! Content of attributes and text / CDATA content of elements (including mixed
137//! text and CDATA content), which represents a space-delimited lists, as
138//! specified in the XML Schema specification for [`xs:list`] `simpleType`:
139//!
140//! ```xml
141//! <... ...="element1 element2 ..." />
142//! ```
143//! ```xml
144//! <...>
145//!   element1
146//!   element2
147//!   ...
148//! </...>
149//! ```
150//! ```xml
151//! <...><![CDATA[
152//!   element1
153//!   element2
154//!   ...
155//! ]]></...>
156//! ```
157//!
158//! [`xs:list`]: https://www.w3.org/TR/xmlschema11-2/#list-datatypes
159//! </td>
160//! <td>
161//!
162//! Use any type that deserialized using [`deserialize_seq()`] call, for example:
163//!
164//! ```
165//! type List = Vec<u32>;
166//! ```
167//!
168//! See the next row to learn where in your struct definition you should
169//! use that type.
170//!
171//! According to the XML Schema specification, delimiters for elements is one
172//! or more space (`' '`, `'\r'`, `'\n'`, and `'\t'`) character(s).
173//!
174//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
175//!
176//! NOTE: according to the XML Schema restrictions, you cannot escape those
177//! white-space characters, so list elements will _never_ contain them.
178//! In practice you will usually use `xs:list`s for lists of numbers or enumerated
179//! values which looks like identifiers in many languages, for example, `item`,
180//! `some_item` or `some-item`, so that shouldn't be a problem.
181//!
182//! NOTE: according to the XML Schema specification, list elements can be
183//! delimited only by spaces. Other delimiters (for example, commas) are not
184//! allowed.
185//!
186//! </div>
187//!
188//! [`deserialize_seq()`]: de::Deserializer::deserialize_seq
189//! </td>
190//! </tr>
191//! <!-- 3 ===================================================================================== -->
192//! <tr>
193//! <td>
194//! A typical XML with attributes. The root tag name does not matter:
195//!
196//! ```xml
197//! <any-tag one="..." two="..."/>
198//! ```
199//! </td>
200//! <td>
201//!
202//! A structure where each XML attribute is mapped to a field with a name
203//! starting with `@`. Because Rust identifiers do not permit the `@` character,
204//! you should use the `#[serde(rename = "@...")]` attribute to rename it.
205//! The name of the struct itself does not matter:
206//!
207//! ```
208//! # use serde::Deserialize;
209//! # type T = ();
210//! # type U = ();
211//! // Get both attributes
212//! # #[derive(Debug, PartialEq)]
213//! #[derive(Deserialize)]
214//! struct AnyName {
215//!   #[serde(rename = "@one")]
216//!   one: T,
217//!
218//!   #[serde(rename = "@two")]
219//!   two: U,
220//! }
221//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..." two="..."/>"#).unwrap();
222//! ```
223//! ```
224//! # use serde::Deserialize;
225//! # type T = ();
226//! // Get only the one attribute, ignore the other
227//! # #[derive(Debug, PartialEq)]
228//! #[derive(Deserialize)]
229//! struct AnyName {
230//!   #[serde(rename = "@one")]
231//!   one: T,
232//! }
233//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..." two="..."/>"#).unwrap();
234//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..."/>"#).unwrap();
235//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..."><one>...</one></any-tag>"#).unwrap();
236//! ```
237//! ```
238//! # use serde::Deserialize;
239//! // Ignore all attributes
240//! // You can also use the `()` type (unit type)
241//! # #[derive(Debug, PartialEq)]
242//! #[derive(Deserialize)]
243//! struct AnyName;
244//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..." two="..."/>"#).unwrap();
245//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..."><one>...</one></any-tag>"#).unwrap();
246//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag><one>...</one><two>...</two></any-tag>"#).unwrap();
247//! ```
248//!
249//! All these structs can be used to deserialize from an XML on the
250//! left side depending on amount of information that you want to get.
251//! Of course, you can combine them with elements extractor structs (see below).
252//!
253//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
254//!
255//! NOTE: XML allows you to have an attribute and an element with the same name
256//! inside the one element. quick-xml deals with that by prepending a `@` prefix
257//! to the name of attributes.
258//! </div>
259//! </td>
260//! </tr>
261//! <!-- 4 ===================================================================================== -->
262//! <tr>
263//! <td>
264//! A typical XML with child elements. The root tag name does not matter:
265//!
266//! ```xml
267//! <any-tag>
268//!   <one>...</one>
269//!   <two>...</two>
270//! </any-tag>
271//! ```
272//! </td>
273//! <td>
274//! A structure where each XML child element is mapped to the field.
275//! Each element name becomes a name of field. The name of the struct itself
276//! does not matter:
277//!
278//! ```
279//! # use serde::Deserialize;
280//! # type T = ();
281//! # type U = ();
282//! // Get both elements
283//! # #[derive(Debug, PartialEq)]
284//! #[derive(Deserialize)]
285//! struct AnyName {
286//!   one: T,
287//!   two: U,
288//! }
289//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag><one>...</one><two>...</two></any-tag>"#).unwrap();
290//! #
291//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..." two="..."/>"#).unwrap_err();
292//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..."><two>...</two></any-tag>"#).unwrap_err();
293//! ```
294//! ```
295//! # use serde::Deserialize;
296//! # type T = ();
297//! // Get only the one element, ignore the other
298//! # #[derive(Debug, PartialEq)]
299//! #[derive(Deserialize)]
300//! struct AnyName {
301//!   one: T,
302//! }
303//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag><one>...</one><two>...</two></any-tag>"#).unwrap();
304//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..."><one>...</one></any-tag>"#).unwrap();
305//! ```
306//! ```
307//! # use serde::Deserialize;
308//! // Ignore all elements
309//! // You can also use the `()` type (unit type)
310//! # #[derive(Debug, PartialEq)]
311//! #[derive(Deserialize)]
312//! struct AnyName;
313//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..." two="..."/>"#).unwrap();
314//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag><one>...</one><two>...</two></any-tag>"#).unwrap();
315//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..."><two>...</two></any-tag>"#).unwrap();
316//! # quick_xml::de::from_str::<AnyName>(r#"<any-tag one="..."><one>...</one></any-tag>"#).unwrap();
317//! ```
318//!
319//! All these structs can be used to deserialize from an XML on the
320//! left side depending on amount of information that you want to get.
321//! Of course, you can combine them with attributes extractor structs (see above).
322//!
323//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
324//!
325//! NOTE: XML allows you to have an attribute and an element with the same name
326//! inside the one element. quick-xml deals with that by prepending a `@` prefix
327//! to the name of attributes.
328//! </div>
329//! </td>
330//! </tr>
331//! <!-- 5 ===================================================================================== -->
332//! <tr>
333//! <td>
334//! An XML with an attribute and a child element named equally:
335//!
336//! ```xml
337//! <any-tag field="...">
338//!   <field>...</field>
339//! </any-tag>
340//! ```
341//! </td>
342//! <td>
343//!
344//! You MUST specify `#[serde(rename = "@field")]` on a field that will be used
345//! for an attribute:
346//!
347//! ```
348//! # use pretty_assertions::assert_eq;
349//! # use serde::Deserialize;
350//! # type T = ();
351//! # type U = ();
352//! # #[derive(Debug, PartialEq)]
353//! #[derive(Deserialize)]
354//! struct AnyName {
355//!   #[serde(rename = "@field")]
356//!   attribute: T,
357//!   field: U,
358//! }
359//! # assert_eq!(
360//! #   AnyName { attribute: (), field: () },
361//! #   quick_xml::de::from_str(r#"
362//! #     <any-tag field="...">
363//! #       <field>...</field>
364//! #     </any-tag>
365//! #   "#).unwrap(),
366//! # );
367//! ```
368//! </td>
369//! </tr>
370//! <!-- ======================================================================================= -->
371//! <tr><th colspan="2">
372//!
373//! ## Optional attributes and elements
374//!
375//! </th></tr>
376//! <tr><th>To parse all these XML's...</th><th>...use these Rust type(s)</th></tr>
377//! <!-- 6 ===================================================================================== -->
378//! <tr>
379//! <td>
380//! An optional XML attribute that you want to capture.
381//! The root tag name does not matter:
382//!
383//! ```xml
384//! <any-tag optional="..."/>
385//! ```
386//! ```xml
387//! <any-tag/>
388//! ```
389//! </td>
390//! <td>
391//!
392//! A structure with an optional field, renamed according to the requirements
393//! for attributes:
394//!
395//! ```
396//! # use pretty_assertions::assert_eq;
397//! # use serde::Deserialize;
398//! # type T = ();
399//! # #[derive(Debug, PartialEq)]
400//! #[derive(Deserialize)]
401//! struct AnyName {
402//!   #[serde(rename = "@optional")]
403//!   optional: Option<T>,
404//! }
405//! # assert_eq!(AnyName { optional: Some(()) }, quick_xml::de::from_str(r#"<any-tag optional="..."/>"#).unwrap());
406//! # assert_eq!(AnyName { optional: None     }, quick_xml::de::from_str(r#"<any-tag/>"#).unwrap());
407//! ```
408//! When the XML attribute is present, type `T` will be deserialized from
409//! an attribute value (which is a string). Note, that if `T = String` or other
410//! string type, the empty attribute is mapped to a `Some("")`, whereas `None`
411//! represents the missed attribute:
412//! ```xml
413//! <any-tag optional="..."/><!-- Some("...") -->
414//! <any-tag optional=""/>   <!-- Some("") -->
415//! <any-tag/>               <!-- None -->
416//! ```
417//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
418//!
419//! NOTE: The behaviour is not symmetric by default. `None` will be serialized as
420//! `optional=""`. This behaviour is consistent across serde crates. You should add
421//! `#[serde(skip_serializing_if = "Option::is_none")]` attribute to the field to
422//! skip `None`s.
423//! </div>
424//! </td>
425//! </tr>
426//! <!-- 7 ===================================================================================== -->
427//! <tr>
428//! <td>
429//! An optional XML elements that you want to capture.
430//! The root tag name does not matter:
431//!
432//! ```xml
433//! <any-tag/>
434//!   <optional>...</optional>
435//! </any-tag>
436//! ```
437//! ```xml
438//! <any-tag/>
439//!   <optional/>
440//! </any-tag>
441//! ```
442//! ```xml
443//! <any-tag/>
444//! ```
445//! </td>
446//! <td>
447//!
448//! A structure with an optional field:
449//!
450//! ```
451//! # use pretty_assertions::assert_eq;
452//! # use serde::Deserialize;
453//! # type T = ();
454//! # #[derive(Debug, PartialEq)]
455//! #[derive(Deserialize)]
456//! struct AnyName {
457//!   optional: Option<T>,
458//! }
459//! # assert_eq!(AnyName { optional: Some(()) }, quick_xml::de::from_str(r#"<any-tag><optional>...</optional></any-tag>"#).unwrap());
460//! # assert_eq!(AnyName { optional: None     }, quick_xml::de::from_str(r#"<any-tag/>"#).unwrap());
461//! ```
462//! When the XML element is present, type `T` will be deserialized from an
463//! element (which is a string or a multi-mapping -- i.e. mapping which can have
464//! duplicated keys).
465//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
466//!
467//! NOTE: The behaviour is not symmetric by default. `None` will be serialized as
468//! `<optional/>`. This behaviour is consistent across serde crates. You should add
469//! `#[serde(skip_serializing_if = "Option::is_none")]` attribute to the field to
470//! skip `None`s.
471//!
472//! NOTE: Deserializer will automatically handle a [`xsi:nil`] attribute and set field to `None`.
473//! For more info see [Mapping of `xsi:nil`](#mapping-of-xsinil).
474//! </div>
475//! </td>
476//! </tr>
477//! <!-- ======================================================================================= -->
478//! <tr><th colspan="2">
479//!
480//! ## Choices (`xs:choice` XML Schema type)
481//!
482//! </th></tr>
483//! <tr><th>To parse all these XML's...</th><th>...use these Rust type(s)</th></tr>
484//! <!-- 8 ===================================================================================== -->
485//! <tr>
486//! <td>
487//! An XML with different root tag names, as well as text / CDATA content:
488//!
489//! ```xml
490//! <one field1="...">...</one>
491//! ```
492//! ```xml
493//! <two>
494//!   <field2>...</field2>
495//! </two>
496//! ```
497//! ```xml
498//! Text <![CDATA[or (mixed)
499//! CDATA]]> content
500//! ```
501//! </td>
502//! <td>
503//!
504//! An enum where each variant has the name of a possible root tag. The name of
505//! the enum itself does not matter.
506//!
507//! If you need to get the textual content, mark a variant with `#[serde(rename = "$text")]`.
508//!
509//! All these structs can be used to deserialize from any XML on the
510//! left side depending on amount of information that you want to get:
511//!
512//! ```
513//! # use pretty_assertions::assert_eq;
514//! # use serde::Deserialize;
515//! # type T = ();
516//! # type U = ();
517//! # #[derive(Debug, PartialEq)]
518//! #[derive(Deserialize)]
519//! #[serde(rename_all = "snake_case")]
520//! enum AnyName {
521//!   One { #[serde(rename = "@field1")] field1: T },
522//!   Two { field2: U },
523//!
524//!   /// Use unit variant, if you do not care of a content.
525//!   /// You can use tuple variant if you want to parse
526//!   /// textual content as an xs:list.
527//!   /// Struct variants are will pass a string to the
528//!   /// struct enum variant visitor, which typically
529//!   /// returns Err(Custom)
530//!   #[serde(rename = "$text")]
531//!   Text(String),
532//! }
533//! # assert_eq!(AnyName::One { field1: () }, quick_xml::de::from_str(r#"<one field1="...">...</one>"#).unwrap());
534//! # assert_eq!(AnyName::Two { field2: () }, quick_xml::de::from_str(r#"<two><field2>...</field2></two>"#).unwrap());
535//! # assert_eq!(AnyName::Text("text  cdata ".into()), quick_xml::de::from_str(r#"text <![CDATA[ cdata ]]>"#).unwrap());
536//! ```
537//! ```
538//! # use pretty_assertions::assert_eq;
539//! # use serde::Deserialize;
540//! # type T = ();
541//! # #[derive(Debug, PartialEq)]
542//! #[derive(Deserialize)]
543//! struct Two {
544//!   field2: T,
545//! }
546//! # #[derive(Debug, PartialEq)]
547//! #[derive(Deserialize)]
548//! #[serde(rename_all = "snake_case")]
549//! enum AnyName {
550//!   // `field1` content discarded
551//!   One,
552//!   Two(Two),
553//!   #[serde(rename = "$text")]
554//!   Text,
555//! }
556//! # assert_eq!(AnyName::One,                     quick_xml::de::from_str(r#"<one field1="...">...</one>"#).unwrap());
557//! # assert_eq!(AnyName::Two(Two { field2: () }), quick_xml::de::from_str(r#"<two><field2>...</field2></two>"#).unwrap());
558//! # assert_eq!(AnyName::Text,                    quick_xml::de::from_str(r#"text <![CDATA[ cdata ]]>"#).unwrap());
559//! ```
560//! ```
561//! # use pretty_assertions::assert_eq;
562//! # use serde::Deserialize;
563//! # #[derive(Debug, PartialEq)]
564//! #[derive(Deserialize)]
565//! #[serde(rename_all = "snake_case")]
566//! enum AnyName {
567//!   One,
568//!   // the <two> and textual content will be mapped to this
569//!   #[serde(other)]
570//!   Other,
571//! }
572//! # assert_eq!(AnyName::One,   quick_xml::de::from_str(r#"<one field1="...">...</one>"#).unwrap());
573//! # assert_eq!(AnyName::Other, quick_xml::de::from_str(r#"<two><field2>...</field2></two>"#).unwrap());
574//! # assert_eq!(AnyName::Other, quick_xml::de::from_str(r#"text <![CDATA[ cdata ]]>"#).unwrap());
575//! ```
576//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
577//!
578//! NOTE: You should have variants for all possible tag names in your enum
579//! or have an `#[serde(other)]` variant.
580//! <!-- TODO: document an error type if that requirement is violated -->
581//! </div>
582//! </td>
583//! </tr>
584//! <!-- 9 ===================================================================================== -->
585//! <tr>
586//! <td>
587//!
588//! `<xs:choice>` embedded in the other element, and at the same time you want
589//! to get access to other attributes that can appear in the same container
590//! (`<any-tag>`). Also this case can be described, as if you want to choose
591//! Rust enum variant based on a tag name:
592//!
593//! ```xml
594//! <any-tag field="...">
595//!   <one>...</one>
596//! </any-tag>
597//! ```
598//! ```xml
599//! <any-tag field="...">
600//!   <two>...</two>
601//! </any-tag>
602//! ```
603//! ```xml
604//! <any-tag field="...">
605//!   Text <![CDATA[or (mixed)
606//!   CDATA]]> content
607//! </any-tag>
608//! ```
609//! </td>
610//! <td>
611//!
612//! A structure with a field which type is an `enum`.
613//!
614//! If you need to get a textual content, mark a variant with `#[serde(rename = "$text")]`.
615//!
616//! Names of the enum, struct, and struct field with `Choice` type does not matter:
617//!
618//! ```
619//! # use pretty_assertions::assert_eq;
620//! # use serde::Deserialize;
621//! # type T = ();
622//! # #[derive(Debug, PartialEq)]
623//! #[derive(Deserialize)]
624//! #[serde(rename_all = "snake_case")]
625//! enum Choice {
626//!   One,
627//!   Two,
628//!
629//!   /// Use unit variant, if you do not care of a content.
630//!   /// You can use tuple variant if you want to parse
631//!   /// textual content as an xs:list.
632//!   /// Struct variants are will pass a string to the
633//!   /// struct enum variant visitor, which typically
634//!   /// returns Err(Custom)
635//!   #[serde(rename = "$text")]
636//!   Text(String),
637//! }
638//! # #[derive(Debug, PartialEq)]
639//! #[derive(Deserialize)]
640//! struct AnyName {
641//!   #[serde(rename = "@field")]
642//!   field: T,
643//!
644//!   #[serde(rename = "$value")]
645//!   any_name: Choice,
646//! }
647//! # assert_eq!(
648//! #   AnyName { field: (), any_name: Choice::One },
649//! #   quick_xml::de::from_str(r#"<any-tag field="..."><one>...</one></any-tag>"#).unwrap(),
650//! # );
651//! # assert_eq!(
652//! #   AnyName { field: (), any_name: Choice::Two },
653//! #   quick_xml::de::from_str(r#"<any-tag field="..."><two>...</two></any-tag>"#).unwrap(),
654//! # );
655//! # assert_eq!(
656//! #   AnyName { field: (), any_name: Choice::Text("text  cdata ".into()) },
657//! #   quick_xml::de::from_str(r#"<any-tag field="...">text <![CDATA[ cdata ]]></any-tag>"#).unwrap(),
658//! # );
659//! ```
660//! </td>
661//! </tr>
662//! <!-- 10 ==================================================================================== -->
663//! <tr>
664//! <td>
665//!
666//! `<xs:choice>` embedded in the other element, and at the same time you want
667//! to get access to other elements that can appear in the same container
668//! (`<any-tag>`). Also this case can be described, as if you want to choose
669//! Rust enum variant based on a tag name:
670//!
671//! ```xml
672//! <any-tag>
673//!   <field>...</field>
674//!   <one>...</one>
675//! </any-tag>
676//! ```
677//! ```xml
678//! <any-tag>
679//!   <two>...</two>
680//!   <field>...</field>
681//! </any-tag>
682//! ```
683//! </td>
684//! <td>
685//!
686//! A structure with a field which type is an `enum`.
687//!
688//! Names of the enum, struct, and struct field with `Choice` type does not matter:
689//!
690//! ```
691//! # use pretty_assertions::assert_eq;
692//! # use serde::Deserialize;
693//! # type T = ();
694//! # #[derive(Debug, PartialEq)]
695//! #[derive(Deserialize)]
696//! #[serde(rename_all = "snake_case")]
697//! enum Choice {
698//!   One,
699//!   Two,
700//! }
701//! # #[derive(Debug, PartialEq)]
702//! #[derive(Deserialize)]
703//! struct AnyName {
704//!   field: T,
705//!
706//!   #[serde(rename = "$value")]
707//!   any_name: Choice,
708//! }
709//! # assert_eq!(
710//! #   AnyName { field: (), any_name: Choice::One },
711//! #   quick_xml::de::from_str(r#"<any-tag><field>...</field><one>...</one></any-tag>"#).unwrap(),
712//! # );
713//! # assert_eq!(
714//! #   AnyName { field: (), any_name: Choice::Two },
715//! #   quick_xml::de::from_str(r#"<any-tag><two>...</two><field>...</field></any-tag>"#).unwrap(),
716//! # );
717//! ```
718//!
719//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
720//!
721//! NOTE: if your `Choice` enum would contain an `#[serde(other)]`
722//! variant, element `<field>` will be mapped to the `field` and not to the enum
723//! variant.
724//! </div>
725//!
726//! </td>
727//! </tr>
728//! <!-- 11 ==================================================================================== -->
729//! <tr>
730//! <td>
731//!
732//! `<xs:choice>` encapsulated in other element with a fixed name:
733//!
734//! ```xml
735//! <any-tag field="...">
736//!   <choice>
737//!     <one>...</one>
738//!   </choice>
739//! </any-tag>
740//! ```
741//! ```xml
742//! <any-tag field="...">
743//!   <choice>
744//!     <two>...</two>
745//!   </choice>
746//! </any-tag>
747//! ```
748//! </td>
749//! <td>
750//!
751//! A structure with a field of an intermediate type with one field of `enum` type.
752//! Actually, this example is not necessary, because you can construct it by yourself
753//! using the composition rules that were described above. However the XML construction
754//! described here is very common, so it is shown explicitly.
755//!
756//! Names of the enum and struct does not matter:
757//!
758//! ```
759//! # use pretty_assertions::assert_eq;
760//! # use serde::Deserialize;
761//! # type T = ();
762//! # #[derive(Debug, PartialEq)]
763//! #[derive(Deserialize)]
764//! #[serde(rename_all = "snake_case")]
765//! enum Choice {
766//!   One,
767//!   Two,
768//! }
769//! # #[derive(Debug, PartialEq)]
770//! #[derive(Deserialize)]
771//! struct Holder {
772//!   #[serde(rename = "$value")]
773//!   any_name: Choice,
774//! }
775//! # #[derive(Debug, PartialEq)]
776//! #[derive(Deserialize)]
777//! struct AnyName {
778//!   #[serde(rename = "@field")]
779//!   field: T,
780//!
781//!   choice: Holder,
782//! }
783//! # assert_eq!(
784//! #   AnyName { field: (), choice: Holder { any_name: Choice::One } },
785//! #   quick_xml::de::from_str(r#"<any-tag field="..."><choice><one>...</one></choice></any-tag>"#).unwrap(),
786//! # );
787//! # assert_eq!(
788//! #   AnyName { field: (), choice: Holder { any_name: Choice::Two } },
789//! #   quick_xml::de::from_str(r#"<any-tag field="..."><choice><two>...</two></choice></any-tag>"#).unwrap(),
790//! # );
791//! ```
792//! </td>
793//! </tr>
794//! <!-- 12 ==================================================================================== -->
795//! <tr>
796//! <td>
797//!
798//! `<xs:choice>` encapsulated in other element with a fixed name:
799//!
800//! ```xml
801//! <any-tag>
802//!   <field>...</field>
803//!   <choice>
804//!     <one>...</one>
805//!   </choice>
806//! </any-tag>
807//! ```
808//! ```xml
809//! <any-tag>
810//!   <choice>
811//!     <two>...</two>
812//!   </choice>
813//!   <field>...</field>
814//! </any-tag>
815//! ```
816//! </td>
817//! <td>
818//!
819//! A structure with a field of an intermediate type with one field of `enum` type.
820//! Actually, this example is not necessary, because you can construct it by yourself
821//! using the composition rules that were described above. However the XML construction
822//! described here is very common, so it is shown explicitly.
823//!
824//! Names of the enum and struct does not matter:
825//!
826//! ```
827//! # use pretty_assertions::assert_eq;
828//! # use serde::Deserialize;
829//! # type T = ();
830//! # #[derive(Debug, PartialEq)]
831//! #[derive(Deserialize)]
832//! #[serde(rename_all = "snake_case")]
833//! enum Choice {
834//!   One,
835//!   Two,
836//! }
837//! # #[derive(Debug, PartialEq)]
838//! #[derive(Deserialize)]
839//! struct Holder {
840//!   #[serde(rename = "$value")]
841//!   any_name: Choice,
842//! }
843//! # #[derive(Debug, PartialEq)]
844//! #[derive(Deserialize)]
845//! struct AnyName {
846//!   field: T,
847//!
848//!   choice: Holder,
849//! }
850//! # assert_eq!(
851//! #   AnyName { field: (), choice: Holder { any_name: Choice::One } },
852//! #   quick_xml::de::from_str(r#"<any-tag><field>...</field><choice><one>...</one></choice></any-tag>"#).unwrap(),
853//! # );
854//! # assert_eq!(
855//! #   AnyName { field: (), choice: Holder { any_name: Choice::Two } },
856//! #   quick_xml::de::from_str(r#"<any-tag><choice><two>...</two></choice><field>...</field></any-tag>"#).unwrap(),
857//! # );
858//! ```
859//! </td>
860//! </tr>
861//! <!-- ======================================================================================== -->
862//! <tr><th colspan="2">
863//!
864//! ## Sequences (`xs:all` and `xs:sequence` XML Schema types)
865//!
866//! </th></tr>
867//! <tr><th>To parse all these XML's...</th><th>...use these Rust type(s)</th></tr>
868//! <!-- 13 ==================================================================================== -->
869//! <tr>
870//! <td>
871//! A sequence inside of a tag without a dedicated name:
872//!
873//! ```xml
874//! <any-tag/>
875//! ```
876//! ```xml
877//! <any-tag>
878//!   <item/>
879//! </any-tag>
880//! ```
881//! ```xml
882//! <any-tag>
883//!   <item/>
884//!   <item/>
885//!   <item/>
886//! </any-tag>
887//! ```
888//! </td>
889//! <td>
890//!
891//! A structure with a field which is a sequence type, for example, [`Vec`].
892//! Because XML syntax does not distinguish between empty sequences and missed
893//! elements, we should indicate that on the Rust side, because serde will require
894//! that field `item` exists. You can do that in two possible ways:
895//!
896//! Use the `#[serde(default)]` attribute for a [field] or the entire [struct]:
897//! ```
898//! # use pretty_assertions::assert_eq;
899//! # use serde::Deserialize;
900//! # type Item = ();
901//! # #[derive(Debug, PartialEq)]
902//! #[derive(Deserialize)]
903//! struct AnyName {
904//!   #[serde(default)]
905//!   item: Vec<Item>,
906//! }
907//! # assert_eq!(
908//! #   AnyName { item: vec![] },
909//! #   quick_xml::de::from_str(r#"<any-tag/>"#).unwrap(),
910//! # );
911//! # assert_eq!(
912//! #   AnyName { item: vec![()] },
913//! #   quick_xml::de::from_str(r#"<any-tag><item/></any-tag>"#).unwrap(),
914//! # );
915//! # assert_eq!(
916//! #   AnyName { item: vec![(), (), ()] },
917//! #   quick_xml::de::from_str(r#"<any-tag><item/><item/><item/></any-tag>"#).unwrap(),
918//! # );
919//! ```
920//!
921//! Use the [`Option`]. In that case inner array will always contains at least one
922//! element after deserialization:
923//! ```ignore
924//! # use pretty_assertions::assert_eq;
925//! # use serde::Deserialize;
926//! # type Item = ();
927//! # #[derive(Debug, PartialEq)]
928//! #[derive(Deserialize)]
929//! struct AnyName {
930//!   item: Option<Vec<Item>>,
931//! }
932//! # assert_eq!(
933//! #   AnyName { item: None },
934//! #   quick_xml::de::from_str(r#"<any-tag/>"#).unwrap(),
935//! # );
936//! # assert_eq!(
937//! #   AnyName { item: Some(vec![()]) },
938//! #   quick_xml::de::from_str(r#"<any-tag><item/></any-tag>"#).unwrap(),
939//! # );
940//! # assert_eq!(
941//! #   AnyName { item: Some(vec![(), (), ()]) },
942//! #   quick_xml::de::from_str(r#"<any-tag><item/><item/><item/></any-tag>"#).unwrap(),
943//! # );
944//! ```
945//!
946//! See also [Frequently Used Patterns](#element-lists).
947//!
948//! [field]: https://serde.rs/field-attrs.html#default
949//! [struct]: https://serde.rs/container-attrs.html#default
950//! </td>
951//! </tr>
952//! <!-- 14 ==================================================================================== -->
953//! <tr>
954//! <td>
955//! A sequence with a strict order, probably with mixed content
956//! (text / CDATA and tags):
957//!
958//! ```xml
959//! <one>...</one>
960//! text
961//! <![CDATA[cdata]]>
962//! <two>...</two>
963//! <one>...</one>
964//! ```
965//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
966//!
967//! NOTE: this is just an example for showing mapping. XML does not allow
968//! multiple root tags -- you should wrap the sequence into a tag.
969//! </div>
970//! </td>
971//! <td>
972//!
973//! All elements mapped to the heterogeneous sequential type: tuple or named tuple.
974//! Each element of the tuple should be able to be deserialized from the nested
975//! element content (`...`), except the enum types which would be deserialized
976//! from the full element (`<one>...</one>`), so they could use the element name
977//! to choose the right variant:
978//!
979//! ```
980//! # use pretty_assertions::assert_eq;
981//! # use serde::Deserialize;
982//! # type One = ();
983//! # type Two = ();
984//! # /*
985//! type One = ...;
986//! type Two = ...;
987//! # */
988//! # #[derive(Debug, PartialEq)]
989//! #[derive(Deserialize)]
990//! struct AnyName(One, String, Two, One);
991//! # assert_eq!(
992//! #   AnyName((), "text cdata".into(), (), ()),
993//! #   quick_xml::de::from_str(r#"<one>...</one>text <![CDATA[cdata]]><two>...</two><one>...</one>"#).unwrap(),
994//! # );
995//! ```
996//! ```
997//! # use pretty_assertions::assert_eq;
998//! # use serde::Deserialize;
999//! # #[derive(Debug, PartialEq)]
1000//! #[derive(Deserialize)]
1001//! #[serde(rename_all = "snake_case")]
1002//! enum Choice {
1003//!   One,
1004//! }
1005//! # type Two = ();
1006//! # /*
1007//! type Two = ...;
1008//! # */
1009//! type AnyName = (Choice, String, Two, Choice);
1010//! # assert_eq!(
1011//! #   (Choice::One, "text cdata".to_string(), (), Choice::One),
1012//! #   quick_xml::de::from_str(r#"<one>...</one>text <![CDATA[cdata]]><two>...</two><one>...</one>"#).unwrap(),
1013//! # );
1014//! ```
1015//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
1016//!
1017//! NOTE: consequent text and CDATA nodes are merged into the one text node,
1018//! so you cannot have two adjacent string types in your sequence.
1019//!
1020//! NOTE: In the case that the list might contain tags that are overlapped with
1021//! tags that do not correspond to the list you should add the feature [`overlapped-lists`].
1022//! </div>
1023//! </td>
1024//! </tr>
1025//! <!-- 15 ==================================================================================== -->
1026//! <tr>
1027//! <td>
1028//! A sequence with a non-strict order, probably with a mixed content
1029//! (text / CDATA and tags).
1030//!
1031//! ```xml
1032//! <one>...</one>
1033//! text
1034//! <![CDATA[cdata]]>
1035//! <two>...</two>
1036//! <one>...</one>
1037//! ```
1038//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
1039//!
1040//! NOTE: this is just an example for showing mapping. XML does not allow
1041//! multiple root tags -- you should wrap the sequence into a tag.
1042//! </div>
1043//! </td>
1044//! <td>
1045//! A homogeneous sequence of elements with a fixed or dynamic size:
1046//!
1047//! ```
1048//! # use pretty_assertions::assert_eq;
1049//! # use serde::Deserialize;
1050//! # #[derive(Debug, PartialEq)]
1051//! #[derive(Deserialize)]
1052//! #[serde(rename_all = "snake_case")]
1053//! enum Choice {
1054//!   One,
1055//!   Two,
1056//!   #[serde(other)]
1057//!   Other,
1058//! }
1059//! type AnyName = [Choice; 4];
1060//! # assert_eq!(
1061//! #   [Choice::One, Choice::Other, Choice::Two, Choice::One],
1062//! #   quick_xml::de::from_str::<AnyName>(r#"<one>...</one>text <![CDATA[cdata]]><two>...</two><one>...</one>"#).unwrap(),
1063//! # );
1064//! ```
1065//! ```
1066//! # use pretty_assertions::assert_eq;
1067//! # use serde::Deserialize;
1068//! # #[derive(Debug, PartialEq)]
1069//! #[derive(Deserialize)]
1070//! #[serde(rename_all = "snake_case")]
1071//! enum Choice {
1072//!   One,
1073//!   Two,
1074//!   #[serde(rename = "$text")]
1075//!   Other(String),
1076//! }
1077//! type AnyName = Vec<Choice>;
1078//! # assert_eq!(
1079//! #   vec![
1080//! #     Choice::One,
1081//! #     Choice::Other("text cdata".into()),
1082//! #     Choice::Two,
1083//! #     Choice::One,
1084//! #   ],
1085//! #   quick_xml::de::from_str::<AnyName>(r#"<one>...</one>text <![CDATA[cdata]]><two>...</two><one>...</one>"#).unwrap(),
1086//! # );
1087//! ```
1088//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
1089//!
1090//! NOTE: consequent text and CDATA nodes are merged into the one text node,
1091//! so you cannot have two adjacent string types in your sequence.
1092//! </div>
1093//! </td>
1094//! </tr>
1095//! <!-- 16 ==================================================================================== -->
1096//! <tr>
1097//! <td>
1098//! A sequence with a strict order, probably with a mixed content,
1099//! (text and tags) inside of the other element:
1100//!
1101//! ```xml
1102//! <any-tag attribute="...">
1103//!   <one>...</one>
1104//!   text
1105//!   <![CDATA[cdata]]>
1106//!   <two>...</two>
1107//!   <one>...</one>
1108//! </any-tag>
1109//! ```
1110//! </td>
1111//! <td>
1112//!
1113//! A structure where all child elements mapped to the one field which have
1114//! a heterogeneous sequential type: tuple or named tuple. Each element of the
1115//! tuple should be able to be deserialized from the full element (`<one>...</one>`).
1116//!
1117//! You MUST specify `#[serde(rename = "$value")]` on that field:
1118//!
1119//! ```
1120//! # use pretty_assertions::assert_eq;
1121//! # use serde::Deserialize;
1122//! # type One = ();
1123//! # type Two = ();
1124//! # /*
1125//! type One = ...;
1126//! type Two = ...;
1127//! # */
1128//!
1129//! # #[derive(Debug, PartialEq)]
1130//! #[derive(Deserialize)]
1131//! struct AnyName {
1132//!   #[serde(rename = "@attribute")]
1133//! # attribute: (),
1134//! # /*
1135//!   attribute: ...,
1136//! # */
1137//!   // Does not (yet?) supported by the serde
1138//!   // https://github.com/serde-rs/serde/issues/1905
1139//!   // #[serde(flatten)]
1140//!   #[serde(rename = "$value")]
1141//!   any_name: (One, String, Two, One),
1142//! }
1143//! # assert_eq!(
1144//! #   AnyName { attribute: (), any_name: ((), "text cdata".into(), (), ()) },
1145//! #   quick_xml::de::from_str("\
1146//! #     <any-tag attribute='...'>\
1147//! #       <one>...</one>\
1148//! #       text \
1149//! #       <![CDATA[cdata]]>\
1150//! #       <two>...</two>\
1151//! #       <one>...</one>\
1152//! #     </any-tag>"
1153//! #   ).unwrap(),
1154//! # );
1155//! ```
1156//! ```
1157//! # use pretty_assertions::assert_eq;
1158//! # use serde::Deserialize;
1159//! # type One = ();
1160//! # type Two = ();
1161//! # /*
1162//! type One = ...;
1163//! type Two = ...;
1164//! # */
1165//!
1166//! # #[derive(Debug, PartialEq)]
1167//! #[derive(Deserialize)]
1168//! struct NamedTuple(One, String, Two, One);
1169//!
1170//! # #[derive(Debug, PartialEq)]
1171//! #[derive(Deserialize)]
1172//! struct AnyName {
1173//!   #[serde(rename = "@attribute")]
1174//! # attribute: (),
1175//! # /*
1176//!   attribute: ...,
1177//! # */
1178//!   // Does not (yet?) supported by the serde
1179//!   // https://github.com/serde-rs/serde/issues/1905
1180//!   // #[serde(flatten)]
1181//!   #[serde(rename = "$value")]
1182//!   any_name: NamedTuple,
1183//! }
1184//! # assert_eq!(
1185//! #   AnyName { attribute: (), any_name: NamedTuple((), "text cdata".into(), (), ()) },
1186//! #   quick_xml::de::from_str("\
1187//! #     <any-tag attribute='...'>\
1188//! #       <one>...</one>\
1189//! #       text \
1190//! #       <![CDATA[cdata]]>\
1191//! #       <two>...</two>\
1192//! #       <one>...</one>\
1193//! #     </any-tag>"
1194//! #   ).unwrap(),
1195//! # );
1196//! ```
1197//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
1198//!
1199//! NOTE: consequent text and CDATA nodes are merged into the one text node,
1200//! so you cannot have two adjacent string types in your sequence.
1201//! </div>
1202//! </td>
1203//! </tr>
1204//! <!-- 17 ==================================================================================== -->
1205//! <tr>
1206//! <td>
1207//! A sequence with a non-strict order, probably with a mixed content
1208//! (text / CDATA and tags) inside of the other element:
1209//!
1210//! ```xml
1211//! <any-tag>
1212//!   <one>...</one>
1213//!   text
1214//!   <![CDATA[cdata]]>
1215//!   <two>...</two>
1216//!   <one>...</one>
1217//! </any-tag>
1218//! ```
1219//! </td>
1220//! <td>
1221//!
1222//! A structure where all child elements mapped to the one field which have
1223//! a homogeneous sequential type: array-like container. A container type `T`
1224//! should be able to be deserialized from the nested element content (`...`),
1225//! except if it is an enum type which would be deserialized from the full
1226//! element (`<one>...</one>`).
1227//!
1228//! You MUST specify `#[serde(rename = "$value")]` on that field:
1229//!
1230//! ```
1231//! # use pretty_assertions::assert_eq;
1232//! # use serde::Deserialize;
1233//! # #[derive(Debug, PartialEq)]
1234//! #[derive(Deserialize)]
1235//! #[serde(rename_all = "snake_case")]
1236//! enum Choice {
1237//!   One,
1238//!   Two,
1239//!   #[serde(rename = "$text")]
1240//!   Other(String),
1241//! }
1242//! # #[derive(Debug, PartialEq)]
1243//! #[derive(Deserialize)]
1244//! struct AnyName {
1245//!   #[serde(rename = "@attribute")]
1246//! # attribute: (),
1247//! # /*
1248//!   attribute: ...,
1249//! # */
1250//!   // Does not (yet?) supported by the serde
1251//!   // https://github.com/serde-rs/serde/issues/1905
1252//!   // #[serde(flatten)]
1253//!   #[serde(rename = "$value")]
1254//!   any_name: [Choice; 4],
1255//! }
1256//! # assert_eq!(
1257//! #   AnyName { attribute: (), any_name: [
1258//! #     Choice::One,
1259//! #     Choice::Other("text cdata".into()),
1260//! #     Choice::Two,
1261//! #     Choice::One,
1262//! #   ] },
1263//! #   quick_xml::de::from_str("\
1264//! #     <any-tag attribute='...'>\
1265//! #       <one>...</one>\
1266//! #       text \
1267//! #       <![CDATA[cdata]]>\
1268//! #       <two>...</two>\
1269//! #       <one>...</one>\
1270//! #     </any-tag>"
1271//! #   ).unwrap(),
1272//! # );
1273//! ```
1274//! ```
1275//! # use pretty_assertions::assert_eq;
1276//! # use serde::Deserialize;
1277//! # #[derive(Debug, PartialEq)]
1278//! #[derive(Deserialize)]
1279//! #[serde(rename_all = "snake_case")]
1280//! enum Choice {
1281//!   One,
1282//!   Two,
1283//!   #[serde(rename = "$text")]
1284//!   Other(String),
1285//! }
1286//! # #[derive(Debug, PartialEq)]
1287//! #[derive(Deserialize)]
1288//! struct AnyName {
1289//!   #[serde(rename = "@attribute")]
1290//! # attribute: (),
1291//! # /*
1292//!   attribute: ...,
1293//! # */
1294//!   // Does not (yet?) supported by the serde
1295//!   // https://github.com/serde-rs/serde/issues/1905
1296//!   // #[serde(flatten)]
1297//!   #[serde(rename = "$value")]
1298//!   any_name: Vec<Choice>,
1299//! }
1300//! # assert_eq!(
1301//! #   AnyName { attribute: (), any_name: vec![
1302//! #     Choice::One,
1303//! #     Choice::Other("text cdata".into()),
1304//! #     Choice::Two,
1305//! #     Choice::One,
1306//! #   ] },
1307//! #   quick_xml::de::from_str("\
1308//! #     <any-tag attribute='...'>\
1309//! #       <one>...</one>\
1310//! #       text \
1311//! #       <![CDATA[cdata]]>\
1312//! #       <two>...</two>\
1313//! #       <one>...</one>\
1314//! #     </any-tag>"
1315//! #   ).unwrap(),
1316//! # );
1317//! ```
1318//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
1319//!
1320//! NOTE: consequent text and CDATA nodes are merged into the one text node,
1321//! so you cannot have two adjacent string types in your sequence.
1322//! </div>
1323//! </td>
1324//! </tr>
1325//! </tbody>
1326//! </table>
1327//!
1328//!
1329//! Mapping of `xsi:nil`
1330//! ====================
1331//!
1332//! quick-xml supports handling of [`xsi:nil`] special attribute. When field of optional
1333//! type is mapped to the XML element which have `xsi:nil="true"` set, or if that attribute
1334//! is placed on parent XML element, the deserializer will call [`Visitor::visit_none`]
1335//! and skip XML element corresponding to a field.
1336//!
1337//! Examples:
1338//!
1339//! ```
1340//! # use pretty_assertions::assert_eq;
1341//! # use serde::Deserialize;
1342//! #[derive(Deserialize, Debug, PartialEq)]
1343//! struct TypeWithOptionalField {
1344//!   element: Option<String>,
1345//! }
1346//!
1347//! assert_eq!(
1348//!   TypeWithOptionalField {
1349//!     element: None,
1350//!   },
1351//!   quick_xml::de::from_str("
1352//!     <any-tag xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
1353//!       <element xsi:nil='true'>Content is skiped because of xsi:nil='true'</element>
1354//!     </any-tag>
1355//!   ").unwrap(),
1356//! );
1357//! ```
1358//!
1359//! You can capture attributes from the optional type, because ` xsi:nil="true"` elements can have
1360//! attributes:
1361//! ```
1362//! # use pretty_assertions::assert_eq;
1363//! # use serde::Deserialize;
1364//! #[derive(Deserialize, Debug, PartialEq)]
1365//! struct TypeWithOptionalField {
1366//!   #[serde(rename = "@attribute")]
1367//!   attribute: usize,
1368//!
1369//!   element: Option<String>,
1370//!   non_optional: String,
1371//! }
1372//!
1373//! assert_eq!(
1374//!   TypeWithOptionalField {
1375//!     attribute: 42,
1376//!     element: None,
1377//!     non_optional: "Note, that non-optional fields will be deserialized as usual".to_string(),
1378//!   },
1379//!   quick_xml::de::from_str("
1380//!     <any-tag attribute='42' xsi:nil='true' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
1381//!       <element>Content is skiped because of xsi:nil='true'</element>
1382//!       <non_optional>Note, that non-optional fields will be deserialized as usual</non_optional>
1383//!     </any-tag>
1384//!   ").unwrap(),
1385//! );
1386//! ```
1387//!
1388//! Generate Rust types from XML
1389//! ============================
1390//!
1391//! To speed up the creation of Rust types that represent a given XML file you can
1392//! use the [xml_schema_generator](https://github.com/Thomblin/xml_schema_generator).
1393//! It provides a standalone binary and a Rust library that parses one or more XML files
1394//! and generates a collection of structs that are compatible with quick_xml::de.
1395//!
1396//!
1397//!
1398//! Composition Rules
1399//! =================
1400//!
1401//! The XML format is very different from other formats supported by `serde`.
1402//! One such difference it is how data in the serialized form is related to
1403//! the Rust type. Usually each byte in the data can be associated only with
1404//! one field in the data structure. However, XML is an exception.
1405//!
1406//! For example, took this XML:
1407//!
1408//! ```xml
1409//! <any>
1410//!   <key attr="value"/>
1411//! </any>
1412//! ```
1413//!
1414//! and try to deserialize it to the struct `AnyName`:
1415//!
1416//! ```no_run
1417//! # use serde::Deserialize;
1418//! #[derive(Deserialize)]
1419//! struct AnyName { // AnyName calls `deserialize_struct` on `<any><key attr="value"/></any>`
1420//!                  //                         Used data:          ^^^^^^^^^^^^^^^^^^^
1421//!   key: Inner,    // Inner   calls `deserialize_struct` on `<key attr="value"/>`
1422//!                  //                         Used data:          ^^^^^^^^^^^^
1423//! }
1424//! #[derive(Deserialize)]
1425//! struct Inner {
1426//!   #[serde(rename = "@attr")]
1427//!   attr: String,  // String  calls `deserialize_string` on `value`
1428//!                  //                         Used data:     ^^^^^
1429//! }
1430//! ```
1431//!
1432//! Comments shows what methods of a [`Deserializer`] called by each struct
1433//! `deserialize` method and which input their seen. **Used data** shows, what
1434//! content is actually used for deserializing. As you see, name of the inner
1435//! `<key>` tag used both as a map key / outer struct field name and as part
1436//! of the inner struct (although _value_ of the tag, i.e. `key` is not used
1437//! by it).
1438//!
1439//!
1440//!
1441//! Enum Representations
1442//! ====================
1443//!
1444//! `quick-xml` represents enums differently in normal fields, `$text` fields and
1445//! `$value` fields. A normal representation is compatible with serde's adjacent
1446//! and internal tags feature -- tag for adjacently and internally tagged enums
1447//! are serialized using [`Serializer::serialize_unit_variant`] and deserialized
1448//! using [`Deserializer::deserialize_enum`].
1449//!
1450//! Use those simple rules to remember, how enum would be represented in XML:
1451//! - In `$value` field the representation is always the same as top-level representation;
1452//! - In `$text` field the representation is always the same as in normal field,
1453//!   but surrounding tags with field name are removed;
1454//! - In normal field the representation is always contains a tag with a field name.
1455//!
1456//! Normal enum variant
1457//! -------------------
1458//!
1459//! To model an `xs:choice` XML construct use `$value` field.
1460//! To model a top-level `xs:choice` just use the enum type.
1461//!
1462//! |Kind   |Top-level and in `$value` field          |In normal field      |In `$text` field     |
1463//! |-------|-----------------------------------------|---------------------|---------------------|
1464//! |Unit   |`<Unit/>`                                |`<field>Unit</field>`|`Unit`               |
1465//! |Newtype|`<Newtype>42</Newtype>`                  |Err(Custom) [^0]     |Err(Custom) [^0]     |
1466//! |Tuple  |`<Tuple>42</Tuple><Tuple>answer</Tuple>` |Err(Custom) [^0]     |Err(Custom) [^0]     |
1467//! |Struct |`<Struct><q>42</q><a>answer</a></Struct>`|Err(Custom) [^0]     |Err(Custom) [^0]     |
1468//!
1469//! `$text` enum variant
1470//! --------------------
1471//!
1472//! |Kind   |Top-level and in `$value` field          |In normal field      |In `$text` field     |
1473//! |-------|-----------------------------------------|---------------------|---------------------|
1474//! |Unit   |_(empty)_                                |`<field/>`           |_(empty)_            |
1475//! |Newtype|`42`                                     |Err(Custom) [^0] [^1]|Err(Custom) [^0] [^2]|
1476//! |Tuple  |`42 answer`                              |Err(Custom) [^0] [^3]|Err(Custom) [^0] [^4]|
1477//! |Struct |Err(Custom) [^0]                         |Err(Custom) [^0]     |Err(Custom) [^0]     |
1478//!
1479//! [^0]: Error is returned by the deserialized type. In case of derived implementation a `Custom`
1480//!       error will be returned, but custom deserialize implementation can successfully deserialize
1481//!       value from a string which will be passed to it.
1482//!
1483//! [^1]: If this serialize as `<field>42</field>` then it will be ambiguity during deserialization,
1484//!       because it clash with `Unit` representation in normal field.
1485//!
1486//! [^2]: If this serialize as `42` then it will be ambiguity during deserialization,
1487//!       because it clash with `Unit` representation in `$text` field.
1488//!
1489//! [^3]: If this serialize as `<field>42 answer</field>` then it will be ambiguity during deserialization,
1490//!       because it clash with `Unit` representation in normal field.
1491//!
1492//! [^4]: If this serialize as `42 answer` then it will be ambiguity during deserialization,
1493//!       because it clash with `Unit` representation in `$text` field.
1494//!
1495//!
1496//!
1497//! `$text` and `$value` special names
1498//! ==================================
1499//!
1500//! quick-xml supports two special names for fields -- `$text` and `$value`.
1501//! Although they may seem the same, there is a distinction. Two different
1502//! names is required mostly for serialization, because quick-xml should know
1503//! how you want to serialize certain constructs, which could be represented
1504//! through XML in multiple different ways.
1505//!
1506//! The only difference is in how complex types and sequences are serialized.
1507//! If you doubt which one you should select, begin with [`$value`](#value).
1508//!
1509//! If you have both `$text` and `$value` in you struct, then text events will be
1510//! mapped to the `$text` field:
1511//!
1512//! ```
1513//! # use serde::Deserialize;
1514//! # use quick_xml::de::from_str;
1515//! #[derive(Deserialize, PartialEq, Debug)]
1516//! struct TextAndValue {
1517//!     #[serde(rename = "$text")]
1518//!     text: Option<String>,
1519//!
1520//!     #[serde(rename = "$value")]
1521//!     value: Option<String>,
1522//! }
1523//!
1524//! let object: TextAndValue = from_str("<AnyName>text <![CDATA[and CDATA]]></AnyName>").unwrap();
1525//! assert_eq!(object, TextAndValue {
1526//!     text: Some("text and CDATA".to_string()),
1527//!     value: None,
1528//! });
1529//! ```
1530//!
1531//! ## `$text`
1532//! `$text` is used when you want to write your XML as a text or a CDATA content.
1533//! More formally, field with that name represents simple type definition with
1534//! `{variety} = atomic` or `{variety} = union` whose basic members are all atomic,
1535//! as described in the [specification].
1536//!
1537//! As a result, not all types of such fields can be serialized. Only serialization
1538//! of following types are supported:
1539//! - all primitive types (strings, numbers, booleans)
1540//! - unit variants of enumerations (serializes to a name of a variant)
1541//! - newtypes (delegates serialization to inner type)
1542//! - [`Option`] of above (`None` serializes to nothing)
1543//! - sequences (including tuples and tuple variants of enumerations) of above,
1544//!   excluding `None` and empty string elements (because it will not be possible
1545//!   to deserialize them back). The elements are separated by space(s)
1546//! - unit type `()` and unit structs (serializes to nothing)
1547//!
1548//! Complex types, such as structs and maps, are not supported in this field.
1549//! If you want them, you should use `$value`.
1550//!
1551//! Sequences serialized to a space-delimited string, that is why only certain
1552//! types are allowed in this mode:
1553//!
1554//! ```
1555//! # use serde::{Deserialize, Serialize};
1556//! # use quick_xml::de::from_str;
1557//! # use quick_xml::se::to_string;
1558//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1559//! struct AnyName {
1560//!     #[serde(rename = "$text")]
1561//!     field: Vec<usize>,
1562//! }
1563//!
1564//! let obj = AnyName { field: vec![1, 2, 3] };
1565//! let xml = to_string(&obj).unwrap();
1566//! assert_eq!(xml, "<AnyName>1 2 3</AnyName>");
1567//!
1568//! let object: AnyName = from_str(&xml).unwrap();
1569//! assert_eq!(object, obj);
1570//! ```
1571//!
1572//! ## `$value`
1573//! <div style="background:rgba(120,145,255,0.45);padding:0.75em;">
1574//!
1575//! NOTE: a name `#content` would better explain the purpose of that field,
1576//! but `$value` is used for compatibility with other XML serde crates, which
1577//! uses that name. This will allow you to switch XML crates more smoothly if required.
1578//! </div>
1579//!
1580//! The representation of primitive types in `$value` does not differ from their
1581//! representation in `$text` fields. The difference is how sequences are serialized
1582//! and deserialized. `$value` serializes each sequence item as a separate XML element.
1583//! How the name of the XML element is chosen depends on the field's type. For
1584//! `enum`s, the variant name is used. For `struct`s, the name of the `struct`
1585//! is used.
1586//!
1587//! During deserialization, if the `$value` field is an enum, then the variant's
1588//! name is matched against. That's **not** the case with structs, however, since
1589//! `serde` does not expose type names of nested fields. This does mean that **any**
1590//! type could be deserialized into a `$value` struct-type field, so long as the
1591//! struct's fields have compatible types (or are captured as text by `String`
1592//! or similar-behaving types). This can be handy when using generic types in fields
1593//! where one knows in advance what to expect. If you do not know what to expect,
1594//! however, prefer an enum with all possible variants.
1595//!
1596//! Unit structs and unit type `()` serialize to nothing and can be deserialized
1597//! from any content.
1598//!
1599//! Serialization and deserialization of `$value` field performed as usual, except
1600//! that name for an XML element will be given by the serialized type, instead of
1601//! field. The latter allow to serialize enumerated types, where variant is encoded
1602//! as a tag name, and, so, represent an XSD `xs:choice` schema by the Rust `enum`.
1603//!
1604//! In the example below, field will be serialized as `<field/>`, because elements
1605//! get their names from the field name. It cannot be deserialized, because `Enum`
1606//! expects elements `<A/>`, `<B/>` or `<C/>`, but `AnyName` looked only for `<field/>`:
1607//!
1608//! ```
1609//! # use serde::{Deserialize, Serialize};
1610//! # use pretty_assertions::assert_eq;
1611//! # #[derive(PartialEq, Debug)]
1612//! #[derive(Deserialize, Serialize)]
1613//! enum Enum { A, B, C }
1614//!
1615//! # #[derive(PartialEq, Debug)]
1616//! #[derive(Deserialize, Serialize)]
1617//! struct AnyName {
1618//!     // <field>A</field>, <field>B</field>, or <field>C</field>
1619//!     field: Enum,
1620//! }
1621//! # assert_eq!(
1622//! #     quick_xml::se::to_string(&AnyName { field: Enum::A }).unwrap(),
1623//! #     "<AnyName><field>A</field></AnyName>",
1624//! # );
1625//! # assert_eq!(
1626//! #     AnyName { field: Enum::B },
1627//! #     quick_xml::de::from_str("<root><field>B</field></root>").unwrap(),
1628//! # );
1629//! ```
1630//!
1631//! If you rename field to `$value`, then `field` would be serialized as `<A/>`,
1632//! `<B/>` or `<C/>`, depending on the its content. It is also possible to
1633//! deserialize it from the same elements:
1634//!
1635//! ```
1636//! # use serde::{Deserialize, Serialize};
1637//! # use pretty_assertions::assert_eq;
1638//! # #[derive(Deserialize, Serialize, PartialEq, Debug)]
1639//! # enum Enum { A, B, C }
1640//! #
1641//! # #[derive(PartialEq, Debug)]
1642//! #[derive(Deserialize, Serialize)]
1643//! struct AnyName {
1644//!     // <A/>, <B/> or <C/>
1645//!     #[serde(rename = "$value")]
1646//!     field: Enum,
1647//! }
1648//! # assert_eq!(
1649//! #     quick_xml::se::to_string(&AnyName { field: Enum::A }).unwrap(),
1650//! #     "<AnyName><A/></AnyName>",
1651//! # );
1652//! # assert_eq!(
1653//! #     AnyName { field: Enum::B },
1654//! #     quick_xml::de::from_str("<root><B/></root>").unwrap(),
1655//! # );
1656//! ```
1657//!
1658//! The next example demonstrates how generic types can be used in conjunction
1659//! with `$value`-named fields to allow the reuse of wrapping structs. A common
1660//! example use case for this feature is SOAP messages, which can be commmonly
1661//! found wrapped around `<soapenv:Envelope> ... </soapenv:Envelope>`.
1662//!
1663//! ```rust
1664//! # use pretty_assertions::assert_eq;
1665//! # use quick_xml::de::from_str;
1666//! # use quick_xml::se::to_string;
1667//! # use serde::{Deserialize, Serialize};
1668//! #
1669//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1670//! struct Envelope<T> {
1671//!     body: Body<T>,
1672//! }
1673//!
1674//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1675//! struct Body<T> {
1676//!     #[serde(rename = "$value")]
1677//!     inner: T,
1678//! }
1679//!
1680//! #[derive(Serialize, PartialEq, Debug)]
1681//! struct Example {
1682//!     a: i32,
1683//! }
1684//!
1685//! assert_eq!(
1686//!     to_string(&Envelope { body: Body { inner: Example { a: 42 } } }).unwrap(),
1687//!     // Notice how `inner` is not present in the XML
1688//!     "<Envelope><body><Example><a>42</a></Example></body></Envelope>",
1689//! );
1690//!
1691//! #[derive(Deserialize, PartialEq, Debug)]
1692//! struct AnotherExample {
1693//!     a: i32,
1694//! }
1695//!
1696//! assert_eq!(
1697//!     // Notice that tag the name does nothing for struct in `$value` field
1698//!     Envelope { body: Body { inner: AnotherExample { a: 42 } } },
1699//!     from_str("<Envelope><body><Example><a>42</a></Example></body></Envelope>").unwrap(),
1700//! );
1701//! ```
1702//!
1703//! ### Primitives and sequences of primitives
1704//!
1705//! Sequences serialized to a list of elements. Note, that types that does not
1706//! produce their own tag (i. e. primitives) will produce [`SeError::Unsupported`]
1707//! if they contains more that one element, because such sequence cannot be
1708//! deserialized to the same value:
1709//!
1710//! ```
1711//! # use serde::{Deserialize, Serialize};
1712//! # use pretty_assertions::assert_eq;
1713//! # use quick_xml::de::from_str;
1714//! # use quick_xml::se::to_string;
1715//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1716//! struct AnyName {
1717//!     #[serde(rename = "$value")]
1718//!     field: Vec<usize>,
1719//! }
1720//!
1721//! let obj = AnyName { field: vec![1, 2, 3] };
1722//! // If this object were serialized, it would be represented as "<AnyName>123</AnyName>"
1723//! to_string(&obj).unwrap_err();
1724//!
1725//! let object: AnyName = from_str("<AnyName>123</AnyName>").unwrap();
1726//! assert_eq!(object, AnyName { field: vec![123] });
1727//!
1728//! // `1 2 3` is mapped to a single `usize` element
1729//! // It is impossible to deserialize list of primitives to such field
1730//! from_str::<AnyName>("<AnyName>1 2 3</AnyName>").unwrap_err();
1731//! ```
1732//!
1733//! A particular case of that example is a string `$value` field, which probably
1734//! would be a most used example of that attribute:
1735//!
1736//! ```
1737//! # use serde::{Deserialize, Serialize};
1738//! # use pretty_assertions::assert_eq;
1739//! # use quick_xml::de::from_str;
1740//! # use quick_xml::se::to_string;
1741//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1742//! struct AnyName {
1743//!     #[serde(rename = "$value")]
1744//!     field: String,
1745//! }
1746//!
1747//! let obj = AnyName { field: "content".to_string() };
1748//! let xml = to_string(&obj).unwrap();
1749//! assert_eq!(xml, "<AnyName>content</AnyName>");
1750//! ```
1751//!
1752//! ### Structs and sequences of structs
1753//!
1754//! Note, that structures do not have a serializable name as well (name of the
1755//! type is never used), so it is impossible to serialize non-unit struct or
1756//! sequence of non-unit structs in `$value` field. (sequences of) unit structs
1757//! are serialized as empty string, because units itself serializing
1758//! to nothing:
1759//!
1760//! ```
1761//! # use serde::{Deserialize, Serialize};
1762//! # use pretty_assertions::assert_eq;
1763//! # use quick_xml::de::from_str;
1764//! # use quick_xml::se::to_string;
1765//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1766//! struct Unit;
1767//!
1768//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1769//! struct AnyName {
1770//!     // #[serde(default)] is required to deserialization of empty lists
1771//!     // This is a general note, not related to $value
1772//!     #[serde(rename = "$value", default)]
1773//!     field: Vec<Unit>,
1774//! }
1775//!
1776//! let obj = AnyName { field: vec![Unit, Unit, Unit] };
1777//! let xml = to_string(&obj).unwrap();
1778//! assert_eq!(xml, "<AnyName/>");
1779//!
1780//! let object: AnyName = from_str("<AnyName/>").unwrap();
1781//! assert_eq!(object, AnyName { field: vec![] });
1782//!
1783//! let object: AnyName = from_str("<AnyName></AnyName>").unwrap();
1784//! assert_eq!(object, AnyName { field: vec![] });
1785//!
1786//! let object: AnyName = from_str("<AnyName><A/><B/><C/></AnyName>").unwrap();
1787//! assert_eq!(object, AnyName { field: vec![Unit, Unit, Unit] });
1788//! ```
1789//!
1790//! ### Enums and sequences of enums
1791//!
1792//! Enumerations uses the variant name as an element name:
1793//!
1794//! ```
1795//! # use serde::{Deserialize, Serialize};
1796//! # use pretty_assertions::assert_eq;
1797//! # use quick_xml::de::from_str;
1798//! # use quick_xml::se::to_string;
1799//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1800//! struct AnyName {
1801//!     #[serde(rename = "$value")]
1802//!     field: Vec<Enum>,
1803//! }
1804//!
1805//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
1806//! enum Enum { A, B, C }
1807//!
1808//! let obj = AnyName { field: vec![Enum::A, Enum::B, Enum::C] };
1809//! let xml = to_string(&obj).unwrap();
1810//! assert_eq!(
1811//!     xml,
1812//!     "<AnyName>\
1813//!         <A/>\
1814//!         <B/>\
1815//!         <C/>\
1816//!      </AnyName>"
1817//! );
1818//!
1819//! let object: AnyName = from_str(&xml).unwrap();
1820//! assert_eq!(object, obj);
1821//! ```
1822//!
1823//!
1824//!
1825//! Frequently Used Patterns
1826//! ========================
1827//!
1828//! Some XML constructs used so frequent, that it is worth to document the recommended
1829//! way to represent them in the Rust. The sections below describes them.
1830//!
1831//! `<element>` lists
1832//! -----------------
1833//! Many XML formats wrap lists of elements in the additional container,
1834//! although this is not required by the XML rules:
1835//!
1836//! ```xml
1837//! <root>
1838//!   <field1/>
1839//!   <field2/>
1840//!   <list><!-- Container -->
1841//!     <element/>
1842//!     <element/>
1843//!     <element/>
1844//!   </list>
1845//!   <field3/>
1846//! </root>
1847//! ```
1848//! In this case, there is a great desire to describe this XML in this way:
1849//! ```
1850//! /// Represents <element/>
1851//! type Element = ();
1852//!
1853//! /// Represents <root>...</root>
1854//! struct AnyName {
1855//!     // Incorrect
1856//!     list: Vec<Element>,
1857//! }
1858//! ```
1859//! This will not work, because potentially `<list>` element can have attributes
1860//! and other elements inside. You should define the struct for the `<list>`
1861//! explicitly, as you do that in the XSD for that XML:
1862//! ```
1863//! /// Represents <element/>
1864//! type Element = ();
1865//!
1866//! /// Represents <root>...</root>
1867//! struct AnyName {
1868//!     // Correct
1869//!     list: List,
1870//! }
1871//! /// Represents <list>...</list>
1872//! struct List {
1873//!     element: Vec<Element>,
1874//! }
1875//! ```
1876//!
1877//! If you want to simplify your API, you could write a simple function for unwrapping
1878//! inner list and apply it via [`deserialize_with`]:
1879//!
1880//! ```
1881//! # use pretty_assertions::assert_eq;
1882//! use quick_xml::de::from_str;
1883//! use serde::{Deserialize, Deserializer};
1884//!
1885//! /// Represents <element/>
1886//! type Element = ();
1887//!
1888//! /// Represents <root>...</root>
1889//! #[derive(Deserialize, Debug, PartialEq)]
1890//! struct AnyName {
1891//!     #[serde(deserialize_with = "unwrap_list")]
1892//!     list: Vec<Element>,
1893//! }
1894//!
1895//! fn unwrap_list<'de, D>(deserializer: D) -> Result<Vec<Element>, D::Error>
1896//! where
1897//!     D: Deserializer<'de>,
1898//! {
1899//!     /// Represents <list>...</list>
1900//!     #[derive(Deserialize)]
1901//!     struct List {
1902//!         // default allows empty list
1903//!         #[serde(default)]
1904//!         element: Vec<Element>,
1905//!     }
1906//!     Ok(List::deserialize(deserializer)?.element)
1907//! }
1908//!
1909//! assert_eq!(
1910//!     AnyName { list: vec![(), (), ()] },
1911//!     from_str("
1912//!         <root>
1913//!           <list>
1914//!             <element/>
1915//!             <element/>
1916//!             <element/>
1917//!           </list>
1918//!         </root>
1919//!     ").unwrap(),
1920//! );
1921//! ```
1922//!
1923//! Instead of writing such functions manually, you also could try <https://lib.rs/crates/serde-query>.
1924//!
1925//! Overlapped (Out-of-Order) Elements
1926//! ----------------------------------
1927//! In the case that the list might contain tags that are overlapped with
1928//! tags that do not correspond to the list (this is a usual case in XML
1929//! documents) like this:
1930//! ```xml
1931//! <any-name>
1932//!   <item/>
1933//!   <another-item/>
1934//!   <item/>
1935//!   <item/>
1936//! </any-name>
1937//! ```
1938//! you should enable the [`overlapped-lists`] feature to make it possible
1939//! to deserialize this to:
1940//! ```no_run
1941//! # use serde::Deserialize;
1942//! #[derive(Deserialize)]
1943//! #[serde(rename_all = "kebab-case")]
1944//! struct AnyName {
1945//!     item: Vec<()>,
1946//!     another_item: (),
1947//! }
1948//! ```
1949//!
1950//!
1951//! Internally Tagged Enums
1952//! -----------------------
1953//! [Tagged enums] are currently not supported because of an issue in the Serde
1954//! design (see [serde#1183] and [quick-xml#586]) and missing optimizations in
1955//! Serde which could be useful for XML parsing ([serde#1495]). This can be worked
1956//! around by manually implementing deserialize with `#[serde(deserialize_with = "func")]`
1957//! or implementing [`Deserialize`], but this can get very tedious very fast for
1958//! files with large amounts of tagged enums. To help with this issue quick-xml
1959//! provides a macro [`impl_deserialize_for_internally_tagged_enum!`]. See the
1960//! macro documentation for details.
1961//!
1962//!
1963//! [`overlapped-lists`]: ../index.html#overlapped-lists
1964//! [specification]: https://www.w3.org/TR/xmlschema11-1/#Simple_Type_Definition
1965//! [`deserialize_with`]: https://serde.rs/field-attrs.html#deserialize_with
1966//! [`xsi:nil`]: https://www.w3.org/TR/xmlschema-1/#xsi_nil
1967//! [`Serializer::serialize_unit_variant`]: serde::Serializer::serialize_unit_variant
1968//! [`Deserializer::deserialize_enum`]: serde::Deserializer::deserialize_enum
1969//! [`SeError::Unsupported`]: crate::errors::serialize::SeError::Unsupported
1970//! [Tagged enums]: https://serde.rs/enum-representations.html#internally-tagged
1971//! [serde#1183]: https://github.com/serde-rs/serde/issues/1183
1972//! [serde#1495]: https://github.com/serde-rs/serde/issues/1495
1973//! [quick-xml#586]: https://github.com/tafia/quick-xml/issues/586
1974//! [`impl_deserialize_for_internally_tagged_enum!`]: crate::impl_deserialize_for_internally_tagged_enum
1975
1976macro_rules! forward_to_simple_type {
1977    ($deserialize:ident, $($mut:tt)?) => {
1978        #[inline]
1979        fn $deserialize<V>($($mut)? self, visitor: V) -> Result<V::Value, DeError>
1980        where
1981            V: Visitor<'de>,
1982        {
1983            SimpleTypeDeserializer::from_text(self.read_string()?).$deserialize(visitor)
1984        }
1985    };
1986}
1987
1988/// Implement deserialization methods for scalar types, such as numbers, strings,
1989/// byte arrays, booleans and identifiers.
1990macro_rules! deserialize_primitives {
1991    ($($mut:tt)?) => {
1992        forward_to_simple_type!(deserialize_i8, $($mut)?);
1993        forward_to_simple_type!(deserialize_i16, $($mut)?);
1994        forward_to_simple_type!(deserialize_i32, $($mut)?);
1995        forward_to_simple_type!(deserialize_i64, $($mut)?);
1996
1997        forward_to_simple_type!(deserialize_u8, $($mut)?);
1998        forward_to_simple_type!(deserialize_u16, $($mut)?);
1999        forward_to_simple_type!(deserialize_u32, $($mut)?);
2000        forward_to_simple_type!(deserialize_u64, $($mut)?);
2001
2002        forward_to_simple_type!(deserialize_i128, $($mut)?);
2003        forward_to_simple_type!(deserialize_u128, $($mut)?);
2004
2005        forward_to_simple_type!(deserialize_f32, $($mut)?);
2006        forward_to_simple_type!(deserialize_f64, $($mut)?);
2007
2008        forward_to_simple_type!(deserialize_bool, $($mut)?);
2009        forward_to_simple_type!(deserialize_char, $($mut)?);
2010
2011        forward_to_simple_type!(deserialize_str, $($mut)?);
2012        forward_to_simple_type!(deserialize_string, $($mut)?);
2013
2014        /// Forwards deserialization to the [`deserialize_any`](#method.deserialize_any).
2015        #[inline]
2016        fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, DeError>
2017        where
2018            V: Visitor<'de>,
2019        {
2020            self.deserialize_any(visitor)
2021        }
2022
2023        /// Forwards deserialization to the [`deserialize_bytes`](#method.deserialize_bytes).
2024        #[inline]
2025        fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, DeError>
2026        where
2027            V: Visitor<'de>,
2028        {
2029            self.deserialize_bytes(visitor)
2030        }
2031
2032        /// Representation of the named units the same as [unnamed units](#method.deserialize_unit).
2033        #[inline]
2034        fn deserialize_unit_struct<V>(
2035            self,
2036            _name: &'static str,
2037            visitor: V,
2038        ) -> Result<V::Value, DeError>
2039        where
2040            V: Visitor<'de>,
2041        {
2042            self.deserialize_unit(visitor)
2043        }
2044
2045        /// Representation of tuples the same as [sequences](#method.deserialize_seq).
2046        #[inline]
2047        fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, DeError>
2048        where
2049            V: Visitor<'de>,
2050        {
2051            self.deserialize_seq(visitor)
2052        }
2053
2054        /// Representation of named tuples the same as [unnamed tuples](#method.deserialize_tuple).
2055        #[inline]
2056        fn deserialize_tuple_struct<V>(
2057            self,
2058            _name: &'static str,
2059            len: usize,
2060            visitor: V,
2061        ) -> Result<V::Value, DeError>
2062        where
2063            V: Visitor<'de>,
2064        {
2065            self.deserialize_tuple(len, visitor)
2066        }
2067
2068        /// Forwards deserialization to the [`deserialize_struct`](#method.deserialize_struct)
2069        /// with empty name and fields.
2070        #[inline]
2071        fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, DeError>
2072        where
2073            V: Visitor<'de>,
2074        {
2075            self.deserialize_struct("", &[], visitor)
2076        }
2077
2078        /// Identifiers represented as [strings](#method.deserialize_str).
2079        #[inline]
2080        fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, DeError>
2081        where
2082            V: Visitor<'de>,
2083        {
2084            self.deserialize_str(visitor)
2085        }
2086
2087        /// Forwards deserialization to the [`deserialize_unit`](#method.deserialize_unit).
2088        #[inline]
2089        fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, DeError>
2090        where
2091            V: Visitor<'de>,
2092        {
2093            self.deserialize_unit(visitor)
2094        }
2095    };
2096}
2097
2098mod attributes;
2099mod key;
2100mod map;
2101mod resolver;
2102mod simple_type;
2103mod text;
2104mod var;
2105
2106pub use self::attributes::AttributesDeserializer;
2107pub use self::resolver::{EntityResolver, PredefinedEntityResolver};
2108pub use self::simple_type::SimpleTypeDeserializer;
2109pub use crate::errors::serialize::DeError;
2110use crate::XmlVersion;
2111
2112use crate::{
2113    de::map::ElementMapAccess,
2114    encoding::Decoder,
2115    errors::Error,
2116    escape::{parse_number, EscapeError},
2117    events::{BytesCData, BytesEnd, BytesRef, BytesStart, BytesText, Event},
2118    name::QName,
2119    reader::NsReader,
2120};
2121use serde::de::{
2122    self, Deserialize, DeserializeOwned, DeserializeSeed, IntoDeserializer, SeqAccess, Visitor,
2123};
2124use std::borrow::Cow;
2125#[cfg(feature = "overlapped-lists")]
2126use std::collections::VecDeque;
2127use std::io::BufRead;
2128use std::mem::replace;
2129#[cfg(feature = "overlapped-lists")]
2130use std::num::NonZeroUsize;
2131use std::ops::{Deref, Range};
2132
2133/// Data represented by a text node or a CDATA node. XML markup is not expected
2134pub(crate) const TEXT_KEY: &str = "$text";
2135/// Data represented by any XML markup inside
2136pub(crate) const VALUE_KEY: &str = "$value";
2137
2138/// A function to check whether the character is a whitespace (blank, new line, carriage return or tab).
2139#[inline]
2140const fn is_non_whitespace(ch: char) -> bool {
2141    !matches!(ch, ' ' | '\r' | '\n' | '\t')
2142}
2143
2144/// Decoded and concatenated content of consequent [`Text`] and [`CData`]
2145/// events. _Consequent_ means that events should follow each other or be
2146/// delimited only by (any count of) [`Comment`] or [`PI`] events.
2147///
2148/// Internally text is stored in `Cow<str>`. Cloning of text is cheap while it
2149/// is borrowed and makes copies of data when it is owned.
2150///
2151/// [`Text`]: Event::Text
2152/// [`CData`]: Event::CData
2153/// [`Comment`]: Event::Comment
2154/// [`PI`]: Event::PI
2155#[derive(Clone, Debug, PartialEq, Eq)]
2156pub struct Text<'a> {
2157    /// Untrimmed text after concatenating content of all
2158    /// [`Text`] and [`CData`] events
2159    ///
2160    /// [`Text`]: Event::Text
2161    /// [`CData`]: Event::CData
2162    text: Cow<'a, str>,
2163    /// A range into `text` which contains data after trimming
2164    content: Range<usize>,
2165}
2166
2167impl<'a> Text<'a> {
2168    fn new(text: Cow<'a, str>) -> Self {
2169        let start = text.find(is_non_whitespace).unwrap_or(0);
2170        let end = text.rfind(is_non_whitespace).map_or(0, |i| i + 1);
2171
2172        let content = if start >= end { 0..0 } else { start..end };
2173
2174        Self { text, content }
2175    }
2176
2177    /// Returns text without leading and trailing whitespaces as [defined] by XML specification.
2178    ///
2179    /// If you want to only check if text contains only whitespaces, use [`is_blank`](Self::is_blank),
2180    /// which will not allocate.
2181    ///
2182    /// # Example
2183    ///
2184    /// ```
2185    /// # use quick_xml::de::Text;
2186    /// # use pretty_assertions::assert_eq;
2187    /// #
2188    /// let text = Text::from("");
2189    /// assert_eq!(text.trimmed(), "");
2190    ///
2191    /// let text = Text::from(" \r\n\t ");
2192    /// assert_eq!(text.trimmed(), "");
2193    ///
2194    /// let text = Text::from("  some useful text  ");
2195    /// assert_eq!(text.trimmed(), "some useful text");
2196    /// ```
2197    ///
2198    /// [defined]: https://www.w3.org/TR/xml11/#NT-S
2199    pub fn trimmed(&self) -> Cow<'a, str> {
2200        match self.text {
2201            Cow::Borrowed(text) => Cow::Borrowed(&text[self.content.clone()]),
2202            Cow::Owned(ref text) => Cow::Owned(text[self.content.clone()].to_string()),
2203        }
2204    }
2205
2206    /// Returns `true` if text is empty or contains only whitespaces as [defined] by XML specification.
2207    ///
2208    /// # Example
2209    ///
2210    /// ```
2211    /// # use quick_xml::de::Text;
2212    /// # use pretty_assertions::assert_eq;
2213    /// #
2214    /// let text = Text::from("");
2215    /// assert_eq!(text.is_blank(), true);
2216    ///
2217    /// let text = Text::from(" \r\n\t ");
2218    /// assert_eq!(text.is_blank(), true);
2219    ///
2220    /// let text = Text::from("  some useful text  ");
2221    /// assert_eq!(text.is_blank(), false);
2222    /// ```
2223    ///
2224    /// [defined]: https://www.w3.org/TR/xml11/#NT-S
2225    pub fn is_blank(&self) -> bool {
2226        self.content.is_empty()
2227    }
2228}
2229
2230impl<'a> Deref for Text<'a> {
2231    type Target = str;
2232
2233    #[inline]
2234    fn deref(&self) -> &Self::Target {
2235        self.text.deref()
2236    }
2237}
2238
2239impl<'a> From<&'a str> for Text<'a> {
2240    #[inline]
2241    fn from(text: &'a str) -> Self {
2242        Self::new(Cow::Borrowed(text))
2243    }
2244}
2245
2246impl<'a> From<String> for Text<'a> {
2247    #[inline]
2248    fn from(text: String) -> Self {
2249        Self::new(Cow::Owned(text))
2250    }
2251}
2252
2253impl<'a> From<Cow<'a, str>> for Text<'a> {
2254    #[inline]
2255    fn from(text: Cow<'a, str>) -> Self {
2256        Self::new(text)
2257    }
2258}
2259
2260////////////////////////////////////////////////////////////////////////////////////////////////////
2261
2262/// Simplified event which contains only these variants that used by deserializer
2263#[derive(Clone, Debug, PartialEq, Eq)]
2264pub enum DeEvent<'a> {
2265    /// Start tag (with attributes) `<tag attr="value">`.
2266    Start(BytesStart<'a>),
2267    /// End tag `</tag>`.
2268    End(BytesEnd<'a>),
2269    /// Decoded and concatenated content of consequent [`Text`] and [`CData`]
2270    /// events. _Consequent_ means that events should follow each other or be
2271    /// delimited only by (any count of) [`Comment`] or [`PI`] events.
2272    ///
2273    /// [`Text`]: Event::Text
2274    /// [`CData`]: Event::CData
2275    /// [`Comment`]: Event::Comment
2276    /// [`PI`]: Event::PI
2277    Text(Text<'a>),
2278    /// End of XML document.
2279    Eof,
2280}
2281
2282////////////////////////////////////////////////////////////////////////////////////////////////////
2283
2284/// Simplified event which contains only these variants that used by deserializer,
2285/// but [`Text`] events not yet fully processed.
2286///
2287/// [`Text`] events should be trimmed if they does not surrounded by the other
2288/// [`Text`] or [`CData`] events. This event contains intermediate state of [`Text`]
2289/// event, where they are trimmed from the start, but not from the end. To trim
2290/// end spaces we should lookahead by one deserializer event (i. e. skip all
2291/// comments and processing instructions).
2292///
2293/// [`Text`]: Event::Text
2294/// [`CData`]: Event::CData
2295#[derive(Clone, Debug, PartialEq, Eq)]
2296pub enum PayloadEvent<'a> {
2297    /// Start tag (with attributes) `<tag attr="value">`.
2298    Start(BytesStart<'a>),
2299    /// End tag `</tag>`.
2300    End(BytesEnd<'a>),
2301    /// Escaped character data between tags.
2302    Text(BytesText<'a>),
2303    /// Unescaped character data stored in `<![CDATA[...]]>`.
2304    CData(BytesCData<'a>),
2305    /// Document type definition data (DTD) stored in `<!DOCTYPE ...>`.
2306    DocType(BytesText<'a>),
2307    /// Reference `&ref;` in the textual data.
2308    GeneralRef(BytesRef<'a>),
2309    /// End of XML document.
2310    Eof,
2311}
2312
2313impl<'a> PayloadEvent<'a> {
2314    /// Ensures that all data is owned to extend the object's lifetime if necessary.
2315    #[inline]
2316    fn into_owned(self) -> PayloadEvent<'static> {
2317        match self {
2318            PayloadEvent::Start(e) => PayloadEvent::Start(e.into_owned()),
2319            PayloadEvent::End(e) => PayloadEvent::End(e.into_owned()),
2320            PayloadEvent::Text(e) => PayloadEvent::Text(e.into_owned()),
2321            PayloadEvent::CData(e) => PayloadEvent::CData(e.into_owned()),
2322            PayloadEvent::DocType(e) => PayloadEvent::DocType(e.into_owned()),
2323            PayloadEvent::GeneralRef(e) => PayloadEvent::GeneralRef(e.into_owned()),
2324            PayloadEvent::Eof => PayloadEvent::Eof,
2325        }
2326    }
2327}
2328
2329/// An intermediate reader that consumes [`PayloadEvent`]s and produces final [`DeEvent`]s.
2330/// [`PayloadEvent::Text`] events, that followed by any event except
2331/// [`PayloadEvent::Text`] or [`PayloadEvent::CData`], are trimmed from the end.
2332struct XmlReader<'i, R: XmlRead<'i>, E: EntityResolver = PredefinedEntityResolver> {
2333    /// A source of low-level XML events
2334    reader: R,
2335    /// Intermediate event, that could be returned by the next call to `next()`.
2336    /// If that is the `Text` event then leading spaces already trimmed, but
2337    /// trailing spaces is not. Before the event will be returned, trimming of
2338    /// the spaces could be necessary
2339    lookahead: Result<PayloadEvent<'i>, DeError>,
2340
2341    /// Used to resolve unknown entities that would otherwise cause the parser
2342    /// to return an [`EscapeError::UnrecognizedEntity`] error.
2343    ///
2344    /// [`EscapeError::UnrecognizedEntity`]: crate::escape::EscapeError::UnrecognizedEntity
2345    entity_resolver: E,
2346}
2347
2348impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
2349    fn new(mut reader: R, entity_resolver: E) -> Self {
2350        // Lookahead by one event immediately, so we do not need to check in the
2351        // loop if we need lookahead or not
2352        let lookahead = reader.next();
2353
2354        Self {
2355            reader,
2356            lookahead,
2357            entity_resolver,
2358        }
2359    }
2360
2361    /// Returns `true` if all events was consumed
2362    const fn is_empty(&self) -> bool {
2363        matches!(self.lookahead, Ok(PayloadEvent::Eof))
2364    }
2365
2366    /// Read next event and put it in lookahead, return the current lookahead
2367    #[inline(always)]
2368    fn next_impl(&mut self) -> Result<PayloadEvent<'i>, DeError> {
2369        replace(&mut self.lookahead, self.reader.next())
2370    }
2371
2372    /// Returns `true` when next event is not a text event in any form.
2373    #[inline(always)]
2374    const fn current_event_is_last_text(&self) -> bool {
2375        // If next event is a text-like event or a DocType (which is
2376        // metadata and invisible to the data model), we should not
2377        // trim trailing spaces — there is more content to drain, and
2378        // any DocType between us and the next text run needs to be
2379        // absorbed so `read_text` does not later see two consecutive
2380        // `DeEvent::Text`. Without DocType here, an input like
2381        // `<a>x<!DOCTYPE y>z</a>` produces two text events for `x`
2382        // and `z`, tripping `unreachable!()` in `read_text`.
2383        !matches!(
2384            self.lookahead,
2385            Ok(PayloadEvent::Text(_)
2386                | PayloadEvent::CData(_)
2387                | PayloadEvent::GeneralRef(_)
2388                | PayloadEvent::DocType(_))
2389        )
2390    }
2391
2392    /// Read all consequent [`Text`] and [`CData`] events until non-text event
2393    /// occurs. Content of all events would be appended to `result` and returned
2394    /// as [`DeEvent::Text`].
2395    ///
2396    /// DocType events that fall between text events are absorbed by the
2397    /// entity resolver and do not break the run — see
2398    /// [`Self::current_event_is_last_text`] for the rationale.
2399    ///
2400    /// [`Text`]: PayloadEvent::Text
2401    /// [`CData`]: PayloadEvent::CData
2402    fn drain_text(&mut self, mut result: Cow<'i, str>) -> Result<DeEvent<'i>, DeError> {
2403        loop {
2404            if self.current_event_is_last_text() {
2405                break;
2406            }
2407
2408            match self.next_impl()? {
2409                PayloadEvent::Text(e) => result
2410                    .to_mut()
2411                    .push_str(&e.xml_content(self.reader.xml_version())?),
2412                PayloadEvent::CData(e) => result
2413                    .to_mut()
2414                    .push_str(&e.xml_content(self.reader.xml_version())?),
2415                PayloadEvent::GeneralRef(e) => self.resolve_reference(result.to_mut(), e)?,
2416                PayloadEvent::DocType(e) => {
2417                    self.entity_resolver
2418                        .capture(e)
2419                        .map_err(|err| DeError::Custom(format!("cannot parse DTD: {}", err)))?;
2420                }
2421
2422                // SAFETY: current_event_is_last_text checks that event is Text, CData, GeneralRef, or DocType
2423                _ => unreachable!(
2424                    "Only `Text`, `CData`, `GeneralRef` or `DocType` events can come here"
2425                ),
2426            }
2427        }
2428        Ok(DeEvent::Text(Text::new(result)))
2429    }
2430
2431    /// Return an input-borrowing event.
2432    fn next(&mut self) -> Result<DeEvent<'i>, DeError> {
2433        loop {
2434            return match self.next_impl()? {
2435                PayloadEvent::Start(e) => Ok(DeEvent::Start(e)),
2436                PayloadEvent::End(e) => Ok(DeEvent::End(e)),
2437                PayloadEvent::Text(e) => self.drain_text(e.xml_content(self.reader.xml_version())?),
2438                PayloadEvent::CData(e) => {
2439                    self.drain_text(e.xml_content(self.reader.xml_version())?)
2440                }
2441                PayloadEvent::DocType(e) => {
2442                    self.entity_resolver
2443                        .capture(e)
2444                        .map_err(|err| DeError::Custom(format!("cannot parse DTD: {}", err)))?;
2445                    continue;
2446                }
2447                PayloadEvent::GeneralRef(e) => {
2448                    let mut text = String::new();
2449                    self.resolve_reference(&mut text, e)?;
2450                    self.drain_text(text.into())
2451                }
2452                PayloadEvent::Eof => Ok(DeEvent::Eof),
2453            };
2454        }
2455    }
2456
2457    fn resolve_reference(&mut self, result: &mut String, event: BytesRef) -> Result<(), DeError> {
2458        let len = event.len();
2459        let reference = self.decoder().decode(&event)?;
2460
2461        if let Some(num) = reference.strip_prefix('#') {
2462            let codepoint = parse_number(num).map_err(EscapeError::InvalidCharRef)?;
2463            result.push_str(codepoint.encode_utf8(&mut [0u8; 4]));
2464            return Ok(());
2465        }
2466        if let Some(value) = self.entity_resolver.resolve(reference.as_ref()) {
2467            result.push_str(value);
2468            return Ok(());
2469        }
2470        Err(EscapeError::UnrecognizedEntity(0..len, reference.to_string()).into())
2471    }
2472
2473    #[inline]
2474    fn read_to_end(&mut self, name: QName) -> Result<(), DeError> {
2475        match self.lookahead {
2476            // We pre-read event with the same name that is required to be skipped.
2477            // First call of `read_to_end` will end out pre-read event, the second
2478            // will consume other events
2479            Ok(PayloadEvent::Start(ref e)) if e.name() == name => {
2480                let result1 = self.reader.read_to_end(name);
2481                let result2 = self.reader.read_to_end(name);
2482
2483                // In case of error `next_impl` returns `Eof`
2484                let _ = self.next_impl();
2485                result1?;
2486                result2?;
2487            }
2488            // We pre-read event with the same name that is required to be skipped.
2489            // Because this is end event, we already consume the whole tree, so
2490            // nothing to do, just update lookahead
2491            Ok(PayloadEvent::End(ref e)) if e.name() == name => {
2492                let _ = self.next_impl();
2493            }
2494            Ok(_) => {
2495                let result = self.reader.read_to_end(name);
2496
2497                // In case of error `next_impl` returns `Eof`
2498                let _ = self.next_impl();
2499                result?;
2500            }
2501            // Read next lookahead event, unpack error from the current lookahead
2502            Err(_) => {
2503                self.next_impl()?;
2504            }
2505        }
2506        Ok(())
2507    }
2508
2509    #[inline]
2510    fn decoder(&self) -> Decoder {
2511        self.reader.decoder()
2512    }
2513}
2514
2515////////////////////////////////////////////////////////////////////////////////////////////////////
2516
2517/// Deserialize an instance of type `T` from a string of XML text.
2518pub fn from_str<'de, T>(s: &'de str) -> Result<T, DeError>
2519where
2520    T: Deserialize<'de>,
2521{
2522    let mut de = Deserializer::from_str(s);
2523    T::deserialize(&mut de)
2524}
2525
2526/// Deserialize from a reader. This method will do internal copies of data
2527/// read from `reader`. If you want have a `&str` input and want to borrow
2528/// as much as possible, use [`from_str`].
2529pub fn from_reader<R, T>(reader: R) -> Result<T, DeError>
2530where
2531    R: BufRead,
2532    T: DeserializeOwned,
2533{
2534    let mut de = Deserializer::from_reader(reader);
2535    T::deserialize(&mut de)
2536}
2537
2538////////////////////////////////////////////////////////////////////////////////////////////////////
2539
2540/// A structure that deserializes XML into Rust values.
2541pub struct Deserializer<'de, R, E: EntityResolver = PredefinedEntityResolver>
2542where
2543    R: XmlRead<'de>,
2544{
2545    /// An XML reader that streams events into this deserializer
2546    reader: XmlReader<'de, R, E>,
2547
2548    /// When deserializing sequences sometimes we have to skip unwanted events.
2549    /// That events should be stored and then replayed. This is a replay buffer,
2550    /// that streams events while not empty. When it exhausted, events will
2551    /// requested from [`Self::reader`].
2552    #[cfg(feature = "overlapped-lists")]
2553    read: VecDeque<DeEvent<'de>>,
2554    /// When deserializing sequences sometimes we have to skip events, because XML
2555    /// is tolerant to elements order and even if in the XSD order is strictly
2556    /// specified (using `xs:sequence`) most of XML parsers allows order violations.
2557    /// That means, that elements, forming a sequence, could be overlapped with
2558    /// other elements, do not related to that sequence.
2559    ///
2560    /// In order to support this, deserializer will scan events and skip unwanted
2561    /// events, store them here. After call [`Self::start_replay()`] all events
2562    /// moved from this to [`Self::read`].
2563    #[cfg(feature = "overlapped-lists")]
2564    write: VecDeque<DeEvent<'de>>,
2565    /// Maximum number of events that can be skipped when processing sequences
2566    /// that occur out-of-order. This field is used to prevent potential
2567    /// denial-of-service (DoS) attacks which could cause infinite memory
2568    /// consumption when parsing a very large amount of XML into a sequence field.
2569    #[cfg(feature = "overlapped-lists")]
2570    limit: Option<NonZeroUsize>,
2571
2572    #[cfg(not(feature = "overlapped-lists"))]
2573    peek: Option<DeEvent<'de>>,
2574
2575    /// Buffer to store attribute name as a field name exposed to serde consumers
2576    key_buf: String,
2577}
2578
2579impl<'de, R, E> Deserializer<'de, R, E>
2580where
2581    R: XmlRead<'de>,
2582    E: EntityResolver,
2583{
2584    /// Create an XML deserializer from one of the possible quick_xml input sources.
2585    ///
2586    /// Typically it is more convenient to use one of these methods instead:
2587    ///
2588    ///  - [`Deserializer::from_str`]
2589    ///  - [`Deserializer::from_reader`]
2590    fn new(reader: R, entity_resolver: E) -> Self {
2591        Self {
2592            reader: XmlReader::new(reader, entity_resolver),
2593
2594            #[cfg(feature = "overlapped-lists")]
2595            read: VecDeque::new(),
2596            #[cfg(feature = "overlapped-lists")]
2597            write: VecDeque::new(),
2598            #[cfg(feature = "overlapped-lists")]
2599            limit: None,
2600
2601            #[cfg(not(feature = "overlapped-lists"))]
2602            peek: None,
2603
2604            key_buf: String::new(),
2605        }
2606    }
2607
2608    /// Returns `true` if all events was consumed.
2609    pub fn is_empty(&self) -> bool {
2610        #[cfg(feature = "overlapped-lists")]
2611        let event = self.read.front();
2612
2613        #[cfg(not(feature = "overlapped-lists"))]
2614        let event = self.peek.as_ref();
2615
2616        match event {
2617            None | Some(DeEvent::Eof) => self.reader.is_empty(),
2618            _ => false,
2619        }
2620    }
2621
2622    /// Returns the underlying XML reader.
2623    ///
2624    /// ```
2625    /// # use pretty_assertions::assert_eq;
2626    /// use serde::Deserialize;
2627    /// use quick_xml::de::Deserializer;
2628    /// use quick_xml::NsReader;
2629    ///
2630    /// #[derive(Deserialize)]
2631    /// struct SomeStruct {
2632    ///     field1: String,
2633    ///     field2: String,
2634    /// }
2635    ///
2636    /// // Try to deserialize from broken XML
2637    /// let mut de = Deserializer::from_str(
2638    ///     "<SomeStruct><field1><field2></SomeStruct>"
2639    /// //   0                           ^= 28        ^= 41
2640    /// );
2641    ///
2642    /// let err = SomeStruct::deserialize(&mut de);
2643    /// assert!(err.is_err());
2644    ///
2645    /// let reader: &NsReader<_> = de.get_ref().get_ref();
2646    ///
2647    /// assert_eq!(reader.error_position(), 28);
2648    /// assert_eq!(reader.buffer_position(), 41);
2649    /// ```
2650    pub const fn get_ref(&self) -> &R {
2651        &self.reader.reader
2652    }
2653
2654    /// Set the maximum number of events that could be skipped during deserialization
2655    /// of sequences.
2656    ///
2657    /// If `<element>` contains more than specified nested elements, `$text` or
2658    /// CDATA nodes, then [`DeError::TooManyEvents`] will be returned during
2659    /// deserialization of sequence field (any type that uses [`deserialize_seq`]
2660    /// for the deserialization, for example, `Vec<T>`).
2661    ///
2662    /// This method can be used to prevent a [DoS] attack and infinite memory
2663    /// consumption when parsing a very large XML to a sequence field.
2664    ///
2665    /// It is strongly recommended to set limit to some value when you parse data
2666    /// from untrusted sources. You should choose a value that your typical XMLs
2667    /// can have _between_ different elements that corresponds to the same sequence.
2668    ///
2669    /// # Examples
2670    ///
2671    /// Let's imagine, that we deserialize such structure:
2672    /// ```
2673    /// struct List {
2674    ///   item: Vec<()>,
2675    /// }
2676    /// ```
2677    ///
2678    /// The XML that we try to parse look like this:
2679    /// ```xml
2680    /// <any-name>
2681    ///   <item/>
2682    ///   <!-- Bufferization starts at this point -->
2683    ///   <another-item>
2684    ///     <some-element>with text</some-element>
2685    ///     <yet-another-element/>
2686    ///   </another-item>
2687    ///   <!-- Buffer will be emptied at this point; 7 events were buffered -->
2688    ///   <item/>
2689    ///   <!-- There is nothing to buffer, because elements follows each other -->
2690    ///   <item/>
2691    /// </any-name>
2692    /// ```
2693    ///
2694    /// There, when we deserialize the `item` field, we need to buffer 7 events,
2695    /// before we can deserialize the second `<item/>`:
2696    ///
2697    /// - `<another-item>`
2698    /// - `<some-element>`
2699    /// - `$text(with text)`
2700    /// - `</some-element>`
2701    /// - `<yet-another-element/>` (virtual start event)
2702    /// - `<yet-another-element/>` (virtual end event)
2703    /// - `</another-item>`
2704    ///
2705    /// Note, that `<yet-another-element/>` internally represented as 2 events:
2706    /// one for the start tag and one for the end tag. In the future this can be
2707    /// eliminated, but for now we use [auto-expanding feature] of a reader,
2708    /// because this simplifies deserializer code.
2709    ///
2710    /// [`deserialize_seq`]: serde::Deserializer::deserialize_seq
2711    /// [DoS]: https://en.wikipedia.org/wiki/Denial-of-service_attack
2712    /// [auto-expanding feature]: crate::reader::Config::expand_empty_elements
2713    #[cfg(feature = "overlapped-lists")]
2714    pub fn event_buffer_size(&mut self, limit: Option<NonZeroUsize>) -> &mut Self {
2715        self.limit = limit;
2716        self
2717    }
2718
2719    #[cfg(feature = "overlapped-lists")]
2720    fn peek(&mut self) -> Result<&DeEvent<'de>, DeError> {
2721        if self.read.is_empty() {
2722            self.read.push_front(self.reader.next()?);
2723        }
2724        if let Some(event) = self.read.front() {
2725            return Ok(event);
2726        }
2727        // SAFETY: `self.read` was filled in the code above.
2728        // NOTE: Can be replaced with `unsafe { std::hint::unreachable_unchecked() }`
2729        // if unsafe code will be allowed
2730        unreachable!()
2731    }
2732    #[cfg(not(feature = "overlapped-lists"))]
2733    fn peek(&mut self) -> Result<&DeEvent<'de>, DeError> {
2734        match &mut self.peek {
2735            Some(event) => Ok(event),
2736            empty_peek @ None => Ok(empty_peek.insert(self.reader.next()?)),
2737        }
2738    }
2739
2740    #[inline]
2741    fn last_peeked(&self) -> &DeEvent<'de> {
2742        #[cfg(feature = "overlapped-lists")]
2743        {
2744            self.read
2745                .front()
2746                .expect("`Deserializer::peek()` should be called")
2747        }
2748        #[cfg(not(feature = "overlapped-lists"))]
2749        {
2750            self.peek
2751                .as_ref()
2752                .expect("`Deserializer::peek()` should be called")
2753        }
2754    }
2755
2756    fn next(&mut self) -> Result<DeEvent<'de>, DeError> {
2757        // Replay skipped or peeked events
2758        #[cfg(feature = "overlapped-lists")]
2759        if let Some(event) = self.read.pop_front() {
2760            return Ok(event);
2761        }
2762        #[cfg(not(feature = "overlapped-lists"))]
2763        if let Some(e) = self.peek.take() {
2764            return Ok(e);
2765        }
2766        self.reader.next()
2767    }
2768
2769    fn skip_whitespaces(&mut self) -> Result<(), DeError> {
2770        loop {
2771            match self.peek()? {
2772                DeEvent::Text(e) if e.is_blank() => {
2773                    self.next()?;
2774                }
2775                _ => break,
2776            }
2777        }
2778        Ok(())
2779    }
2780
2781    /// Returns the mark after which all events, skipped by [`Self::skip()`] call,
2782    /// should be replayed after calling [`Self::start_replay()`].
2783    #[cfg(feature = "overlapped-lists")]
2784    #[inline]
2785    #[must_use = "returned checkpoint should be used in `start_replay`"]
2786    fn skip_checkpoint(&self) -> usize {
2787        self.write.len()
2788    }
2789
2790    /// Extracts XML tree of events from and stores them in the skipped events
2791    /// buffer from which they can be retrieved later. You MUST call
2792    /// [`Self::start_replay()`] after calling this to give access to the skipped
2793    /// events and release internal buffers.
2794    #[cfg(feature = "overlapped-lists")]
2795    fn skip(&mut self) -> Result<(), DeError> {
2796        let event = self.next()?;
2797        self.skip_event(event)?;
2798        // Skip all subtree, if we skip a start event
2799        if let Some(DeEvent::Start(e)) = self.write.back() {
2800            let end = e.name().as_ref().to_owned();
2801            let mut depth = 0;
2802            loop {
2803                let event = self.next()?;
2804                match event {
2805                    DeEvent::Start(ref e) if e.name().as_ref() == end => {
2806                        self.skip_event(event)?;
2807                        depth += 1;
2808                    }
2809                    DeEvent::End(ref e) if e.name().as_ref() == end => {
2810                        self.skip_event(event)?;
2811                        if depth == 0 {
2812                            break;
2813                        }
2814                        depth -= 1;
2815                    }
2816                    DeEvent::Eof => {
2817                        self.skip_event(event)?;
2818                        break;
2819                    }
2820                    _ => self.skip_event(event)?,
2821                }
2822            }
2823        }
2824        Ok(())
2825    }
2826
2827    #[cfg(feature = "overlapped-lists")]
2828    #[inline]
2829    fn skip_event(&mut self, event: DeEvent<'de>) -> Result<(), DeError> {
2830        if let Some(max) = self.limit {
2831            if self.write.len() >= max.get() {
2832                return Err(DeError::TooManyEvents(max));
2833            }
2834        }
2835        self.write.push_back(event);
2836        Ok(())
2837    }
2838
2839    /// Moves buffered events, skipped after given `checkpoint` from [`Self::write`]
2840    /// skip buffer to [`Self::read`] buffer.
2841    ///
2842    /// After calling this method, [`Self::peek()`] and [`Self::next()`] starts
2843    /// return events that was skipped previously by calling [`Self::skip()`],
2844    /// and only when all that events will be consumed, the deserializer starts
2845    /// to drain events from underlying reader.
2846    ///
2847    /// This method MUST be called if any number of [`Self::skip()`] was called
2848    /// after [`Self::new()`] or `start_replay()` or you'll lost events.
2849    #[cfg(feature = "overlapped-lists")]
2850    fn start_replay(&mut self, checkpoint: usize) {
2851        if checkpoint == 0 {
2852            self.write.append(&mut self.read);
2853            std::mem::swap(&mut self.read, &mut self.write);
2854        } else {
2855            let mut read = self.write.split_off(checkpoint);
2856            read.append(&mut self.read);
2857            self.read = read;
2858        }
2859    }
2860
2861    #[inline]
2862    fn read_string(&mut self) -> Result<Cow<'de, str>, DeError> {
2863        self.read_string_impl(true)
2864    }
2865
2866    /// Consumes consequent [`Text`] and [`CData`] (both a referred below as a _text_)
2867    /// events, merge them into one string. If there are no such events, returns
2868    /// an empty string.
2869    ///
2870    /// If `allow_start` is `false`, then only text events are consumed, for other
2871    /// events an error is returned (see table below).
2872    ///
2873    /// If `allow_start` is `true`, then two or three events are expected:
2874    /// - [`DeEvent::Start`];
2875    /// - _(optional)_ [`DeEvent::Text`] which content is returned;
2876    /// - [`DeEvent::End`]. If text event was missed, an empty string is returned.
2877    ///
2878    /// Corresponding events are consumed.
2879    ///
2880    /// # Handling events
2881    ///
2882    /// The table below shows how events is handled by this method:
2883    ///
2884    /// |Event             |XML                        |Handling
2885    /// |------------------|---------------------------|----------------------------------------
2886    /// |[`DeEvent::Start`]|`<tag>...</tag>`           |if `allow_start == true`, result determined by the second table, otherwise emits [`UnexpectedStart("tag")`](DeError::UnexpectedStart)
2887    /// |[`DeEvent::End`]  |`</any-tag>`               |This is impossible situation, the method will panic if it happens
2888    /// |[`DeEvent::Text`] |`text content` or `<![CDATA[cdata content]]>` (probably mixed)|Returns event content unchanged
2889    /// |[`DeEvent::Eof`]  |                           |Emits [`UnexpectedEof`](DeError::UnexpectedEof)
2890    ///
2891    /// Second event, consumed if [`DeEvent::Start`] was received and `allow_start == true`:
2892    ///
2893    /// |Event             |XML                        |Handling
2894    /// |------------------|---------------------------|----------------------------------------------------------------------------------
2895    /// |[`DeEvent::Start`]|`<any-tag>...</any-tag>`   |Emits [`UnexpectedStart("any-tag")`](DeError::UnexpectedStart)
2896    /// |[`DeEvent::End`]  |`</tag>`                   |Returns an empty slice. The reader guarantee that tag will match the open one
2897    /// |[`DeEvent::Text`] |`text content` or `<![CDATA[cdata content]]>` (probably mixed)|Returns event content unchanged, expects the `</tag>` after that
2898    /// |[`DeEvent::Eof`]  |                           |Emits [`InvalidXml(IllFormed(MissingEndTag))`](DeError::InvalidXml)
2899    ///
2900    /// [`Text`]: Event::Text
2901    /// [`CData`]: Event::CData
2902    fn read_string_impl(&mut self, allow_start: bool) -> Result<Cow<'de, str>, DeError> {
2903        match self.next()? {
2904            // Reached by doc tests only: this file, lines 979 and 996
2905            DeEvent::Text(e) => Ok(e.text),
2906            // allow one nested level
2907            // Reached by trivial::{...}::{field, field_nested, field_tag_after, field_tag_before, nested, tag_after, tag_before, wrapped}
2908            DeEvent::Start(e) if allow_start => self.read_text(e.name()),
2909            // TODO: not reached by any tests
2910            DeEvent::Start(e) => Err(DeError::UnexpectedStart(e.name().as_ref().to_owned())),
2911            // SAFETY: The reader is guaranteed that we don't have unmatched tags
2912            // If we here, then our deserializer has a bug
2913            DeEvent::End(e) => unreachable!("{:?}", e),
2914            // Reached by trivial::{empty_doc, only_comment}
2915            DeEvent::Eof => Err(DeError::UnexpectedEof),
2916        }
2917    }
2918    /// Consumes one [`DeEvent::Text`] event and ensures that it is followed by the
2919    /// [`DeEvent::End`] event.
2920    ///
2921    /// # Parameters
2922    /// - `name`: name of a tag opened before reading text. The corresponding end tag
2923    ///   should present in input just after the text
2924    fn read_text(&mut self, name: QName) -> Result<Cow<'de, str>, DeError> {
2925        match self.next()? {
2926            DeEvent::Text(e) => match self.next()? {
2927                // The matching tag name is guaranteed by the reader
2928                // Reached by trivial::{...}::{field, wrapped}
2929                DeEvent::End(_) => Ok(e.text),
2930                // SAFETY: Cannot be two consequent Text events, they would be merged into one
2931                DeEvent::Text(_) => unreachable!(),
2932                // Reached by trivial::{...}::{field_tag_after, tag_after}
2933                DeEvent::Start(e) => Err(DeError::UnexpectedStart(e.name().as_ref().to_owned())),
2934                // Reached by struct_::non_closed::elements_child
2935                DeEvent::Eof => Err(Error::missed_end(name, self.reader.decoder()).into()),
2936            },
2937            // We can get End event in case of `<tag></tag>` or `<tag/>` input
2938            // Return empty text in that case
2939            // The matching tag name is guaranteed by the reader
2940            // Reached by {...}::xs_list::empty
2941            DeEvent::End(_) => Ok("".into()),
2942            // Reached by trivial::{...}::{field_nested, field_tag_before, nested, tag_before}
2943            DeEvent::Start(s) => Err(DeError::UnexpectedStart(s.name().as_ref().to_owned())),
2944            // Reached by struct_::non_closed::elements_child
2945            DeEvent::Eof => Err(Error::missed_end(name, self.reader.decoder()).into()),
2946        }
2947    }
2948
2949    /// Drops all events until event with [name](BytesEnd::name()) `name` won't be
2950    /// dropped. This method should be called after [`Self::next()`]
2951    #[cfg(feature = "overlapped-lists")]
2952    fn read_to_end(&mut self, name: QName) -> Result<(), DeError> {
2953        let mut depth = 0;
2954        loop {
2955            match self.read.pop_front() {
2956                Some(DeEvent::Start(e)) if e.name() == name => {
2957                    depth += 1;
2958                }
2959                Some(DeEvent::End(e)) if e.name() == name => {
2960                    if depth == 0 {
2961                        break;
2962                    }
2963                    depth -= 1;
2964                }
2965
2966                // Drop all other skipped events
2967                Some(_) => continue,
2968
2969                // If we do not have skipped events, use effective reading that will
2970                // not allocate memory for events
2971                None => {
2972                    // We should close all opened tags, because we could buffer
2973                    // Start events, but not the corresponding End events. So we
2974                    // keep reading events until we exit all nested tags.
2975                    // `read_to_end()` will return an error if an Eof was encountered
2976                    // preliminary (in case of malformed XML).
2977                    //
2978                    // <tag><tag></tag></tag>
2979                    // ^^^^^^^^^^             - buffered in `self.read`, when `self.read_to_end()` is called, depth = 2
2980                    //           ^^^^^^       - read by the first call of `self.reader.read_to_end()`
2981                    //                 ^^^^^^ - read by the second call of `self.reader.read_to_end()`
2982                    loop {
2983                        self.reader.read_to_end(name)?;
2984                        if depth == 0 {
2985                            break;
2986                        }
2987                        depth -= 1;
2988                    }
2989                    break;
2990                }
2991            }
2992        }
2993        Ok(())
2994    }
2995    #[cfg(not(feature = "overlapped-lists"))]
2996    fn read_to_end(&mut self, name: QName) -> Result<(), DeError> {
2997        // First one might be in self.peek
2998        match self.next()? {
2999            DeEvent::Start(e) => self.reader.read_to_end(e.name())?,
3000            DeEvent::End(e) if e.name() == name => return Ok(()),
3001            _ => (),
3002        }
3003        self.reader.read_to_end(name)
3004    }
3005
3006    fn skip_next_tree(&mut self) -> Result<(), DeError> {
3007        let DeEvent::Start(start) = self.next()? else {
3008            unreachable!("Only call this if the next event is a start event")
3009        };
3010        let name = start.name();
3011        self.read_to_end(name)
3012    }
3013
3014    /// Method for testing Deserializer implementation. Checks that all events was consumed during
3015    /// deserialization. Panics if the next event will not be [`DeEvent::Eof`].
3016    #[doc(hidden)]
3017    #[track_caller]
3018    pub fn check_eof_reached(&mut self) {
3019        // Deserializer may not consume trailing spaces, that is normal
3020        self.skip_whitespaces().expect("cannot skip whitespaces");
3021        let event = self.peek().expect("cannot peek event");
3022        assert_eq!(
3023            *event,
3024            DeEvent::Eof,
3025            "the whole XML document should be consumed, expected `Eof`",
3026        );
3027    }
3028}
3029
3030impl<'de> Deserializer<'de, SliceReader<'de>> {
3031    /// Create a new deserializer that will borrow data from the specified string.
3032    ///
3033    /// Deserializer created with this method will not resolve custom entities.
3034    #[allow(clippy::should_implement_trait)]
3035    pub fn from_str(source: &'de str) -> Self {
3036        Self::from_str_with_resolver(source, PredefinedEntityResolver)
3037    }
3038
3039    /// Create a new deserializer that will borrow data from the specified preconfigured
3040    /// reader.
3041    ///
3042    /// Deserializer created with this method will not resolve custom entities.
3043    ///
3044    /// Note, that config option [`Config::expand_empty_elements`] will be set to `true`.
3045    ///
3046    /// # Example
3047    ///
3048    /// ```
3049    /// # use pretty_assertions::assert_eq;
3050    /// # use quick_xml::de::Deserializer;
3051    /// # use quick_xml::NsReader;
3052    /// # use serde::Deserialize;
3053    /// #
3054    /// #[derive(Deserialize, PartialEq, Debug)]
3055    /// struct Object<'a> {
3056    ///     tag: &'a str,
3057    /// }
3058    ///
3059    /// let mut reader = NsReader::from_str("<xml><tag>    test    </tag></xml>");
3060    ///
3061    /// let mut de = Deserializer::borrowing(reader.clone());
3062    /// let obj = Object::deserialize(&mut de).unwrap();
3063    /// assert_eq!(obj, Object { tag: "    test    " });
3064    ///
3065    /// reader.config_mut().trim_text(true);
3066    ///
3067    /// let mut de = Deserializer::borrowing(reader);
3068    /// let obj = Object::deserialize(&mut de).unwrap();
3069    /// assert_eq!(obj, Object { tag: "test" });
3070    /// ```
3071    ///
3072    /// [`Config::expand_empty_elements`]: crate::reader::Config::expand_empty_elements
3073    #[inline]
3074    pub fn borrowing(reader: NsReader<&'de [u8]>) -> Self {
3075        Self::borrowing_with_resolver(reader, PredefinedEntityResolver)
3076    }
3077}
3078
3079impl<'de, E> Deserializer<'de, SliceReader<'de>, E>
3080where
3081    E: EntityResolver,
3082{
3083    /// Create a new deserializer that will borrow data from the specified string
3084    /// and use the specified entity resolver.
3085    pub fn from_str_with_resolver(source: &'de str, entity_resolver: E) -> Self {
3086        Self::borrowing_with_resolver(NsReader::from_str(source), entity_resolver)
3087    }
3088
3089    /// Create a new deserializer that will borrow data from the specified preconfigured
3090    /// reader and use the specified entity resolver.
3091    ///
3092    /// Note, that config option [`Config::expand_empty_elements`] will be set to `true`.
3093    ///
3094    /// [`Config::expand_empty_elements`]: crate::reader::Config::expand_empty_elements
3095    pub fn borrowing_with_resolver(mut reader: NsReader<&'de [u8]>, entity_resolver: E) -> Self {
3096        let config = reader.config_mut();
3097        config.expand_empty_elements = true;
3098
3099        Self::new(
3100            SliceReader {
3101                reader,
3102                version: XmlVersion::Implicit1_0,
3103            },
3104            entity_resolver,
3105        )
3106    }
3107}
3108
3109impl<'de, R> Deserializer<'de, IoReader<R>>
3110where
3111    R: BufRead,
3112{
3113    /// Create a new deserializer that will copy data from the specified reader
3114    /// into internal buffer.
3115    ///
3116    /// If you already have a string use [`Self::from_str`] instead, because it
3117    /// will borrow instead of copy. If you have `&[u8]` which is known to represent
3118    /// UTF-8, you can decode it first before using [`from_str`].
3119    ///
3120    /// Deserializer created with this method will not resolve custom entities.
3121    pub fn from_reader(reader: R) -> Self {
3122        Self::with_resolver(reader, PredefinedEntityResolver)
3123    }
3124
3125    /// Create a new deserializer that will copy data from the specified preconfigured
3126    /// reader into internal buffer.
3127    ///
3128    /// Deserializer created with this method will not resolve custom entities.
3129    ///
3130    /// Note, that config option [`Config::expand_empty_elements`] will be set to `true`.
3131    ///
3132    /// # Example
3133    ///
3134    /// ```
3135    /// # use pretty_assertions::assert_eq;
3136    /// # use quick_xml::de::Deserializer;
3137    /// # use quick_xml::NsReader;
3138    /// # use serde::Deserialize;
3139    /// #
3140    /// #[derive(Deserialize, PartialEq, Debug)]
3141    /// struct Object {
3142    ///     tag: String,
3143    /// }
3144    ///
3145    /// let mut reader = NsReader::from_str("<xml><tag>    test    </tag></xml>");
3146    ///
3147    /// let mut de = Deserializer::buffering(reader.clone());
3148    /// let obj = Object::deserialize(&mut de).unwrap();
3149    /// assert_eq!(obj, Object { tag: "    test    ".to_string() });
3150    ///
3151    /// reader.config_mut().trim_text(true);
3152    ///
3153    /// let mut de = Deserializer::buffering(reader);
3154    /// let obj = Object::deserialize(&mut de).unwrap();
3155    /// assert_eq!(obj, Object { tag: "test".to_string() });
3156    /// ```
3157    ///
3158    /// [`Config::expand_empty_elements`]: crate::reader::Config::expand_empty_elements
3159    #[inline]
3160    pub fn buffering(reader: NsReader<R>) -> Self {
3161        Self::buffering_with_resolver(reader, PredefinedEntityResolver)
3162    }
3163}
3164
3165impl<'de, R, E> Deserializer<'de, IoReader<R>, E>
3166where
3167    R: BufRead,
3168    E: EntityResolver,
3169{
3170    /// Create a new deserializer that will copy data from the specified reader
3171    /// into internal buffer and use the specified entity resolver.
3172    ///
3173    /// If you already have a string use [`Self::from_str`] instead, because it
3174    /// will borrow instead of copy. If you have `&[u8]` which is known to represent
3175    /// UTF-8, you can decode it first before using [`from_str`].
3176    pub fn with_resolver(reader: R, entity_resolver: E) -> Self {
3177        let mut reader = NsReader::from_reader(reader);
3178        let config = reader.config_mut();
3179        config.expand_empty_elements = true;
3180
3181        Self::new(
3182            IoReader {
3183                reader,
3184                buf: Vec::new(),
3185                version: XmlVersion::Implicit1_0,
3186            },
3187            entity_resolver,
3188        )
3189    }
3190
3191    /// Create new deserializer that will copy data from the specified preconfigured reader
3192    /// into internal buffer and use the specified entity resolver.
3193    ///
3194    /// Note, that config option [`Config::expand_empty_elements`] will be set to `true`.
3195    ///
3196    /// [`Config::expand_empty_elements`]: crate::reader::Config::expand_empty_elements
3197    pub fn buffering_with_resolver(mut reader: NsReader<R>, entity_resolver: E) -> Self {
3198        let config = reader.config_mut();
3199        config.expand_empty_elements = true;
3200
3201        Self::new(
3202            IoReader {
3203                reader,
3204                buf: Vec::new(),
3205                version: XmlVersion::Implicit1_0,
3206            },
3207            entity_resolver,
3208        )
3209    }
3210}
3211
3212impl<'de, R, E> de::Deserializer<'de> for &mut Deserializer<'de, R, E>
3213where
3214    R: XmlRead<'de>,
3215    E: EntityResolver,
3216{
3217    type Error = DeError;
3218
3219    deserialize_primitives!();
3220
3221    fn deserialize_struct<V>(
3222        self,
3223        _name: &'static str,
3224        fields: &'static [&'static str],
3225        visitor: V,
3226    ) -> Result<V::Value, DeError>
3227    where
3228        V: Visitor<'de>,
3229    {
3230        // When document is pretty-printed there could be whitespaces before the root element
3231        self.skip_whitespaces()?;
3232        match self.next()? {
3233            DeEvent::Start(e) => visitor.visit_map(ElementMapAccess::new(self, e, fields)),
3234            // SAFETY: The reader is guaranteed that we don't have unmatched tags
3235            // If we here, then our deserializer has a bug
3236            DeEvent::End(e) => unreachable!("{:?}", e),
3237            // Deserializer methods are only hints, if deserializer could not satisfy
3238            // request, it should return the data that it has. It is responsibility
3239            // of a Visitor to return an error if it does not understand the data
3240            DeEvent::Text(e) => match e.text {
3241                Cow::Borrowed(s) => visitor.visit_borrowed_str(s),
3242                Cow::Owned(s) => visitor.visit_string(s),
3243            },
3244            DeEvent::Eof => Err(DeError::UnexpectedEof),
3245        }
3246    }
3247
3248    /// Unit represented in XML as a `xs:element` or text/CDATA content.
3249    /// Any content inside `xs:element` is ignored and skipped.
3250    ///
3251    /// Produces unit struct from any of following inputs:
3252    /// - any `<tag ...>...</tag>`
3253    /// - any `<tag .../>`
3254    /// - any consequent text / CDATA content (can consist of several parts
3255    ///   delimited by comments and processing instructions)
3256    ///
3257    /// # Events handling
3258    ///
3259    /// |Event             |XML                        |Handling
3260    /// |------------------|---------------------------|-------------------------------------------
3261    /// |[`DeEvent::Start`]|`<tag>...</tag>`           |Calls `visitor.visit_unit()`, consumes all events up to and including corresponding `End` event
3262    /// |[`DeEvent::End`]  |`</tag>`                   |This is impossible situation, the method will panic if it happens
3263    /// |[`DeEvent::Text`] |`text content` or `<![CDATA[cdata content]]>` (probably mixed)|Calls `visitor.visit_unit()`. The content is ignored
3264    /// |[`DeEvent::Eof`]  |                           |Emits [`UnexpectedEof`](DeError::UnexpectedEof)
3265    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, DeError>
3266    where
3267        V: Visitor<'de>,
3268    {
3269        match self.next()? {
3270            DeEvent::Start(s) => {
3271                self.read_to_end(s.name())?;
3272                visitor.visit_unit()
3273            }
3274            DeEvent::Text(_) => visitor.visit_unit(),
3275            // SAFETY: The reader is guaranteed that we don't have unmatched tags
3276            // If we here, then our deserializer has a bug
3277            DeEvent::End(e) => unreachable!("{:?}", e),
3278            DeEvent::Eof => Err(DeError::UnexpectedEof),
3279        }
3280    }
3281
3282    /// Forwards deserialization of the inner type. Always calls [`Visitor::visit_newtype_struct`]
3283    /// with the same deserializer.
3284    fn deserialize_newtype_struct<V>(
3285        self,
3286        _name: &'static str,
3287        visitor: V,
3288    ) -> Result<V::Value, DeError>
3289    where
3290        V: Visitor<'de>,
3291    {
3292        visitor.visit_newtype_struct(self)
3293    }
3294
3295    fn deserialize_enum<V>(
3296        self,
3297        _name: &'static str,
3298        _variants: &'static [&'static str],
3299        visitor: V,
3300    ) -> Result<V::Value, DeError>
3301    where
3302        V: Visitor<'de>,
3303    {
3304        // When document is pretty-printed there could be whitespaces before the root element
3305        // which represents the enum variant
3306        // Checked by `top_level::list_of_enum` test in serde-de-seq
3307        self.skip_whitespaces()?;
3308        visitor.visit_enum(var::EnumAccess::new(self))
3309    }
3310
3311    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, DeError>
3312    where
3313        V: Visitor<'de>,
3314    {
3315        visitor.visit_seq(self)
3316    }
3317
3318    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, DeError>
3319    where
3320        V: Visitor<'de>,
3321    {
3322        // We cannot use result of `peek()` directly because of borrow checker
3323        let _ = self.peek()?;
3324        match self.last_peeked() {
3325            DeEvent::Text(t) if t.is_empty() => visitor.visit_none(),
3326            DeEvent::Eof => visitor.visit_none(),
3327            // if the `xsi:nil` attribute is set to true we got a none value
3328            DeEvent::Start(start) if self.reader.reader.has_nil_attr(start) => {
3329                self.skip_next_tree()?;
3330                visitor.visit_none()
3331            }
3332            _ => visitor.visit_some(self),
3333        }
3334    }
3335
3336    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, DeError>
3337    where
3338        V: Visitor<'de>,
3339    {
3340        match self.peek()? {
3341            DeEvent::Text(_) => self.deserialize_str(visitor),
3342            _ => self.deserialize_map(visitor),
3343        }
3344    }
3345}
3346
3347/// An accessor to sequence elements forming a value for top-level sequence of XML
3348/// elements.
3349///
3350/// Technically, multiple top-level elements violates XML rule of only one top-level
3351/// element, but we consider this as several concatenated XML documents.
3352impl<'de, R, E> SeqAccess<'de> for &mut Deserializer<'de, R, E>
3353where
3354    R: XmlRead<'de>,
3355    E: EntityResolver,
3356{
3357    type Error = DeError;
3358
3359    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
3360    where
3361        T: DeserializeSeed<'de>,
3362    {
3363        // When document is pretty-printed there could be whitespaces before, between
3364        // and after root elements. We cannot defer decision if we need to skip spaces
3365        // or not: if we have a sequence of type that does not accept blank text, it
3366        // will need to return something and it can return only error. For example,
3367        // it can be enum without `$text` variant
3368        // Checked by `top_level::list_of_enum` test in serde-de-seq
3369        self.skip_whitespaces()?;
3370        match self.peek()? {
3371            DeEvent::Eof => Ok(None),
3372
3373            // Start(tag), End(tag), Text
3374            _ => seed.deserialize(&mut **self).map(Some),
3375        }
3376    }
3377}
3378
3379impl<'de, R, E> IntoDeserializer<'de, DeError> for &mut Deserializer<'de, R, E>
3380where
3381    R: XmlRead<'de>,
3382    E: EntityResolver,
3383{
3384    type Deserializer = Self;
3385
3386    #[inline]
3387    fn into_deserializer(self) -> Self {
3388        self
3389    }
3390}
3391
3392////////////////////////////////////////////////////////////////////////////////////////////////////
3393
3394/// Converts raw reader's event into a payload event.
3395/// Returns `None`, if event should be skipped.
3396#[inline(always)]
3397fn skip_uninterested<'a>(event: Event<'a>) -> Option<PayloadEvent<'a>> {
3398    let event = match event {
3399        Event::DocType(e) => PayloadEvent::DocType(e),
3400        Event::Start(e) => PayloadEvent::Start(e),
3401        Event::End(e) => PayloadEvent::End(e),
3402        Event::Eof => PayloadEvent::Eof,
3403
3404        // Do not trim next text event after Text, CDATA or reference event
3405        Event::CData(e) => PayloadEvent::CData(e),
3406        Event::Text(e) => PayloadEvent::Text(e),
3407        Event::GeneralRef(e) => PayloadEvent::GeneralRef(e),
3408
3409        _ => return None,
3410    };
3411    Some(event)
3412}
3413
3414////////////////////////////////////////////////////////////////////////////////////////////////////
3415
3416/// Trait used by the deserializer for iterating over input. This is manually
3417/// "specialized" for iterating over `&[u8]`.
3418///
3419/// You do not need to implement this trait, it is needed to abstract from
3420/// [borrowing](SliceReader) and [copying](IoReader) data sources and reuse code in
3421/// deserializer
3422pub trait XmlRead<'i> {
3423    /// Return an input-borrowing event.
3424    fn next(&mut self) -> Result<PayloadEvent<'i>, DeError>;
3425
3426    /// Skips until end element is found. Unlike `next()` it will not allocate
3427    /// when it cannot satisfy the lifetime.
3428    fn read_to_end(&mut self, name: QName) -> Result<(), DeError>;
3429
3430    /// Return an XML version of the source.
3431    fn xml_version(&self) -> XmlVersion;
3432
3433    /// A copy of the reader's decoder used to decode strings.
3434    fn decoder(&self) -> Decoder;
3435
3436    /// Checks if the `start` tag has a [`xsi:nil`] attribute. This method ignores
3437    /// any errors in attributes.
3438    ///
3439    /// [`xsi:nil`]: https://www.w3.org/TR/xmlschema-1/#xsi_nil
3440    fn has_nil_attr(&self, start: &BytesStart) -> bool;
3441}
3442
3443/// XML input source that reads from a std::io input stream.
3444///
3445/// You cannot create it, it is created automatically when you call
3446/// [`Deserializer::from_reader`]
3447pub struct IoReader<R: BufRead> {
3448    reader: NsReader<R>,
3449    buf: Vec<u8>,
3450    version: XmlVersion,
3451}
3452
3453impl<R: BufRead> IoReader<R> {
3454    /// Returns the underlying XML reader.
3455    ///
3456    /// ```
3457    /// # use pretty_assertions::assert_eq;
3458    /// use serde::Deserialize;
3459    /// use std::io::Cursor;
3460    /// use quick_xml::de::Deserializer;
3461    /// use quick_xml::NsReader;
3462    ///
3463    /// #[derive(Deserialize)]
3464    /// struct SomeStruct {
3465    ///     field1: String,
3466    ///     field2: String,
3467    /// }
3468    ///
3469    /// // Try to deserialize from broken XML
3470    /// let mut de = Deserializer::from_reader(Cursor::new(
3471    ///     "<SomeStruct><field1><field2></SomeStruct>"
3472    /// //   0                           ^= 28        ^= 41
3473    /// ));
3474    ///
3475    /// let err = SomeStruct::deserialize(&mut de);
3476    /// assert!(err.is_err());
3477    ///
3478    /// let reader: &NsReader<Cursor<&str>> = de.get_ref().get_ref();
3479    ///
3480    /// assert_eq!(reader.error_position(), 28);
3481    /// assert_eq!(reader.buffer_position(), 41);
3482    /// ```
3483    pub const fn get_ref(&self) -> &NsReader<R> {
3484        &self.reader
3485    }
3486}
3487
3488impl<'i, R: BufRead> XmlRead<'i> for IoReader<R> {
3489    fn next(&mut self) -> Result<PayloadEvent<'static>, DeError> {
3490        loop {
3491            self.buf.clear();
3492
3493            let event = self.reader.read_event_into(&mut self.buf)?;
3494            if let Event::Decl(e) = &event {
3495                self.version = e.xml_version()?;
3496            }
3497            if let Some(event) = skip_uninterested(event) {
3498                return Ok(event.into_owned());
3499            }
3500        }
3501    }
3502
3503    fn read_to_end(&mut self, name: QName) -> Result<(), DeError> {
3504        match self.reader.read_to_end_into(name, &mut self.buf) {
3505            Err(e) => Err(e.into()),
3506            Ok(_) => Ok(()),
3507        }
3508    }
3509
3510    #[inline]
3511    fn xml_version(&self) -> XmlVersion {
3512        self.version
3513    }
3514
3515    #[inline]
3516    fn decoder(&self) -> Decoder {
3517        self.reader.decoder()
3518    }
3519
3520    fn has_nil_attr(&self, start: &BytesStart) -> bool {
3521        start.attributes().has_nil(self.reader.resolver())
3522    }
3523}
3524
3525/// XML input source that reads from a slice of bytes and can borrow from it.
3526///
3527/// You cannot create it, it is created automatically when you call
3528/// [`Deserializer::from_str`].
3529pub struct SliceReader<'de> {
3530    reader: NsReader<&'de [u8]>,
3531    version: XmlVersion,
3532}
3533
3534impl<'de> SliceReader<'de> {
3535    /// Returns the underlying XML reader.
3536    ///
3537    /// ```
3538    /// # use pretty_assertions::assert_eq;
3539    /// use serde::Deserialize;
3540    /// use quick_xml::de::Deserializer;
3541    /// use quick_xml::NsReader;
3542    ///
3543    /// #[derive(Deserialize)]
3544    /// struct SomeStruct {
3545    ///     field1: String,
3546    ///     field2: String,
3547    /// }
3548    ///
3549    /// // Try to deserialize from broken XML
3550    /// let mut de = Deserializer::from_str(
3551    ///     "<SomeStruct><field1><field2></SomeStruct>"
3552    /// //   0                           ^= 28        ^= 41
3553    /// );
3554    ///
3555    /// let err = SomeStruct::deserialize(&mut de);
3556    /// assert!(err.is_err());
3557    ///
3558    /// let reader: &NsReader<&[u8]> = de.get_ref().get_ref();
3559    ///
3560    /// assert_eq!(reader.error_position(), 28);
3561    /// assert_eq!(reader.buffer_position(), 41);
3562    /// ```
3563    pub const fn get_ref(&self) -> &NsReader<&'de [u8]> {
3564        &self.reader
3565    }
3566}
3567
3568impl<'de> XmlRead<'de> for SliceReader<'de> {
3569    fn next(&mut self) -> Result<PayloadEvent<'de>, DeError> {
3570        loop {
3571            let event = self.reader.read_event()?;
3572            if let Event::Decl(e) = &event {
3573                self.version = e.xml_version()?;
3574            }
3575            if let Some(event) = skip_uninterested(event) {
3576                return Ok(event);
3577            }
3578        }
3579    }
3580
3581    fn read_to_end(&mut self, name: QName) -> Result<(), DeError> {
3582        match self.reader.read_to_end(name) {
3583            Err(e) => Err(e.into()),
3584            Ok(_) => Ok(()),
3585        }
3586    }
3587
3588    #[inline]
3589    fn xml_version(&self) -> XmlVersion {
3590        self.version
3591    }
3592
3593    #[inline]
3594    fn decoder(&self) -> Decoder {
3595        self.reader.decoder()
3596    }
3597
3598    fn has_nil_attr(&self, start: &BytesStart) -> bool {
3599        start.attributes().has_nil(self.reader.resolver())
3600    }
3601}
3602
3603#[cfg(test)]
3604mod tests {
3605    use super::*;
3606    use crate::errors::IllFormedError;
3607    use pretty_assertions::assert_eq;
3608
3609    fn make_de<'de>(source: &'de str) -> Deserializer<'de, SliceReader<'de>> {
3610        dbg!(source);
3611        Deserializer::from_str(source)
3612    }
3613
3614    #[cfg(feature = "overlapped-lists")]
3615    mod skip {
3616        use super::*;
3617        use crate::de::DeEvent::*;
3618        use crate::events::BytesEnd;
3619        use pretty_assertions::assert_eq;
3620
3621        /// Checks that `peek()` and `read()` behaves correctly after `skip()`
3622        #[test]
3623        fn read_and_peek() {
3624            let mut de = make_de(
3625                "\
3626                <root>\
3627                    <inner>\
3628                        text\
3629                        <inner/>\
3630                    </inner>\
3631                    <next/>\
3632                    <target/>\
3633                </root>\
3634                ",
3635            );
3636
3637            // Initial conditions - both are empty
3638            assert_eq!(de.read, vec![]);
3639            assert_eq!(de.write, vec![]);
3640
3641            assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));
3642            assert_eq!(de.peek().unwrap(), &Start(BytesStart::new("inner")));
3643
3644            // Mark that start_replay() should begin replay from this point
3645            let checkpoint = de.skip_checkpoint();
3646            assert_eq!(checkpoint, 0);
3647
3648            // Should skip first <inner> tree
3649            de.skip().unwrap();
3650            assert_eq!(de.read, vec![]);
3651            assert_eq!(
3652                de.write,
3653                vec![
3654                    Start(BytesStart::new("inner")),
3655                    Text("text".into()),
3656                    Start(BytesStart::new("inner")),
3657                    End(BytesEnd::new("inner")),
3658                    End(BytesEnd::new("inner")),
3659                ]
3660            );
3661
3662            // Consume <next/>. Now unconsumed XML looks like:
3663            //
3664            //   <inner>
3665            //     text
3666            //     <inner/>
3667            //   </inner>
3668            //   <target/>
3669            // </root>
3670            assert_eq!(de.next().unwrap(), Start(BytesStart::new("next")));
3671            assert_eq!(de.next().unwrap(), End(BytesEnd::new("next")));
3672
3673            // We finish writing. Next call to `next()` should start replay that messages:
3674            //
3675            //   <inner>
3676            //     text
3677            //     <inner/>
3678            //   </inner>
3679            //
3680            // and after that stream that messages:
3681            //
3682            //   <target/>
3683            // </root>
3684            de.start_replay(checkpoint);
3685            assert_eq!(
3686                de.read,
3687                vec![
3688                    Start(BytesStart::new("inner")),
3689                    Text("text".into()),
3690                    Start(BytesStart::new("inner")),
3691                    End(BytesEnd::new("inner")),
3692                    End(BytesEnd::new("inner")),
3693                ]
3694            );
3695            assert_eq!(de.write, vec![]);
3696            assert_eq!(de.next().unwrap(), Start(BytesStart::new("inner")));
3697
3698            // Mark that start_replay() should begin replay from this point
3699            let checkpoint = de.skip_checkpoint();
3700            assert_eq!(checkpoint, 0);
3701
3702            // Skip `$text` node and consume <inner/> after it
3703            de.skip().unwrap();
3704            assert_eq!(
3705                de.read,
3706                vec![
3707                    Start(BytesStart::new("inner")),
3708                    End(BytesEnd::new("inner")),
3709                    End(BytesEnd::new("inner")),
3710                ]
3711            );
3712            assert_eq!(
3713                de.write,
3714                vec![
3715                    // This comment here to keep the same formatting of both arrays
3716                    // otherwise rustfmt suggest one-line it
3717                    Text("text".into()),
3718                ]
3719            );
3720
3721            assert_eq!(de.next().unwrap(), Start(BytesStart::new("inner")));
3722            assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
3723
3724            // We finish writing. Next call to `next()` should start replay messages:
3725            //
3726            //     text
3727            //   </inner>
3728            //
3729            // and after that stream that messages:
3730            //
3731            //   <target/>
3732            // </root>
3733            de.start_replay(checkpoint);
3734            assert_eq!(
3735                de.read,
3736                vec![
3737                    // This comment here to keep the same formatting as others
3738                    // otherwise rustfmt suggest one-line it
3739                    Text("text".into()),
3740                    End(BytesEnd::new("inner")),
3741                ]
3742            );
3743            assert_eq!(de.write, vec![]);
3744            assert_eq!(de.next().unwrap(), Text("text".into()));
3745            assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
3746            assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
3747            assert_eq!(de.next().unwrap(), End(BytesEnd::new("target")));
3748            assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
3749            assert_eq!(de.next().unwrap(), Eof);
3750        }
3751
3752        /// Checks that `read_to_end()` behaves correctly after `skip()`
3753        #[test]
3754        fn read_to_end() {
3755            let mut de = make_de(
3756                "\
3757                <root>\
3758                    <skip>\
3759                        text\
3760                        <skip/>\
3761                    </skip>\
3762                    <target>\
3763                        <target/>\
3764                    </target>\
3765                </root>\
3766                ",
3767            );
3768
3769            // Initial conditions - both are empty
3770            assert_eq!(de.read, vec![]);
3771            assert_eq!(de.write, vec![]);
3772
3773            assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));
3774
3775            // Mark that start_replay() should begin replay from this point
3776            let checkpoint = de.skip_checkpoint();
3777            assert_eq!(checkpoint, 0);
3778
3779            // Skip the <skip> tree
3780            de.skip().unwrap();
3781            assert_eq!(de.read, vec![]);
3782            assert_eq!(
3783                de.write,
3784                vec![
3785                    Start(BytesStart::new("skip")),
3786                    Text("text".into()),
3787                    Start(BytesStart::new("skip")),
3788                    End(BytesEnd::new("skip")),
3789                    End(BytesEnd::new("skip")),
3790                ]
3791            );
3792
3793            // Drop all events that represents <target> tree. Now unconsumed XML looks like:
3794            //
3795            //   <skip>
3796            //     text
3797            //     <skip/>
3798            //   </skip>
3799            // </root>
3800            assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
3801            de.read_to_end(QName(b"target")).unwrap();
3802            assert_eq!(de.read, vec![]);
3803            assert_eq!(
3804                de.write,
3805                vec![
3806                    Start(BytesStart::new("skip")),
3807                    Text("text".into()),
3808                    Start(BytesStart::new("skip")),
3809                    End(BytesEnd::new("skip")),
3810                    End(BytesEnd::new("skip")),
3811                ]
3812            );
3813
3814            // We finish writing. Next call to `next()` should start replay that messages:
3815            //
3816            //   <skip>
3817            //     text
3818            //     <skip/>
3819            //   </skip>
3820            //
3821            // and after that stream that messages:
3822            //
3823            // </root>
3824            de.start_replay(checkpoint);
3825            assert_eq!(
3826                de.read,
3827                vec![
3828                    Start(BytesStart::new("skip")),
3829                    Text("text".into()),
3830                    Start(BytesStart::new("skip")),
3831                    End(BytesEnd::new("skip")),
3832                    End(BytesEnd::new("skip")),
3833                ]
3834            );
3835            assert_eq!(de.write, vec![]);
3836
3837            assert_eq!(de.next().unwrap(), Start(BytesStart::new("skip")));
3838            de.read_to_end(QName(b"skip")).unwrap();
3839
3840            assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
3841            assert_eq!(de.next().unwrap(), Eof);
3842        }
3843
3844        /// Checks that replay replayes only part of events
3845        /// Test for https://github.com/tafia/quick-xml/issues/435
3846        #[test]
3847        fn partial_replay() {
3848            let mut de = make_de(
3849                "\
3850                <root>\
3851                    <skipped-1/>\
3852                    <skipped-2/>\
3853                    <inner>\
3854                        <skipped-3/>\
3855                        <skipped-4/>\
3856                        <target-2/>\
3857                    </inner>\
3858                    <target-1/>\
3859                </root>\
3860                ",
3861            );
3862
3863            // Initial conditions - both are empty
3864            assert_eq!(de.read, vec![]);
3865            assert_eq!(de.write, vec![]);
3866
3867            assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));
3868
3869            // start_replay() should start replay from this point
3870            let checkpoint1 = de.skip_checkpoint();
3871            assert_eq!(checkpoint1, 0);
3872
3873            // Should skip first and second <skipped-N/> elements
3874            de.skip().unwrap(); // skipped-1
3875            de.skip().unwrap(); // skipped-2
3876            assert_eq!(de.read, vec![]);
3877            assert_eq!(
3878                de.write,
3879                vec![
3880                    Start(BytesStart::new("skipped-1")),
3881                    End(BytesEnd::new("skipped-1")),
3882                    Start(BytesStart::new("skipped-2")),
3883                    End(BytesEnd::new("skipped-2")),
3884                ]
3885            );
3886
3887            ////////////////////////////////////////////////////////////////////////////////////////
3888
3889            assert_eq!(de.next().unwrap(), Start(BytesStart::new("inner")));
3890            assert_eq!(de.peek().unwrap(), &Start(BytesStart::new("skipped-3")));
3891            assert_eq!(
3892                de.read,
3893                vec![
3894                    // This comment here to keep the same formatting of both arrays
3895                    // otherwise rustfmt suggest one-line it
3896                    Start(BytesStart::new("skipped-3")),
3897                ]
3898            );
3899            assert_eq!(
3900                de.write,
3901                vec![
3902                    Start(BytesStart::new("skipped-1")),
3903                    End(BytesEnd::new("skipped-1")),
3904                    Start(BytesStart::new("skipped-2")),
3905                    End(BytesEnd::new("skipped-2")),
3906                ]
3907            );
3908
3909            // start_replay() should start replay from this point
3910            let checkpoint2 = de.skip_checkpoint();
3911            assert_eq!(checkpoint2, 4);
3912
3913            // Should skip third and forth <skipped-N/> elements
3914            de.skip().unwrap(); // skipped-3
3915            de.skip().unwrap(); // skipped-4
3916            assert_eq!(de.read, vec![]);
3917            assert_eq!(
3918                de.write,
3919                vec![
3920                    // checkpoint 1
3921                    Start(BytesStart::new("skipped-1")),
3922                    End(BytesEnd::new("skipped-1")),
3923                    Start(BytesStart::new("skipped-2")),
3924                    End(BytesEnd::new("skipped-2")),
3925                    // checkpoint 2
3926                    Start(BytesStart::new("skipped-3")),
3927                    End(BytesEnd::new("skipped-3")),
3928                    Start(BytesStart::new("skipped-4")),
3929                    End(BytesEnd::new("skipped-4")),
3930                ]
3931            );
3932            assert_eq!(de.next().unwrap(), Start(BytesStart::new("target-2")));
3933            assert_eq!(de.next().unwrap(), End(BytesEnd::new("target-2")));
3934            assert_eq!(de.peek().unwrap(), &End(BytesEnd::new("inner")));
3935            assert_eq!(
3936                de.read,
3937                vec![
3938                    // This comment here to keep the same formatting of both arrays
3939                    // otherwise rustfmt suggest one-line it
3940                    End(BytesEnd::new("inner")),
3941                ]
3942            );
3943            assert_eq!(
3944                de.write,
3945                vec![
3946                    // checkpoint 1
3947                    Start(BytesStart::new("skipped-1")),
3948                    End(BytesEnd::new("skipped-1")),
3949                    Start(BytesStart::new("skipped-2")),
3950                    End(BytesEnd::new("skipped-2")),
3951                    // checkpoint 2
3952                    Start(BytesStart::new("skipped-3")),
3953                    End(BytesEnd::new("skipped-3")),
3954                    Start(BytesStart::new("skipped-4")),
3955                    End(BytesEnd::new("skipped-4")),
3956                ]
3957            );
3958
3959            // Start replay events from checkpoint 2
3960            de.start_replay(checkpoint2);
3961            assert_eq!(
3962                de.read,
3963                vec![
3964                    Start(BytesStart::new("skipped-3")),
3965                    End(BytesEnd::new("skipped-3")),
3966                    Start(BytesStart::new("skipped-4")),
3967                    End(BytesEnd::new("skipped-4")),
3968                    End(BytesEnd::new("inner")),
3969                ]
3970            );
3971            assert_eq!(
3972                de.write,
3973                vec![
3974                    Start(BytesStart::new("skipped-1")),
3975                    End(BytesEnd::new("skipped-1")),
3976                    Start(BytesStart::new("skipped-2")),
3977                    End(BytesEnd::new("skipped-2")),
3978                ]
3979            );
3980
3981            // Replayed events
3982            assert_eq!(de.next().unwrap(), Start(BytesStart::new("skipped-3")));
3983            assert_eq!(de.next().unwrap(), End(BytesEnd::new("skipped-3")));
3984            assert_eq!(de.next().unwrap(), Start(BytesStart::new("skipped-4")));
3985            assert_eq!(de.next().unwrap(), End(BytesEnd::new("skipped-4")));
3986
3987            assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
3988            assert_eq!(de.read, vec![]);
3989            assert_eq!(
3990                de.write,
3991                vec![
3992                    Start(BytesStart::new("skipped-1")),
3993                    End(BytesEnd::new("skipped-1")),
3994                    Start(BytesStart::new("skipped-2")),
3995                    End(BytesEnd::new("skipped-2")),
3996                ]
3997            );
3998
3999            ////////////////////////////////////////////////////////////////////////////////////////
4000
4001            // New events
4002            assert_eq!(de.next().unwrap(), Start(BytesStart::new("target-1")));
4003            assert_eq!(de.next().unwrap(), End(BytesEnd::new("target-1")));
4004
4005            assert_eq!(de.read, vec![]);
4006            assert_eq!(
4007                de.write,
4008                vec![
4009                    Start(BytesStart::new("skipped-1")),
4010                    End(BytesEnd::new("skipped-1")),
4011                    Start(BytesStart::new("skipped-2")),
4012                    End(BytesEnd::new("skipped-2")),
4013                ]
4014            );
4015
4016            // Start replay events from checkpoint 1
4017            de.start_replay(checkpoint1);
4018            assert_eq!(
4019                de.read,
4020                vec![
4021                    Start(BytesStart::new("skipped-1")),
4022                    End(BytesEnd::new("skipped-1")),
4023                    Start(BytesStart::new("skipped-2")),
4024                    End(BytesEnd::new("skipped-2")),
4025                ]
4026            );
4027            assert_eq!(de.write, vec![]);
4028
4029            // Replayed events
4030            assert_eq!(de.next().unwrap(), Start(BytesStart::new("skipped-1")));
4031            assert_eq!(de.next().unwrap(), End(BytesEnd::new("skipped-1")));
4032            assert_eq!(de.next().unwrap(), Start(BytesStart::new("skipped-2")));
4033            assert_eq!(de.next().unwrap(), End(BytesEnd::new("skipped-2")));
4034
4035            assert_eq!(de.read, vec![]);
4036            assert_eq!(de.write, vec![]);
4037
4038            // New events
4039            assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
4040            assert_eq!(de.next().unwrap(), Eof);
4041        }
4042
4043        /// Checks that limiting buffer size works correctly
4044        #[test]
4045        fn limit() {
4046            use serde::Deserialize;
4047
4048            #[derive(Debug, Deserialize)]
4049            #[allow(unused)]
4050            struct List {
4051                item: Vec<()>,
4052            }
4053
4054            let mut de = make_de(
4055                "\
4056                <any-name>\
4057                    <item/>\
4058                    <another-item>\
4059                        <some-element>with text</some-element>\
4060                        <yet-another-element/>\
4061                    </another-item>\
4062                    <item/>\
4063                    <item/>\
4064                </any-name>\
4065                ",
4066            );
4067            de.event_buffer_size(NonZeroUsize::new(3));
4068
4069            match List::deserialize(&mut de) {
4070                Err(DeError::TooManyEvents(count)) => assert_eq!(count.get(), 3),
4071                e => panic!("Expected `Err(TooManyEvents(3))`, but got `{:?}`", e),
4072            }
4073        }
4074
4075        /// Without handling Eof in `skip` this test failed with memory allocation
4076        #[test]
4077        fn invalid_xml() {
4078            use crate::de::DeEvent::*;
4079
4080            let mut de = make_de("<root>");
4081
4082            // Cache all events
4083            let checkpoint = de.skip_checkpoint();
4084            de.skip().unwrap();
4085            de.start_replay(checkpoint);
4086            assert_eq!(de.read, vec![Start(BytesStart::new("root")), Eof]);
4087        }
4088    }
4089
4090    mod read_to_end {
4091        use super::*;
4092        use crate::de::DeEvent::*;
4093        use pretty_assertions::assert_eq;
4094
4095        #[test]
4096        fn complex() {
4097            let mut de = make_de(
4098                r#"
4099                <root>
4100                    <tag a="1"><tag>text</tag>content</tag>
4101                    <tag a="2"><![CDATA[cdata content]]></tag>
4102                    <self-closed/>
4103                </root>
4104                "#,
4105            );
4106
4107            assert_eq!(de.next().unwrap(), Text("\n                ".into()));
4108            assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));
4109
4110            assert_eq!(de.next().unwrap(), Text("\n                    ".into()));
4111            assert_eq!(
4112                de.next().unwrap(),
4113                Start(BytesStart::from_content(r#"tag a="1""#, 3))
4114            );
4115            assert_eq!(de.read_to_end(QName(b"tag")).unwrap(), ());
4116
4117            assert_eq!(de.next().unwrap(), Text("\n                    ".into()));
4118            assert_eq!(
4119                de.next().unwrap(),
4120                Start(BytesStart::from_content(r#"tag a="2""#, 3))
4121            );
4122            assert_eq!(de.next().unwrap(), Text("cdata content".into()));
4123            assert_eq!(de.next().unwrap(), End(BytesEnd::new("tag")));
4124
4125            assert_eq!(de.next().unwrap(), Text("\n                    ".into()));
4126            assert_eq!(de.next().unwrap(), Start(BytesStart::new("self-closed")));
4127            assert_eq!(de.read_to_end(QName(b"self-closed")).unwrap(), ());
4128
4129            assert_eq!(de.next().unwrap(), Text("\n                ".into()));
4130            assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
4131            assert_eq!(de.next().unwrap(), Text("\n                ".into()));
4132            assert_eq!(de.next().unwrap(), Eof);
4133        }
4134
4135        #[test]
4136        fn invalid_xml1() {
4137            let mut de = make_de("<tag><tag></tag>");
4138
4139            assert_eq!(de.next().unwrap(), Start(BytesStart::new("tag")));
4140            assert_eq!(de.peek().unwrap(), &Start(BytesStart::new("tag")));
4141
4142            match de.read_to_end(QName(b"tag")) {
4143                Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4144                    assert_eq!(cause, IllFormedError::MissingEndTag("tag".into()))
4145                }
4146                x => panic!(
4147                    "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4148                    x
4149                ),
4150            }
4151            assert_eq!(de.next().unwrap(), Eof);
4152        }
4153
4154        #[test]
4155        fn invalid_xml2() {
4156            let mut de = make_de("<tag><![CDATA[]]><tag></tag>");
4157
4158            assert_eq!(de.next().unwrap(), Start(BytesStart::new("tag")));
4159            assert_eq!(de.peek().unwrap(), &Text("".into()));
4160
4161            match de.read_to_end(QName(b"tag")) {
4162                Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4163                    assert_eq!(cause, IllFormedError::MissingEndTag("tag".into()))
4164                }
4165                x => panic!(
4166                    "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4167                    x
4168                ),
4169            }
4170            assert_eq!(de.next().unwrap(), Eof);
4171        }
4172    }
4173
4174    #[test]
4175    fn borrowing_reader_parity() {
4176        let s = r#"
4177            <item name="hello" source="world.rs">Some text</item>
4178            <item2/>
4179            <item3 value="world" />
4180        "#;
4181
4182        let mut reader1 = IoReader {
4183            reader: NsReader::from_reader(s.as_bytes()),
4184            buf: Vec::new(),
4185            version: XmlVersion::Implicit1_0,
4186        };
4187        let mut reader2 = SliceReader {
4188            reader: NsReader::from_str(s),
4189            version: XmlVersion::Implicit1_0,
4190        };
4191
4192        loop {
4193            let event1 = reader1.next().unwrap();
4194            let event2 = reader2.next().unwrap();
4195
4196            if let (PayloadEvent::Eof, PayloadEvent::Eof) = (&event1, &event2) {
4197                break;
4198            }
4199
4200            assert_eq!(event1, event2);
4201        }
4202    }
4203
4204    #[test]
4205    fn borrowing_reader_events() {
4206        let s = r#"
4207            <item name="hello" source="world.rs">Some text</item>
4208            <item2></item2>
4209            <item3/>
4210            <item4 value="world" />
4211        "#;
4212
4213        let mut reader = SliceReader {
4214            reader: NsReader::from_str(s),
4215            version: XmlVersion::Implicit1_0,
4216        };
4217
4218        let config = reader.reader.config_mut();
4219        config.expand_empty_elements = true;
4220
4221        let mut events = Vec::new();
4222
4223        loop {
4224            let event = reader.next().unwrap();
4225            if let PayloadEvent::Eof = event {
4226                break;
4227            }
4228            events.push(event);
4229        }
4230
4231        use crate::de::PayloadEvent::*;
4232
4233        assert_eq!(
4234            events,
4235            vec![
4236                Text(BytesText::from_escaped("\n            ")),
4237                Start(BytesStart::from_content(
4238                    r#"item name="hello" source="world.rs""#,
4239                    4
4240                )),
4241                Text(BytesText::from_escaped("Some text")),
4242                End(BytesEnd::new("item")),
4243                Text(BytesText::from_escaped("\n            ")),
4244                Start(BytesStart::from_content("item2", 5)),
4245                End(BytesEnd::new("item2")),
4246                Text(BytesText::from_escaped("\n            ")),
4247                Start(BytesStart::from_content("item3", 5)),
4248                End(BytesEnd::new("item3")),
4249                Text(BytesText::from_escaped("\n            ")),
4250                Start(BytesStart::from_content(r#"item4 value="world" "#, 5)),
4251                End(BytesEnd::new("item4")),
4252                Text(BytesText::from_escaped("\n        ")),
4253            ]
4254        )
4255    }
4256
4257    /// Ensures, that [`Deserializer::read_string()`] never can get an `End` event,
4258    /// because parser reports error early
4259    #[test]
4260    fn read_string() {
4261        match from_str::<String>(r#"</root>"#) {
4262            Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4263                assert_eq!(cause, IllFormedError::UnmatchedEndTag("root".into()));
4264            }
4265            x => panic!(
4266                "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4267                x
4268            ),
4269        }
4270
4271        let s: String = from_str(r#"<root></root>"#).unwrap();
4272        assert_eq!(s, "");
4273
4274        match from_str::<String>(r#"<root></other>"#) {
4275            Err(DeError::InvalidXml(Error::IllFormed(cause))) => assert_eq!(
4276                cause,
4277                IllFormedError::MismatchedEndTag {
4278                    expected: "root".into(),
4279                    found: "other".into(),
4280                }
4281            ),
4282            x => panic!("Expected `Err(InvalidXml(IllFormed(_))`, but got `{:?}`", x),
4283        }
4284    }
4285
4286    /// Tests for https://github.com/tafia/quick-xml/issues/474.
4287    ///
4288    /// That tests ensures that comments and processed instructions is ignored
4289    /// and can split one logical string in pieces.
4290    mod merge_text {
4291        use super::*;
4292        use pretty_assertions::assert_eq;
4293
4294        #[test]
4295        fn text() {
4296            let mut de = make_de("text");
4297            assert_eq!(de.next().unwrap(), DeEvent::Text("text".into()));
4298        }
4299
4300        #[test]
4301        fn cdata() {
4302            let mut de = make_de("<![CDATA[cdata]]>");
4303            assert_eq!(de.next().unwrap(), DeEvent::Text("cdata".into()));
4304        }
4305
4306        #[test]
4307        fn text_and_cdata() {
4308            let mut de = make_de("text and <![CDATA[cdata]]>");
4309            assert_eq!(de.next().unwrap(), DeEvent::Text("text and cdata".into()));
4310        }
4311
4312        #[test]
4313        fn text_and_empty_cdata() {
4314            let mut de = make_de("text and <![CDATA[]]>");
4315            assert_eq!(de.next().unwrap(), DeEvent::Text("text and ".into()));
4316        }
4317
4318        #[test]
4319        fn cdata_and_text() {
4320            let mut de = make_de("<![CDATA[cdata]]> and text");
4321            assert_eq!(de.next().unwrap(), DeEvent::Text("cdata and text".into()));
4322        }
4323
4324        #[test]
4325        fn empty_cdata_and_text() {
4326            let mut de = make_de("<![CDATA[]]> and text");
4327            assert_eq!(de.next().unwrap(), DeEvent::Text(" and text".into()));
4328        }
4329
4330        #[test]
4331        fn cdata_and_cdata() {
4332            let mut de = make_de(
4333                "\
4334                    <![CDATA[cdata]]]]>\
4335                    <![CDATA[>cdata]]>\
4336                ",
4337            );
4338            assert_eq!(de.next().unwrap(), DeEvent::Text("cdata]]>cdata".into()));
4339        }
4340
4341        mod comment_between {
4342            use super::*;
4343            use pretty_assertions::assert_eq;
4344
4345            #[test]
4346            fn text() {
4347                let mut de = make_de(
4348                    "\
4349                        text \
4350                        <!--comment 1--><!--comment 2--> \
4351                        text\
4352                    ",
4353                );
4354                assert_eq!(de.next().unwrap(), DeEvent::Text("text  text".into()));
4355            }
4356
4357            #[test]
4358            fn cdata() {
4359                let mut de = make_de(
4360                    "\
4361                        <![CDATA[cdata]]]]>\
4362                        <!--comment 1--><!--comment 2-->\
4363                        <![CDATA[>cdata]]>\
4364                    ",
4365                );
4366                assert_eq!(de.next().unwrap(), DeEvent::Text("cdata]]>cdata".into()));
4367            }
4368
4369            #[test]
4370            fn text_and_cdata() {
4371                let mut de = make_de(
4372                    "\
4373                        text \
4374                        <!--comment 1--><!--comment 2-->\
4375                        <![CDATA[ cdata]]>\
4376                    ",
4377                );
4378                assert_eq!(de.next().unwrap(), DeEvent::Text("text  cdata".into()));
4379            }
4380
4381            #[test]
4382            fn text_and_empty_cdata() {
4383                let mut de = make_de(
4384                    "\
4385                        text \
4386                        <!--comment 1--><!--comment 2-->\
4387                        <![CDATA[]]>\
4388                    ",
4389                );
4390                assert_eq!(de.next().unwrap(), DeEvent::Text("text ".into()));
4391            }
4392
4393            #[test]
4394            fn cdata_and_text() {
4395                let mut de = make_de(
4396                    "\
4397                        <![CDATA[cdata ]]>\
4398                        <!--comment 1--><!--comment 2--> \
4399                        text \
4400                    ",
4401                );
4402                assert_eq!(de.next().unwrap(), DeEvent::Text("cdata  text ".into()));
4403            }
4404
4405            #[test]
4406            fn empty_cdata_and_text() {
4407                let mut de = make_de(
4408                    "\
4409                        <![CDATA[]]>\
4410                        <!--comment 1--><!--comment 2--> \
4411                        text \
4412                    ",
4413                );
4414                assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4415            }
4416
4417            #[test]
4418            fn cdata_and_cdata() {
4419                let mut de = make_de(
4420                    "\
4421                        <![CDATA[cdata]]]>\
4422                        <!--comment 1--><!--comment 2-->\
4423                        <![CDATA[]>cdata]]>\
4424                    ",
4425                );
4426                assert_eq!(de.next().unwrap(), DeEvent::Text("cdata]]>cdata".into()));
4427            }
4428        }
4429
4430        mod pi_between {
4431            use super::*;
4432            use pretty_assertions::assert_eq;
4433
4434            #[test]
4435            fn text() {
4436                let mut de = make_de(
4437                    "\
4438                        text \
4439                        <?pi 1?><?pi 2?> \
4440                        text\
4441                    ",
4442                );
4443                assert_eq!(de.next().unwrap(), DeEvent::Text("text  text".into()));
4444            }
4445
4446            #[test]
4447            fn cdata() {
4448                let mut de = make_de(
4449                    "\
4450                        <![CDATA[cdata]]]]>\
4451                        <?pi 1?><?pi 2?>\
4452                        <![CDATA[>cdata]]>\
4453                    ",
4454                );
4455                assert_eq!(de.next().unwrap(), DeEvent::Text("cdata]]>cdata".into()));
4456            }
4457
4458            #[test]
4459            fn text_and_cdata() {
4460                let mut de = make_de(
4461                    "\
4462                        text \
4463                        <?pi 1?><?pi 2?>\
4464                        <![CDATA[ cdata]]>\
4465                    ",
4466                );
4467                assert_eq!(de.next().unwrap(), DeEvent::Text("text  cdata".into()));
4468            }
4469
4470            #[test]
4471            fn text_and_empty_cdata() {
4472                let mut de = make_de(
4473                    "\
4474                        text \
4475                        <?pi 1?><?pi 2?>\
4476                        <![CDATA[]]>\
4477                    ",
4478                );
4479                assert_eq!(de.next().unwrap(), DeEvent::Text("text ".into()));
4480            }
4481
4482            #[test]
4483            fn cdata_and_text() {
4484                let mut de = make_de(
4485                    "\
4486                        <![CDATA[cdata ]]>\
4487                        <?pi 1?><?pi 2?> \
4488                        text \
4489                    ",
4490                );
4491                assert_eq!(de.next().unwrap(), DeEvent::Text("cdata  text ".into()));
4492            }
4493
4494            #[test]
4495            fn empty_cdata_and_text() {
4496                let mut de = make_de(
4497                    "\
4498                        <![CDATA[]]>\
4499                        <?pi 1?><?pi 2?> \
4500                        text \
4501                    ",
4502                );
4503                assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4504            }
4505
4506            #[test]
4507            fn cdata_and_cdata() {
4508                let mut de = make_de(
4509                    "\
4510                        <![CDATA[cdata]]]>\
4511                        <?pi 1?><?pi 2?>\
4512                        <![CDATA[]>cdata]]>\
4513                    ",
4514                );
4515                assert_eq!(de.next().unwrap(), DeEvent::Text("cdata]]>cdata".into()));
4516            }
4517        }
4518    }
4519
4520    /// Tests for https://github.com/tafia/quick-xml/issues/474.
4521    ///
4522    /// This tests ensures that any combination of payload data is processed
4523    /// as expected.
4524    mod triples {
4525        use super::*;
4526        use pretty_assertions::assert_eq;
4527
4528        mod start {
4529            use super::*;
4530
4531            /// <tag1><tag2>...
4532            // The same name is intentional
4533            #[allow(clippy::module_inception)]
4534            mod start {
4535                use super::*;
4536                use pretty_assertions::assert_eq;
4537
4538                #[test]
4539                fn start() {
4540                    let mut de = make_de("<tag1><tag2><tag3>");
4541                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag1")));
4542                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4543                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag3")));
4544                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4545                }
4546
4547                /// Not matching end tag will result to error
4548                #[test]
4549                fn end() {
4550                    let mut de = make_de("<tag1><tag2></tag2>");
4551                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag1")));
4552                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4553                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag2")));
4554                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4555                }
4556
4557                #[test]
4558                fn text() {
4559                    let mut de = make_de("<tag1><tag2> text ");
4560                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag1")));
4561                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4562                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4563                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4564                }
4565
4566                #[test]
4567                fn cdata() {
4568                    let mut de = make_de("<tag1><tag2><![CDATA[ cdata ]]>");
4569                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag1")));
4570                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4571                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4572                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4573                }
4574
4575                #[test]
4576                fn eof() {
4577                    let mut de = make_de("<tag1><tag2>");
4578                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag1")));
4579                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4580                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4581                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4582                }
4583            }
4584
4585            /// <tag></tag>...
4586            mod end {
4587                use super::*;
4588                use pretty_assertions::assert_eq;
4589
4590                #[test]
4591                fn start() {
4592                    let mut de = make_de("<tag></tag><tag2>");
4593                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4594                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4595                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4596                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4597                }
4598
4599                #[test]
4600                fn end() {
4601                    let mut de = make_de("<tag></tag></tag2>");
4602                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4603                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4604                    match de.next() {
4605                        Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4606                            assert_eq!(cause, IllFormedError::UnmatchedEndTag("tag2".into()));
4607                        }
4608                        x => panic!(
4609                            "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4610                            x
4611                        ),
4612                    }
4613                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4614                }
4615
4616                #[test]
4617                fn text() {
4618                    let mut de = make_de("<tag></tag> text ");
4619                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4620                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4621                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4622                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4623                }
4624
4625                #[test]
4626                fn cdata() {
4627                    let mut de = make_de("<tag></tag><![CDATA[ cdata ]]>");
4628                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4629                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4630                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4631                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4632                }
4633
4634                #[test]
4635                fn eof() {
4636                    let mut de = make_de("<tag></tag>");
4637                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4638                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4639                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4640                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4641                }
4642            }
4643
4644            /// <tag> text ...
4645            mod text {
4646                use super::*;
4647                use pretty_assertions::assert_eq;
4648
4649                #[test]
4650                fn start() {
4651                    let mut de = make_de("<tag> text <tag2>");
4652                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4653                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4654                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4655                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4656                }
4657
4658                #[test]
4659                fn end() {
4660                    let mut de = make_de("<tag> text </tag>");
4661                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4662                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4663                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4664                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4665                }
4666
4667                // start::text::text has no difference from start::text
4668
4669                #[test]
4670                fn cdata() {
4671                    let mut de = make_de("<tag> text <![CDATA[ cdata ]]>");
4672                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4673                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text  cdata ".into()));
4674                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4675                }
4676
4677                #[test]
4678                fn eof() {
4679                    let mut de = make_de("<tag> text ");
4680                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4681                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4682                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4683                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4684                }
4685            }
4686
4687            /// <tag><![CDATA[ cdata ]]>...
4688            mod cdata {
4689                use super::*;
4690                use pretty_assertions::assert_eq;
4691
4692                #[test]
4693                fn start() {
4694                    let mut de = make_de("<tag><![CDATA[ cdata ]]><tag2>");
4695                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4696                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4697                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4698                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4699                }
4700
4701                #[test]
4702                fn end() {
4703                    let mut de = make_de("<tag><![CDATA[ cdata ]]></tag>");
4704                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4705                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4706                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4707                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4708                }
4709
4710                #[test]
4711                fn text() {
4712                    let mut de = make_de("<tag><![CDATA[ cdata ]]> text ");
4713                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4714                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata  text ".into()));
4715                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4716                }
4717
4718                #[test]
4719                fn cdata() {
4720                    let mut de = make_de("<tag><![CDATA[ cdata ]]><![CDATA[ cdata2 ]]>");
4721                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4722                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata  cdata2 ".into()));
4723                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4724                }
4725
4726                #[test]
4727                fn eof() {
4728                    let mut de = make_de("<tag><![CDATA[ cdata ]]>");
4729                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4730                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4731                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4732                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4733                }
4734            }
4735        }
4736
4737        /// Start from End event will always generate an error
4738        #[test]
4739        fn end() {
4740            let mut de = make_de("</tag>");
4741            match de.next() {
4742                Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4743                    assert_eq!(cause, IllFormedError::UnmatchedEndTag("tag".into()));
4744                }
4745                x => panic!(
4746                    "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4747                    x
4748                ),
4749            }
4750            assert_eq!(de.next().unwrap(), DeEvent::Eof);
4751        }
4752
4753        mod text {
4754            use super::*;
4755            use pretty_assertions::assert_eq;
4756
4757            mod start {
4758                use super::*;
4759                use pretty_assertions::assert_eq;
4760
4761                #[test]
4762                fn start() {
4763                    let mut de = make_de(" text <tag1><tag2>");
4764                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4765                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag1")));
4766                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4767                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4768                }
4769
4770                /// Not matching end tag will result in error
4771                #[test]
4772                fn end() {
4773                    let mut de = make_de(" text <tag></tag>");
4774                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4775                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4776                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4777                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4778                }
4779
4780                #[test]
4781                fn text() {
4782                    let mut de = make_de(" text <tag> text2 ");
4783                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4784                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4785                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text2 ".into()));
4786                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4787                }
4788
4789                #[test]
4790                fn cdata() {
4791                    let mut de = make_de(" text <tag><![CDATA[ cdata ]]>");
4792                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4793                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4794                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4795                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4796                }
4797
4798                #[test]
4799                fn eof() {
4800                    let mut de = make_de(" text <tag>");
4801                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4802                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4803                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4804                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4805                }
4806            }
4807
4808            /// End event without corresponding start event will always generate an error
4809            #[test]
4810            fn end() {
4811                let mut de = make_de(" text </tag>");
4812                assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4813                match de.next() {
4814                    Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4815                        assert_eq!(cause, IllFormedError::UnmatchedEndTag("tag".into()));
4816                    }
4817                    x => panic!(
4818                        "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4819                        x
4820                    ),
4821                }
4822                assert_eq!(de.next().unwrap(), DeEvent::Eof);
4823            }
4824
4825            // text::text::something is equivalent to text::something
4826
4827            mod cdata {
4828                use super::*;
4829                use pretty_assertions::assert_eq;
4830
4831                #[test]
4832                fn start() {
4833                    let mut de = make_de(" text <![CDATA[ cdata ]]><tag>");
4834                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text  cdata ".into()));
4835                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4836                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4837                }
4838
4839                #[test]
4840                fn end() {
4841                    let mut de = make_de(" text <![CDATA[ cdata ]]></tag>");
4842                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text  cdata ".into()));
4843                    match de.next() {
4844                        Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4845                            assert_eq!(cause, IllFormedError::UnmatchedEndTag("tag".into()));
4846                        }
4847                        x => panic!(
4848                            "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4849                            x
4850                        ),
4851                    }
4852                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4853                }
4854
4855                #[test]
4856                fn text() {
4857                    let mut de = make_de(" text <![CDATA[ cdata ]]> text2 ");
4858                    assert_eq!(
4859                        de.next().unwrap(),
4860                        DeEvent::Text(" text  cdata  text2 ".into())
4861                    );
4862                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4863                }
4864
4865                #[test]
4866                fn cdata() {
4867                    let mut de = make_de(" text <![CDATA[ cdata ]]><![CDATA[ cdata2 ]]>");
4868                    assert_eq!(
4869                        de.next().unwrap(),
4870                        DeEvent::Text(" text  cdata  cdata2 ".into())
4871                    );
4872                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4873                }
4874
4875                #[test]
4876                fn eof() {
4877                    let mut de = make_de(" text <![CDATA[ cdata ]]>");
4878                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text  cdata ".into()));
4879                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4880                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4881                }
4882            }
4883        }
4884
4885        mod cdata {
4886            use super::*;
4887            use pretty_assertions::assert_eq;
4888
4889            mod start {
4890                use super::*;
4891                use pretty_assertions::assert_eq;
4892
4893                #[test]
4894                fn start() {
4895                    let mut de = make_de("<![CDATA[ cdata ]]><tag1><tag2>");
4896                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4897                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag1")));
4898                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag2")));
4899                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4900                }
4901
4902                /// Not matching end tag will result in error
4903                #[test]
4904                fn end() {
4905                    let mut de = make_de("<![CDATA[ cdata ]]><tag></tag>");
4906                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4907                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4908                    assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
4909                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4910                }
4911
4912                #[test]
4913                fn text() {
4914                    let mut de = make_de("<![CDATA[ cdata ]]><tag> text ");
4915                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4916                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4917                    assert_eq!(de.next().unwrap(), DeEvent::Text(" text ".into()));
4918                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4919                }
4920
4921                #[test]
4922                fn cdata() {
4923                    let mut de = make_de("<![CDATA[ cdata ]]><tag><![CDATA[ cdata2 ]]>");
4924                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4925                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4926                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata2 ".into()));
4927                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4928                }
4929
4930                #[test]
4931                fn eof() {
4932                    let mut de = make_de("<![CDATA[ cdata ]]><tag>");
4933                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4934                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4935                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4936                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4937                }
4938            }
4939
4940            /// End event without corresponding start event will always generate an error
4941            #[test]
4942            fn end() {
4943                let mut de = make_de("<![CDATA[ cdata ]]></tag>");
4944                assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata ".into()));
4945                match de.next() {
4946                    Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4947                        assert_eq!(cause, IllFormedError::UnmatchedEndTag("tag".into()));
4948                    }
4949                    x => panic!(
4950                        "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4951                        x
4952                    ),
4953                }
4954                assert_eq!(de.next().unwrap(), DeEvent::Eof);
4955            }
4956
4957            mod text {
4958                use super::*;
4959                use pretty_assertions::assert_eq;
4960
4961                #[test]
4962                fn start() {
4963                    let mut de = make_de("<![CDATA[ cdata ]]> text <tag>");
4964                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata  text ".into()));
4965                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
4966                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4967                }
4968
4969                #[test]
4970                fn end() {
4971                    let mut de = make_de("<![CDATA[ cdata ]]> text </tag>");
4972                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata  text ".into()));
4973                    match de.next() {
4974                        Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
4975                            assert_eq!(cause, IllFormedError::UnmatchedEndTag("tag".into()));
4976                        }
4977                        x => panic!(
4978                            "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
4979                            x
4980                        ),
4981                    }
4982                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4983                }
4984
4985                // cdata::text::text is equivalent to cdata::text
4986
4987                #[test]
4988                fn cdata() {
4989                    let mut de = make_de("<![CDATA[ cdata ]]> text <![CDATA[ cdata2 ]]>");
4990                    assert_eq!(
4991                        de.next().unwrap(),
4992                        DeEvent::Text(" cdata  text  cdata2 ".into())
4993                    );
4994                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
4995                }
4996
4997                #[test]
4998                fn eof() {
4999                    let mut de = make_de("<![CDATA[ cdata ]]> text ");
5000                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata  text ".into()));
5001                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
5002                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
5003                }
5004            }
5005
5006            // The same name is intentional
5007            #[allow(clippy::module_inception)]
5008            mod cdata {
5009                use super::*;
5010                use pretty_assertions::assert_eq;
5011
5012                #[test]
5013                fn start() {
5014                    let mut de = make_de("<![CDATA[ cdata ]]><![CDATA[ cdata2 ]]><tag>");
5015                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata  cdata2 ".into()));
5016                    assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
5017                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
5018                }
5019
5020                #[test]
5021                fn end() {
5022                    let mut de = make_de("<![CDATA[ cdata ]]><![CDATA[ cdata2 ]]></tag>");
5023                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata  cdata2 ".into()));
5024                    match de.next() {
5025                        Err(DeError::InvalidXml(Error::IllFormed(cause))) => {
5026                            assert_eq!(cause, IllFormedError::UnmatchedEndTag("tag".into()));
5027                        }
5028                        x => panic!(
5029                            "Expected `Err(InvalidXml(IllFormed(_)))`, but got `{:?}`",
5030                            x
5031                        ),
5032                    }
5033                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
5034                }
5035
5036                #[test]
5037                fn text() {
5038                    let mut de = make_de("<![CDATA[ cdata ]]><![CDATA[ cdata2 ]]> text ");
5039                    assert_eq!(
5040                        de.next().unwrap(),
5041                        DeEvent::Text(" cdata  cdata2  text ".into())
5042                    );
5043                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
5044                }
5045
5046                #[test]
5047                fn cdata() {
5048                    let mut de =
5049                        make_de("<![CDATA[ cdata ]]><![CDATA[ cdata2 ]]><![CDATA[ cdata3 ]]>");
5050                    assert_eq!(
5051                        de.next().unwrap(),
5052                        DeEvent::Text(" cdata  cdata2  cdata3 ".into())
5053                    );
5054                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
5055                }
5056
5057                #[test]
5058                fn eof() {
5059                    let mut de = make_de("<![CDATA[ cdata ]]><![CDATA[ cdata2 ]]>");
5060                    assert_eq!(de.next().unwrap(), DeEvent::Text(" cdata  cdata2 ".into()));
5061                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
5062                    assert_eq!(de.next().unwrap(), DeEvent::Eof);
5063                }
5064            }
5065        }
5066    }
5067}