From 2d14587d83f5c90158bc5b5193b07b49ed6946ee Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 7 Mar 2024 12:25:22 +0100 Subject: Refactor decoder --- src/dav/decoder.rs | 433 ++++++++++++++++++++++++++++------------------------- src/dav/xml.rs | 131 +++++++++------- 2 files changed, 305 insertions(+), 259 deletions(-) (limited to 'src') diff --git a/src/dav/decoder.rs b/src/dav/decoder.rs index 4acd3e8..fd11cf0 100644 --- a/src/dav/decoder.rs +++ b/src/dav/decoder.rs @@ -25,36 +25,32 @@ use super::xml::{Node, QRead, Reader, IRead, DAV_URN, CAL_URN}; /// Propfind request impl QRead> for PropFind { async fn qread(xml: &mut Reader) -> Result { - // Find propfind xml.open(DAV_URN, "propfind").await?; - - // Find any tag let propfind: PropFind = loop { - match xml.peek() { - Event::Start(_) if xml.is_tag(DAV_URN, "allprop") => { - xml.open(DAV_URN, "allprop").await?; - let includ = xml.maybe_find::>().await?; - let r = PropFind::AllProp(includ); - xml.tag_stop(DAV_URN, "allprop").await?; - break r - }, - Event::Start(_) if xml.is_tag(DAV_URN, "prop") => { - break PropFind::Prop(xml.find::>().await?); - }, - Event::Empty(_) if xml.is_tag(DAV_URN, "allprop") => { - 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?; }, + // allprop + if let Some(_) = xml.maybe_open(DAV_URN, "allprop").await? { + let includ = xml.maybe_find::>().await?; + xml.close().await?; + break PropFind::AllProp(includ) } - }; - // Close tag - xml.tag_stop(DAV_URN, "propfind").await?; + // propname + if let Some(_) = xml.maybe_open(DAV_URN, "propname").await? { + xml.close().await?; + break PropFind::PropName + } + + // prop + let (mut maybe_prop, mut dirty) = (None, false); + xml.maybe_read::>(&mut maybe_prop, &mut dirty).await?; + if let Some(prop) = maybe_prop { + break PropFind::Prop(prop) + } + + // not found, skipping + xml.skip().await?; + }; + xml.close().await?; Ok(propfind) } @@ -65,7 +61,7 @@ impl QRead> for PropertyUpdate { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "propertyupdate").await?; let collected_items = xml.collect::>().await?; - xml.tag_stop(DAV_URN, "propertyupdate").await?; + xml.close().await?; Ok(PropertyUpdate(collected_items)) } } @@ -89,7 +85,7 @@ impl> QRead> for Multistatus { } } - xml.tag_stop(DAV_URN, "multistatus").await?; + xml.close().await?; Ok(Multistatus { responses, responsedescription }) } } @@ -112,7 +108,7 @@ impl QRead for LockInfo { }; } } - xml.tag_stop(DAV_URN, "lockinfo").await?; + xml.close().await?; match (m_scope, m_type) { (Some(lockscope), Some(locktype)) => Ok(LockInfo { lockscope, locktype, owner }), _ => Err(ParsingError::MissingChild), @@ -125,7 +121,7 @@ impl QRead> for PropValue { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "prop").await?; let mut acc = xml.collect::>().await?; - xml.tag_stop(DAV_URN, "prop").await?; + xml.close().await?; Ok(PropValue(acc)) } } @@ -136,7 +132,7 @@ impl QRead> for Error { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "error").await?; let violations = xml.collect::>().await?; - xml.tag_stop(DAV_URN, "error").await?; + xml.close().await?; Ok(Error(violations)) } } @@ -168,7 +164,7 @@ impl> QRead> for Response { } } - xml.tag_stop(DAV_URN, "response").await?; + xml.close().await?; match (status, &propstat[..], &href[..]) { (Some(status), &[], &[_, ..]) => Ok(Response { status_or_propstat: StatusOrPropstat::Status(href, status), @@ -205,7 +201,7 @@ impl> QRead> for PropStat { } } - xml.tag_stop(DAV_URN, "propstat").await?; + xml.close().await?; match (m_prop, m_status) { (Some(prop), Some(status)) => Ok(PropStat { prop, status, error, responsedescription }), _ => Err(ParsingError::MissingChild), @@ -219,7 +215,7 @@ impl QRead for Status { let fullcode = xml.tag_string().await?; let txtcode = fullcode.splitn(3, ' ').nth(1).ok_or(ParsingError::InvalidValue)?; let code = http::status::StatusCode::from_bytes(txtcode.as_bytes()).or(Err(ParsingError::InvalidValue))?; - xml.tag_stop(DAV_URN, "status").await?; + xml.close().await?; Ok(Status(code)) } } @@ -228,7 +224,7 @@ impl QRead for ResponseDescription { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "responsedescription").await?; let cnt = xml.tag_string().await?; - xml.tag_stop(DAV_URN, "responsedescription").await?; + xml.close().await?; Ok(ResponseDescription(cnt)) } } @@ -237,7 +233,7 @@ impl QRead for Location { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "location").await?; let href = xml.find::().await?; - xml.tag_stop(DAV_URN, "location").await?; + xml.close().await?; Ok(Location(href)) } } @@ -256,7 +252,7 @@ impl QRead> for Remove { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "remove").await?; let propname = xml.find::>().await?; - xml.tag_stop(DAV_URN, "remove").await?; + xml.close().await?; Ok(Remove(propname)) } } @@ -265,61 +261,39 @@ impl QRead> for Set { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "set").await?; let propvalue = xml.find::>().await?; - xml.tag_stop(DAV_URN, "set").await?; + xml.close().await?; Ok(Set(propvalue)) } } impl QRead> for Violation { async fn qread(xml: &mut Reader) -> Result { - let bs = match xml.peek() { - Event::Start(b) | Event::Empty(b) => b, - _ => return Err(ParsingError::Recoverable), - }; - - // Option 1: a pure DAV property - let (ns, loc) = xml.rdr.resolve_element(bs.name()); - if matches!(ns, Bound(Namespace(ns)) if ns == DAV_URN) { - match loc.into_inner() { - b"lock-token-matches-request-uri" => { - xml.next().await?; - return Ok(Violation::LockTokenMatchesRequestUri) - }, - b"lock-token-submitted" => { - xml.next().await?; - let links = xml.collect::().await?; - xml.tag_stop(DAV_URN, "lock-token-submitted").await?; - return Ok(Violation::LockTokenSubmitted(links)) - }, - b"no-conflicting-lock" => { - // start tag - xml.next().await?; - let links = xml.collect::().await?; - xml.tag_stop(DAV_URN, "no-conflicting-lock").await?; - return Ok(Violation::NoConflictingLock(links)) - }, - b"no-external-entities" => { - xml.next().await?; - return Ok(Violation::NoExternalEntities) - }, - b"preserved-live-properties" => { - xml.next().await?; - return Ok(Violation::PreservedLiveProperties) - }, - b"propfind-finite-depth" => { - xml.next().await?; - return Ok(Violation::PropfindFiniteDepth) - }, - b"cannot-modify-protected-property" => { - xml.next().await?; - return Ok(Violation::CannotModifyProtectedProperty) - }, - _ => (), - }; + if xml.maybe_open(DAV_URN, "lock-token-matches-request-uri").await?.is_some() { + xml.close().await?; + Ok(Violation::LockTokenMatchesRequestUri) + } else if xml.maybe_open(DAV_URN, "lock-token-submitted").await?.is_some() { + let links = xml.collect::().await?; + xml.close().await?; + Ok(Violation::LockTokenSubmitted(links)) + } else if xml.maybe_open(DAV_URN, "no-conflicting-lock").await?.is_some() { + let links = xml.collect::().await?; + xml.close().await?; + Ok(Violation::NoConflictingLock(links)) + } else if xml.maybe_open(DAV_URN, "no-external-entities").await?.is_some() { + xml.close().await?; + Ok(Violation::NoExternalEntities) + } else if xml.maybe_open(DAV_URN, "preserved-live-properties").await?.is_some() { + xml.close().await?; + Ok(Violation::PreservedLiveProperties) + } else if xml.maybe_open(DAV_URN, "propfind-finite-depth").await?.is_some() { + xml.close().await?; + Ok(Violation::PropfindFiniteDepth) + } else if xml.maybe_open(DAV_URN, "cannot-modify-protected-property").await?.is_some() { + xml.close().await?; + Ok(Violation::CannotModifyProtectedProperty) + } else { + E::Error::qread(xml).await.map(Violation::Extension) } - - // Option 2: an extension property, delegating - E::Error::qread(xml).await.map(Violation::Extension) } } @@ -327,7 +301,7 @@ impl QRead> for Include { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "include").await?; let acc = xml.collect::>().await?; - xml.tag_stop(DAV_URN, "include").await?; + xml.close().await?; Ok(Include(acc)) } } @@ -336,43 +310,44 @@ impl QRead> for PropName { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "prop").await?; let acc = xml.collect::>().await?; - xml.tag_stop(DAV_URN, "prop").await?; + xml.close().await?; Ok(PropName(acc)) } } impl QRead> for PropertyRequest { async fn qread(xml: &mut Reader) -> Result { - let bs = match xml.peek() { - Event::Start(b) | Event::Empty(b) => b, - _ => return Err(ParsingError::Recoverable), + let maybe = if xml.maybe_open(DAV_URN, "creationdate").await?.is_some() { + Some(PropertyRequest::CreationDate) + } else if xml.maybe_open(DAV_URN, "displayname").await?.is_some() { + Some(PropertyRequest::DisplayName) + } else if xml.maybe_open(DAV_URN, "getcontentlanguage").await?.is_some() { + Some(PropertyRequest::GetContentLanguage) + } else if xml.maybe_open(DAV_URN, "getcontentlength").await?.is_some() { + Some(PropertyRequest::GetContentLength) + } else if xml.maybe_open(DAV_URN, "getcontenttype").await?.is_some() { + Some(PropertyRequest::GetContentType) + } else if xml.maybe_open(DAV_URN, "getetag").await?.is_some() { + Some(PropertyRequest::GetEtag) + } else if xml.maybe_open(DAV_URN, "getlastmodified").await?.is_some() { + Some(PropertyRequest::GetLastModified) + } else if xml.maybe_open(DAV_URN, "lockdiscovery").await?.is_some() { + Some(PropertyRequest::LockDiscovery) + } else if xml.maybe_open(DAV_URN, "resourcetype").await?.is_some() { + Some(PropertyRequest::ResourceType) + } else if xml.maybe_open(DAV_URN, "supportedlock").await?.is_some() { + Some(PropertyRequest::SupportedLock) + } else { + None }; - // Option 1: a pure core DAV property - let (ns, loc) = xml.rdr.resolve_element(bs.name()); - if matches!(ns, Bound(Namespace(ns)) if ns == DAV_URN) { - let maybe_res = match loc.into_inner() { - b"creationdate" => Some(PropertyRequest::CreationDate), - b"displayname" => Some(PropertyRequest::DisplayName), - b"getcontentlanguage" => Some(PropertyRequest::GetContentLanguage), - b"getcontentlength" => Some(PropertyRequest::GetContentLength), - b"getcontenttype" => Some(PropertyRequest::GetContentType), - b"getetag" => Some(PropertyRequest::GetEtag), - b"getlastmodified" => Some(PropertyRequest::GetLastModified), - b"lockdiscovery" => Some(PropertyRequest::LockDiscovery), - b"resourcetype" => Some(PropertyRequest::ResourceType), - b"supportedlock" => Some(PropertyRequest::SupportedLock), - _ => None, - }; - // Close the current tag if we read something - if let Some(res) = maybe_res { - xml.skip().await?; - return Ok(res) - } + match maybe { + Some(pr) => { + xml.close().await?; + Ok(pr) + }, + None => E::PropertyRequest::qread(xml).await.map(PropertyRequest::Extension), } - - // Option 2: an extension property, delegating - E::PropertyRequest::qread(xml).await.map(PropertyRequest::Extension) } } @@ -380,66 +355,47 @@ impl QRead> for Property { async fn qread(xml: &mut Reader) -> Result { use chrono::{DateTime, FixedOffset, TimeZone}; - let bs = match xml.peek() { - Event::Start(b) | Event::Empty(b) => b, - _ => return Err(ParsingError::Recoverable), - }; - - // Option 1: a pure core DAV property - let (ns, loc) = xml.rdr.resolve_element(bs.name()); - if matches!(ns, Bound(Namespace(ns)) if ns == DAV_URN) { - match loc.into_inner() { - b"creationdate" => { - xml.next().await?; - let datestr = xml.tag_string().await?; - return Ok(Property::CreationDate(DateTime::parse_from_rfc3339(datestr.as_str())?)) - }, - b"displayname" => { - xml.next().await?; - return Ok(Property::DisplayName(xml.tag_string().await?)) - }, - b"getcontentlanguage" => { - xml.next().await?; - return Ok(Property::GetContentLanguage(xml.tag_string().await?)) - }, - b"getcontentlength" => { - xml.next().await?; - let cl = xml.tag_string().await?.parse::()?; - return Ok(Property::GetContentLength(cl)) - }, - b"getcontenttype" => { - xml.next().await?; - return Ok(Property::GetContentType(xml.tag_string().await?)) - }, - b"getetag" => { - xml.next().await?; - return Ok(Property::GetEtag(xml.tag_string().await?)) - }, - b"getlastmodified" => { - xml.next().await?; - let datestr = xml.tag_string().await?; - return Ok(Property::CreationDate(DateTime::parse_from_rfc2822(datestr.as_str())?)) - }, - b"lockdiscovery" => { - xml.next().await?; - let acc = xml.collect::().await?; - xml.tag_stop(DAV_URN, "lockdiscovery").await?; - return Ok(Property::LockDiscovery(acc)) - }, - b"resourcetype" => { - xml.next().await?; - let acc = xml.collect::>().await?; - xml.tag_stop(DAV_URN, "resourcetype").await?; - return Ok(Property::ResourceType(acc)) - }, - b"supportedlock" => { - xml.next().await?; - let acc = xml.collect::().await?; - xml.tag_stop(DAV_URN, "supportedlock").await?; - return Ok(Property::SupportedLock(acc)) - }, - _ => (), - }; + // Core WebDAV properties + if xml.maybe_open(DAV_URN, "creationdate").await?.is_some() { + let datestr = xml.tag_string().await?; + xml.close().await?; + return Ok(Property::CreationDate(DateTime::parse_from_rfc3339(datestr.as_str())?)) + } else if xml.maybe_open(DAV_URN, "displayname").await?.is_some() { + let name = xml.tag_string().await?; + xml.close().await?; + return Ok(Property::DisplayName(name)) + } else if xml.maybe_open(DAV_URN, "getcontentlanguage").await?.is_some() { + let lang = xml.tag_string().await?; + xml.close().await?; + return Ok(Property::GetContentLanguage(lang)) + } else if xml.maybe_open(DAV_URN, "getcontentlength").await?.is_some() { + let cl = xml.tag_string().await?.parse::()?; + xml.close().await?; + return Ok(Property::GetContentLength(cl)) + } else if xml.maybe_open(DAV_URN, "getcontenttype").await?.is_some() { + let ct = xml.tag_string().await?; + xml.close().await?; + return Ok(Property::GetContentType(ct)) + } else if xml.maybe_open(DAV_URN, "getetag").await?.is_some() { + let etag = xml.tag_string().await?; + xml.close().await?; + return Ok(Property::GetEtag(etag)) + } else if xml.maybe_open(DAV_URN, "getlastmodified").await?.is_some() { + let datestr = xml.tag_string().await?; + xml.close().await?; + return Ok(Property::CreationDate(DateTime::parse_from_rfc2822(datestr.as_str())?)) + } else if xml.maybe_open(DAV_URN, "lockdiscovery").await?.is_some() { + let acc = xml.collect::().await?; + xml.close().await?; + return Ok(Property::LockDiscovery(acc)) + } else if xml.maybe_open(DAV_URN, "resourcetype").await?.is_some() { + let acc = xml.collect::>().await?; + xml.close().await?; + return Ok(Property::ResourceType(acc)) + } else if xml.maybe_open(DAV_URN, "supportedlock").await?.is_some() { + let acc = xml.collect::().await?; + xml.close().await?; + return Ok(Property::SupportedLock(acc)) } // Option 2: an extension property, delegating @@ -471,7 +427,7 @@ impl QRead for ActiveLock { } } - xml.tag_stop(DAV_URN, "activelock").await?; + xml.close().await?; match (m_scope, m_type, m_depth, m_root) { (Some(lockscope), Some(locktype), Some(depth), Some(lockroot)) => Ok(ActiveLock { lockscope, locktype, depth, owner, timeout, locktoken, lockroot }), @@ -484,7 +440,7 @@ impl QRead for Depth { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "depth").await?; let depth_str = xml.tag_string().await?; - xml.tag_stop(DAV_URN, "depth").await?; + xml.close().await?; match depth_str.as_str() { "0" => Ok(Depth::Zero), "1" => Ok(Depth::One), @@ -518,7 +474,7 @@ impl QRead for Owner { _ => { xml.skip().await?; }, } }; - xml.tag_stop(DAV_URN, "owner").await?; + xml.close().await?; Ok(owner) } } @@ -536,7 +492,7 @@ impl QRead for Timeout { }, }; - xml.tag_stop(DAV_URN, "timeout").await?; + xml.close().await?; Ok(timeout) } } @@ -545,7 +501,7 @@ impl QRead for LockToken { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "locktoken").await?; let href = Href::qread(xml).await?; - xml.tag_stop(DAV_URN, "locktoken").await?; + xml.close().await?; Ok(LockToken(href)) } } @@ -554,20 +510,19 @@ impl QRead for LockRoot { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "lockroot").await?; let href = Href::qread(xml).await?; - xml.tag_stop(DAV_URN, "lockroot").await?; + xml.close().await?; Ok(LockRoot(href)) } } impl QRead> for ResourceType { async fn qread(xml: &mut Reader) -> Result { - match xml.peek() { - Event::Empty(b) if xml.is_tag(DAV_URN, "collection") => { - xml.next().await?; - Ok(ResourceType::Collection) - }, - _ => E::ResourceType::qread(xml).await.map(ResourceType::Extension), + if xml.maybe_open(DAV_URN, "collection").await?.is_some() { + xml.close().await?; + return Ok(ResourceType::Collection) } + + E::ResourceType::qread(xml).await.map(ResourceType::Extension) } } @@ -588,7 +543,7 @@ impl QRead for LockEntry { } } - xml.tag_stop(DAV_URN, "lockentry").await?; + xml.close().await?; match (maybe_scope, maybe_type) { (Some(lockscope), Some(locktype)) => Ok(LockEntry { lockscope, locktype }), _ => Err(ParsingError::MissingChild), @@ -601,20 +556,18 @@ impl QRead for LockScope { xml.open(DAV_URN, "lockscope").await?; let lockscope = loop { - match xml.peek() { - Event::Empty(_) if xml.is_tag(DAV_URN, "exclusive") => { - xml.next().await?; - break LockScope::Exclusive - }, - Event::Empty(_) if xml.is_tag(DAV_URN, "shared") => { - xml.next().await?; - break LockScope::Shared - } - _ => xml.skip().await?, - }; + if xml.maybe_open(DAV_URN, "exclusive").await?.is_some() { + xml.close().await?; + break LockScope::Exclusive + } else if xml.maybe_open(DAV_URN, "shared").await?.is_some() { + xml.close().await?; + break LockScope::Shared + } + + xml.skip().await?; }; - xml.tag_stop(DAV_URN, "lockscope").await?; + xml.close().await?; Ok(lockscope) } } @@ -624,15 +577,15 @@ impl QRead for LockType { xml.open(DAV_URN, "locktype").await?; let locktype = loop { - match xml.peek() { - Event::Empty(b) if xml.is_tag(DAV_URN, "write") => { - xml.next().await?; - break LockType::Write - } - _ => xml.skip().await?, - }; + if xml.maybe_open(DAV_URN, "write").await?.is_some() { + xml.close().await?; + break LockType::Write + } + + xml.skip().await?; }; - xml.tag_stop(DAV_URN, "locktype").await?; + + xml.close().await?; Ok(locktype) } } @@ -641,7 +594,7 @@ impl QRead for Href { async fn qread(xml: &mut Reader) -> Result { xml.open(DAV_URN, "href").await?; let mut url = xml.tag_string().await?; - xml.tag_stop(DAV_URN, "href").await?; + xml.close().await?; Ok(Href(url)) } } @@ -859,4 +812,68 @@ mod tests { responsedescription: None, }); } + + + #[tokio::test] + async fn rfc_multistatus_value() { + let src = r#" + + + + /container/ + + + Box type A + Hadrian + 1997-12-01T17:42:21-08:00 + Example collection + + + + + + + + + + + + + HTTP/1.1 200 OK + + + + /container/front.html + + + Box type B + + 1997-12-01T18:27:21-08:00 + Example HTML resource + 4525 + text/html + "zzyzx" + Mon, 12 Jan 1998 09:25:56 GMT + + + + + + + + + + + + + HTTP/1.1 200 OK + + + "#; + + let mut rdr = Reader::new(NsReader::from_reader(src.as_bytes())).await.unwrap(); + let got = rdr.find::>>().await.unwrap(); + } + } diff --git a/src/dav/xml.rs b/src/dav/xml.rs index d465d60..d34322a 100644 --- a/src/dav/xml.rs +++ b/src/dav/xml.rs @@ -53,25 +53,33 @@ impl Writer { /// Transform an XML stream of characters into a Rust object pub struct Reader { pub rdr: NsReader, - evt: Event<'static>, + cur: Event<'static>, + parents: Vec>, buf: Vec, } impl Reader { pub async fn new(mut rdr: NsReader) -> Result { let mut buf: Vec = vec![]; - let evt = rdr.read_event_into_async(&mut buf).await?.into_owned(); + let cur = rdr.read_event_into_async(&mut buf).await?.into_owned(); + let parents = vec![]; buf.clear(); - Ok(Self { evt, rdr, buf }) + Ok(Self { cur, parents, rdr, buf }) } - pub fn peek(&self) -> &Event<'static> { - &self.evt + /// read one more tag + /// do not expose it publicly + async fn next(&mut self) -> Result, ParsingError> { + let evt = self.rdr.read_event_into_async(&mut self.buf).await?.into_owned(); + self.buf.clear(); + let old_evt = std::mem::replace(&mut self.cur, evt); + Ok(old_evt) } - /// skip tag. Can't skip end, can't skip eof. + /// skip a node at current level + /// I would like to make this one private but not ready pub async fn skip(&mut self) -> Result, ParsingError> { - println!("skip on {:?}", &self.evt); - match &self.evt { + println!("skipping inside node {:?}", self.parents.last()); + match &self.cur { Event::Start(b) => { let _span = self.rdr.read_to_end_into_async(b.to_end().name(), &mut self.buf).await?; self.next().await @@ -82,17 +90,8 @@ impl Reader { } } - /// read one more tag - pub async fn next(&mut self) -> Result, ParsingError> { - let evt = self.rdr.read_event_into_async(&mut self.buf).await?.into_owned(); - self.buf.clear(); - let old_evt = std::mem::replace(&mut self.evt, evt); - Ok(old_evt) - } - - /// check if this is the desired tag - pub fn is_tag(&self, ns: &[u8], key: &str) -> bool { + 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(), @@ -111,43 +110,25 @@ 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); - let peek = self.peek(); - match peek { - Event::Start(_) | Event::Empty(_) if self.is_tag(ns, key) => Ok(Some(self.next().await?)), - _ => Ok(None), - } + fn parent_has_child(&self) -> bool { + matches!(self.parents.last(), Some(Event::Start(_)) | None) } - /// find start tag - pub async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result, ParsingError> { - loop { - match self.peek() { - Event::Start(b) if self.is_tag(ns, key) => break, - _ => { self.skip().await?; }, - } + fn ensure_parent_has_child(&self) -> Result<(), ParsingError> { + match self.parent_has_child() { + true => Ok(()), + false => Err(ParsingError::Recoverable), } - self.next().await } - */ - // find stop tag - pub async fn tag_stop(&mut self, ns: &[u8], key: &str) -> Result, ParsingError> { - println!("search stop tag {}", key); - loop { - match self.peek() { - Event::End(b) if self.is_tag(ns, key) => break, - _ => { self.skip().await?; }, - } - } - self.next().await + pub fn peek(&self) -> &Event<'static> { + &self.cur } + // NEW API pub async fn tag_string(&mut self) -> Result { + self.ensure_parent_has_child()?; + let mut acc = String::new(); loop { match self.peek() { @@ -165,8 +146,11 @@ impl Reader { } } - // NEW API pub async fn maybe_read>(&mut self, t: &mut Option, dirty: &mut bool) -> Result<(), ParsingError> { + if !self.parent_has_child() { + return Ok(()) + } + match N::qread(self).await { Ok(v) => { *t = Some(v); @@ -179,6 +163,10 @@ impl Reader { } pub async fn maybe_push>(&mut self, t: &mut Vec, dirty: &mut bool) -> Result<(), ParsingError> { + if !self.parent_has_child() { + return Ok(()) + } + match N::qread(self).await { Ok(v) => { t.push(v); @@ -191,6 +179,8 @@ impl Reader { } pub async fn find>(&mut self) -> Result { + self.ensure_parent_has_child()?; + loop { // Try parse match N::qread(self).await { @@ -204,6 +194,8 @@ impl Reader { } pub async fn maybe_find>(&mut self) -> Result, ParsingError> { + self.ensure_parent_has_child()?; + loop { // Try parse match N::qread(self).await { @@ -219,7 +211,9 @@ impl Reader { } pub async fn collect>(&mut self) -> Result, ParsingError> { + self.ensure_parent_has_child()?; let mut acc = Vec::new(); + loop { match N::qread(self).await { Err(ParsingError::Recoverable) => match self.peek() { @@ -235,10 +229,45 @@ impl Reader { } pub async fn open(&mut self, ns: &[u8], key: &str) -> Result, ParsingError> { - if self.is_tag(ns, key) { - return self.next().await + let evt = match self.peek() { + Event::Empty(_) if self.is_tag(ns, key) => self.cur.clone(), + Event::Start(_) if self.is_tag(ns, key) => self.next().await?, + _ => return Err(ParsingError::Recoverable), + }; + + println!("open tag {:?}", evt); + self.parents.push(evt.clone()); + Ok(evt) + } + + pub async fn maybe_open(&mut self, ns: &[u8], key: &str) -> Result>, ParsingError> { + match self.open(ns, key).await { + Ok(v) => Ok(Some(v)), + Err(ParsingError::Recoverable) => Ok(None), + Err(e) => Err(e), + } + } + + // find stop tag + pub async fn close(&mut self) -> Result, ParsingError> { + println!("close tag {:?}", self.parents.last()); + + // Handle the empty case + if !self.parent_has_child() { + self.parents.pop(); + return self.next().await + } + + // Handle the start/end case + loop { + match self.peek() { + Event::End(_) => { + self.parents.pop(); + return self.next().await + }, + _ => self.skip().await?, + }; } - return Err(ParsingError::Recoverable); } } -- cgit v1.2.3