@@ -12,13 +12,13 @@ use crate::{
12
12
name:: QName ,
13
13
} ;
14
14
use serde:: de:: value:: BorrowedStrDeserializer ;
15
- use serde:: de:: { self , DeserializeSeed , SeqAccess , Visitor } ;
15
+ use serde:: de:: { self , DeserializeSeed , MapAccess , SeqAccess , Visitor } ;
16
16
use serde:: serde_if_integer128;
17
17
use std:: borrow:: Cow ;
18
18
use std:: ops:: Range ;
19
19
20
20
/// Defines a source that should be used to deserialize a value in the next call
21
- /// to [`next_value_seed()`](de:: MapAccess::next_value_seed)
21
+ /// to [`next_value_seed()`](MapAccess::next_value_seed)
22
22
#[ derive( Debug , PartialEq ) ]
23
23
enum ValueSource {
24
24
/// Source are not specified, because [`next_key_seed()`] not yet called.
@@ -28,8 +28,8 @@ enum ValueSource {
28
28
/// Attempt to call [`next_value_seed()`] while accessor in this state would
29
29
/// return a [`DeError::KeyNotRead`] error.
30
30
///
31
- /// [`next_key_seed()`]: de:: MapAccess::next_key_seed
32
- /// [`next_value_seed()`]: de:: MapAccess::next_value_seed
31
+ /// [`next_key_seed()`]: MapAccess::next_key_seed
32
+ /// [`next_value_seed()`]: MapAccess::next_value_seed
33
33
Unknown ,
34
34
/// Next value should be deserialized from an attribute value; value is located
35
35
/// at specified span.
@@ -62,7 +62,7 @@ enum ValueSource {
62
62
/// When in this state, next event, returned by [`next()`], will be a [`Start`],
63
63
/// which represents both a key, and a value. Value would be deserialized from
64
64
/// the whole element and how is will be done determined by the value deserializer.
65
- /// The [`MapAccess `] do not consume any events in that state.
65
+ /// The [`ElementMapAccess `] do not consume any events in that state.
66
66
///
67
67
/// Because in that state any encountered `<tag>` is mapped to the [`VALUE_KEY`]
68
68
/// field, it is possible to use tag name as an enum discriminator, so `enum`s
@@ -105,7 +105,7 @@ enum ValueSource {
105
105
/// [`next()`]: Deserializer::next()
106
106
/// [`name()`]: BytesStart::name()
107
107
/// [`Text`]: Self::Text
108
- /// [list of known fields]: MapAccess ::fields
108
+ /// [list of known fields]: ElementMapAccess ::fields
109
109
Content ,
110
110
/// Next value should be deserialized from an element with a dedicated name.
111
111
/// If deserialized type is a sequence, then that sequence will collect all
@@ -118,7 +118,7 @@ enum ValueSource {
118
118
/// When in this state, next event, returned by [`next()`], will be a [`Start`],
119
119
/// which represents both a key, and a value. Value would be deserialized from
120
120
/// the whole element and how is will be done determined by the value deserializer.
121
- /// The [`MapAccess `] do not consume any events in that state.
121
+ /// The [`ElementMapAccess `] do not consume any events in that state.
122
122
///
123
123
/// An illustration below shows, what data is used to deserialize key and value:
124
124
/// ```xml
@@ -164,16 +164,16 @@ enum ValueSource {
164
164
/// internal buffer of deserializer (i.e. deserializer itself) or an input
165
165
/// (in that case it is possible to approach zero-copy deserialization).
166
166
///
167
- /// - `'a ` lifetime represents a parent deserializer, which could own the data
167
+ /// - `'d ` lifetime represents a parent deserializer, which could own the data
168
168
/// buffer.
169
- pub ( crate ) struct MapAccess < ' de , ' a , R , E >
169
+ pub ( crate ) struct ElementMapAccess < ' de , ' d , R , E >
170
170
where
171
171
R : XmlRead < ' de > ,
172
172
E : EntityResolver ,
173
173
{
174
174
/// Tag -- owner of attributes
175
175
start : BytesStart < ' de > ,
176
- de : & ' a mut Deserializer < ' de , R , E > ,
176
+ de : & ' d mut Deserializer < ' de , R , E > ,
177
177
/// State of the iterator over attributes. Contains the next position in the
178
178
/// inner `start` slice, from which next attribute should be parsed.
179
179
iter : IterState ,
@@ -192,18 +192,18 @@ where
192
192
has_value_field : bool ,
193
193
}
194
194
195
- impl < ' de , ' a , R , E > MapAccess < ' de , ' a , R , E >
195
+ impl < ' de , ' d , R , E > ElementMapAccess < ' de , ' d , R , E >
196
196
where
197
197
R : XmlRead < ' de > ,
198
198
E : EntityResolver ,
199
199
{
200
- /// Create a new MapAccess
200
+ /// Create a new ElementMapAccess
201
201
pub fn new (
202
- de : & ' a mut Deserializer < ' de , R , E > ,
202
+ de : & ' d mut Deserializer < ' de , R , E > ,
203
203
start : BytesStart < ' de > ,
204
204
fields : & ' static [ & ' static str ] ,
205
205
) -> Result < Self , DeError > {
206
- Ok ( MapAccess {
206
+ Ok ( Self {
207
207
de,
208
208
iter : IterState :: new ( start. name ( ) . as_ref ( ) . len ( ) , false ) ,
209
209
start,
@@ -214,7 +214,7 @@ where
214
214
}
215
215
}
216
216
217
- impl < ' de , ' a , R , E > de :: MapAccess < ' de > for MapAccess < ' de , ' a , R , E >
217
+ impl < ' de , ' d , R , E > MapAccess < ' de > for ElementMapAccess < ' de , ' d , R , E >
218
218
where
219
219
R : XmlRead < ' de > ,
220
220
E : EntityResolver ,
@@ -289,9 +289,14 @@ where
289
289
seed. deserialize ( de) . map ( Some )
290
290
}
291
291
// Stop iteration after reaching a closing tag
292
- DeEvent :: End ( e) if e. name ( ) == self . start . name ( ) => Ok ( None ) ,
293
- // This is a unmatched closing tag, so the XML is invalid
294
- DeEvent :: End ( e) => Err ( DeError :: UnexpectedEnd ( e. name ( ) . as_ref ( ) . to_owned ( ) ) ) ,
292
+ // The matching tag name is guaranteed by the reader if our
293
+ // deserializer implementation is correct
294
+ DeEvent :: End ( e) => {
295
+ debug_assert_eq ! ( self . start. name( ) , e. name( ) ) ;
296
+ // Consume End
297
+ self . de . next ( ) ?;
298
+ Ok ( None )
299
+ }
295
300
// We cannot get `Eof` legally, because we always inside of the
296
301
// opened tag `self.start`
297
302
DeEvent :: Eof => Err ( DeError :: UnexpectedEof ) ,
@@ -403,7 +408,7 @@ macro_rules! forward {
403
408
/// with the same deserializer;
404
409
/// - sequences, tuples and tuple structs are deserialized by iterating within the
405
410
/// parent tag and deserializing each tag or text content using [`SeqItemDeserializer`];
406
- /// - structs and maps are deserialized using new instance of [`MapAccess `];
411
+ /// - structs and maps are deserialized using new instance of [`ElementMapAccess `];
407
412
/// - enums:
408
413
/// - in case of [`DeEvent::Text`] event the text content is deserialized as
409
414
/// a `$text` variant. Enum content is deserialized from the text using
@@ -419,14 +424,14 @@ macro_rules! forward {
419
424
///
420
425
/// [`deserialize_tuple`]: #method.deserialize_tuple
421
426
/// [`deserialize_struct`]: #method.deserialize_struct
422
- struct MapValueDeserializer < ' de , ' a , ' m , R , E >
427
+ struct MapValueDeserializer < ' de , ' d , ' m , R , E >
423
428
where
424
429
R : XmlRead < ' de > ,
425
430
E : EntityResolver ,
426
431
{
427
432
/// Access to the map that created this deserializer. Gives access to the
428
433
/// context, such as list of fields, that current map known about.
429
- map : & ' m mut MapAccess < ' de , ' a , R , E > ,
434
+ map : & ' m mut ElementMapAccess < ' de , ' d , R , E > ,
430
435
/// Determines, should [`Deserializer::read_string_impl()`] expand the second
431
436
/// level of tags or not.
432
437
///
@@ -504,7 +509,7 @@ where
504
509
allow_start : bool ,
505
510
}
506
511
507
- impl < ' de , ' a , ' m , R , E > MapValueDeserializer < ' de , ' a , ' m , R , E >
512
+ impl < ' de , ' d , ' m , R , E > MapValueDeserializer < ' de , ' d , ' m , R , E >
508
513
where
509
514
R : XmlRead < ' de > ,
510
515
E : EntityResolver ,
@@ -520,7 +525,7 @@ where
520
525
}
521
526
}
522
527
523
- impl < ' de , ' a , ' m , R , E > de:: Deserializer < ' de > for MapValueDeserializer < ' de , ' a , ' m , R , E >
528
+ impl < ' de , ' d , ' m , R , E > de:: Deserializer < ' de > for MapValueDeserializer < ' de , ' d , ' m , R , E >
524
529
where
525
530
R : XmlRead < ' de > ,
526
531
E : EntityResolver ,
@@ -543,11 +548,14 @@ where
543
548
544
549
forward ! ( deserialize_any) ;
545
550
546
- fn deserialize_option < V > ( self , visitor : V ) -> Result < V :: Value , DeError >
551
+ fn deserialize_option < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
547
552
where
548
553
V : Visitor < ' de > ,
549
554
{
550
- deserialize_option ! ( self . map. de, self , visitor)
555
+ match self . map . de . peek ( ) ? {
556
+ DeEvent :: Text ( t) if t. is_empty ( ) => visitor. visit_none ( ) ,
557
+ _ => visitor. visit_some ( self ) ,
558
+ }
551
559
}
552
560
553
561
/// Forwards deserialization of the inner type. Always calls [`Visitor::visit_newtype_struct`]
@@ -582,7 +590,7 @@ where
582
590
// Clone is cheap if event borrows from the input
583
591
DeEvent :: Start ( e) => TagFilter :: Include ( e. clone ( ) ) ,
584
592
// SAFETY: we use that deserializer with `allow_start == true`
585
- // only from the `MapAccess ::next_value_seed` and only when we
593
+ // only from the `ElementMapAccess ::next_value_seed` and only when we
586
594
// peeked `Start` event
587
595
_ => unreachable ! ( ) ,
588
596
}
@@ -597,11 +605,6 @@ where
597
605
filter,
598
606
} )
599
607
}
600
-
601
- #[ inline]
602
- fn is_human_readable ( & self ) -> bool {
603
- self . map . de . is_human_readable ( )
604
- }
605
608
}
606
609
607
610
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -693,14 +696,14 @@ impl<'de> TagFilter<'de> {
693
696
///
694
697
/// [`Text`]: crate::events::Event::Text
695
698
/// [`CData`]: crate::events::Event::CData
696
- struct MapValueSeqAccess < ' de , ' a , ' m , R , E >
699
+ struct MapValueSeqAccess < ' de , ' d , ' m , R , E >
697
700
where
698
701
R : XmlRead < ' de > ,
699
702
E : EntityResolver ,
700
703
{
701
704
/// Accessor to a map that creates this accessor and to a deserializer for
702
705
/// a sequence items.
703
- map : & ' m mut MapAccess < ' de , ' a , R , E > ,
706
+ map : & ' m mut ElementMapAccess < ' de , ' d , R , E > ,
704
707
/// Filter that determines whether a tag is a part of this sequence.
705
708
///
706
709
/// When feature [`overlapped-lists`] is not activated, iteration will stop
@@ -720,7 +723,7 @@ where
720
723
}
721
724
722
725
#[ cfg( feature = "overlapped-lists" ) ]
723
- impl < ' de , ' a , ' m , R , E > Drop for MapValueSeqAccess < ' de , ' a , ' m , R , E >
726
+ impl < ' de , ' d , ' m , R , E > Drop for MapValueSeqAccess < ' de , ' d , ' m , R , E >
724
727
where
725
728
R : XmlRead < ' de > ,
726
729
E : EntityResolver ,
@@ -730,7 +733,7 @@ where
730
733
}
731
734
}
732
735
733
- impl < ' de , ' a , ' m , R , E > SeqAccess < ' de > for MapValueSeqAccess < ' de , ' a , ' m , R , E >
736
+ impl < ' de , ' d , ' m , R , E > SeqAccess < ' de > for MapValueSeqAccess < ' de , ' d , ' m , R , E >
734
737
where
735
738
R : XmlRead < ' de > ,
736
739
E : EntityResolver ,
@@ -810,7 +813,7 @@ where
810
813
/// contains something else other than text, an error is returned, but if it
811
814
/// contains a text and something else (for example, `<item>text<tag/></item>`),
812
815
/// then the trail is just ignored;
813
- /// - structs and maps are deserialized using new [`MapAccess `];
816
+ /// - structs and maps are deserialized using new [`ElementMapAccess `];
814
817
/// - enums:
815
818
/// - in case of [`DeEvent::Text`] event the text content is deserialized as
816
819
/// a `$text` variant. Enum content is deserialized from the text using
@@ -826,17 +829,17 @@ where
826
829
///
827
830
/// [`deserialize_tuple`]: #method.deserialize_tuple
828
831
/// [`deserialize_struct`]: #method.deserialize_struct
829
- struct SeqItemDeserializer < ' de , ' a , ' m , R , E >
832
+ struct SeqItemDeserializer < ' de , ' d , ' m , R , E >
830
833
where
831
834
R : XmlRead < ' de > ,
832
835
E : EntityResolver ,
833
836
{
834
837
/// Access to the map that created this deserializer. Gives access to the
835
838
/// context, such as list of fields, that current map known about.
836
- map : & ' m mut MapAccess < ' de , ' a , R , E > ,
839
+ map : & ' m mut ElementMapAccess < ' de , ' d , R , E > ,
837
840
}
838
841
839
- impl < ' de , ' a , ' m , R , E > SeqItemDeserializer < ' de , ' a , ' m , R , E >
842
+ impl < ' de , ' d , ' m , R , E > SeqItemDeserializer < ' de , ' d , ' m , R , E >
840
843
where
841
844
R : XmlRead < ' de > ,
842
845
E : EntityResolver ,
@@ -852,7 +855,7 @@ where
852
855
}
853
856
}
854
857
855
- impl < ' de , ' a , ' m , R , E > de:: Deserializer < ' de > for SeqItemDeserializer < ' de , ' a , ' m , R , E >
858
+ impl < ' de , ' d , ' m , R , E > de:: Deserializer < ' de > for SeqItemDeserializer < ' de , ' d , ' m , R , E >
856
859
where
857
860
R : XmlRead < ' de > ,
858
861
E : EntityResolver ,
@@ -875,25 +878,27 @@ where
875
878
876
879
forward ! ( deserialize_any) ;
877
880
878
- fn deserialize_option < V > ( self , visitor : V ) -> Result < V :: Value , DeError >
881
+ fn deserialize_option < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
879
882
where
880
883
V : Visitor < ' de > ,
881
884
{
882
- deserialize_option ! ( self . map. de, self , visitor)
885
+ match self . map . de . peek ( ) ? {
886
+ DeEvent :: Text ( t) if t. is_empty ( ) => visitor. visit_none ( ) ,
887
+ _ => visitor. visit_some ( self ) ,
888
+ }
883
889
}
884
890
885
891
/// Forwards deserialization of the inner type. Always calls [`Visitor::visit_newtype_struct`]
886
- /// with the [`SimpleTypeDeserializer`] .
892
+ /// with this deserializer .
887
893
fn deserialize_newtype_struct < V > (
888
- mut self ,
894
+ self ,
889
895
_name : & ' static str ,
890
896
visitor : V ,
891
897
) -> Result < V :: Value , Self :: Error >
892
898
where
893
899
V : Visitor < ' de > ,
894
900
{
895
- let text = self . read_string ( ) ?;
896
- visitor. visit_newtype_struct ( SimpleTypeDeserializer :: from_text ( text) )
901
+ visitor. visit_newtype_struct ( self )
897
902
}
898
903
899
904
/// This method deserializes a sequence inside of element that itself is a
@@ -915,11 +920,6 @@ where
915
920
let text = self . read_string ( ) ?;
916
921
SimpleTypeDeserializer :: from_text ( text) . deserialize_seq ( visitor)
917
922
}
918
-
919
- #[ inline]
920
- fn is_human_readable ( & self ) -> bool {
921
- self . map . de . is_human_readable ( )
922
- }
923
923
}
924
924
925
925
////////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments