@@ -373,6 +373,52 @@ macro_rules! forward {
373373/// A deserializer for a value of map or struct. That deserializer slightly
374374/// differently processes events for a primitive types and sequences than
375375/// a [`Deserializer`].
376+ ///
377+ /// This deserializer can see two kind of events at the start:
378+ /// - [`DeEvent::Text`]
379+ /// - [`DeEvent::Start`]
380+ ///
381+ /// which represents two possible variants of items:
382+ /// ```xml
383+ /// <item>A tag item</item>
384+ /// A text item
385+ /// <yet another="tag item"/>
386+ /// ```
387+ ///
388+ /// This deserializer are very similar to a [`SeqItemDeserializer`]. The only difference
389+ /// in the `deserialize_seq` method. This deserializer will act as an iterator
390+ /// over tags / text within it's parent tag, whereas the [`SeqItemDeserializer`]
391+ /// will represent sequences as an `xs:list`.
392+ ///
393+ /// This deserializer processes items as following:
394+ /// - primitives (numbers, booleans, strings, characters) are deserialized either
395+ /// from a text content, or unwrapped from a one level of a tag. So, `123` and
396+ /// `<int>123</int>` both can be deserialized into an `u32`;
397+ /// - `Option`:
398+ /// - empty text of [`DeEvent::Text`] is deserialized as `None`;
399+ /// - everything else are deserialized as `Some` using the same deserializer,
400+ /// including `<tag/>` or `<tag></tag>`;
401+ /// - units (`()`) and unit structs consumes the whole text or element subtree;
402+ /// - newtype structs are deserialized by forwarding deserialization of inner type
403+ /// with the same deserializer;
404+ /// - sequences, tuples and tuple structs are deserialized by iterating within the
405+ /// parent tag and deserializing each tag or text content using [`SeqItemDeserializer`];
406+ /// - structs and maps are deserialized using new instance of [`MapAccess`];
407+ /// - enums:
408+ /// - in case of [`DeEvent::Text`] event the text content is deserialized as
409+ /// a `$text` variant. Enum content is deserialized from the text using
410+ /// [`SimpleTypeDeserializer`];
411+ /// - in case of [`DeEvent::Start`] event the tag name is deserialized as
412+ /// an enum tag, and the content inside are deserialized as an enum content.
413+ /// Depending on a variant kind deserialization is performed as:
414+ /// - unit variants: consuming text content or a subtree;
415+ /// - newtype variants: forward deserialization to the inner type using
416+ /// this deserializer;
417+ /// - tuple variants: call [`deserialize_tuple`] of this deserializer;
418+ /// - struct variants: call [`deserialize_struct`] of this deserializer.
419+ ///
420+ /// [`deserialize_tuple`]: #method.deserialize_tuple
421+ /// [`deserialize_struct`]: #method.deserialize_struct
376422struct MapValueDeserializer < ' de , ' a , ' m , R , E >
377423where
378424 R : XmlRead < ' de > ,
@@ -485,7 +531,6 @@ where
485531
486532 forward ! ( deserialize_unit) ;
487533
488- forward ! ( deserialize_map) ;
489534 forward ! ( deserialize_struct(
490535 name: & ' static str ,
491536 fields: & ' static [ & ' static str ]
@@ -497,7 +542,6 @@ where
497542 ) ) ;
498543
499544 forward ! ( deserialize_any) ;
500- forward ! ( deserialize_ignored_any) ;
501545
502546 fn deserialize_option < V > ( self , visitor : V ) -> Result < V :: Value , DeError >
503547 where
@@ -506,6 +550,19 @@ where
506550 deserialize_option ! ( self . map. de, self , visitor)
507551 }
508552
553+ /// Forwards deserialization of the inner type. Always calls [`Visitor::visit_newtype_struct`]
554+ /// with the same deserializer.
555+ fn deserialize_newtype_struct < V > (
556+ self ,
557+ _name : & ' static str ,
558+ visitor : V ,
559+ ) -> Result < V :: Value , Self :: Error >
560+ where
561+ V : Visitor < ' de > ,
562+ {
563+ visitor. visit_newtype_struct ( self )
564+ }
565+
509566 /// Deserializes each `<tag>` in
510567 /// ```xml
511568 /// <any-tag>
@@ -716,7 +773,59 @@ where
716773
717774////////////////////////////////////////////////////////////////////////////////////////////////////
718775
719- /// A deserializer for a single item of a sequence.
776+ /// A deserializer for a single item of a mixed sequence of tags and text.
777+ ///
778+ /// This deserializer can see two kind of events at the start:
779+ /// - [`DeEvent::Text`]
780+ /// - [`DeEvent::Start`]
781+ ///
782+ /// which represents two possible variants of items:
783+ /// ```xml
784+ /// <item>A tag item</item>
785+ /// A text item
786+ /// <yet another="tag item"/>
787+ /// ```
788+ ///
789+ /// This deserializer are very similar to a [`MapValueDeserializer`]. The only difference
790+ /// in the `deserialize_seq` method. This deserializer will perform deserialization
791+ /// from the textual content (the text itself in case of [`DeEvent::Text`] event
792+ /// and the text between tags in case of [`DeEvent::Start`] event), whereas
793+ /// the [`MapValueDeserializer`] will iterate over tags / text within it's parent tag.
794+ ///
795+ /// This deserializer processes items as following:
796+ /// - primitives (numbers, booleans, strings, characters) are deserialized either
797+ /// from a text content, or unwrapped from a one level of a tag. So, `123` and
798+ /// `<int>123</int>` both can be deserialized into an `u32`;
799+ /// - `Option`:
800+ /// - empty text of [`DeEvent::Text`] is deserialized as `None`;
801+ /// - everything else are deserialized as `Some` using the same deserializer,
802+ /// including `<tag/>` or `<tag></tag>`;
803+ /// - units (`()`) and unit structs consumes the whole text or element subtree;
804+ /// - newtype structs are deserialized as tuple structs with one element;
805+ /// - sequences, tuples and tuple structs are deserialized using [`SimpleTypeDeserializer`]
806+ /// (this is the difference):
807+ /// - in case of [`DeEvent::Text`] event text content passed to the deserializer directly;
808+ /// - in case of [`DeEvent::Start`] event the start and end tags are stripped,
809+ /// and text between them is passed to [`SimpleTypeDeserializer`]. If the tag
810+ /// contains something else other than text, an error is returned, but if it
811+ /// contains a text and something else (for example, `<item>text<tag/></item>`),
812+ /// then the trail is just ignored;
813+ /// - structs and maps are deserialized using new [`MapAccess`];
814+ /// - enums:
815+ /// - in case of [`DeEvent::Text`] event the text content is deserialized as
816+ /// a `$text` variant. Enum content is deserialized from the text using
817+ /// [`SimpleTypeDeserializer`];
818+ /// - in case of [`DeEvent::Start`] event the tag name is deserialized as
819+ /// an enum tag, and the content inside are deserialized as an enum content.
820+ /// Depending on a variant kind deserialization is performed as:
821+ /// - unit variants: consuming text content or a subtree;
822+ /// - newtype variants: forward deserialization to the inner type using
823+ /// this deserializer;
824+ /// - tuple variants: deserialize it as an `xs:list`;
825+ /// - struct variants: call [`deserialize_struct`] of this deserializer.
826+ ///
827+ /// [`deserialize_tuple`]: #method.deserialize_tuple
828+ /// [`deserialize_struct`]: #method.deserialize_struct
720829struct SeqItemDeserializer < ' de , ' a , ' m , R , E >
721830where
722831 R : XmlRead < ' de > ,
@@ -754,7 +863,6 @@ where
754863
755864 forward ! ( deserialize_unit) ;
756865
757- forward ! ( deserialize_map) ;
758866 forward ! ( deserialize_struct(
759867 name: & ' static str ,
760868 fields: & ' static [ & ' static str ]
@@ -766,7 +874,6 @@ where
766874 ) ) ;
767875
768876 forward ! ( deserialize_any) ;
769- forward ! ( deserialize_ignored_any) ;
770877
771878 fn deserialize_option < V > ( self , visitor : V ) -> Result < V :: Value , DeError >
772879 where
@@ -775,6 +882,20 @@ where
775882 deserialize_option ! ( self . map. de, self , visitor)
776883 }
777884
885+ /// Forwards deserialization of the inner type. Always calls [`Visitor::visit_newtype_struct`]
886+ /// with the [`SimpleTypeDeserializer`].
887+ fn deserialize_newtype_struct < V > (
888+ mut self ,
889+ _name : & ' static str ,
890+ visitor : V ,
891+ ) -> Result < V :: Value , Self :: Error >
892+ where
893+ V : Visitor < ' de > ,
894+ {
895+ let text = self . read_string ( ) ?;
896+ visitor. visit_newtype_struct ( SimpleTypeDeserializer :: from_text ( text) )
897+ }
898+
778899 /// This method deserializes a sequence inside of element that itself is a
779900 /// sequence element:
780901 ///
@@ -787,34 +908,12 @@ where
787908 /// ...
788909 /// </>
789910 /// ```
790- fn deserialize_seq < V > ( self , visitor : V ) -> Result < V :: Value , Self :: Error >
911+ fn deserialize_seq < V > ( mut self , visitor : V ) -> Result < V :: Value , Self :: Error >
791912 where
792913 V : Visitor < ' de > ,
793914 {
794- match self . map . de . next ( ) ? {
795- DeEvent :: Text ( e) => {
796- SimpleTypeDeserializer :: from_text_content ( e) . deserialize_seq ( visitor)
797- }
798- // This is a sequence element. We cannot treat it as another flatten
799- // sequence if type will require `deserialize_seq` We instead forward
800- // it to `xs:simpleType` implementation
801- DeEvent :: Start ( e) => {
802- let value = match self . map . de . next ( ) ? {
803- DeEvent :: Text ( e) => {
804- SimpleTypeDeserializer :: from_text_content ( e) . deserialize_seq ( visitor)
805- }
806- e => Err ( DeError :: Unsupported (
807- format ! ( "unsupported event {:?}" , e) . into ( ) ,
808- ) ) ,
809- } ;
810- // TODO: May be assert that here we expect only matching closing tag?
811- self . map . de . read_to_end ( e. name ( ) ) ?;
812- value
813- }
814- // SAFETY: we use that deserializer only when Start(element) or Text
815- // event was peeked already
816- _ => unreachable ! ( ) ,
817- }
915+ let text = self . read_string ( ) ?;
916+ SimpleTypeDeserializer :: from_text ( text) . deserialize_seq ( visitor)
818917 }
819918
820919 #[ inline]
0 commit comments