forked from tafia/quick-xml
/
mod.rs
1764 lines (1616 loc) · 62.3 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Serde `Deserializer` module
//!
//! # Difference between `$text` and `$value` special names
//!
//! quick-xml supports two special names for fields -- `$text` and `$value`.
//! Although they may seem the same, there is a distinction. Two different
//! names is required mostly for serialization, because quick-xml should know
//! how you want to serialize certain constructs, which could be represented
//! through XML in multiple different ways.
//!
//! The only difference in how complex types and sequences are serialized.
//! If you doubt which one you should select, begin with [`$value`](#value).
//!
//! ## `$text`
//! `$text` is used when you want to write your XML as a text or a CDATA content.
//! More formally, field with that name represents simple type definition with
//! `{variety} = atomic` or `{variety} = union` whose basic members are all atomic,
//! as described in the [specification].
//!
//! As a result, not all types of such fields can be serialized. Only serialization
//! of following types are supported:
//! - all primitive types (strings, numbers, booleans)
//! - unit variants of enumerations (serializes to a name of a variant)
//! - newtypes (delegates serialization to inner type)
//! - [`Option`] of above (`None` serializes to nothing)
//! - sequences (including tuples and tuple variants of enumerations) of above,
//! excluding `None` and empty string elements (because it will not be possible
//! to deserialize them back). The elements are separated by space(s)
//! - unit type `()` and unit structs (serializes to nothing)
//!
//! Complex types, such as structs and maps, are not supported in this field.
//! If you want them, you should use `$value`.
//!
//! Sequences serialized to a space-delimited string, that is why only certain
//! types are allowed in this mode:
//!
//! ```
//! # use serde::{Deserialize, Serialize};
//! # use quick_xml::de::from_str;
//! # use quick_xml::se::to_string;
//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
//! struct AnyName {
//! #[serde(rename = "$text")]
//! field: Vec<usize>,
//! }
//!
//! let obj = AnyName { field: vec![1, 2, 3] };
//! let xml = to_string(&obj).unwrap();
//! assert_eq!(xml, "<AnyName>1 2 3</AnyName>");
//!
//! let object: AnyName = from_str(&xml).unwrap();
//! assert_eq!(object, obj);
//! ```
//!
//! ## `$value`
//! > Note: a name `#content` would better explain the purpose of that field,
//! > but `$value` is used for compatibility with other XML serde crates, which
//! > uses that name. This allow you to switch XML crate more smoothly if required.
//!
//! Representation of primitive types in `$value` does not differ from their
//! representation in `$text` field. The difference is how sequences are serialized.
//! `$value` serializes each sequence item as a separate XML element. The name
//! of that element is taken from serialized type, and because only `enum`s provide
//! such name (their variant name), only they should be used for such fields.
//!
//! `$value` fields does not support `struct` types with fields, the serialization
//! of such types would end with an `Err(Unsupported)`. Unit structs and unit
//! type `()` serializing to nothing and can be deserialized from any content.
//!
//! Serialization and deserialization of `$value` field performed as usual, except
//! that name for an XML element will be given by the serialized type, instead of
//! field. The latter allow to serialize enumerated types, where variant is encoded
//! as a tag name, and, so, represent an XSD `xs:choice` schema by the Rust `enum`.
//!
//! In the example below, field will be serialized as `<field/>`, because elements
//! get their names from the field name. It cannot be deserialized, because `Enum`
//! expects elements `<A/>`, `<B/>` or `<C/>`, but `AnyName` looked only for `<field/>`:
//!
//! ```no_run
//! # use serde::{Deserialize, Serialize};
//! #[derive(Deserialize, Serialize)]
//! enum Enum { A, B, C }
//!
//! #[derive(Deserialize, Serialize)]
//! struct AnyName {
//! // <field/>
//! field: Enum,
//! }
//! ```
//!
//! If you rename field to `$value`, then `field` would be serialized as `<A/>`,
//! `<B/>` or `<C/>`, depending on the its content. It is also possible to
//! deserialize it from the same elements:
//!
//! ```no_run
//! # use serde::{Deserialize, Serialize};
//! # #[derive(Deserialize, Serialize)]
//! # enum Enum { A, B, C }
//! #
//! #[derive(Deserialize, Serialize)]
//! struct AnyName {
//! // <A/>, <B/> or <C/>
//! #[serde(rename = "$value")]
//! field: Enum,
//! }
//! ```
//!
//! ### Primitives and sequences of primitives
//!
//! Sequences serialized to a list of elements. Note, that types that does not
//! produce their own tag (i. e. primitives) are written as is, without delimiters:
//!
//! ```
//! # use serde::{Deserialize, Serialize};
//! # use quick_xml::de::from_str;
//! # use quick_xml::se::to_string;
//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
//! struct AnyName {
//! #[serde(rename = "$value")]
//! field: Vec<usize>,
//! }
//!
//! let obj = AnyName { field: vec![1, 2, 3] };
//! let xml = to_string(&obj).unwrap();
//! // Note, that types that does not produce their own tag are written as is!
//! assert_eq!(xml, "<AnyName>123</AnyName>");
//!
//! let object: AnyName = from_str("<AnyName>123</AnyName>").unwrap();
//! assert_eq!(object, AnyName { field: vec![123] });
//!
//! // `1 2 3` is mapped to a single `usize` element
//! // It is impossible to deserialize list of primitives to such field
//! from_str::<AnyName>("<AnyName>1 2 3</AnyName>").unwrap_err();
//! ```
//!
//! A particular case of that example is a string `$value` field, which probably
//! would be a most used example of that attribute:
//!
//! ```
//! # use serde::{Deserialize, Serialize};
//! # use quick_xml::de::from_str;
//! # use quick_xml::se::to_string;
//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
//! struct AnyName {
//! #[serde(rename = "$value")]
//! field: String,
//! }
//!
//! let obj = AnyName { field: "content".to_string() };
//! let xml = to_string(&obj).unwrap();
//! assert_eq!(xml, "<AnyName>content</AnyName>");
//! ```
//!
//! ### Structs and sequences of structs
//!
//! Note, that structures does not have serializable name as well (name of the
//! type are never used), so it is impossible to serialize non-unit struct or
//! sequence of non-unit structs in `$value` field. (sequences of) unit structs
//! are serialized as empty string, although, because units itself serializing
//! to nothing:
//!
//! ```
//! # use serde::{Deserialize, Serialize};
//! # use quick_xml::de::from_str;
//! # use quick_xml::se::to_string;
//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
//! struct Unit;
//!
//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
//! struct AnyName {
//! // #[serde(default)] is required to deserialization of empty lists
//! // This is a general note, not related to $value
//! #[serde(rename = "$value", default)]
//! field: Vec<Unit>,
//! }
//!
//! let obj = AnyName { field: vec![Unit, Unit, Unit] };
//! let xml = to_string(&obj).unwrap();
//! assert_eq!(xml, "<AnyName/>");
//!
//! let object: AnyName = from_str("<AnyName/>").unwrap();
//! assert_eq!(object, AnyName { field: vec![] });
//!
//! let object: AnyName = from_str("<AnyName></AnyName>").unwrap();
//! assert_eq!(object, AnyName { field: vec![] });
//!
//! let object: AnyName = from_str("<AnyName><A/><B/><C/></AnyName>").unwrap();
//! assert_eq!(object, AnyName { field: vec![Unit, Unit, Unit] });
//! ```
//!
//! ### Enums and sequences of enums
//!
//! Enumerations uses the variant name as an element name:
//!
//! ```
//! # use serde::{Deserialize, Serialize};
//! # use quick_xml::de::from_str;
//! # use quick_xml::se::to_string;
//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
//! struct AnyName {
//! #[serde(rename = "$value")]
//! field: Vec<Enum>,
//! }
//!
//! #[derive(Deserialize, Serialize, PartialEq, Debug)]
//! enum Enum { A, B, C }
//!
//! let obj = AnyName { field: vec![Enum::A, Enum::B, Enum::C] };
//! let xml = to_string(&obj).unwrap();
//! assert_eq!(
//! xml,
//! "<AnyName>\
//! <A/>\
//! <B/>\
//! <C/>\
//! </AnyName>"
//! );
//!
//! let object: AnyName = from_str(&xml).unwrap();
//! assert_eq!(object, obj);
//! ```
//!
//! ----------------------------------------------------------------------------
//!
//! You can have either `$text` or `$value` field in your structs. Unfortunately,
//! that is not enforced, so you can theoretically have both, but you should
//! avoid that.
//!
//! [specification]: https://www.w3.org/TR/xmlschema11-1/#Simple_Type_Definition
// Macros should be defined before the modules that using them
// Also, macros should be imported before using them
use serde::serde_if_integer128;
macro_rules! deserialize_type {
($deserialize:ident => $visit:ident, $($mut:tt)?) => {
fn $deserialize<V>($($mut)? self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
// No need to unescape because valid integer representations cannot be escaped
let text = self.read_string(false)?;
visitor.$visit(text.parse()?)
}
};
}
/// Implement deserialization methods for scalar types, such as numbers, strings,
/// byte arrays, booleans and identifiers.
macro_rules! deserialize_primitives {
($($mut:tt)?) => {
deserialize_type!(deserialize_i8 => visit_i8, $($mut)?);
deserialize_type!(deserialize_i16 => visit_i16, $($mut)?);
deserialize_type!(deserialize_i32 => visit_i32, $($mut)?);
deserialize_type!(deserialize_i64 => visit_i64, $($mut)?);
deserialize_type!(deserialize_u8 => visit_u8, $($mut)?);
deserialize_type!(deserialize_u16 => visit_u16, $($mut)?);
deserialize_type!(deserialize_u32 => visit_u32, $($mut)?);
deserialize_type!(deserialize_u64 => visit_u64, $($mut)?);
serde_if_integer128! {
deserialize_type!(deserialize_i128 => visit_i128, $($mut)?);
deserialize_type!(deserialize_u128 => visit_u128, $($mut)?);
}
deserialize_type!(deserialize_f32 => visit_f32, $($mut)?);
deserialize_type!(deserialize_f64 => visit_f64, $($mut)?);
fn deserialize_bool<V>($($mut)? self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
// No need to unescape because valid boolean representations cannot be escaped
let text = self.read_string(false)?;
str2bool(&text, visitor)
}
/// Representation of owned strings the same as [non-owned](#method.deserialize_str).
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_str(visitor)
}
/// Character represented as [strings](#method.deserialize_str).
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_str(visitor)
}
fn deserialize_str<V>($($mut)? self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
let text = self.read_string(true)?;
match text {
Cow::Borrowed(string) => visitor.visit_borrowed_str(string),
Cow::Owned(string) => visitor.visit_string(string),
}
}
/// Returns [`DeError::Unsupported`]
fn deserialize_bytes<V>(self, _visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
Err(DeError::Unsupported("binary data content is not supported by XML format".into()))
}
/// Forwards deserialization to the [`deserialize_bytes`](#method.deserialize_bytes).
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_bytes(visitor)
}
/// Identifiers represented as [strings](#method.deserialize_str).
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_str(visitor)
}
};
}
mod escape;
mod key;
mod map;
mod simple_type;
mod var;
pub use crate::errors::serialize::DeError;
use crate::{
encoding::Decoder,
errors::Error,
events::{BytesCData, BytesEnd, BytesStart, BytesText, Event},
name::QName,
reader::Reader,
};
use serde::de::{self, Deserialize, DeserializeOwned, DeserializeSeed, SeqAccess, Visitor};
use std::borrow::Cow;
#[cfg(feature = "overlapped-lists")]
use std::collections::VecDeque;
use std::io::BufRead;
#[cfg(feature = "overlapped-lists")]
use std::num::NonZeroUsize;
/// Data represented by a text node or a CDATA node. XML markup is not expected
pub(crate) const TEXT_KEY: &str = "$text";
/// Data represented by any XML markup inside
pub(crate) const VALUE_KEY: &str = "$value";
/// Simplified event which contains only these variants that used by deserializer
#[derive(Debug, PartialEq, Eq)]
pub enum DeEvent<'a> {
/// Start tag (with attributes) `<tag attr="value">`.
Start(BytesStart<'a>),
/// End tag `</tag>`.
End(BytesEnd<'a>),
/// Escaped character data between `Start` and `End` element.
Text(BytesText<'a>),
/// Unescaped character data between `Start` and `End` element,
/// stored in `<![CDATA[...]]>`.
CData(BytesCData<'a>),
/// End of XML document.
Eof,
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// A structure that deserializes XML into Rust values.
pub struct Deserializer<'de, R>
where
R: XmlRead<'de>,
{
/// An XML reader that streams events into this deserializer
reader: R,
/// When deserializing sequences sometimes we have to skip unwanted events.
/// That events should be stored and then replayed. This is a replay buffer,
/// that streams events while not empty. When it exhausted, events will
/// requested from [`Self::reader`].
#[cfg(feature = "overlapped-lists")]
read: VecDeque<DeEvent<'de>>,
/// When deserializing sequences sometimes we have to skip events, because XML
/// is tolerant to elements order and even if in the XSD order is strictly
/// specified (using `xs:sequence`) most of XML parsers allows order violations.
/// That means, that elements, forming a sequence, could be overlapped with
/// other elements, do not related to that sequence.
///
/// In order to support this, deserializer will scan events and skip unwanted
/// events, store them here. After call [`Self::start_replay()`] all events
/// moved from this to [`Self::read`].
#[cfg(feature = "overlapped-lists")]
write: VecDeque<DeEvent<'de>>,
/// Maximum number of events that can be skipped when processing sequences
/// that occur out-of-order. This field is used to prevent potential
/// denial-of-service (DoS) attacks which could cause infinite memory
/// consumption when parsing a very large amount of XML into a sequence field.
#[cfg(feature = "overlapped-lists")]
limit: Option<NonZeroUsize>,
#[cfg(not(feature = "overlapped-lists"))]
peek: Option<DeEvent<'de>>,
}
/// Deserialize an instance of type `T` from a string of XML text.
pub fn from_str<'de, T>(s: &'de str) -> Result<T, DeError>
where
T: Deserialize<'de>,
{
let mut de = Deserializer::from_str(s);
T::deserialize(&mut de)
}
/// Deserialize from a reader. This method will do internal copies of data
/// readed from `reader`. If you want have a `&str` input and want to borrow
/// as much as possible, use [`from_str`].
pub fn from_reader<R, T>(reader: R) -> Result<T, DeError>
where
R: BufRead,
T: DeserializeOwned,
{
let mut de = Deserializer::from_reader(reader);
T::deserialize(&mut de)
}
// TODO: According to the https://www.w3.org/TR/xmlschema-2/#boolean,
// valid boolean representations are only "true", "false", "1", and "0"
fn str2bool<'de, V>(value: &str, visitor: V) -> Result<V::Value, DeError>
where
V: de::Visitor<'de>,
{
match value {
"true" | "1" | "True" | "TRUE" | "t" | "Yes" | "YES" | "yes" | "y" => {
visitor.visit_bool(true)
}
"false" | "0" | "False" | "FALSE" | "f" | "No" | "NO" | "no" | "n" => {
visitor.visit_bool(false)
}
_ => Err(DeError::InvalidBoolean(value.into())),
}
}
fn deserialize_bool<'de, V>(value: &[u8], decoder: Decoder, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
#[cfg(feature = "encoding")]
{
let value = decoder.decode(value)?;
// No need to unescape because valid boolean representations cannot be escaped
str2bool(value.as_ref(), visitor)
}
#[cfg(not(feature = "encoding"))]
{
// No need to unescape because valid boolean representations cannot be escaped
match value {
b"true" | b"1" | b"True" | b"TRUE" | b"t" | b"Yes" | b"YES" | b"yes" | b"y" => {
visitor.visit_bool(true)
}
b"false" | b"0" | b"False" | b"FALSE" | b"f" | b"No" | b"NO" | b"no" | b"n" => {
visitor.visit_bool(false)
}
e => Err(DeError::InvalidBoolean(decoder.decode(e)?.into())),
}
}
}
impl<'de, R> Deserializer<'de, R>
where
R: XmlRead<'de>,
{
/// Create an XML deserializer from one of the possible quick_xml input sources.
///
/// Typically it is more convenient to use one of these methods instead:
///
/// - [`Deserializer::from_str`]
/// - [`Deserializer::from_reader`]
fn new(reader: R) -> Self {
Deserializer {
reader,
#[cfg(feature = "overlapped-lists")]
read: VecDeque::new(),
#[cfg(feature = "overlapped-lists")]
write: VecDeque::new(),
#[cfg(feature = "overlapped-lists")]
limit: None,
#[cfg(not(feature = "overlapped-lists"))]
peek: None,
}
}
/// Set the maximum number of events that could be skipped during deserialization
/// of sequences.
///
/// If `<element>` contains more than specified nested elements, `$text` or
/// CDATA nodes, then [`DeError::TooManyEvents`] will be returned during
/// deserialization of sequence field (any type that uses [`deserialize_seq`]
/// for the deserialization, for example, `Vec<T>`).
///
/// This method can be used to prevent a [DoS] attack and infinite memory
/// consumption when parsing a very large XML to a sequence field.
///
/// It is strongly recommended to set limit to some value when you parse data
/// from untrusted sources. You should choose a value that your typical XMLs
/// can have _between_ different elements that corresponds to the same sequence.
///
/// # Examples
///
/// Let's imagine, that we deserialize such structure:
/// ```
/// struct List {
/// item: Vec<()>,
/// }
/// ```
///
/// The XML that we try to parse look like this:
/// ```xml
/// <any-name>
/// <item/>
/// <!-- Bufferization starts at this point -->
/// <another-item>
/// <some-element>with text</some-element>
/// <yet-another-element/>
/// </another-item>
/// <!-- Buffer will be emptied at this point; 7 events were buffered -->
/// <item/>
/// <!-- There is nothing to buffer, because elements follows each other -->
/// <item/>
/// </any-name>
/// ```
///
/// There, when we deserialize the `item` field, we need to buffer 7 events,
/// before we can deserialize the second `<item/>`:
///
/// - `<another-item>`
/// - `<some-element>`
/// - `$text(with text)`
/// - `</some-element>`
/// - `<yet-another-element/>` (virtual start event)
/// - `<yet-another-element/>` (virtual end event)
/// - `</another-item>`
///
/// Note, that `<yet-another-element/>` internally represented as 2 events:
/// one for the start tag and one for the end tag. In the future this can be
/// eliminated, but for now we use [auto-expanding feature] of a reader,
/// because this simplifies deserializer code.
///
/// [`deserialize_seq`]: serde::Deserializer::deserialize_seq
/// [DoS]: https://en.wikipedia.org/wiki/Denial-of-service_attack
/// [auto-expanding feature]: Reader::expand_empty_elements
#[cfg(feature = "overlapped-lists")]
pub fn event_buffer_size(&mut self, limit: Option<NonZeroUsize>) -> &mut Self {
self.limit = limit;
self
}
#[cfg(feature = "overlapped-lists")]
fn peek(&mut self) -> Result<&DeEvent<'de>, DeError> {
if self.read.is_empty() {
self.read.push_front(self.reader.next()?);
}
if let Some(event) = self.read.front() {
return Ok(event);
}
// SAFETY: `self.read` was filled in the code above.
// NOTE: Can be replaced with `unsafe { std::hint::unreachable_unchecked() }`
// if unsafe code will be allowed
unreachable!()
}
#[cfg(not(feature = "overlapped-lists"))]
fn peek(&mut self) -> Result<&DeEvent<'de>, DeError> {
if self.peek.is_none() {
self.peek = Some(self.reader.next()?);
}
match self.peek.as_ref() {
Some(v) => Ok(v),
// SAFETY: a `None` variant for `self.peek` would have been replaced
// by a `Some` variant in the code above.
// TODO: Can be replaced with `unsafe { std::hint::unreachable_unchecked() }`
// if unsafe code will be allowed
None => unreachable!(),
}
}
fn next(&mut self) -> Result<DeEvent<'de>, DeError> {
// Replay skipped or peeked events
#[cfg(feature = "overlapped-lists")]
if let Some(event) = self.read.pop_front() {
return Ok(event);
}
#[cfg(not(feature = "overlapped-lists"))]
if let Some(e) = self.peek.take() {
return Ok(e);
}
self.reader.next()
}
/// Returns the mark after which all events, skipped by [`Self::skip()`] call,
/// should be replayed after calling [`Self::start_replay()`].
#[cfg(feature = "overlapped-lists")]
#[inline]
fn skip_checkpoint(&self) -> usize {
self.write.len()
}
/// Extracts XML tree of events from and stores them in the skipped events
/// buffer from which they can be retrieved later. You MUST call
/// [`Self::start_replay()`] after calling this to give access to the skipped
/// events and release internal buffers.
#[cfg(feature = "overlapped-lists")]
fn skip(&mut self) -> Result<(), DeError> {
let event = self.next()?;
self.skip_event(event)?;
match self.write.back() {
// Skip all subtree, if we skip a start event
Some(DeEvent::Start(e)) => {
let end = e.name().as_ref().to_owned();
let mut depth = 0;
loop {
let event = self.next()?;
match event {
DeEvent::Start(ref e) if e.name().as_ref() == end => {
self.skip_event(event)?;
depth += 1;
}
DeEvent::End(ref e) if e.name().as_ref() == end => {
self.skip_event(event)?;
if depth == 0 {
return Ok(());
}
depth -= 1;
}
_ => self.skip_event(event)?,
}
}
}
_ => Ok(()),
}
}
#[cfg(feature = "overlapped-lists")]
#[inline]
fn skip_event(&mut self, event: DeEvent<'de>) -> Result<(), DeError> {
if let Some(max) = self.limit {
if self.write.len() >= max.get() {
return Err(DeError::TooManyEvents(max));
}
}
self.write.push_back(event);
Ok(())
}
/// Moves buffered events, skipped after given `checkpoint` from [`Self::write`]
/// skip buffer to [`Self::read`] buffer.
///
/// After calling this method, [`Self::peek()`] and [`Self::next()`] starts
/// return events that was skipped previously by calling [`Self::skip()`],
/// and only when all that events will be consumed, the deserializer starts
/// to drain events from underlying reader.
///
/// This method MUST be called if any number of [`Self::skip()`] was called
/// after [`Self::new()`] or `start_replay()` or you'll lost events.
#[cfg(feature = "overlapped-lists")]
fn start_replay(&mut self, checkpoint: usize) {
if checkpoint == 0 {
self.write.append(&mut self.read);
std::mem::swap(&mut self.read, &mut self.write);
} else {
let mut read = self.write.split_off(checkpoint);
read.append(&mut self.read);
self.read = read;
}
}
#[inline]
fn read_string(&mut self, unescape: bool) -> Result<Cow<'de, str>, DeError> {
self.read_string_impl(unescape, true)
}
/// Consumes a one XML element or an XML tree, returns associated text or
/// an empty string.
///
/// If `allow_start` is `false`, then only one event is consumed. If that
/// event is [`DeEvent::Start`], then [`DeError::UnexpectedStart`] is returned.
///
/// If `allow_start` is `true`, then first text of CDATA event inside it is
/// returned and all other content is skipped until corresponding end tag
/// will be consumed.
///
/// # Handling events
///
/// The table below shows how events is handled by this method:
///
/// |Event |XML |Handling
/// |------------------|---------------------------|----------------------------------------
/// |[`DeEvent::Start`]|`<tag>...</tag>` |if `allow_start == true`, result determined by the second table, otherwise emits [`UnexpectedStart("tag")`](DeError::UnexpectedStart)
/// |[`DeEvent::End`] |`</any-tag>` |Emits [`UnexpectedEnd("any-tag")`](DeError::UnexpectedEnd)
/// |[`DeEvent::Text`] |`text content` |Unescapes `text content` and returns it
/// |[`DeEvent::CData`]|`<![CDATA[cdata content]]>`|Returns `cdata content` unchanged
/// |[`DeEvent::Eof`] | |Emits [`UnexpectedEof`](DeError::UnexpectedEof)
///
/// Second event, consumed if [`DeEvent::Start`] was received and `allow_start == true`:
///
/// |Event |XML |Handling
/// |------------------|---------------------------|----------------------------------------------------------------------------------
/// |[`DeEvent::Start`]|`<any-tag>...</any-tag>` |Emits [`UnexpectedStart("any-tag")`](DeError::UnexpectedStart)
/// |[`DeEvent::End`] |`</tag>` |Returns an empty slice, if close tag matched the open one
/// |[`DeEvent::End`] |`</any-tag>` |Emits [`UnexpectedEnd("any-tag")`](DeError::UnexpectedEnd)
/// |[`DeEvent::Text`] |`text content` |Unescapes `text content` and returns it, consumes events up to `</tag>`
/// |[`DeEvent::CData`]|`<![CDATA[cdata content]]>`|Returns `cdata content` unchanged, consumes events up to `</tag>`
/// |[`DeEvent::Eof`] | |Emits [`UnexpectedEof`](DeError::UnexpectedEof)
fn read_string_impl(
&mut self,
unescape: bool,
allow_start: bool,
) -> Result<Cow<'de, str>, DeError> {
match self.next()? {
DeEvent::Text(e) => Ok(e.decode(unescape)?),
DeEvent::CData(e) => Ok(e.decode()?),
DeEvent::Start(e) if allow_start => {
// allow one nested level
let inner = self.next()?;
let t = match inner {
DeEvent::Text(t) => t.decode(unescape)?,
DeEvent::CData(t) => t.decode()?,
DeEvent::Start(s) => {
return Err(DeError::UnexpectedStart(s.name().as_ref().to_owned()))
}
// We can get End event in case of `<tag></tag>` or `<tag/>` input
// Return empty text in that case
DeEvent::End(end) if end.name() == e.name() => {
return Ok("".into());
}
DeEvent::End(end) => {
return Err(DeError::UnexpectedEnd(end.name().as_ref().to_owned()))
}
DeEvent::Eof => return Err(DeError::UnexpectedEof),
};
self.read_to_end(e.name())?;
Ok(t)
}
DeEvent::Start(e) => Err(DeError::UnexpectedStart(e.name().as_ref().to_owned())),
DeEvent::End(e) => Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
DeEvent::Eof => Err(DeError::UnexpectedEof),
}
}
/// Drops all events until event with [name](BytesEnd::name()) `name` won't be
/// dropped. This method should be called after [`Self::next()`]
#[cfg(feature = "overlapped-lists")]
fn read_to_end(&mut self, name: QName) -> Result<(), DeError> {
let mut depth = 0;
loop {
match self.read.pop_front() {
Some(DeEvent::Start(e)) if e.name() == name => {
depth += 1;
}
Some(DeEvent::End(e)) if e.name() == name => {
if depth == 0 {
return Ok(());
}
depth -= 1;
}
// Drop all other skipped events
Some(_) => continue,
// If we do not have skipped events, use effective reading that will
// not allocate memory for events
None => return self.reader.read_to_end(name),
}
}
}
#[cfg(not(feature = "overlapped-lists"))]
fn read_to_end(&mut self, name: QName) -> Result<(), DeError> {
// First one might be in self.peek
match self.next()? {
DeEvent::Start(e) => self.reader.read_to_end(e.name())?,
DeEvent::End(e) if e.name() == name => return Ok(()),
_ => (),
}
self.reader.read_to_end(name)
}
}
impl<'de> Deserializer<'de, SliceReader<'de>> {
/// Create new deserializer that will borrow data from the specified string
pub fn from_str(s: &'de str) -> Self {
let mut reader = Reader::from_str(s);
reader
.expand_empty_elements(true)
.check_end_names(true)
.trim_text(true);
Self::new(SliceReader { reader })
}
}
impl<'de, R> Deserializer<'de, IoReader<R>>
where
R: BufRead,
{
/// Create new deserializer that will copy data from the specified reader
/// into internal buffer. If you already have a string use [`Self::from_str`]
/// instead, because it will borrow instead of copy. If you have `&[u8]` which
/// is known to represent UTF-8, you can decode it first before using [`from_str`].
pub fn from_reader(reader: R) -> Self {
let mut reader = Reader::from_reader(reader);
reader
.expand_empty_elements(true)
.check_end_names(true)
.trim_text(true);
Self::new(IoReader {
reader,
buf: Vec::new(),
})
}
}
impl<'de, 'a, R> de::Deserializer<'de> for &'a mut Deserializer<'de, R>
where
R: XmlRead<'de>,
{
type Error = DeError;
deserialize_primitives!();
fn deserialize_struct<V>(
self,
_name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
match self.next()? {
DeEvent::Start(e) => {
let name = e.name().as_ref().to_vec();
let map = map::MapAccess::new(self, e, fields)?;
let value = visitor.visit_map(map)?;
self.read_to_end(QName(&name))?;
Ok(value)
}
DeEvent::End(e) => Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
DeEvent::Text(_) | DeEvent::CData(_) => Err(DeError::ExpectedStart),
DeEvent::Eof => Err(DeError::UnexpectedEof),
}
}
/// Unit represented in XML as a `xs:element` or text/CDATA content.
/// Any content inside `xs:element` is ignored and skipped.
///
/// Produces unit struct from any of following inputs:
/// - any `<tag ...>...</tag>`
/// - any `<tag .../>`
/// - any text content
/// - any CDATA content
///
/// # Events handling
///
/// |Event |XML |Handling
/// |------------------|---------------------------|-------------------------------------------
/// |[`DeEvent::Start`]|`<tag>...</tag>` |Calls `visitor.visit_unit()`, consumes all events up to corresponding `End` event
/// |[`DeEvent::End`] |`</tag>` |Emits [`UnexpectedEnd("tag")`](DeError::UnexpectedEnd)
/// |[`DeEvent::Text`] |`text content` |Calls `visitor.visit_unit()`. Text content is ignored
/// |[`DeEvent::CData`]|`<![CDATA[cdata content]]>`|Calls `visitor.visit_unit()`. CDATA content is ignored
/// |[`DeEvent::Eof`] | |Emits [`UnexpectedEof`](DeError::UnexpectedEof)
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
match self.next()? {
DeEvent::Start(s) => {
self.read_to_end(s.name())?;
visitor.visit_unit()
}
DeEvent::Text(_) | DeEvent::CData(_) => visitor.visit_unit(),
DeEvent::End(e) => Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
DeEvent::Eof => Err(DeError::UnexpectedEof),
}
}
/// Representation of the names units the same as [unnamed units](#method.deserialize_unit)
fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_unit(visitor)
}
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_tuple(1, visitor)
}
/// Representation of tuples the same as [sequences](#method.deserialize_seq).
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_seq(visitor)
}
/// Representation of named tuples the same as [unnamed tuples](#method.deserialize_tuple).
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_tuple(len, visitor)
}
fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
let value = visitor.visit_enum(var::EnumAccess::new(self))?;
Ok(value)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
visitor.visit_seq(self)
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
self.deserialize_struct("", &[], visitor)
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
match self.peek()? {
DeEvent::Text(t) if t.is_empty() => visitor.visit_none(),
DeEvent::CData(t) if t.is_empty() => visitor.visit_none(),
DeEvent::Eof => visitor.visit_none(),
_ => visitor.visit_some(self),
}
}
/// Always call `visitor.visit_unit()` because returned value ignored in any case.
///
/// This method consumes any single [event][DeEvent] except the [`Start`][DeEvent::Start]
/// event, in which case all events up to corresponding [`End`][DeEvent::End] event will
/// be consumed.
///
/// This method returns error if current event is [`End`][DeEvent::End] or [`Eof`][DeEvent::Eof]
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, DeError>
where
V: Visitor<'de>,
{
match self.next()? {
DeEvent::Start(e) => self.read_to_end(e.name())?,
DeEvent::End(e) => return Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
DeEvent::Eof => return Err(DeError::UnexpectedEof),
_ => (),
}
visitor.visit_unit()
}
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, DeError>
where