mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 02:22:21 +00:00
xml: Custom serializers
This commit is contained in:
@@ -59,6 +59,7 @@ pub struct FieldAttrs {
|
||||
pub common: TagAttrs,
|
||||
pub flatten: Flag,
|
||||
pub default: Option<syn::ExprPath>,
|
||||
pub serialize_with: Option<syn::ExprPath>,
|
||||
#[darling(default, rename = "ty")]
|
||||
pub xml_ty: FieldType,
|
||||
}
|
||||
|
||||
@@ -251,6 +251,11 @@ impl Field {
|
||||
pub fn tag_writer(&self) -> Option<proc_macro2::TokenStream> {
|
||||
let field_ident = self.field_ident();
|
||||
let field_name = self.xml_name();
|
||||
let serializer = if let Some(serialize_with) = &self.attrs.serialize_with {
|
||||
quote! { #serialize_with }
|
||||
} else {
|
||||
quote! { ::rustical_xml::XmlSerialize::serialize }
|
||||
};
|
||||
|
||||
match (&self.attrs.xml_ty, self.attrs.flatten.is_present()) {
|
||||
(FieldType::Attr, _) => None,
|
||||
@@ -264,19 +269,19 @@ impl Field {
|
||||
}),
|
||||
(FieldType::Tag, true) => Some(quote! {
|
||||
for item in self.#field_ident.iter() {
|
||||
::rustical_xml::XmlSerialize::serialize(item, None, Some(#field_name), writer)?;
|
||||
#serializer(item, None, Some(#field_name), writer)?;
|
||||
}
|
||||
}),
|
||||
(FieldType::Tag, false) => Some(quote! {
|
||||
::rustical_xml::XmlSerialize::serialize(&self.#field_ident, None, Some(#field_name), writer)?;
|
||||
#serializer(&self.#field_ident, None, Some(#field_name), writer)?;
|
||||
}),
|
||||
(FieldType::Untagged, true) => Some(quote! {
|
||||
for item in self.#field_ident.iter() {
|
||||
::rustical_xml::XmlSerialize::serialize(item, None, None, writer)?;
|
||||
#serializer(item, None, None, writer)?;
|
||||
}
|
||||
}),
|
||||
(FieldType::Untagged, false) => Some(quote! {
|
||||
::rustical_xml::XmlSerialize::serialize(&self.#field_ident, None, None, writer)?;
|
||||
#serializer(&self.#field_ident, None, None, writer)?;
|
||||
}),
|
||||
// TODO: Think about what to do here
|
||||
(FieldType::TagName | FieldType::Namespace, _) => None,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use quick_xml::Writer;
|
||||
use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot};
|
||||
use xml_derive::XmlDeserialize;
|
||||
|
||||
@@ -120,3 +121,32 @@ fn test_struct_vec() {
|
||||
"<document><href>okay</href><href>wow</href></document>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_struct_serialize_with() {
|
||||
#[derive(Debug, XmlRootTag, XmlSerialize, PartialEq)]
|
||||
#[xml(root = b"document")]
|
||||
struct Document {
|
||||
#[xml(serialize_with = "serialize_href")]
|
||||
href: String,
|
||||
}
|
||||
|
||||
fn serialize_href<W: ::std::io::Write>(
|
||||
val: &String,
|
||||
ns: Option<&[u8]>,
|
||||
tag: Option<&[u8]>,
|
||||
writer: &mut Writer<W>,
|
||||
) -> std::io::Result<()> {
|
||||
val.to_uppercase().serialize(ns, tag, writer)
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
href: "okay".to_owned(),
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
assert_eq!(out, "<document><href>OKAY</href></document>");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user