mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 11:42:25 +00:00
xml: Custom serializers
This commit is contained in:
@@ -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