mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 04:42:15 +00:00
xml: Add serialization
This commit is contained in:
@@ -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>);
|
||||
|
||||
|
||||
9
crates/xml/src/se.rs
Normal file
9
crates/xml/src/se.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
pub use xml_derive::XmlSerialize;
|
||||
|
||||
pub trait XmlSerialize {
|
||||
fn serialize<W: std::io::Write>(
|
||||
&self,
|
||||
tag: Option<&[u8]>,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
) -> std::io::Result<()>;
|
||||
}
|
||||
Reference in New Issue
Block a user