Skip to content

Commit 478e19a

Browse files
committed
Fix serialization of $text variants in $text fields
Fixed (1): without_root::enum_::externally_tagged::text_variant::text_field::uni
1 parent 6ef01ec commit 478e19a

File tree

6 files changed

+262
-49
lines changed

6 files changed

+262
-49
lines changed

src/se/content.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ pub(super) mod tests {
781781
content: Enum::Newtype(42),
782782
after: "answer",
783783
}
784-
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));
784+
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as text content value"));
785785

786786
// Sequences are serialized separated by spaces, all spaces inside are escaped
787787
text!(seq: vec![1, 2, 3] => "1 2 3");
@@ -798,7 +798,7 @@ pub(super) mod tests {
798798
content: Enum::Tuple("first", 42),
799799
after: "answer",
800800
}
801-
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an attribute or text content value"));
801+
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as text content value"));
802802

803803
// Complex types cannot be serialized in `$text` field
804804
err!(map:
@@ -807,21 +807,21 @@ pub(super) mod tests {
807807
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
808808
after: "answer",
809809
}
810-
=> Unsupported("cannot serialize map as an attribute or text content value"));
810+
=> Unsupported("cannot serialize map as text content value"));
811811
err!(struct_:
812812
SpecialEnum::Text {
813813
before: "answer",
814814
content: Struct { key: "answer", val: (42, 42) },
815815
after: "answer",
816816
}
817-
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
817+
=> Unsupported("cannot serialize struct `Struct` as text content value"));
818818
err!(enum_struct:
819819
SpecialEnum::Text {
820820
before: "answer",
821821
content: Enum::Struct { key: "answer", val: (42, 42) },
822822
after: "answer",
823823
}
824-
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
824+
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
825825
}
826826

827827
/// `$value` field inside a struct variant of an enum
@@ -1234,7 +1234,7 @@ pub(super) mod tests {
12341234
content: Enum::Newtype(42),
12351235
after: "answer",
12361236
}
1237-
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));
1237+
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as text content value"));
12381238

12391239
// Sequences are serialized separated by spaces, all spaces inside are escaped
12401240
text!(seq: vec![1, 2, 3] => "1 2 3");
@@ -1251,7 +1251,7 @@ pub(super) mod tests {
12511251
content: Enum::Tuple("first", 42),
12521252
after: "answer",
12531253
}
1254-
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an attribute or text content value"));
1254+
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as text content value"));
12551255

12561256
// Complex types cannot be serialized in `$text` field
12571257
err!(map:
@@ -1260,21 +1260,21 @@ pub(super) mod tests {
12601260
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
12611261
after: "answer",
12621262
}
1263-
=> Unsupported("cannot serialize map as an attribute or text content value"));
1263+
=> Unsupported("cannot serialize map as text content value"));
12641264
err!(struct_:
12651265
SpecialEnum::Text {
12661266
before: "answer",
12671267
content: Struct { key: "answer", val: (42, 42) },
12681268
after: "answer",
12691269
}
1270-
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
1270+
=> Unsupported("cannot serialize struct `Struct` as text content value"));
12711271
err!(enum_struct:
12721272
SpecialEnum::Text {
12731273
before: "answer",
12741274
content: Enum::Struct { key: "answer", val: (42, 42) },
12751275
after: "answer",
12761276
}
1277-
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
1277+
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
12781278
}
12791279

12801280
/// `$value` field inside a struct variant of an enum

src/se/element.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::errors::serialize::DeError;
55
use crate::se::content::ContentSerializer;
66
use crate::se::key::QNameSerializer;
77
use crate::se::simple_type::{QuoteTarget, SimpleSeq, SimpleTypeSerializer};
8+
use crate::se::text::TextSerializer;
89
use crate::se::{Indent, XmlName};
910
use serde::ser::{
1011
Impossible, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
@@ -438,7 +439,7 @@ impl<'w, 'k, W: Write> Struct<'w, 'k, W> {
438439
};
439440

440441
if key == TEXT_KEY {
441-
value.serialize(ser.into_simple_type_serializer())?;
442+
value.serialize(TextSerializer(ser.into_simple_type_serializer()))?;
442443
} else if key == VALUE_KEY {
443444
value.serialize(ser)?;
444445
} else {
@@ -821,7 +822,7 @@ mod tests {
821822
content: Enum::Newtype(42),
822823
after: "answer",
823824
}
824-
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));
825+
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as text content value"));
825826

826827
// Sequences are serialized separated by spaces, all spaces inside are escaped
827828
text!(seq: vec![1, 2, 3] => "1 2 3");
@@ -838,7 +839,7 @@ mod tests {
838839
content: Enum::Tuple("first", 42),
839840
after: "answer",
840841
}
841-
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an attribute or text content value"));
842+
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as text content value"));
842843

843844
// Complex types cannot be serialized in `$text` field
844845
err!(map:
@@ -847,21 +848,21 @@ mod tests {
847848
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
848849
after: "answer",
849850
}
850-
=> Unsupported("cannot serialize map as an attribute or text content value"));
851+
=> Unsupported("cannot serialize map as text content value"));
851852
err!(struct_:
852853
Text {
853854
before: "answer",
854855
content: Struct { key: "answer", val: (42, 42) },
855856
after: "answer",
856857
}
857-
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
858+
=> Unsupported("cannot serialize struct `Struct` as text content value"));
858859
err!(enum_struct:
859860
Text {
860861
before: "answer",
861862
content: Enum::Struct { key: "answer", val: (42, 42) },
862863
after: "answer",
863864
}
864-
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
865+
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
865866
}
866867

867868
/// `$text` field inside a struct
@@ -948,7 +949,7 @@ mod tests {
948949
content: Enum::Newtype(42),
949950
after: "answer",
950951
}
951-
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));
952+
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as text content value"));
952953

953954
// Sequences are serialized separated by spaces, all spaces inside are escaped
954955
text!(seq: vec![1, 2, 3] => "1 2 3");
@@ -965,7 +966,7 @@ mod tests {
965966
content: Enum::Tuple("first", 42),
966967
after: "answer",
967968
}
968-
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an attribute or text content value"));
969+
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as text content value"));
969970

970971
// Complex types cannot be serialized in `$text` field
971972
err!(map:
@@ -974,21 +975,21 @@ mod tests {
974975
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
975976
after: "answer",
976977
}
977-
=> Unsupported("cannot serialize map as an attribute or text content value"));
978+
=> Unsupported("cannot serialize map as text content value"));
978979
err!(struct_:
979980
Text {
980981
before: "answer",
981982
content: Struct { key: "answer", val: (42, 42) },
982983
after: "answer",
983984
}
984-
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
985+
=> Unsupported("cannot serialize struct `Struct` as text content value"));
985986
err!(enum_struct:
986987
Text {
987988
before: "answer",
988989
content: Enum::Struct { key: "answer", val: (42, 42) },
989990
after: "answer",
990991
}
991-
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
992+
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
992993
}
993994
}
994995

@@ -1527,7 +1528,7 @@ mod tests {
15271528
content: Enum::Newtype(42),
15281529
after: "answer",
15291530
}
1530-
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));
1531+
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as text content value"));
15311532

15321533
// Sequences are serialized separated by spaces, all spaces inside are escaped
15331534
text!(seq: vec![1, 2, 3] => "1 2 3");
@@ -1544,7 +1545,7 @@ mod tests {
15441545
content: Enum::Tuple("first", 42),
15451546
after: "answer",
15461547
}
1547-
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an attribute or text content value"));
1548+
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as text content value"));
15481549

15491550
// Complex types cannot be serialized in `$text` field
15501551
err!(map:
@@ -1553,21 +1554,21 @@ mod tests {
15531554
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
15541555
after: "answer",
15551556
}
1556-
=> Unsupported("cannot serialize map as an attribute or text content value"));
1557+
=> Unsupported("cannot serialize map as text content value"));
15571558
err!(struct_:
15581559
Text {
15591560
before: "answer",
15601561
content: Struct { key: "answer", val: (42, 42) },
15611562
after: "answer",
15621563
}
1563-
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
1564+
=> Unsupported("cannot serialize struct `Struct` as text content value"));
15641565
err!(enum_struct:
15651566
Text {
15661567
before: "answer",
15671568
content: Enum::Struct { key: "answer", val: (42, 42) },
15681569
after: "answer",
15691570
}
1570-
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
1571+
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
15711572
}
15721573

15731574
/// `$text` field inside a struct
@@ -1666,7 +1667,7 @@ mod tests {
16661667
content: Enum::Newtype(42),
16671668
after: "answer",
16681669
}
1669-
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));
1670+
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as text content value"));
16701671

16711672
// Sequences are serialized separated by spaces, all spaces inside are escaped
16721673
text!(seq: vec![1, 2, 3] => "1 2 3");
@@ -1683,7 +1684,7 @@ mod tests {
16831684
content: Enum::Tuple("first", 42),
16841685
after: "answer",
16851686
}
1686-
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as an attribute or text content value"));
1687+
=> Unsupported("cannot serialize enum tuple variant `Enum::Tuple` as text content value"));
16871688

16881689
// Complex types cannot be serialized in `$text` field
16891690
err!(map:
@@ -1692,21 +1693,21 @@ mod tests {
16921693
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
16931694
after: "answer",
16941695
}
1695-
=> Unsupported("cannot serialize map as an attribute or text content value"));
1696+
=> Unsupported("cannot serialize map as text content value"));
16961697
err!(struct_:
16971698
Text {
16981699
before: "answer",
16991700
content: Struct { key: "answer", val: (42, 42) },
17001701
after: "answer",
17011702
}
1702-
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
1703+
=> Unsupported("cannot serialize struct `Struct` as text content value"));
17031704
err!(enum_struct:
17041705
Text {
17051706
before: "answer",
17061707
content: Enum::Struct { key: "answer", val: (42, 42) },
17071708
after: "answer",
17081709
}
1709-
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
1710+
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
17101711
}
17111712
}
17121713

src/se/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ mod content;
7777
mod element;
7878
pub(crate) mod key;
7979
pub(crate) mod simple_type;
80+
mod text;
8081

8182
use self::content::ContentSerializer;
8283
use self::element::{ElementSerializer, Map, Struct, Tuple};

src/se/simple_type.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::errors::serialize::DeError;
77
use crate::escapei::_escape;
88
use crate::se::{Indent, QuoteLevel};
99
use serde::ser::{
10-
Impossible, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct, Serializer,
10+
Impossible, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct,
11+
SerializeTupleVariant, Serializer,
1112
};
1213
use serde::serde_if_integer128;
1314
use std::borrow::Cow;
@@ -612,6 +613,24 @@ impl<'i, W: Write> SerializeTupleStruct for SimpleSeq<'i, W> {
612613
}
613614
}
614615

616+
impl<'i, W: Write> SerializeTupleVariant for SimpleSeq<'i, W> {
617+
type Ok = W;
618+
type Error = DeError;
619+
620+
#[inline]
621+
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
622+
where
623+
T: ?Sized + Serialize,
624+
{
625+
SerializeSeq::serialize_element(self, value)
626+
}
627+
628+
#[inline]
629+
fn end(self) -> Result<Self::Ok, Self::Error> {
630+
SerializeSeq::end(self)
631+
}
632+
}
633+
615634
////////////////////////////////////////////////////////////////////////////////////////////////////
616635

617636
#[cfg(test)]

0 commit comments

Comments
 (0)