xml: Fix field serialization and add value serialization

This commit is contained in:
Lennart
2024-12-27 13:03:52 +01:00
parent f77333e55d
commit ff26cf5056
3 changed files with 69 additions and 6 deletions

View File

@@ -258,10 +258,10 @@ impl Field {
writer.write_event(Event::Text(BytesText::new(&self.#field_ident)))?;
}),
FieldType::Tag => Some(quote! {
self.#field_ident.serialize(None, Some(#field_name), writer)?;
::rustical_xml::XmlSerialize::serialize(&self.#field_ident, None, Some(#field_name), writer)?;
}),
FieldType::Untagged => Some(quote! {
self.#field_ident.serialize(None, None, writer)?;
::rustical_xml::XmlSerialize::serialize(&self.#field_ident, None, None, writer)?;
}),
// TODO: Think about what to do here
FieldType::TagName | FieldType::Namespace => None,

View File

@@ -1,9 +1,9 @@
use quick_xml::events::{BytesStart, Event};
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use std::num::{ParseFloatError, ParseIntError};
use std::{convert::Infallible, io::BufRead};
use thiserror::Error;
use crate::{XmlDeError, XmlDeserialize};
use crate::{XmlDeError, XmlDeserialize, XmlSerialize};
#[derive(Debug, Error)]
pub enum ParseValueError {
@@ -79,3 +79,29 @@ impl<T: Value> XmlDeserialize for T {
Value::deserialize(&string)
}
}
impl<T: Value> XmlSerialize for T {
fn serialize<W: std::io::Write>(
&self,
ns: Option<&[u8]>,
tag: Option<&[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
let tag_str = tag.map(String::from_utf8_lossy);
if let Some(tag) = &tag_str {
writer.write_event(Event::Start(BytesStart::new(tag.clone())))?;
}
writer.write_event(Event::Text(BytesText::new(&self.serialize())))?;
if let Some(tag) = &tag_str {
writer.write_event(Event::End(BytesEnd::new(tag.clone())))?;
}
Ok(())
}
#[allow(refining_impl_trait)]
fn attributes<'a>(&self) -> Vec<quick_xml::events::attributes::Attribute<'a>> {
vec![]
}
}

View File

@@ -25,7 +25,6 @@ fn test_struct_document() {
.serialize_root(&mut writer)
.unwrap();
let out = String::from_utf8(buf).unwrap();
dbg!(out);
}
#[test]
@@ -56,5 +55,43 @@ fn test_struct_untagged_attr() {
.serialize_root(&mut writer)
.unwrap();
let out = String::from_utf8(buf).unwrap();
dbg!(out);
}
#[test]
fn test_struct_value_tagged() {
#[derive(Debug, XmlRootTag, XmlSerialize, PartialEq)]
#[xml(root = b"document")]
struct Document {
href: String,
}
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>");
}
#[test]
fn test_struct_value_untagged() {
#[derive(Debug, XmlRootTag, XmlSerialize, PartialEq)]
#[xml(root = b"document")]
struct Document {
#[xml(ty = "untagged")]
href: String,
}
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>okay</document>");
}