mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 16:32:29 +00:00
xml: Fix field serialization and add value serialization
This commit is contained in:
@@ -258,10 +258,10 @@ impl Field {
|
|||||||
writer.write_event(Event::Text(BytesText::new(&self.#field_ident)))?;
|
writer.write_event(Event::Text(BytesText::new(&self.#field_ident)))?;
|
||||||
}),
|
}),
|
||||||
FieldType::Tag => Some(quote! {
|
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! {
|
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
|
// TODO: Think about what to do here
|
||||||
FieldType::TagName | FieldType::Namespace => None,
|
FieldType::TagName | FieldType::Namespace => None,
|
||||||
|
|||||||
@@ -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::num::{ParseFloatError, ParseIntError};
|
||||||
use std::{convert::Infallible, io::BufRead};
|
use std::{convert::Infallible, io::BufRead};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::{XmlDeError, XmlDeserialize};
|
use crate::{XmlDeError, XmlDeserialize, XmlSerialize};
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum ParseValueError {
|
pub enum ParseValueError {
|
||||||
@@ -79,3 +79,29 @@ impl<T: Value> XmlDeserialize for T {
|
|||||||
Value::deserialize(&string)
|
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![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ fn test_struct_document() {
|
|||||||
.serialize_root(&mut writer)
|
.serialize_root(&mut writer)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let out = String::from_utf8(buf).unwrap();
|
let out = String::from_utf8(buf).unwrap();
|
||||||
dbg!(out);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -56,5 +55,43 @@ fn test_struct_untagged_attr() {
|
|||||||
.serialize_root(&mut writer)
|
.serialize_root(&mut writer)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let out = String::from_utf8(buf).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>");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user