mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 16:32:29 +00:00
xml serialize: Support flatten and Option<T>
This commit is contained in:
@@ -252,19 +252,34 @@ impl Field {
|
|||||||
let field_ident = self.field_ident();
|
let field_ident = self.field_ident();
|
||||||
let field_name = self.xml_name();
|
let field_name = self.xml_name();
|
||||||
|
|
||||||
match self.attrs.xml_ty {
|
match (&self.attrs.xml_ty, self.attrs.flatten.is_present()) {
|
||||||
FieldType::Attr => None,
|
(FieldType::Attr, _) => None,
|
||||||
FieldType::Text => Some(quote! {
|
(FieldType::Text, true) => Some(quote! {
|
||||||
|
for item in self.#field_ident.iter() {
|
||||||
|
writer.write_event(Event::Text(BytesText::new(item)))?;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
(FieldType::Text, false) => Some(quote! {
|
||||||
writer.write_event(Event::Text(BytesText::new(&self.#field_ident)))?;
|
writer.write_event(Event::Text(BytesText::new(&self.#field_ident)))?;
|
||||||
}),
|
}),
|
||||||
FieldType::Tag => Some(quote! {
|
(FieldType::Tag, true) => Some(quote! {
|
||||||
|
for item in self.#field_ident.iter() {
|
||||||
|
::rustical_xml::XmlSerialize::serialize(item, None, Some(#field_name), writer)?;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
(FieldType::Tag, false) => Some(quote! {
|
||||||
::rustical_xml::XmlSerialize::serialize(&self.#field_ident, None, Some(#field_name), writer)?;
|
::rustical_xml::XmlSerialize::serialize(&self.#field_ident, None, Some(#field_name), writer)?;
|
||||||
}),
|
}),
|
||||||
FieldType::Untagged => Some(quote! {
|
(FieldType::Untagged, true) => Some(quote! {
|
||||||
|
for item in self.#field_ident.iter() {
|
||||||
|
::rustical_xml::XmlSerialize::serialize(item, None, None, writer)?;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
(FieldType::Untagged, false) => Some(quote! {
|
||||||
::rustical_xml::XmlSerialize::serialize(&self.#field_ident, None, None, writer)?;
|
::rustical_xml::XmlSerialize::serialize(&self.#field_ident, None, None, writer)?;
|
||||||
}),
|
}),
|
||||||
// TODO: Think about what to do here
|
// TODO: Think about what to do here
|
||||||
FieldType::TagName | FieldType::Namespace => None,
|
(FieldType::TagName | FieldType::Namespace, _) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,8 +121,8 @@ impl Enum {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn attributes<'a>(&self) -> Vec<::quick_xml::events::attributes::Attribute<'a>> {
|
fn attributes<'a>(&self) -> Option<Vec<::quick_xml::events::attributes::Attribute<'a>>> {
|
||||||
vec![]
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -187,7 +187,11 @@ impl NamedStruct {
|
|||||||
.filter(|field| field.attrs.xml_ty == FieldType::Untagged)
|
.filter(|field| field.attrs.xml_ty == FieldType::Untagged)
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
let field_ident = field.field_ident();
|
let field_ident = field.field_ident();
|
||||||
quote! { bytes_start.extend_attributes(self.#field_ident.attributes()); }
|
quote! {
|
||||||
|
if let Some(attrs) = self.#field_ident.attributes() {
|
||||||
|
bytes_start.extend_attributes(attrs);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let attributes = self
|
let attributes = self
|
||||||
@@ -219,8 +223,10 @@ impl NamedStruct {
|
|||||||
|
|
||||||
if let Some(tag) = &tag_str {
|
if let Some(tag) = &tag_str {
|
||||||
let mut bytes_start = BytesStart::new(tag.to_owned());
|
let mut bytes_start = BytesStart::new(tag.to_owned());
|
||||||
bytes_start.extend_attributes(self.attributes());
|
if let Some(attrs) = self.attributes() {
|
||||||
#(#untagged_attributes);*
|
bytes_start.extend_attributes(attrs);
|
||||||
|
}
|
||||||
|
// #(#untagged_attributes);*
|
||||||
writer.write_event(Event::Start(bytes_start))?;
|
writer.write_event(Event::Start(bytes_start))?;
|
||||||
}
|
}
|
||||||
#(#tag_writers);*
|
#(#tag_writers);*
|
||||||
@@ -230,8 +236,8 @@ impl NamedStruct {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn attributes<'a>(&self) -> Vec<::quick_xml::events::attributes::Attribute<'a>> {
|
fn attributes<'a>(&self) -> Option<Vec<::quick_xml::events::attributes::Attribute<'a>>> {
|
||||||
vec![ #(#attributes),* ]
|
Some(vec![ #(#attributes),* ])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,8 +51,8 @@ impl XmlSerialize for () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(refining_impl_trait)]
|
#[allow(refining_impl_trait)]
|
||||||
fn attributes<'a>(&self) -> Vec<quick_xml::events::attributes::Attribute<'a>> {
|
fn attributes<'a>(&self) -> Option<Vec<quick_xml::events::attributes::Attribute<'a>>> {
|
||||||
vec![]
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,27 @@ pub trait XmlSerialize {
|
|||||||
writer: &mut quick_xml::Writer<W>,
|
writer: &mut quick_xml::Writer<W>,
|
||||||
) -> std::io::Result<()>;
|
) -> std::io::Result<()>;
|
||||||
|
|
||||||
fn attributes<'a>(&self) -> impl IntoIterator<Item: Into<Attribute<'a>>>;
|
fn attributes<'a>(&self) -> Option<impl IntoIterator<Item: Into<Attribute<'a>>>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: XmlSerialize> XmlSerialize for Option<T> {
|
||||||
|
fn serialize<W: std::io::Write>(
|
||||||
|
&self,
|
||||||
|
ns: Option<&[u8]>,
|
||||||
|
tag: Option<&[u8]>,
|
||||||
|
writer: &mut quick_xml::Writer<W>,
|
||||||
|
) -> std::io::Result<()> {
|
||||||
|
if let Some(some) = self {
|
||||||
|
some.serialize(ns, tag, writer)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(refining_impl_trait)]
|
||||||
|
fn attributes<'a>(&self) -> Option<Vec<Attribute<'a>>> {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait XmlSerializeRoot {
|
pub trait XmlSerializeRoot {
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ impl<T: Value> XmlSerialize for T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(refining_impl_trait)]
|
#[allow(refining_impl_trait)]
|
||||||
fn attributes<'a>(&self) -> Vec<quick_xml::events::attributes::Attribute<'a>> {
|
fn attributes<'a>(&self) -> Option<Vec<quick_xml::events::attributes::Attribute<'a>>> {
|
||||||
vec![]
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,3 +97,26 @@ fn test_struct_value_untagged() {
|
|||||||
let out = String::from_utf8(buf).unwrap();
|
let out = String::from_utf8(buf).unwrap();
|
||||||
assert_eq!(out, "<document>okays</document>");
|
assert_eq!(out, "<document>okays</document>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_struct_vec() {
|
||||||
|
#[derive(Debug, XmlRootTag, XmlSerialize, PartialEq)]
|
||||||
|
#[xml(root = b"document")]
|
||||||
|
struct Document {
|
||||||
|
#[xml(flatten)]
|
||||||
|
href: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||||
|
Document {
|
||||||
|
href: vec!["okay".to_owned(), "wow".to_owned()],
|
||||||
|
}
|
||||||
|
.serialize_root(&mut writer)
|
||||||
|
.unwrap();
|
||||||
|
let out = String::from_utf8(buf).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
out,
|
||||||
|
"<document><href>okay</href><href>wow</href></document>"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user