mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 04:42:15 +00:00
xml: Work on struct serialization
This commit is contained in:
@@ -3,11 +3,11 @@ use quick_xml::name::ResolveResult;
|
||||
use std::io::BufRead;
|
||||
pub use xml_derive::XmlDeserialize;
|
||||
pub use xml_derive::XmlDocument;
|
||||
pub use xml_derive::XmlRootTag;
|
||||
|
||||
use quick_xml::events::{BytesStart, Event};
|
||||
|
||||
use crate::XmlDeError;
|
||||
use crate::XmlRootTag;
|
||||
|
||||
pub trait XmlDeserialize: Sized {
|
||||
fn deserialize<R: BufRead>(
|
||||
@@ -17,11 +17,6 @@ pub trait XmlDeserialize: Sized {
|
||||
) -> Result<Self, XmlDeError>;
|
||||
}
|
||||
|
||||
pub trait XmlRootTag {
|
||||
fn root_tag() -> &'static [u8];
|
||||
fn root_ns() -> Option<&'static [u8]>;
|
||||
}
|
||||
|
||||
pub trait XmlDocument: XmlDeserialize {
|
||||
fn parse<R: BufRead>(reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError>;
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ pub enum XmlDeError {
|
||||
Eof,
|
||||
#[error("Unsupported xml event: {0}")]
|
||||
UnsupportedEvent(&'static str),
|
||||
#[error("{0}")]
|
||||
Other(String),
|
||||
#[error("Invalid variant: {0}")]
|
||||
InvalidVariant(String),
|
||||
#[error("Invalid field name in {0}: {1}")]
|
||||
|
||||
@@ -8,10 +8,11 @@ mod value;
|
||||
|
||||
pub use de::XmlDeserialize;
|
||||
pub use de::XmlDocument;
|
||||
pub use de::XmlRootTag;
|
||||
pub use error::XmlDeError;
|
||||
pub use se::XmlSerialize;
|
||||
pub use se::XmlSerializeRoot;
|
||||
pub use value::Value;
|
||||
pub use xml_derive::XmlRootTag;
|
||||
|
||||
impl XmlDeserialize for () {
|
||||
fn deserialize<R: BufRead>(
|
||||
@@ -58,3 +59,8 @@ impl XmlDeserialize for Unparsed {
|
||||
Ok(Self(start.to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait XmlRootTag {
|
||||
fn root_tag() -> &'static [u8];
|
||||
fn root_ns() -> Option<&'static [u8]>;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,31 @@
|
||||
use quick_xml::events::attributes::Attribute;
|
||||
pub use xml_derive::XmlSerialize;
|
||||
|
||||
use crate::XmlRootTag;
|
||||
|
||||
pub trait XmlSerialize {
|
||||
fn serialize<W: std::io::Write>(
|
||||
&self,
|
||||
ns: Option<&[u8]>,
|
||||
tag: Option<&[u8]>,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
) -> std::io::Result<()>;
|
||||
|
||||
fn attributes<'a>(&self) -> impl IntoIterator<Item: Into<Attribute<'a>>>;
|
||||
}
|
||||
|
||||
pub trait XmlSerializeRoot {
|
||||
fn serialize_root<W: std::io::Write>(
|
||||
&self,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
) -> std::io::Result<()>;
|
||||
}
|
||||
|
||||
impl<T: XmlSerialize + XmlRootTag> XmlSerializeRoot for T {
|
||||
fn serialize_root<W: std::io::Write>(
|
||||
&self,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
) -> std::io::Result<()> {
|
||||
self.serialize(Self::root_ns(), Some(Self::root_tag()), writer)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user