mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 04:42:15 +00:00
Migrate propfind and report to rustical_xml
This commit is contained in:
@@ -74,34 +74,39 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {
|
||||
Self: XmlDeserialize,
|
||||
{
|
||||
let mut buf = Vec::new();
|
||||
let event = reader.read_event_into(&mut buf)?;
|
||||
let empty = matches!(event, Event::Empty(_));
|
||||
match event {
|
||||
Event::Start(start) | Event::Empty(start) => {
|
||||
let (ns, name) = reader.resolve_element(start.name());
|
||||
let matches = match (Self::root_ns(), &ns, name) {
|
||||
// Wrong tag
|
||||
(_, _, name) if name.as_ref() != Self::root_tag() => false,
|
||||
// Wrong namespace
|
||||
(Some(root_ns), ns, _) if &ResolveResult::Bound(Namespace(root_ns)) != ns => {
|
||||
false
|
||||
}
|
||||
_ => true,
|
||||
};
|
||||
if !matches {
|
||||
let root_ns = Self::root_ns();
|
||||
return Err(XmlDeError::InvalidTag(
|
||||
format!("{ns:?}"),
|
||||
String::from_utf8_lossy(name.as_ref()).to_string(),
|
||||
format!("{root_ns:?}"),
|
||||
String::from_utf8_lossy(Self::root_tag()).to_string(),
|
||||
));
|
||||
};
|
||||
loop {
|
||||
let event = reader.read_event_into(&mut buf)?;
|
||||
let empty = matches!(event, Event::Empty(_));
|
||||
match event {
|
||||
Event::Decl(_) => { /* <?xml ... ?> ignore this */ }
|
||||
Event::Comment(_) => { /* ignore this */ }
|
||||
Event::Start(start) | Event::Empty(start) => {
|
||||
let (ns, name) = reader.resolve_element(start.name());
|
||||
let matches = match (Self::root_ns(), &ns, name) {
|
||||
// Wrong tag
|
||||
(_, _, name) if name.as_ref() != Self::root_tag() => false,
|
||||
// Wrong namespace
|
||||
(Some(root_ns), ns, _)
|
||||
if &ResolveResult::Bound(Namespace(root_ns)) != ns =>
|
||||
{
|
||||
false
|
||||
}
|
||||
_ => true,
|
||||
};
|
||||
if !matches {
|
||||
let root_ns = Self::root_ns();
|
||||
return Err(XmlDeError::InvalidTag(
|
||||
format!("{ns:?}"),
|
||||
String::from_utf8_lossy(name.as_ref()).to_string(),
|
||||
format!("{root_ns:?}"),
|
||||
String::from_utf8_lossy(Self::root_tag()).to_string(),
|
||||
));
|
||||
};
|
||||
|
||||
return Self::deserialize(&mut reader, &start, empty);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
Err(XmlDeError::UnknownError)
|
||||
return Self::deserialize(&mut reader, &start, empty);
|
||||
}
|
||||
_ => return Err(XmlDeError::UnknownError),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,15 +12,15 @@ pub use de::XmlRootTag;
|
||||
pub use se::XmlSerialize;
|
||||
pub use value::Value;
|
||||
|
||||
impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
|
||||
fn deserialize<R: BufRead>(
|
||||
reader: &mut quick_xml::NsReader<R>,
|
||||
start: &BytesStart,
|
||||
empty: bool,
|
||||
) -> Result<Self, XmlDeError> {
|
||||
Ok(Some(T::deserialize(reader, start, empty)?))
|
||||
}
|
||||
}
|
||||
// impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
|
||||
// fn deserialize<R: BufRead>(
|
||||
// reader: &mut quick_xml::NsReader<R>,
|
||||
// start: &BytesStart,
|
||||
// empty: bool,
|
||||
// ) -> Result<Self, XmlDeError> {
|
||||
// Ok(Some(T::deserialize(reader, start, empty)?))
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Unit;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use quick_xml::events::{BytesStart, Event};
|
||||
use std::num::{ParseFloatError, ParseIntError};
|
||||
use std::str::FromStr;
|
||||
use std::{convert::Infallible, io::BufRead};
|
||||
use thiserror::Error;
|
||||
|
||||
@@ -21,6 +20,21 @@ pub trait Value: Sized {
|
||||
fn deserialize(val: &str) -> Result<Self, XmlDeError>;
|
||||
}
|
||||
|
||||
// impl<T: Value> Value for Option<T> {
|
||||
// fn serialize(&self) -> String {
|
||||
// match self {
|
||||
// Some(inner) => inner.serialize(),
|
||||
// None => "".to_owned(),
|
||||
// }
|
||||
// }
|
||||
// fn deserialize(val: &str) -> Result<Self, XmlDeError> {
|
||||
// match val {
|
||||
// "" => Ok(None),
|
||||
// val => Ok(Some(T::deserialize(val)?)),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
macro_rules! impl_value_parse {
|
||||
($t:ty) => {
|
||||
impl Value for $t {
|
||||
@@ -54,7 +68,7 @@ impl_value_parse!(usize);
|
||||
impl<T: Value> XmlDeserialize for T {
|
||||
fn deserialize<R: BufRead>(
|
||||
reader: &mut quick_xml::NsReader<R>,
|
||||
start: &BytesStart,
|
||||
_start: &BytesStart,
|
||||
empty: bool,
|
||||
) -> Result<Self, XmlDeError> {
|
||||
let mut string = String::new();
|
||||
@@ -64,17 +78,19 @@ impl<T: Value> XmlDeserialize for T {
|
||||
loop {
|
||||
match reader.read_event_into(&mut buf)? {
|
||||
Event::Text(text) => {
|
||||
if !start.is_empty() {
|
||||
if !string.is_empty() {
|
||||
// Content already written
|
||||
return Err(XmlDeError::UnsupportedEvent("todo"));
|
||||
return Err(XmlDeError::UnsupportedEvent("content already written"));
|
||||
}
|
||||
string = String::from_utf8_lossy(text.as_ref()).to_string();
|
||||
}
|
||||
Event::End(_) => break,
|
||||
Event::Eof => return Err(XmlDeError::Eof),
|
||||
_ => return Err(XmlDeError::UnsupportedEvent("todo")),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
<Self as Value>::deserialize(&string)
|
||||
Value::deserialize(&string)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user