xml: Add serialization

This commit is contained in:
Lennart
2024-12-23 10:46:33 +01:00
parent d9f7d1da2f
commit b5e0f68239
9 changed files with 141 additions and 33 deletions

View File

@@ -2,11 +2,13 @@ use quick_xml::events::{BytesStart, Event};
use std::io::BufRead;
pub mod de;
pub mod se;
mod value;
pub use de::XmlDeError;
pub use de::XmlDeserialize;
pub use de::XmlRoot;
pub use se::XmlSerialize;
pub use value::Value;
impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
@@ -42,33 +44,6 @@ impl XmlDeserialize for Unit {
}
}
impl XmlDeserialize for String {
fn deserialize<R: BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &BytesStart,
empty: bool,
) -> Result<Self, XmlDeError> {
if empty {
return Ok(String::new());
}
let mut content = String::new();
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf)? {
Event::End(e) if e.name() == start.name() => {
break;
}
Event::Eof => return Err(XmlDeError::Eof),
Event::Text(text) => {
content.push_str(&text.unescape()?);
}
_a => return Err(XmlDeError::UnknownError),
};
}
Ok(content)
}
}
// TODO: actually implement
pub struct Unparsed(BytesStart<'static>);