xml: Create empty tags for empty fields

This commit is contained in:
Lennart
2024-12-28 13:14:02 +01:00
parent c16a5214bc
commit 7e74610b75

View File

@@ -221,6 +221,8 @@ impl NamedStruct {
} }
}); });
let is_empty = tag_writers.len() == 0;
quote! { quote! {
impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause { impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause {
fn serialize<W: ::std::io::Write>( fn serialize<W: ::std::io::Write>(
@@ -240,11 +242,17 @@ impl NamedStruct {
bytes_start.extend_attributes(attrs); bytes_start.extend_attributes(attrs);
} }
#(#untagged_attributes);* #(#untagged_attributes);*
writer.write_event(Event::Start(bytes_start))?; if #is_empty {
writer.write_event(Event::Empty(bytes_start))?;
} else {
writer.write_event(Event::Start(bytes_start))?;
}
} }
#(#tag_writers);* if !#is_empty {
if let Some(tag) = &tag_str { #(#tag_writers);*
writer.write_event(Event::End(BytesEnd::new(tag.to_owned())))?; if let Some(tag) = &tag_str {
writer.write_event(Event::End(BytesEnd::new(tag.to_owned())))?;
}
} }
Ok(()) Ok(())
} }