From 5e71a7d84804a86d049d3e0a614c35bedf7cb636 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 6 Mar 2024 23:24:54 +0100 Subject: Rewrote the whole decoder --- src/dav/xml.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 2 deletions(-) (limited to 'src/dav/xml.rs') diff --git a/src/dav/xml.rs b/src/dav/xml.rs index ff121f4..d465d60 100644 --- a/src/dav/xml.rs +++ b/src/dav/xml.rs @@ -19,9 +19,14 @@ pub trait QWrite { async fn qwrite(&self, xml: &mut Writer) -> Result<(), quick_xml::Error>; } pub trait QRead { - async fn qread(xml: &mut Reader) -> Result, ParsingError>; + async fn qread(xml: &mut Reader) -> Result; } +// The representation of an XML node in Rust +pub trait Node = QRead + QWrite + std::fmt::Debug + PartialEq; + +// --------------- + /// Transform a Rust object into an XML stream of characters pub struct Writer { pub q: quick_xml::writer::Writer, @@ -106,6 +111,8 @@ impl Reader { } } + /* + * Disabled /// maybe find start tag pub async fn maybe_tag_start(&mut self, ns: &[u8], key: &str) -> Result>, ParsingError> { println!("maybe start tag {}", key); @@ -118,7 +125,6 @@ impl Reader { /// find start tag pub async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result, ParsingError> { - println!("search start tag {}", key); loop { match self.peek() { Event::Start(b) if self.is_tag(ns, key) => break, @@ -127,6 +133,7 @@ impl Reader { } self.next().await } + */ // find stop tag pub async fn tag_stop(&mut self, ns: &[u8], key: &str) -> Result, ParsingError> { @@ -157,5 +164,81 @@ impl Reader { }; } } + + // NEW API + pub async fn maybe_read>(&mut self, t: &mut Option, dirty: &mut bool) -> Result<(), ParsingError> { + match N::qread(self).await { + Ok(v) => { + *t = Some(v); + *dirty = true; + Ok(()) + }, + Err(ParsingError::Recoverable) => Ok(()), + Err(e) => Err(e), + } + } + + pub async fn maybe_push>(&mut self, t: &mut Vec, dirty: &mut bool) -> Result<(), ParsingError> { + match N::qread(self).await { + Ok(v) => { + t.push(v); + *dirty = true; + Ok(()) + }, + Err(ParsingError::Recoverable) => Ok(()), + Err(e) => Err(e), + } + } + + pub async fn find>(&mut self) -> Result { + loop { + // Try parse + match N::qread(self).await { + Err(ParsingError::Recoverable) => (), + otherwise => return otherwise, + } + + // If recovered, skip the element + self.skip().await?; + } + } + + pub async fn maybe_find>(&mut self) -> Result, ParsingError> { + loop { + // Try parse + match N::qread(self).await { + Err(ParsingError::Recoverable) => (), + otherwise => return otherwise.map(Some), + } + + match self.peek() { + Event::End(_) => return Ok(None), + _ => self.skip().await?, + }; + } + } + + pub async fn collect>(&mut self) -> Result, ParsingError> { + let mut acc = Vec::new(); + loop { + match N::qread(self).await { + Err(ParsingError::Recoverable) => match self.peek() { + Event::End(_) => return Ok(acc), + _ => { + self.skip().await?; + }, + }, + Ok(v) => acc.push(v), + Err(e) => return Err(e), + } + } + } + + pub async fn open(&mut self, ns: &[u8], key: &str) -> Result, ParsingError> { + if self.is_tag(ns, key) { + return self.next().await + } + return Err(ParsingError::Recoverable); + } } -- cgit v1.2.3