mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +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_name = self.xml_name();
|
||||
|
||||
match self.attrs.xml_ty {
|
||||
FieldType::Attr => None,
|
||||
FieldType::Text => Some(quote! {
|
||||
match (&self.attrs.xml_ty, self.attrs.flatten.is_present()) {
|
||||
(FieldType::Attr, _) => None,
|
||||
(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)))?;
|
||||
}),
|
||||
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)?;
|
||||
}),
|
||||
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)?;
|
||||
}),
|
||||
// TODO: Think about what to do here
|
||||
FieldType::TagName | FieldType::Namespace => None,
|
||||
(FieldType::TagName | FieldType::Namespace, _) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,8 +121,8 @@ impl Enum {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn attributes<'a>(&self) -> Vec<::quick_xml::events::attributes::Attribute<'a>> {
|
||||
vec![]
|
||||
fn attributes<'a>(&self) -> Option<Vec<::quick_xml::events::attributes::Attribute<'a>>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +187,11 @@ impl NamedStruct {
|
||||
.filter(|field| field.attrs.xml_ty == FieldType::Untagged)
|
||||
.map(|field| {
|
||||
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
|
||||
@@ -219,8 +223,10 @@ impl NamedStruct {
|
||||
|
||||
if let Some(tag) = &tag_str {
|
||||
let mut bytes_start = BytesStart::new(tag.to_owned());
|
||||
bytes_start.extend_attributes(self.attributes());
|
||||
#(#untagged_attributes);*
|
||||
if let Some(attrs) = self.attributes() {
|
||||
bytes_start.extend_attributes(attrs);
|
||||
}
|
||||
// #(#untagged_attributes);*
|
||||
writer.write_event(Event::Start(bytes_start))?;
|
||||
}
|
||||
#(#tag_writers);*
|
||||
@@ -230,8 +236,8 @@ impl NamedStruct {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn attributes<'a>(&self) -> Vec<::quick_xml::events::attributes::Attribute<'a>> {
|
||||
vec![ #(#attributes),* ]
|
||||
fn attributes<'a>(&self) -> Option<Vec<::quick_xml::events::attributes::Attribute<'a>>> {
|
||||
Some(vec![ #(#attributes),* ])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@ impl XmlSerialize for () {
|
||||
}
|
||||
|
||||
#[allow(refining_impl_trait)]
|
||||
fn attributes<'a>(&self) -> Vec<quick_xml::events::attributes::Attribute<'a>> {
|
||||
vec![]
|
||||
fn attributes<'a>(&self) -> Option<Vec<quick_xml::events::attributes::Attribute<'a>>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,27 @@ pub trait XmlSerialize {
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
) -> 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 {
|
||||
|
||||
@@ -101,7 +101,7 @@ impl<T: Value> XmlSerialize for T {
|
||||
}
|
||||
|
||||
#[allow(refining_impl_trait)]
|
||||
fn attributes<'a>(&self) -> Vec<quick_xml::events::attributes::Attribute<'a>> {
|
||||
vec![]
|
||||
fn attributes<'a>(&self) -> Option<Vec<quick_xml::events::attributes::Attribute<'a>>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,3 +97,26 @@ fn test_struct_value_untagged() {
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
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