Refactor decoder

This commit is contained in:
Quentin 2024-03-07 12:25:22 +01:00
parent db115ca247
commit 2d14587d83
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 307 additions and 261 deletions

View file

@ -25,36 +25,32 @@ use super::xml::{Node, QRead, Reader, IRead, DAV_URN, CAL_URN};
/// Propfind request
impl<E: Extension> QRead<PropFind<E>> for PropFind<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
// Find propfind
xml.open(DAV_URN, "propfind").await?;
// Find any tag
let propfind: PropFind<E> = loop {
match xml.peek() {
Event::Start(_) if xml.is_tag(DAV_URN, "allprop") => {
xml.open(DAV_URN, "allprop").await?;
// allprop
if let Some(_) = xml.maybe_open(DAV_URN, "allprop").await? {
let includ = xml.maybe_find::<Include<E>>().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::<PropName<E>>().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?; },
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::<PropName<E>>(&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<E: Extension> QRead<PropertyUpdate<E>> for PropertyUpdate<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
xml.open(DAV_URN, "propertyupdate").await?;
let collected_items = xml.collect::<PropertyUpdateItem<E>>().await?;
xml.tag_stop(DAV_URN, "propertyupdate").await?;
xml.close().await?;
Ok(PropertyUpdate(collected_items))
}
}
@ -89,7 +85,7 @@ impl<E: Extension, N: Node<N>> QRead<Multistatus<E,N>> for Multistatus<E,N> {
}
}
xml.tag_stop(DAV_URN, "multistatus").await?;
xml.close().await?;
Ok(Multistatus { responses, responsedescription })
}
}
@ -112,7 +108,7 @@ impl QRead<LockInfo> 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<E: Extension> QRead<PropValue<E>> for PropValue<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
xml.open(DAV_URN, "prop").await?;
let mut acc = xml.collect::<Property<E>>().await?;
xml.tag_stop(DAV_URN, "prop").await?;
xml.close().await?;
Ok(PropValue(acc))
}
}
@ -136,7 +132,7 @@ impl<E: Extension> QRead<Error<E>> for Error<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
xml.open(DAV_URN, "error").await?;
let violations = xml.collect::<Violation<E>>().await?;
xml.tag_stop(DAV_URN, "error").await?;
xml.close().await?;
Ok(Error(violations))
}
}
@ -168,7 +164,7 @@ impl<E: Extension, N: Node<N>> QRead<Response<E,N>> for Response<E,N> {
}
}
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<E: Extension, N: Node<N>> QRead<PropStat<E,N>> for PropStat<E,N> {
}
}
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<Status> 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<ResponseDescription> for ResponseDescription {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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<Location> for Location {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
xml.open(DAV_URN, "location").await?;
let href = xml.find::<Href>().await?;
xml.tag_stop(DAV_URN, "location").await?;
xml.close().await?;
Ok(Location(href))
}
}
@ -256,7 +252,7 @@ impl<E: Extension> QRead<Remove<E>> for Remove<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
xml.open(DAV_URN, "remove").await?;
let propname = xml.find::<PropName<E>>().await?;
xml.tag_stop(DAV_URN, "remove").await?;
xml.close().await?;
Ok(Remove(propname))
}
}
@ -265,69 +261,47 @@ impl<E: Extension> QRead<Set<E>> for Set<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
xml.open(DAV_URN, "set").await?;
let propvalue = xml.find::<PropValue<E>>().await?;
xml.tag_stop(DAV_URN, "set").await?;
xml.close().await?;
Ok(Set(propvalue))
}
}
impl<E: Extension> QRead<Violation<E>> for Violation<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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?;
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::<Href>().await?;
xml.tag_stop(DAV_URN, "lock-token-submitted").await?;
return Ok(Violation::LockTokenSubmitted(links))
},
b"no-conflicting-lock" => {
// start tag
xml.next().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::<Href>().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)
},
_ => (),
};
}
// Option 2: an extension property, delegating
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)
}
}
}
impl<E: Extension> QRead<Include<E>> for Include<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
xml.open(DAV_URN, "include").await?;
let acc = xml.collect::<PropertyRequest<E>>().await?;
xml.tag_stop(DAV_URN, "include").await?;
xml.close().await?;
Ok(Include(acc))
}
}
@ -336,110 +310,92 @@ impl<E: Extension> QRead<PropName<E>> for PropName<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
xml.open(DAV_URN, "prop").await?;
let acc = xml.collect::<PropertyRequest<E>>().await?;
xml.tag_stop(DAV_URN, "prop").await?;
xml.close().await?;
Ok(PropName(acc))
}
}
impl<E: Extension> QRead<PropertyRequest<E>> for PropertyRequest<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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)
}
}
impl<E: Extension> QRead<Property<E>> for Property<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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?;
// 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())?))
},
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?;
} 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::<u64>()?;
xml.close().await?;
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?;
} 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())?))
},
b"lockdiscovery" => {
xml.next().await?;
} else if xml.maybe_open(DAV_URN, "lockdiscovery").await?.is_some() {
let acc = xml.collect::<ActiveLock>().await?;
xml.tag_stop(DAV_URN, "lockdiscovery").await?;
xml.close().await?;
return Ok(Property::LockDiscovery(acc))
},
b"resourcetype" => {
xml.next().await?;
} else if xml.maybe_open(DAV_URN, "resourcetype").await?.is_some() {
let acc = xml.collect::<ResourceType<E>>().await?;
xml.tag_stop(DAV_URN, "resourcetype").await?;
xml.close().await?;
return Ok(Property::ResourceType(acc))
},
b"supportedlock" => {
xml.next().await?;
} else if xml.maybe_open(DAV_URN, "supportedlock").await?.is_some() {
let acc = xml.collect::<LockEntry>().await?;
xml.tag_stop(DAV_URN, "supportedlock").await?;
xml.close().await?;
return Ok(Property::SupportedLock(acc))
},
_ => (),
};
}
// Option 2: an extension property, delegating
@ -471,7 +427,7 @@ impl QRead<ActiveLock> 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<Depth> for Depth {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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<Owner> for Owner {
_ => { xml.skip().await?; },
}
};
xml.tag_stop(DAV_URN, "owner").await?;
xml.close().await?;
Ok(owner)
}
}
@ -536,7 +492,7 @@ impl QRead<Timeout> for Timeout {
},
};
xml.tag_stop(DAV_URN, "timeout").await?;
xml.close().await?;
Ok(timeout)
}
}
@ -545,7 +501,7 @@ impl QRead<LockToken> for LockToken {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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<LockRoot> for LockRoot {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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<E: Extension> QRead<ResourceType<E>> for ResourceType<E> {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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<LockEntry> 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<LockScope> 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?;
if xml.maybe_open(DAV_URN, "exclusive").await?.is_some() {
xml.close().await?;
break LockScope::Exclusive
},
Event::Empty(_) if xml.is_tag(DAV_URN, "shared") => {
xml.next().await?;
} else if xml.maybe_open(DAV_URN, "shared").await?.is_some() {
xml.close().await?;
break LockScope::Shared
}
_ => xml.skip().await?,
};
xml.skip().await?;
};
xml.tag_stop(DAV_URN, "lockscope").await?;
xml.close().await?;
Ok(lockscope)
}
}
@ -624,15 +577,15 @@ impl QRead<LockType> 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?;
if xml.maybe_open(DAV_URN, "write").await?.is_some() {
xml.close().await?;
break LockType::Write
}
_ => xml.skip().await?,
xml.skip().await?;
};
};
xml.tag_stop(DAV_URN, "locktype").await?;
xml.close().await?;
Ok(locktype)
}
}
@ -641,7 +594,7 @@ impl QRead<Href> for Href {
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
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#"
<?xml version="1.0" encoding="utf-8" ?>
<D:multistatus xmlns:D="DAV:">
<D:response>
<D:href>/container/</D:href>
<D:propstat>
<D:prop xmlns:R="http://ns.example.com/boxschema/">
<R:bigbox><R:BoxType>Box type A</R:BoxType></R:bigbox>
<R:author><R:Name>Hadrian</R:Name></R:author>
<D:creationdate>1997-12-01T17:42:21-08:00</D:creationdate>
<D:displayname>Example collection</D:displayname>
<D:resourcetype><D:collection/></D:resourcetype>
<D:supportedlock>
<D:lockentry>
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
</D:lockentry>
<D:lockentry>
<D:lockscope><D:shared/></D:lockscope>
<D:locktype><D:write/></D:locktype>
</D:lockentry>
</D:supportedlock>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
<D:response>
<D:href>/container/front.html</D:href>
<D:propstat>
<D:prop xmlns:R="http://ns.example.com/boxschema/">
<R:bigbox><R:BoxType>Box type B</R:BoxType>
</R:bigbox>
<D:creationdate>1997-12-01T18:27:21-08:00</D:creationdate>
<D:displayname>Example HTML resource</D:displayname>
<D:getcontentlength>4525</D:getcontentlength>
<D:getcontenttype>text/html</D:getcontenttype>
<D:getetag>"zzyzx"</D:getetag>
<D:getlastmodified
>Mon, 12 Jan 1998 09:25:56 GMT</D:getlastmodified>
<D:resourcetype/>
<D:supportedlock>
<D:lockentry>
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
</D:lockentry>
<D:lockentry>
<D:lockscope><D:shared/></D:lockscope>
<D:locktype><D:write/></D:locktype>
</D:lockentry>
</D:supportedlock>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
</D:multistatus>"#;
let mut rdr = Reader::new(NsReader::from_reader(src.as_bytes())).await.unwrap();
let got = rdr.find::<Multistatus::<Core, PropValue<Core>>>().await.unwrap();
}
}

View file

@ -53,25 +53,33 @@ impl<T: IWrite> Writer<T> {
/// Transform an XML stream of characters into a Rust object
pub struct Reader<T: IRead> {
pub rdr: NsReader<T>,
evt: Event<'static>,
cur: Event<'static>,
parents: Vec<Event<'static>>,
buf: Vec<u8>,
}
impl<T: IRead> Reader<T> {
pub async fn new(mut rdr: NsReader<T>) -> Result<Self, ParsingError> {
let mut buf: Vec<u8> = 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<Event<'static>, 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<Event<'static>, 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<T: IRead> Reader<T> {
}
}
/// read one more tag
pub async fn next(&mut self) -> Result<Event<'static>, 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<T: IRead> Reader<T> {
}
}
/*
* Disabled
/// maybe find start tag
pub async fn maybe_tag_start(&mut self, ns: &[u8], key: &str) -> Result<Option<Event<'static>>, 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)
}
fn ensure_parent_has_child(&self) -> Result<(), ParsingError> {
match self.parent_has_child() {
true => Ok(()),
false => Err(ParsingError::Recoverable),
}
}
/// find start tag
pub 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
pub async fn tag_stop(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, 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<String, ParsingError> {
self.ensure_parent_has_child()?;
let mut acc = String::new();
loop {
match self.peek() {
@ -165,8 +146,11 @@ impl<T: IRead> Reader<T> {
}
}
// NEW API
pub async fn maybe_read<N: Node<N>>(&mut self, t: &mut Option<N>, 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<T: IRead> Reader<T> {
}
pub async fn maybe_push<N: Node<N>>(&mut self, t: &mut Vec<N>, 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<T: IRead> Reader<T> {
}
pub async fn find<N: Node<N>>(&mut self) -> Result<N, ParsingError> {
self.ensure_parent_has_child()?;
loop {
// Try parse
match N::qread(self).await {
@ -204,6 +194,8 @@ impl<T: IRead> Reader<T> {
}
pub async fn maybe_find<N: Node<N>>(&mut self) -> Result<Option<N>, ParsingError> {
self.ensure_parent_has_child()?;
loop {
// Try parse
match N::qread(self).await {
@ -219,7 +211,9 @@ impl<T: IRead> Reader<T> {
}
pub async fn collect<N: Node<N>>(&mut self) -> Result<Vec<N>, 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<T: IRead> Reader<T> {
}
pub async fn open(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
if self.is_tag(ns, key) {
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<Option<Event<'static>>, 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<Event<'static>, ParsingError> {
println!("close tag {:?}", self.parents.last());
// Handle the empty case
if !self.parent_has_child() {
self.parents.pop();
return self.next().await
}
return Err(ParsingError::Recoverable);
// Handle the start/end case
loop {
match self.peek() {
Event::End(_) => {
self.parents.pop();
return self.next().await
},
_ => self.skip().await?,
};
}
}
}