From 4d627b514a3c7d2b33b8681ffcfc488943bf33e2 Mon Sep 17 00:00:00 2001 From: rsdy
Date: Thu, 29 Sep 2022 17:12:08 +0100
Subject: [PATCH] Use field name as tag for `Vec` serialization
---
instant-xml-macros/src/lib.rs | 2 +-
instant-xml-macros/src/ser.rs | 2 +-
instant-xml/src/impls.rs | 18 +++---------------
instant-xml/src/lib.rs | 3 +++
instant-xml/tests/entities.rs | 10 +++++-----
5 files changed, 13 insertions(+), 22 deletions(-)
diff --git a/instant-xml-macros/src/lib.rs b/instant-xml-macros/src/lib.rs
index 2d5721f..ae6ae88 100644
--- a/instant-xml-macros/src/lib.rs
+++ b/instant-xml-macros/src/lib.rs
@@ -820,7 +820,7 @@ mod tests {
field_1: String,
field_2: u8,
}
- }).to_string(), "impl ToXml for TestStruct { fn serialize < W : :: core :: fmt :: Write + ? :: core :: marker :: Sized > (& self , serializer : & mut instant_xml :: Serializer < W > ,) -> Result < () , instant_xml :: Error > { let prefix = serializer . write_start (\"TestStruct\" , \"\" , false) ? ; debug_assert_eq ! (prefix , None) ; let mut new = :: instant_xml :: ser :: Context :: < 0usize > :: default () ; new . default_ns = \"\" ; let old = serializer . push (new) ? ; serializer . end_start () ? ; match < String as ToXml > :: KIND { :: instant_xml :: Kind :: Element (_) => { self . field_1 . serialize (serializer) ? ; } :: instant_xml :: Kind :: Scalar => { let prefix = serializer . write_start (\"FIELD_1\" , \"\" , true) ? ; serializer . end_start () ? ; self . field_1 . serialize (serializer) ? ; serializer . write_close (prefix , \"FIELD_1\") ? ; } } match < u8 as ToXml > :: KIND { :: instant_xml :: Kind :: Element (_) => { self . field_2 . serialize (serializer) ? ; } :: instant_xml :: Kind :: Scalar => { let prefix = serializer . write_start (\"FIELD_2\" , \"\" , true) ? ; serializer . end_start () ? ; self . field_2 . serialize (serializer) ? ; serializer . write_close (prefix , \"FIELD_2\") ? ; } } serializer . write_close (prefix , \"TestStruct\") ? ; serializer . pop (old) ; Ok (()) } const KIND : :: instant_xml :: Kind = :: instant_xml :: Kind :: Element (:: instant_xml :: Id { ns : \"\" , name : \"TestStruct\" , }) ; } ;");
+ }).to_string(), "impl ToXml for TestStruct { fn serialize < W : :: core :: fmt :: Write + ? :: core :: marker :: Sized > (& self , serializer : & mut instant_xml :: Serializer < W > ,) -> Result < () , instant_xml :: Error > { let prefix = serializer . write_start (\"TestStruct\" , \"\" , false) ? ; debug_assert_eq ! (prefix , None) ; let mut new = :: instant_xml :: ser :: Context :: < 0usize > :: default () ; new . default_ns = \"\" ; let old = serializer . push (new) ? ; serializer . end_start () ? ; match < String as ToXml > :: KIND { :: instant_xml :: Kind :: Element (_) => { self . field_1 . serialize (serializer) ? ; } :: instant_xml :: Kind :: Scalar | :: instant_xml :: Kind :: Vec => { let prefix = serializer . write_start (\"FIELD_1\" , \"\" , true) ? ; serializer . end_start () ? ; self . field_1 . serialize (serializer) ? ; serializer . write_close (prefix , \"FIELD_1\") ? ; } } match < u8 as ToXml > :: KIND { :: instant_xml :: Kind :: Element (_) => { self . field_2 . serialize (serializer) ? ; } :: instant_xml :: Kind :: Scalar | :: instant_xml :: Kind :: Vec => { let prefix = serializer . write_start (\"FIELD_2\" , \"\" , true) ? ; serializer . end_start () ? ; self . field_2 . serialize (serializer) ? ; serializer . write_close (prefix , \"FIELD_2\") ? ; } } serializer . write_close (prefix , \"TestStruct\") ? ; serializer . pop (old) ; Ok (()) } const KIND : :: instant_xml :: Kind = :: instant_xml :: Kind :: Element (:: instant_xml :: Id { ns : \"\" , name : \"TestStruct\" , }) ; } ;");
}
#[test]
diff --git a/instant-xml-macros/src/ser.rs b/instant-xml-macros/src/ser.rs
index 84313b2..2ce955d 100644
--- a/instant-xml-macros/src/ser.rs
+++ b/instant-xml-macros/src/ser.rs
@@ -206,7 +206,7 @@ fn process_named_field(
::instant_xml::Kind::Element(_) => {
self.#field_name.serialize(serializer)?;
}
- ::instant_xml::Kind::Scalar => {
+ ::instant_xml::Kind::Scalar | ::instant_xml::Kind::Vec => {
let prefix = serializer.write_start(#tag, #ns, true)?;
serializer.end_start()?;
self.#field_name.serialize(serializer)?;
diff --git a/instant-xml/src/impls.rs b/instant-xml/src/impls.rs
index 1c876c3..ce8378b 100644
--- a/instant-xml/src/impls.rs
+++ b/instant-xml/src/impls.rs
@@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;
-use crate::{de::Node, Deserializer, Error, FromXml, Id, Kind, Serializer, ToXml};
+use crate::{de::Node, Deserializer, Error, FromXml, Kind, Serializer, ToXml};
// Deserializer
struct FromXmlStr