Decoder is starting to work

This commit is contained in:
Quentin 2024-03-04 22:27:37 +01:00
parent c9edf6c37c
commit b7a990ecdb
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 135 additions and 38 deletions

View file

@ -1,4 +1,5 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::future::Future;
use quick_xml::events::{Event, BytesStart, BytesDecl, BytesText}; use quick_xml::events::{Event, BytesStart, BytesDecl, BytesText};
use quick_xml::events::attributes::AttrError; use quick_xml::events::attributes::AttrError;
@ -13,7 +14,8 @@ pub enum ParsingError {
NamespacePrefixAlreadyUsed, NamespacePrefixAlreadyUsed,
WrongToken, WrongToken,
TagNotFound, TagNotFound,
QuickXml(quick_xml::Error) QuickXml(quick_xml::Error),
Eof
} }
impl From<AttrError> for ParsingError { impl From<AttrError> for ParsingError {
fn from(value: AttrError) -> Self { fn from(value: AttrError) -> Self {
@ -29,15 +31,16 @@ impl From<quick_xml::Error> for ParsingError {
const DAV_URN: &[u8] = b"DAV:"; const DAV_URN: &[u8] = b"DAV:";
const CALDAV_URN: &[u8] = b"urn:ietf:params:xml:ns:caldav"; const CALDAV_URN: &[u8] = b"urn:ietf:params:xml:ns:caldav";
const CARDDAV_URN: &[u8] = b"urn:ietf:params:xml:ns:carddav"; const CARDDAV_URN: &[u8] = b"urn:ietf:params:xml:ns:carddav";
const XML_URN: &[u8] = b"xml"; //const XML_URN: &[u8] = b"xml";
const DAV_NS: ResolveResult = Bound(Namespace(DAV_URN));
pub struct PeekRead<T: AsyncBufRead+Unpin> { trait Reader = AsyncBufRead+Unpin+'static;
pub struct PeekRead<T: Reader> {
evt: Event<'static>, evt: Event<'static>,
rdr: NsReader<T>, rdr: NsReader<T>,
buf: Vec<u8>, buf: Vec<u8>,
} }
impl<T: AsyncBufRead+Unpin> PeekRead<T> { impl<T: Reader> PeekRead<T> {
async fn new(mut rdr: NsReader<T>) -> Result<Self, ParsingError> { async fn new(mut rdr: NsReader<T>) -> Result<Self, ParsingError> {
let mut buf: Vec<u8> = vec![]; let mut buf: Vec<u8> = vec![];
let evt = rdr.read_event_into_async(&mut buf).await?.into_owned(); let evt = rdr.read_event_into_async(&mut buf).await?.into_owned();
@ -48,67 +51,154 @@ impl<T: AsyncBufRead+Unpin> PeekRead<T> {
fn peek(&self) -> &Event<'static> { fn peek(&self) -> &Event<'static> {
&self.evt &self.evt
} }
// skip tag, some tags can't be skipped like end, text, cdata
async fn skip(&mut self) -> Result<(), ParsingError> { /// skip tag. Can't skip end, can't skip eof.
async fn skip(&mut self) -> Result<Event<'static>, ParsingError> {
match &self.evt { match &self.evt {
Event::Start(b) => { Event::Start(b) => {
let _span = self.rdr.read_to_end_into_async(b.to_end().name(), &mut self.buf).await?; let _span = self.rdr.read_to_end_into_async(b.to_end().name(), &mut self.buf).await?;
self.next().await self.next().await
}, },
Event::Empty(_) | Event::Comment(_) | Event::PI(_) | Event::Decl(_) | Event::DocType(_) => self.next().await, Event::End(_) => Err(ParsingError::WrongToken),
_ => return Err(ParsingError::WrongToken), Event::Eof => Err(ParsingError::Eof),
_ => self.next().await,
} }
} }
// read one more tag /// read one more tag
async fn next(&mut self) -> Result<(), ParsingError> { async fn next(&mut self) -> Result<Event<'static>, ParsingError> {
let evt = self.rdr.read_event_into_async(&mut self.buf).await?.into_owned(); let evt = self.rdr.read_event_into_async(&mut self.buf).await?.into_owned();
self.buf.clear(); self.buf.clear();
self.evt = evt; let old_evt = std::mem::replace(&mut self.evt, evt);
Ok(()) Ok(old_evt)
}
/// check if this is the desired tag
fn is_tag(&self, ns: &[u8], key: &str) -> bool {
let qname = match self.peek() {
Event::Start(bs) | Event::Empty(bs) => bs.name(),
Event::End(be) => be.name(),
_ => return false,
};
let (extr_ns, local) = self.rdr.resolve_element(qname);
if local.into_inner() != key.as_bytes() {
return false
}
match extr_ns {
ResolveResult::Bound(v) => v.into_inner() == ns,
_ => false,
} }
} }
pub trait QReadable<T: AsyncBufRead+Unpin>: Sized { /// find start tag
async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
loop {
match self.peek() {
Event::Start(b) if self.is_tag(ns, key) => break,
_ => { self.skip().await?; },
}
}
self.next().await
}
// find stop tag
async fn tag_stop(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
loop {
match self.peek() {
Event::End(b) if self.is_tag(ns, key) => break,
_ => { self.skip().await?; },
}
}
self.next().await
}
}
pub trait QReadable<T: Reader>: Sized {
async fn read(xml: &mut PeekRead<T>) -> Result<Self, ParsingError>; async fn read(xml: &mut PeekRead<T>) -> Result<Self, ParsingError>;
} }
impl<E: Extension, T: AsyncBufRead+Unpin> QReadable<T> for PropFind<E> { impl<E: Extension, T: Reader> QReadable<T> for PropFind<E> {
async fn read(xml: &mut PeekRead<T>) -> Result<PropFind<E>, ParsingError> { async fn read(xml: &mut PeekRead<T>) -> Result<PropFind<E>, ParsingError> {
// Find propfind // Find propfind
loop { xml.tag_start(DAV_URN, "propfind").await?;
match xml.peek() {
Event::Start(b) if b.local_name().into_inner() == &b"propfind"[..] => break,
_ => xml.skip().await?,
}
}
xml.next().await?;
// Find any tag // Find any tag
let propfind = loop { let propfind: PropFind<E> = loop {
match xml.peek() { match xml.peek() {
Event::Start(b) | Event::Empty(b) if b.local_name().into_inner() == &b"allprop"[..] => { Event::Start(_) if xml.is_tag(DAV_URN, "allprop") => {
unimplemented!() xml.tag_start(DAV_URN, "allprop").await?;
let r = PropFind::AllProp(Some(Include::read(xml).await?));
xml.tag_stop(DAV_URN, "allprop").await?;
break r
}, },
Event::Start(b) if b.local_name().into_inner() == &b"prop"[..] => { Event::Start(_) if xml.is_tag(DAV_URN, "prop") => {
unimplemented!(); xml.tag_start(DAV_URN, "prop").await?;
let r = PropFind::Prop(PropName::read(xml).await?);
xml.tag_stop(DAV_URN, "prop").await?;
break r
}, },
Event::Empty(b) if b.local_name().into_inner() == &b"propname"[..] => break PropFind::PropName, Event::Empty(_) if xml.is_tag(DAV_URN, "allprop") => {
_ => xml.skip().await?, xml.next().await?;
break PropFind::AllProp(None)
},
Event::Empty(_) if xml.is_tag(DAV_URN, "propname") => {
xml.next().await?;
break PropFind::PropName
},
_ => { xml.skip().await?; },
} }
}; };
xml.next().await?;
// Close tag // Close tag
loop { xml.tag_stop(DAV_URN, "propfind").await?;
match xml.peek() {
Event::End(b) if b.local_name().into_inner() == &b"propfind"[..] => break, Ok(propfind)
_ => xml.skip().await?,
} }
} }
Ok(propfind)
impl<E: Extension, T: Reader> QReadable<T> for Include<E> {
async fn read(xml: &mut PeekRead<T>) -> Result<Include<E>, ParsingError> {
xml.tag_start(DAV_URN, "include").await?;
let mut acc: Vec<PropertyRequest<E>> = Vec::new();
loop {
match xml.peek() {
Event::Start(_) => acc.push(PropertyRequest::read(xml).await?),
Event::End(_) if xml.is_tag(DAV_URN, "include") => break,
_ => { xml.skip().await?; },
}
}
xml.tag_stop(DAV_URN, "include").await?;
Ok(Include(acc))
}
}
impl<E: Extension, T: Reader> QReadable<T> for PropName<E> {
async fn read(xml: &mut PeekRead<T>) -> Result<PropName<E>, ParsingError> {
xml.tag_start(DAV_URN, "prop").await?;
let mut acc: Vec<PropertyRequest<E>> = Vec::new();
loop {
match xml.peek() {
Event::Start(_) => acc.push(PropertyRequest::read(xml).await?),
Event::End(_) if xml.is_tag(DAV_URN, "prop") => break,
_ => { xml.skip().await?; },
}
}
xml.tag_stop(DAV_URN, "prop").await?;
Ok(PropName(acc))
}
}
impl<E: Extension, T: Reader> QReadable<T> for PropertyRequest<E> {
async fn read(xml: &mut PeekRead<T>) -> Result<PropertyRequest<E>, ParsingError> {
/*match xml.peek() {
}*/
unimplemented!();
} }
} }
@ -118,7 +208,13 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn basic_propfind() { async fn basic_propfind() {
let src = r#"<?xml version="1.0" encoding="utf-8" ?><rando/><garbage><old/></garbage><D:propfind xmlns:D="DAV:"><D:propname/></D:propfind>"#; let src = r#"<?xml version="1.0" encoding="utf-8" ?>
<rando/>
<garbage><old/></garbage>
<D:propfind xmlns:D="DAV:">
<D:propname/>
</D:propfind>
"#;
let mut rdr = PeekRead::new(NsReader::from_reader(src.as_bytes())).await.unwrap(); let mut rdr = PeekRead::new(NsReader::from_reader(src.as_bytes())).await.unwrap();
let got = PropFind::<NoExtension>::read(&mut rdr).await.unwrap(); let got = PropFind::<NoExtension>::read(&mut rdr).await.unwrap();

View file

@ -1,6 +1,7 @@
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
#![feature(async_fn_in_trait)] #![feature(async_fn_in_trait)]
#![feature(async_closure)] #![feature(async_closure)]
#![feature(trait_alias)]
mod auth; mod auth;
mod bayou; mod bayou;