Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve serialization of xs:lists with empty elements #665

Merged
merged 6 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ MSRV bumped to 1.56! Crate now uses Rust 2021 edition.
- [#619]: Allow to raise application errors in `ElementWriter::write_inner_content`
(and newly added `ElementWriter::write_inner_content_async` of course).
- [#662]: Get rid of some allocations during serde deserialization.
- [#665]: Improve serialization of `xs:list`s when some elements serialized to an empty string.

[#545]: https://github.com/tafia/quick-xml/pull/545
[#567]: https://github.com/tafia/quick-xml/issues/567
Expand All @@ -51,6 +52,7 @@ MSRV bumped to 1.56! Crate now uses Rust 2021 edition.
[#660]: https://github.com/tafia/quick-xml/pull/660
[#661]: https://github.com/tafia/quick-xml/pull/661
[#662]: https://github.com/tafia/quick-xml/pull/662
[#665]: https://github.com/tafia/quick-xml/pull/665


## 0.30.0 -- 2023-07-23
Expand Down
7 changes: 5 additions & 2 deletions src/de/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ mod tests {
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Newtype(String);

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Tuple((), ());

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Struct {
key: String,
Expand Down Expand Up @@ -459,8 +462,8 @@ mod tests {
=> Custom("invalid type: string \"name\", expected a sequence"));
err!(tuple: ((), ()) = "name"
=> Custom("invalid type: string \"name\", expected a tuple of size 2"));
err!(tuple_struct: ((), ()) = "name"
=> Custom("invalid type: string \"name\", expected a tuple of size 2"));
err!(tuple_struct: Tuple = "name"
=> Custom("invalid type: string \"name\", expected tuple struct Tuple"));

err!(map: HashMap<(), ()> = "name"
=> Custom("invalid type: string \"name\", expected a map"));
Expand Down
19 changes: 12 additions & 7 deletions src/de/simple_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,9 @@ mod tests {
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Newtype(String);

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Tuple((), ());

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct BorrowedNewtype<'a>(&'a str);

Expand Down Expand Up @@ -950,15 +953,17 @@ mod tests {
assert_eq!(data, $result);

// Roundtrip to ensure that serializer corresponds to deserializer
assert_eq!(
data.serialize(AtomicSerializer {
writer: String::new(),
let mut buffer = String::new();
let has_written = data
.serialize(AtomicSerializer {
writer: &mut buffer,
target: QuoteTarget::Text,
level: QuoteLevel::Full,
indent: Some(Indent::None),
})
.unwrap(),
$input
);
.unwrap();
assert_eq!(buffer, $input);
assert_eq!(has_written, !buffer.is_empty());
}
};
}
Expand Down Expand Up @@ -1040,7 +1045,7 @@ mod tests {
=> Unsupported("sequences are not supported as `xs:list` items"));
err!(tuple: ((), ()) = "non-escaped string"
=> Unsupported("tuples are not supported as `xs:list` items"));
err!(tuple_struct: ((), ()) = "non-escaped string"
err!(tuple_struct: Tuple = "non-escaped string"
=> Unsupported("tuples are not supported as `xs:list` items"));

err!(map: HashMap<(), ()> = "non-escaped string"
Expand Down
8 changes: 4 additions & 4 deletions src/se/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ impl<'w, 'i, W: Write> SerializeTuple for ContentSerializer<'w, 'i, W> {
where
T: ?Sized + Serialize,
{
<Self as SerializeSeq>::serialize_element(self, value)
SerializeSeq::serialize_element(self, value)
}

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

Expand All @@ -356,12 +356,12 @@ impl<'w, 'i, W: Write> SerializeTupleStruct for ContentSerializer<'w, 'i, W> {
where
T: ?Sized + Serialize,
{
<Self as SerializeSeq>::serialize_element(self, value)
SerializeSeq::serialize_element(self, value)
}

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

Expand Down
20 changes: 10 additions & 10 deletions src/se/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ impl<'w, 'k, W: Write> SerializeTuple for ElementSerializer<'w, 'k, W> {
where
T: ?Sized + Serialize,
{
<Self as SerializeSeq>::serialize_element(self, value)
SerializeSeq::serialize_element(self, value)
}

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

Expand All @@ -283,12 +283,12 @@ impl<'w, 'k, W: Write> SerializeTupleStruct for ElementSerializer<'w, 'k, W> {
where
T: ?Sized + Serialize,
{
<Self as SerializeSeq>::serialize_element(self, value)
SerializeSeq::serialize_element(self, value)
}

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

Expand All @@ -314,16 +314,16 @@ impl<'w, 'k, W: Write> SerializeTupleVariant for Tuple<'w, 'k, W> {
T: ?Sized + Serialize,
{
match self {
Tuple::Element(ser) => SerializeTuple::serialize_element(ser, value),
Tuple::Text(ser) => SerializeTuple::serialize_element(ser, value),
Self::Element(ser) => SerializeTuple::serialize_element(ser, value),
Self::Text(ser) => SerializeTuple::serialize_element(ser, value),
}
}

#[inline]
fn end(self) -> Result<Self::Ok, Self::Error> {
match self {
Tuple::Element(ser) => SerializeTuple::end(ser),
Tuple::Text(ser) => SerializeTuple::end(ser).map(|_| ()),
Self::Element(ser) => SerializeTuple::end(ser),
Self::Text(ser) => SerializeTuple::end(ser).map(|_| ()),
}
}
}
Expand Down Expand Up @@ -461,12 +461,12 @@ impl<'w, 'k, W: Write> SerializeStructVariant for Struct<'w, 'k, W> {
where
T: ?Sized + Serialize,
{
<Self as SerializeStruct>::serialize_field(self, key, value)
SerializeStruct::serialize_field(self, key, value)
}

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

Expand Down
4 changes: 4 additions & 0 deletions src/se/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,12 @@ impl<'n> XmlName<'n> {
////////////////////////////////////////////////////////////////////////////////////////////////////

pub(crate) enum Indent<'i> {
/// No indent should be written before the element
None,
/// The specified indent should be written. The type owns the buffer with indent
Owned(Indentation),
/// The specified indent should be written. The type borrows buffer with indent
/// from its owner
Borrow(&'i mut Indentation),
}

Expand Down
Loading
Loading