Skip to content

Commit

Permalink
Fix serialization of $text variants in $text fields
Browse files Browse the repository at this point in the history
Fixed (1):
    without_root::enum_::externally_tagged::text_variant::text_field::uni
  • Loading branch information
Mingun committed Oct 17, 2023
1 parent 6ef01ec commit 478e19a
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 49 deletions.
20 changes: 10 additions & 10 deletions src/se/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ pub(super) mod tests {
content: Enum::Newtype(42),
after: "answer",
}
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as text content value"));

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

// Complex types cannot be serialized in `$text` field
err!(map:
Expand All @@ -807,21 +807,21 @@ pub(super) mod tests {
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
after: "answer",
}
=> Unsupported("cannot serialize map as an attribute or text content value"));
=> Unsupported("cannot serialize map as text content value"));
err!(struct_:
SpecialEnum::Text {
before: "answer",
content: Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize struct `Struct` as text content value"));
err!(enum_struct:
SpecialEnum::Text {
before: "answer",
content: Enum::Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
}

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

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

// Complex types cannot be serialized in `$text` field
err!(map:
Expand All @@ -1260,21 +1260,21 @@ pub(super) mod tests {
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
after: "answer",
}
=> Unsupported("cannot serialize map as an attribute or text content value"));
=> Unsupported("cannot serialize map as text content value"));
err!(struct_:
SpecialEnum::Text {
before: "answer",
content: Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize struct `Struct` as text content value"));
err!(enum_struct:
SpecialEnum::Text {
before: "answer",
content: Enum::Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
}

/// `$value` field inside a struct variant of an enum
Expand Down
43 changes: 22 additions & 21 deletions src/se/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::errors::serialize::DeError;
use crate::se::content::ContentSerializer;
use crate::se::key::QNameSerializer;
use crate::se::simple_type::{QuoteTarget, SimpleSeq, SimpleTypeSerializer};
use crate::se::text::TextSerializer;
use crate::se::{Indent, XmlName};
use serde::ser::{
Impossible, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant,
Expand Down Expand Up @@ -438,7 +439,7 @@ impl<'w, 'k, W: Write> Struct<'w, 'k, W> {
};

if key == TEXT_KEY {
value.serialize(ser.into_simple_type_serializer())?;
value.serialize(TextSerializer(ser.into_simple_type_serializer()))?;
} else if key == VALUE_KEY {
value.serialize(ser)?;
} else {
Expand Down Expand Up @@ -821,7 +822,7 @@ mod tests {
content: Enum::Newtype(42),
after: "answer",
}
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as an attribute or text content value"));
=> Unsupported("cannot serialize enum newtype variant `Enum::Newtype` as text content value"));

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

// Complex types cannot be serialized in `$text` field
err!(map:
Expand All @@ -847,21 +848,21 @@ mod tests {
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
after: "answer",
}
=> Unsupported("cannot serialize map as an attribute or text content value"));
=> Unsupported("cannot serialize map as text content value"));
err!(struct_:
Text {
before: "answer",
content: Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize struct `Struct` as text content value"));
err!(enum_struct:
Text {
before: "answer",
content: Enum::Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
}

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

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

// Complex types cannot be serialized in `$text` field
err!(map:
Expand All @@ -974,21 +975,21 @@ mod tests {
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
after: "answer",
}
=> Unsupported("cannot serialize map as an attribute or text content value"));
=> Unsupported("cannot serialize map as text content value"));
err!(struct_:
Text {
before: "answer",
content: Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize struct `Struct` as text content value"));
err!(enum_struct:
Text {
before: "answer",
content: Enum::Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
}
}

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

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

// Complex types cannot be serialized in `$text` field
err!(map:
Expand All @@ -1553,21 +1554,21 @@ mod tests {
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
after: "answer",
}
=> Unsupported("cannot serialize map as an attribute or text content value"));
=> Unsupported("cannot serialize map as text content value"));
err!(struct_:
Text {
before: "answer",
content: Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize struct `Struct` as text content value"));
err!(enum_struct:
Text {
before: "answer",
content: Enum::Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
}

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

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

// Complex types cannot be serialized in `$text` field
err!(map:
Expand All @@ -1692,21 +1693,21 @@ mod tests {
content: BTreeMap::from([("_1", 2), ("_3", 4)]),
after: "answer",
}
=> Unsupported("cannot serialize map as an attribute or text content value"));
=> Unsupported("cannot serialize map as text content value"));
err!(struct_:
Text {
before: "answer",
content: Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize struct `Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize struct `Struct` as text content value"));
err!(enum_struct:
Text {
before: "answer",
content: Enum::Struct { key: "answer", val: (42, 42) },
after: "answer",
}
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as an attribute or text content value"));
=> Unsupported("cannot serialize enum struct variant `Enum::Struct` as text content value"));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/se/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ mod content;
mod element;
pub(crate) mod key;
pub(crate) mod simple_type;
mod text;

use self::content::ContentSerializer;
use self::element::{ElementSerializer, Map, Struct, Tuple};
Expand Down
21 changes: 20 additions & 1 deletion src/se/simple_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::errors::serialize::DeError;
use crate::escapei::_escape;
use crate::se::{Indent, QuoteLevel};
use serde::ser::{
Impossible, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct, Serializer,
Impossible, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct,
SerializeTupleVariant, Serializer,
};
use serde::serde_if_integer128;
use std::borrow::Cow;
Expand Down Expand Up @@ -612,6 +613,24 @@ impl<'i, W: Write> SerializeTupleStruct for SimpleSeq<'i, W> {
}
}

impl<'i, W: Write> SerializeTupleVariant for SimpleSeq<'i, W> {
type Ok = W;
type Error = DeError;

#[inline]
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: ?Sized + Serialize,
{
SerializeSeq::serialize_element(self, value)
}

#[inline]
fn end(self) -> Result<Self::Ok, Self::Error> {
SerializeSeq::end(self)
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
Expand Down
Loading

0 comments on commit 478e19a

Please sign in to comment.