@@ -5071,6 +5071,8 @@ mod enum_ {
50715071 float : String ,
50725072 }
50735073
5074+ /// Enum tag selector is a name of the `<element>` or text / CDATA content
5075+ /// for a `$text` variant
50745076 mod externally_tagged {
50755077 use super :: * ;
50765078 use pretty_assertions:: assert_eq;
@@ -5269,8 +5271,142 @@ mod enum_ {
52695271 ) ;
52705272 }
52715273 }
5274+
5275+ /// Test deserialization of the specially named variant `$text`
5276+ mod text {
5277+ use super :: * ;
5278+
5279+ mod unit {
5280+ use super :: * ;
5281+ use pretty_assertions:: assert_eq;
5282+
5283+ #[ derive( Debug , Deserialize , PartialEq ) ]
5284+ enum Text {
5285+ #[ serde( rename = "$text" ) ]
5286+ Unit ,
5287+ }
5288+
5289+ #[ test]
5290+ fn text ( ) {
5291+ let data: Text = from_str ( " text " ) . unwrap ( ) ;
5292+ assert_eq ! ( data, Text :: Unit ) ;
5293+ }
5294+
5295+ #[ test]
5296+ fn cdata ( ) {
5297+ let data: Text = from_str ( "<![CDATA[ cdata ]]>" ) . unwrap ( ) ;
5298+ assert_eq ! ( data, Text :: Unit ) ;
5299+ }
5300+
5301+ #[ test]
5302+ #[ ignore = "awaiting fix of https://github.com/tafia/quick-xml/issues/474" ]
5303+ fn mixed ( ) {
5304+ let data: Text = from_str ( " te <![CDATA[ cdata ]]> xt " ) . unwrap ( ) ;
5305+ assert_eq ! ( data, Text :: Unit ) ;
5306+ }
5307+ }
5308+
5309+ mod newtype {
5310+ use super :: * ;
5311+ use pretty_assertions:: assert_eq;
5312+
5313+ #[ derive( Debug , Deserialize , PartialEq ) ]
5314+ enum Text {
5315+ #[ serde( rename = "$text" ) ]
5316+ Newtype ( String ) ,
5317+ }
5318+
5319+ #[ test]
5320+ fn text ( ) {
5321+ let data: Text = from_str ( " text " ) . unwrap ( ) ;
5322+ assert_eq ! ( data, Text :: Newtype ( "text" . into( ) ) ) ;
5323+ }
5324+
5325+ #[ test]
5326+ fn cdata ( ) {
5327+ let data: Text = from_str ( "<![CDATA[ cdata ]]>" ) . unwrap ( ) ;
5328+ assert_eq ! ( data, Text :: Newtype ( " cdata " . into( ) ) ) ;
5329+ }
5330+
5331+ #[ test]
5332+ #[ ignore = "awaiting fix of https://github.com/tafia/quick-xml/issues/474" ]
5333+ fn mixed ( ) {
5334+ let data: Text = from_str ( " te <![CDATA[ cdata ]]> xt " ) . unwrap ( ) ;
5335+ assert_eq ! ( data, Text :: Newtype ( "te cdata xt" . into( ) ) ) ;
5336+ }
5337+ }
5338+
5339+ /// Tuple variant deserialized as an `xs:list`, that is why spaces
5340+ /// are trimmed even in CDATA sections
5341+ mod tuple {
5342+ use super :: * ;
5343+ use pretty_assertions:: assert_eq;
5344+
5345+ #[ derive( Debug , Deserialize , PartialEq ) ]
5346+ enum Text {
5347+ #[ serde( rename = "$text" ) ]
5348+ Tuple ( f64 , String ) ,
5349+ }
5350+
5351+ #[ test]
5352+ fn text ( ) {
5353+ let data: Text = from_str ( " 4.2 text " ) . unwrap ( ) ;
5354+ assert_eq ! ( data, Text :: Tuple ( 4.2 , "text" . into( ) ) ) ;
5355+ }
5356+
5357+ #[ test]
5358+ fn cdata ( ) {
5359+ let data: Text = from_str ( "<![CDATA[ 4.2 cdata ]]>" ) . unwrap ( ) ;
5360+ assert_eq ! ( data, Text :: Tuple ( 4.2 , "cdata" . into( ) ) ) ;
5361+ }
5362+
5363+ #[ test]
5364+ #[ ignore = "awaiting fix of https://github.com/tafia/quick-xml/issues/474" ]
5365+ fn mixed ( ) {
5366+ let data: Text = from_str ( " 4.2 <![CDATA[ cdata ]]>" ) . unwrap ( ) ;
5367+ assert_eq ! ( data, Text :: Tuple ( 4.2 , "cdata" . into( ) ) ) ;
5368+ }
5369+ }
5370+
5371+ /// Struct variant cannot be directly deserialized from `Text` / `CData` events
5372+ mod struct_ {
5373+ use super :: * ;
5374+
5375+ #[ derive( Debug , Deserialize , PartialEq ) ]
5376+ enum Text {
5377+ #[ serde( rename = "$text" ) ]
5378+ Struct { float : f64 , string : String } ,
5379+ }
5380+
5381+ #[ test]
5382+ fn text ( ) {
5383+ match from_str :: < Text > ( " text " ) {
5384+ Err ( DeError :: Unsupported ( _) ) => { }
5385+ x => panic ! ( "Expected `Err(Unsupported(_))`, but found {:?}" , x) ,
5386+ }
5387+ }
5388+
5389+ #[ test]
5390+ fn cdata ( ) {
5391+ match from_str :: < Text > ( "<![CDATA[ cdata ]]>" ) {
5392+ Err ( DeError :: Unsupported ( _) ) => { }
5393+ x => panic ! ( "Expected `Err(Unsupported(_))`, but found {:?}" , x) ,
5394+ }
5395+ }
5396+
5397+ #[ test]
5398+ fn mixed ( ) {
5399+ match from_str :: < Text > ( " te <![CDATA[ cdata ]]> xt " ) {
5400+ Err ( DeError :: Unsupported ( _) ) => { }
5401+ x => panic ! ( "Expected `Err(Unsupported(_))`, but found {:?}" , x) ,
5402+ }
5403+ }
5404+ }
5405+ }
52725406 }
52735407
5408+ /// Enum tag selector either an attribute "tag", or a tag "tag".
5409+ /// `$text` variant could be defined, but that name has no special meaning
52745410 mod internally_tagged {
52755411 use super :: * ;
52765412
@@ -5486,6 +5622,8 @@ mod enum_ {
54865622 }
54875623 }
54885624
5625+ /// Enum tag selector either an attribute "tag", or a tag "tag".
5626+ /// `$text` variant could be defined, but that name has no special meaning
54895627 mod adjacently_tagged {
54905628 use super :: * ;
54915629
@@ -5743,6 +5881,8 @@ mod enum_ {
57435881 }
57445882 }
57455883
5884+ /// Enum tags does not exist.
5885+ /// `$text` variant could be defined, but that name has no special meaning
57465886 mod untagged {
57475887 use super :: * ;
57485888 use pretty_assertions:: assert_eq;
0 commit comments