WIP: Complete work of propfind parsing

This commit is contained in:
Lennart
2025-06-04 18:11:25 +02:00
parent 5ad6ee2e99
commit e57a14cad1
43 changed files with 875 additions and 1036 deletions

View File

@@ -4,7 +4,7 @@ mod resourcetype;
pub mod tag_list;
use derive_more::derive::From;
pub use multistatus::MultistatusElement;
pub use propfind::{PropElement, PropfindElement, PropfindType, Propname};
pub use propfind::{PropElement, PropfindElement, PropfindType};
pub use resourcetype::{Resourcetype, ResourcetypeInner};
use rustical_xml::{XmlDeserialize, XmlSerialize};
pub use tag_list::TagList;

View File

@@ -1,27 +1,85 @@
use quick_xml::events::Event;
use quick_xml::name::ResolveResult;
use rustical_xml::NamespaceOwned;
use rustical_xml::Unparsed;
use rustical_xml::XmlDeserialize;
use rustical_xml::XmlError;
use rustical_xml::XmlRootTag;
#[derive(Debug, Clone, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"propfind", ns = "crate::namespace::NS_DAV")]
pub struct PropfindElement {
pub struct PropfindElement<PN: XmlDeserialize> {
#[xml(ty = "untagged")]
pub prop: PropfindType,
pub prop: PropfindType<PN>,
}
#[derive(Debug, Clone, PartialEq)]
// pub struct PropElement<PN: XmlDeserialize = Propname>(#[xml(ty = "untagged", flatten)] pub Vec<PN>);
pub struct PropElement<PN: XmlDeserialize>(
// valid
pub Vec<PN>,
// invalid
pub Vec<(Option<NamespaceOwned>, String)>,
);
impl<PN: XmlDeserialize> XmlDeserialize for PropElement<PN> {
fn deserialize<R: std::io::BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool,
) -> Result<Self, XmlError> {
if empty {
return Ok(Self(vec![], vec![]));
}
let mut buf = Vec::new();
let mut valid_props = vec![];
let mut invalid_props = vec![];
loop {
let event = reader.read_event_into(&mut buf)?;
match &event {
Event::End(e) if e.name() == start.name() => {
break;
}
Event::Eof => return Err(XmlError::Eof),
// start of a child element
Event::Start(start) | Event::Empty(start) => {
let empty = matches!(event, Event::Empty(_));
let (ns, name) = reader.resolve_element(start.name());
let ns = match ns {
ResolveResult::Bound(ns) => Some(NamespaceOwned::from(ns)),
ResolveResult::Unknown(_ns) => todo!("handle error"),
ResolveResult::Unbound => None,
};
match PN::deserialize(reader, start, empty) {
Ok(propname) => valid_props.push(propname),
Err(XmlError::InvalidVariant(_)) => {
invalid_props
.push((ns, String::from_utf8_lossy(name.as_ref()).to_string()));
// Consume content
Unparsed::deserialize(reader, start, empty)?;
}
Err(err) => return Err(err),
}
}
Event::Text(_) | Event::CData(_) => {
return Err(XmlError::UnsupportedEvent("Not expecting text here"));
}
Event::Decl(_) | Event::Comment(_) | Event::DocType(_) | Event::PI(_) => { /* ignore */
}
Event::End(_end) => {
unreachable!(
"Unexpected closing tag for wrong element, should be handled by quick_xml"
);
}
}
}
Ok(Self(valid_props, invalid_props))
}
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct PropElement<PN: XmlDeserialize = Propname>(#[xml(ty = "untagged", flatten)] pub Vec<PN>);
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct Propname {
#[xml(ty = "namespace")]
pub ns: Option<NamespaceOwned>,
#[xml(ty = "tag_name")]
pub name: String,
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub enum PropfindType<PN: XmlDeserialize = Propname> {
pub enum PropfindType<PN: XmlDeserialize> {
#[xml(ns = "crate::namespace::NS_DAV")]
Propname,
#[xml(ns = "crate::namespace::NS_DAV")]

View File

@@ -1,6 +1,6 @@
use rustical_xml::{ValueDeserialize, ValueSerialize, XmlDeserialize};
use super::{PropfindType, Propname};
use super::PropfindType;
#[derive(Clone, Debug, PartialEq)]
pub enum SyncLevel {
@@ -37,7 +37,7 @@ impl ValueSerialize for SyncLevel {
// <!-- DAV:limit defined in RFC 5323, Section 5.17 -->
// <!-- DAV:prop defined in RFC 4918, Section 14.18 -->
#[xml(ns = "crate::namespace::NS_DAV")]
pub struct SyncCollectionRequest<PN: XmlDeserialize = Propname> {
pub struct SyncCollectionRequest<PN: XmlDeserialize> {
#[xml(ns = "crate::namespace::NS_DAV")]
pub sync_token: String,
#[xml(ns = "crate::namespace::NS_DAV")]

View File

@@ -1,10 +1,13 @@
use derive_more::derive::From;
use quick_xml::name::Namespace;
use rustical_xml::XmlSerialize;
use quick_xml::{
events::{BytesStart, Event},
name::Namespace,
};
use rustical_xml::{NamespaceOwned, XmlSerialize};
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq, From)]
pub struct TagList(Vec<(Option<Namespace<'static>>, String)>);
pub struct TagList(Vec<(Option<NamespaceOwned>, String)>);
impl XmlSerialize for TagList {
fn serialize<W: std::io::Write>(
@@ -14,22 +17,10 @@ impl XmlSerialize for TagList {
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
#[derive(Debug, XmlSerialize, PartialEq)]
struct Inner(#[xml(ty = "untagged", flatten)] Vec<Tag>);
#[derive(Debug, XmlSerialize, PartialEq)]
struct Tag(
#[xml(ty = "namespace")] Option<Namespace<'static>>,
#[xml(ty = "tag_name")] String,
);
Inner(
self.0
.iter()
.map(|(ns, tag)| Tag(ns.to_owned(), tag.to_owned()))
.collect(),
)
.serialize(ns, tag, namespaces, writer)
for (_ns, tag) in &self.0 {
writer.write_event(Event::Empty(BytesStart::new(tag)))?;
}
Ok(())
}
#[allow(refining_impl_trait)]