Refactor decoder
This commit is contained in:
parent
db115ca247
commit
2d14587d83
2 changed files with 307 additions and 261 deletions
|
@ -25,36 +25,32 @@ use super::xml::{Node, QRead, Reader, IRead, DAV_URN, CAL_URN};
|
||||||
/// Propfind request
|
/// Propfind request
|
||||||
impl<E: Extension> QRead<PropFind<E>> for PropFind<E> {
|
impl<E: Extension> QRead<PropFind<E>> for PropFind<E> {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
// Find propfind
|
|
||||||
xml.open(DAV_URN, "propfind").await?;
|
xml.open(DAV_URN, "propfind").await?;
|
||||||
|
|
||||||
// Find any tag
|
|
||||||
let propfind: PropFind<E> = loop {
|
let propfind: PropFind<E> = loop {
|
||||||
match xml.peek() {
|
// allprop
|
||||||
Event::Start(_) if xml.is_tag(DAV_URN, "allprop") => {
|
if let Some(_) = xml.maybe_open(DAV_URN, "allprop").await? {
|
||||||
xml.open(DAV_URN, "allprop").await?;
|
|
||||||
let includ = xml.maybe_find::<Include<E>>().await?;
|
let includ = xml.maybe_find::<Include<E>>().await?;
|
||||||
let r = PropFind::AllProp(includ);
|
xml.close().await?;
|
||||||
xml.tag_stop(DAV_URN, "allprop").await?;
|
break PropFind::AllProp(includ)
|
||||||
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?; },
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// Close tag
|
// propname
|
||||||
xml.tag_stop(DAV_URN, "propfind").await?;
|
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)
|
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> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "propertyupdate").await?;
|
xml.open(DAV_URN, "propertyupdate").await?;
|
||||||
let collected_items = xml.collect::<PropertyUpdateItem<E>>().await?;
|
let collected_items = xml.collect::<PropertyUpdateItem<E>>().await?;
|
||||||
xml.tag_stop(DAV_URN, "propertyupdate").await?;
|
xml.close().await?;
|
||||||
Ok(PropertyUpdate(collected_items))
|
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 })
|
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) {
|
match (m_scope, m_type) {
|
||||||
(Some(lockscope), Some(locktype)) => Ok(LockInfo { lockscope, locktype, owner }),
|
(Some(lockscope), Some(locktype)) => Ok(LockInfo { lockscope, locktype, owner }),
|
||||||
_ => Err(ParsingError::MissingChild),
|
_ => 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> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "prop").await?;
|
xml.open(DAV_URN, "prop").await?;
|
||||||
let mut acc = xml.collect::<Property<E>>().await?;
|
let mut acc = xml.collect::<Property<E>>().await?;
|
||||||
xml.tag_stop(DAV_URN, "prop").await?;
|
xml.close().await?;
|
||||||
Ok(PropValue(acc))
|
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> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "error").await?;
|
xml.open(DAV_URN, "error").await?;
|
||||||
let violations = xml.collect::<Violation<E>>().await?;
|
let violations = xml.collect::<Violation<E>>().await?;
|
||||||
xml.tag_stop(DAV_URN, "error").await?;
|
xml.close().await?;
|
||||||
Ok(Error(violations))
|
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[..]) {
|
match (status, &propstat[..], &href[..]) {
|
||||||
(Some(status), &[], &[_, ..]) => Ok(Response {
|
(Some(status), &[], &[_, ..]) => Ok(Response {
|
||||||
status_or_propstat: StatusOrPropstat::Status(href, status),
|
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) {
|
match (m_prop, m_status) {
|
||||||
(Some(prop), Some(status)) => Ok(PropStat { prop, status, error, responsedescription }),
|
(Some(prop), Some(status)) => Ok(PropStat { prop, status, error, responsedescription }),
|
||||||
_ => Err(ParsingError::MissingChild),
|
_ => Err(ParsingError::MissingChild),
|
||||||
|
@ -219,7 +215,7 @@ impl QRead<Status> for Status {
|
||||||
let fullcode = xml.tag_string().await?;
|
let fullcode = xml.tag_string().await?;
|
||||||
let txtcode = fullcode.splitn(3, ' ').nth(1).ok_or(ParsingError::InvalidValue)?;
|
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))?;
|
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))
|
Ok(Status(code))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -228,7 +224,7 @@ impl QRead<ResponseDescription> for ResponseDescription {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "responsedescription").await?;
|
xml.open(DAV_URN, "responsedescription").await?;
|
||||||
let cnt = xml.tag_string().await?;
|
let cnt = xml.tag_string().await?;
|
||||||
xml.tag_stop(DAV_URN, "responsedescription").await?;
|
xml.close().await?;
|
||||||
Ok(ResponseDescription(cnt))
|
Ok(ResponseDescription(cnt))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,7 +233,7 @@ impl QRead<Location> for Location {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "location").await?;
|
xml.open(DAV_URN, "location").await?;
|
||||||
let href = xml.find::<Href>().await?;
|
let href = xml.find::<Href>().await?;
|
||||||
xml.tag_stop(DAV_URN, "location").await?;
|
xml.close().await?;
|
||||||
Ok(Location(href))
|
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> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "remove").await?;
|
xml.open(DAV_URN, "remove").await?;
|
||||||
let propname = xml.find::<PropName<E>>().await?;
|
let propname = xml.find::<PropName<E>>().await?;
|
||||||
xml.tag_stop(DAV_URN, "remove").await?;
|
xml.close().await?;
|
||||||
Ok(Remove(propname))
|
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> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "set").await?;
|
xml.open(DAV_URN, "set").await?;
|
||||||
let propvalue = xml.find::<PropValue<E>>().await?;
|
let propvalue = xml.find::<PropValue<E>>().await?;
|
||||||
xml.tag_stop(DAV_URN, "set").await?;
|
xml.close().await?;
|
||||||
Ok(Set(propvalue))
|
Ok(Set(propvalue))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Extension> QRead<Violation<E>> for Violation<E> {
|
impl<E: Extension> QRead<Violation<E>> for Violation<E> {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
let bs = match xml.peek() {
|
if xml.maybe_open(DAV_URN, "lock-token-matches-request-uri").await?.is_some() {
|
||||||
Event::Start(b) | Event::Empty(b) => b,
|
xml.close().await?;
|
||||||
_ => return Err(ParsingError::Recoverable),
|
Ok(Violation::LockTokenMatchesRequestUri)
|
||||||
};
|
} else if xml.maybe_open(DAV_URN, "lock-token-submitted").await?.is_some() {
|
||||||
|
|
||||||
// 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::<Href>().await?;
|
let links = xml.collect::<Href>().await?;
|
||||||
xml.tag_stop(DAV_URN, "lock-token-submitted").await?;
|
xml.close().await?;
|
||||||
return Ok(Violation::LockTokenSubmitted(links))
|
Ok(Violation::LockTokenSubmitted(links))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "no-conflicting-lock").await?.is_some() {
|
||||||
b"no-conflicting-lock" => {
|
|
||||||
// start tag
|
|
||||||
xml.next().await?;
|
|
||||||
let links = xml.collect::<Href>().await?;
|
let links = xml.collect::<Href>().await?;
|
||||||
xml.tag_stop(DAV_URN, "no-conflicting-lock").await?;
|
xml.close().await?;
|
||||||
return Ok(Violation::NoConflictingLock(links))
|
Ok(Violation::NoConflictingLock(links))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "no-external-entities").await?.is_some() {
|
||||||
b"no-external-entities" => {
|
xml.close().await?;
|
||||||
xml.next().await?;
|
Ok(Violation::NoExternalEntities)
|
||||||
return Ok(Violation::NoExternalEntities)
|
} else if xml.maybe_open(DAV_URN, "preserved-live-properties").await?.is_some() {
|
||||||
},
|
xml.close().await?;
|
||||||
b"preserved-live-properties" => {
|
Ok(Violation::PreservedLiveProperties)
|
||||||
xml.next().await?;
|
} else if xml.maybe_open(DAV_URN, "propfind-finite-depth").await?.is_some() {
|
||||||
return Ok(Violation::PreservedLiveProperties)
|
xml.close().await?;
|
||||||
},
|
Ok(Violation::PropfindFiniteDepth)
|
||||||
b"propfind-finite-depth" => {
|
} else if xml.maybe_open(DAV_URN, "cannot-modify-protected-property").await?.is_some() {
|
||||||
xml.next().await?;
|
xml.close().await?;
|
||||||
return Ok(Violation::PropfindFiniteDepth)
|
Ok(Violation::CannotModifyProtectedProperty)
|
||||||
},
|
} else {
|
||||||
b"cannot-modify-protected-property" => {
|
|
||||||
xml.next().await?;
|
|
||||||
return Ok(Violation::CannotModifyProtectedProperty)
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Option 2: an extension property, delegating
|
|
||||||
E::Error::qread(xml).await.map(Violation::Extension)
|
E::Error::qread(xml).await.map(Violation::Extension)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Extension> QRead<Include<E>> for Include<E> {
|
impl<E: Extension> QRead<Include<E>> for Include<E> {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "include").await?;
|
xml.open(DAV_URN, "include").await?;
|
||||||
let acc = xml.collect::<PropertyRequest<E>>().await?;
|
let acc = xml.collect::<PropertyRequest<E>>().await?;
|
||||||
xml.tag_stop(DAV_URN, "include").await?;
|
xml.close().await?;
|
||||||
Ok(Include(acc))
|
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> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "prop").await?;
|
xml.open(DAV_URN, "prop").await?;
|
||||||
let acc = xml.collect::<PropertyRequest<E>>().await?;
|
let acc = xml.collect::<PropertyRequest<E>>().await?;
|
||||||
xml.tag_stop(DAV_URN, "prop").await?;
|
xml.close().await?;
|
||||||
Ok(PropName(acc))
|
Ok(PropName(acc))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Extension> QRead<PropertyRequest<E>> for PropertyRequest<E> {
|
impl<E: Extension> QRead<PropertyRequest<E>> for PropertyRequest<E> {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
let bs = match xml.peek() {
|
let maybe = if xml.maybe_open(DAV_URN, "creationdate").await?.is_some() {
|
||||||
Event::Start(b) | Event::Empty(b) => b,
|
Some(PropertyRequest::CreationDate)
|
||||||
_ => return Err(ParsingError::Recoverable),
|
} 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
|
match maybe {
|
||||||
let (ns, loc) = xml.rdr.resolve_element(bs.name());
|
Some(pr) => {
|
||||||
if matches!(ns, Bound(Namespace(ns)) if ns == DAV_URN) {
|
xml.close().await?;
|
||||||
let maybe_res = match loc.into_inner() {
|
Ok(pr)
|
||||||
b"creationdate" => Some(PropertyRequest::CreationDate),
|
},
|
||||||
b"displayname" => Some(PropertyRequest::DisplayName),
|
None => E::PropertyRequest::qread(xml).await.map(PropertyRequest::Extension),
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option 2: an extension property, delegating
|
|
||||||
E::PropertyRequest::qread(xml).await.map(PropertyRequest::Extension)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Extension> QRead<Property<E>> for Property<E> {
|
impl<E: Extension> QRead<Property<E>> for Property<E> {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
use chrono::{DateTime, FixedOffset, TimeZone};
|
use chrono::{DateTime, FixedOffset, TimeZone};
|
||||||
|
|
||||||
let bs = match xml.peek() {
|
// Core WebDAV properties
|
||||||
Event::Start(b) | Event::Empty(b) => b,
|
if xml.maybe_open(DAV_URN, "creationdate").await?.is_some() {
|
||||||
_ => 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?;
|
let datestr = xml.tag_string().await?;
|
||||||
|
xml.close().await?;
|
||||||
return Ok(Property::CreationDate(DateTime::parse_from_rfc3339(datestr.as_str())?))
|
return Ok(Property::CreationDate(DateTime::parse_from_rfc3339(datestr.as_str())?))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "displayname").await?.is_some() {
|
||||||
b"displayname" => {
|
let name = xml.tag_string().await?;
|
||||||
xml.next().await?;
|
xml.close().await?;
|
||||||
return Ok(Property::DisplayName(xml.tag_string().await?))
|
return Ok(Property::DisplayName(name))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "getcontentlanguage").await?.is_some() {
|
||||||
b"getcontentlanguage" => {
|
let lang = xml.tag_string().await?;
|
||||||
xml.next().await?;
|
xml.close().await?;
|
||||||
return Ok(Property::GetContentLanguage(xml.tag_string().await?))
|
return Ok(Property::GetContentLanguage(lang))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "getcontentlength").await?.is_some() {
|
||||||
b"getcontentlength" => {
|
|
||||||
xml.next().await?;
|
|
||||||
let cl = xml.tag_string().await?.parse::<u64>()?;
|
let cl = xml.tag_string().await?.parse::<u64>()?;
|
||||||
|
xml.close().await?;
|
||||||
return Ok(Property::GetContentLength(cl))
|
return Ok(Property::GetContentLength(cl))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "getcontenttype").await?.is_some() {
|
||||||
b"getcontenttype" => {
|
let ct = xml.tag_string().await?;
|
||||||
xml.next().await?;
|
xml.close().await?;
|
||||||
return Ok(Property::GetContentType(xml.tag_string().await?))
|
return Ok(Property::GetContentType(ct))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "getetag").await?.is_some() {
|
||||||
b"getetag" => {
|
let etag = xml.tag_string().await?;
|
||||||
xml.next().await?;
|
xml.close().await?;
|
||||||
return Ok(Property::GetEtag(xml.tag_string().await?))
|
return Ok(Property::GetEtag(etag))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "getlastmodified").await?.is_some() {
|
||||||
b"getlastmodified" => {
|
|
||||||
xml.next().await?;
|
|
||||||
let datestr = xml.tag_string().await?;
|
let datestr = xml.tag_string().await?;
|
||||||
|
xml.close().await?;
|
||||||
return Ok(Property::CreationDate(DateTime::parse_from_rfc2822(datestr.as_str())?))
|
return Ok(Property::CreationDate(DateTime::parse_from_rfc2822(datestr.as_str())?))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "lockdiscovery").await?.is_some() {
|
||||||
b"lockdiscovery" => {
|
|
||||||
xml.next().await?;
|
|
||||||
let acc = xml.collect::<ActiveLock>().await?;
|
let acc = xml.collect::<ActiveLock>().await?;
|
||||||
xml.tag_stop(DAV_URN, "lockdiscovery").await?;
|
xml.close().await?;
|
||||||
return Ok(Property::LockDiscovery(acc))
|
return Ok(Property::LockDiscovery(acc))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "resourcetype").await?.is_some() {
|
||||||
b"resourcetype" => {
|
|
||||||
xml.next().await?;
|
|
||||||
let acc = xml.collect::<ResourceType<E>>().await?;
|
let acc = xml.collect::<ResourceType<E>>().await?;
|
||||||
xml.tag_stop(DAV_URN, "resourcetype").await?;
|
xml.close().await?;
|
||||||
return Ok(Property::ResourceType(acc))
|
return Ok(Property::ResourceType(acc))
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "supportedlock").await?.is_some() {
|
||||||
b"supportedlock" => {
|
|
||||||
xml.next().await?;
|
|
||||||
let acc = xml.collect::<LockEntry>().await?;
|
let acc = xml.collect::<LockEntry>().await?;
|
||||||
xml.tag_stop(DAV_URN, "supportedlock").await?;
|
xml.close().await?;
|
||||||
return Ok(Property::SupportedLock(acc))
|
return Ok(Property::SupportedLock(acc))
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option 2: an extension property, delegating
|
// 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) {
|
match (m_scope, m_type, m_depth, m_root) {
|
||||||
(Some(lockscope), Some(locktype), Some(depth), Some(lockroot)) =>
|
(Some(lockscope), Some(locktype), Some(depth), Some(lockroot)) =>
|
||||||
Ok(ActiveLock { lockscope, locktype, depth, owner, timeout, locktoken, 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> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "depth").await?;
|
xml.open(DAV_URN, "depth").await?;
|
||||||
let depth_str = xml.tag_string().await?;
|
let depth_str = xml.tag_string().await?;
|
||||||
xml.tag_stop(DAV_URN, "depth").await?;
|
xml.close().await?;
|
||||||
match depth_str.as_str() {
|
match depth_str.as_str() {
|
||||||
"0" => Ok(Depth::Zero),
|
"0" => Ok(Depth::Zero),
|
||||||
"1" => Ok(Depth::One),
|
"1" => Ok(Depth::One),
|
||||||
|
@ -518,7 +474,7 @@ impl QRead<Owner> for Owner {
|
||||||
_ => { xml.skip().await?; },
|
_ => { xml.skip().await?; },
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xml.tag_stop(DAV_URN, "owner").await?;
|
xml.close().await?;
|
||||||
Ok(owner)
|
Ok(owner)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -536,7 +492,7 @@ impl QRead<Timeout> for Timeout {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
xml.tag_stop(DAV_URN, "timeout").await?;
|
xml.close().await?;
|
||||||
Ok(timeout)
|
Ok(timeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -545,7 +501,7 @@ impl QRead<LockToken> for LockToken {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "locktoken").await?;
|
xml.open(DAV_URN, "locktoken").await?;
|
||||||
let href = Href::qread(xml).await?;
|
let href = Href::qread(xml).await?;
|
||||||
xml.tag_stop(DAV_URN, "locktoken").await?;
|
xml.close().await?;
|
||||||
Ok(LockToken(href))
|
Ok(LockToken(href))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -554,20 +510,19 @@ impl QRead<LockRoot> for LockRoot {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "lockroot").await?;
|
xml.open(DAV_URN, "lockroot").await?;
|
||||||
let href = Href::qread(xml).await?;
|
let href = Href::qread(xml).await?;
|
||||||
xml.tag_stop(DAV_URN, "lockroot").await?;
|
xml.close().await?;
|
||||||
Ok(LockRoot(href))
|
Ok(LockRoot(href))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Extension> QRead<ResourceType<E>> for ResourceType<E> {
|
impl<E: Extension> QRead<ResourceType<E>> for ResourceType<E> {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
match xml.peek() {
|
if xml.maybe_open(DAV_URN, "collection").await?.is_some() {
|
||||||
Event::Empty(b) if xml.is_tag(DAV_URN, "collection") => {
|
xml.close().await?;
|
||||||
xml.next().await?;
|
return Ok(ResourceType::Collection)
|
||||||
Ok(ResourceType::Collection)
|
|
||||||
},
|
|
||||||
_ => E::ResourceType::qread(xml).await.map(ResourceType::Extension),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
match (maybe_scope, maybe_type) {
|
||||||
(Some(lockscope), Some(locktype)) => Ok(LockEntry { lockscope, locktype }),
|
(Some(lockscope), Some(locktype)) => Ok(LockEntry { lockscope, locktype }),
|
||||||
_ => Err(ParsingError::MissingChild),
|
_ => Err(ParsingError::MissingChild),
|
||||||
|
@ -601,20 +556,18 @@ impl QRead<LockScope> for LockScope {
|
||||||
xml.open(DAV_URN, "lockscope").await?;
|
xml.open(DAV_URN, "lockscope").await?;
|
||||||
|
|
||||||
let lockscope = loop {
|
let lockscope = loop {
|
||||||
match xml.peek() {
|
if xml.maybe_open(DAV_URN, "exclusive").await?.is_some() {
|
||||||
Event::Empty(_) if xml.is_tag(DAV_URN, "exclusive") => {
|
xml.close().await?;
|
||||||
xml.next().await?;
|
|
||||||
break LockScope::Exclusive
|
break LockScope::Exclusive
|
||||||
},
|
} else if xml.maybe_open(DAV_URN, "shared").await?.is_some() {
|
||||||
Event::Empty(_) if xml.is_tag(DAV_URN, "shared") => {
|
xml.close().await?;
|
||||||
xml.next().await?;
|
|
||||||
break LockScope::Shared
|
break LockScope::Shared
|
||||||
}
|
}
|
||||||
_ => xml.skip().await?,
|
|
||||||
};
|
xml.skip().await?;
|
||||||
};
|
};
|
||||||
|
|
||||||
xml.tag_stop(DAV_URN, "lockscope").await?;
|
xml.close().await?;
|
||||||
Ok(lockscope)
|
Ok(lockscope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -624,15 +577,15 @@ impl QRead<LockType> for LockType {
|
||||||
xml.open(DAV_URN, "locktype").await?;
|
xml.open(DAV_URN, "locktype").await?;
|
||||||
|
|
||||||
let locktype = loop {
|
let locktype = loop {
|
||||||
match xml.peek() {
|
if xml.maybe_open(DAV_URN, "write").await?.is_some() {
|
||||||
Event::Empty(b) if xml.is_tag(DAV_URN, "write") => {
|
xml.close().await?;
|
||||||
xml.next().await?;
|
|
||||||
break LockType::Write
|
break LockType::Write
|
||||||
}
|
}
|
||||||
_ => xml.skip().await?,
|
|
||||||
|
xml.skip().await?;
|
||||||
};
|
};
|
||||||
};
|
|
||||||
xml.tag_stop(DAV_URN, "locktype").await?;
|
xml.close().await?;
|
||||||
Ok(locktype)
|
Ok(locktype)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -641,7 +594,7 @@ impl QRead<Href> for Href {
|
||||||
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
async fn qread(xml: &mut Reader<impl IRead>) -> Result<Self, ParsingError> {
|
||||||
xml.open(DAV_URN, "href").await?;
|
xml.open(DAV_URN, "href").await?;
|
||||||
let mut url = xml.tag_string().await?;
|
let mut url = xml.tag_string().await?;
|
||||||
xml.tag_stop(DAV_URN, "href").await?;
|
xml.close().await?;
|
||||||
Ok(Href(url))
|
Ok(Href(url))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -859,4 +812,68 @@ mod tests {
|
||||||
responsedescription: None,
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
133
src/dav/xml.rs
133
src/dav/xml.rs
|
@ -53,25 +53,33 @@ impl<T: IWrite> Writer<T> {
|
||||||
/// Transform an XML stream of characters into a Rust object
|
/// Transform an XML stream of characters into a Rust object
|
||||||
pub struct Reader<T: IRead> {
|
pub struct Reader<T: IRead> {
|
||||||
pub rdr: NsReader<T>,
|
pub rdr: NsReader<T>,
|
||||||
evt: Event<'static>,
|
cur: Event<'static>,
|
||||||
|
parents: Vec<Event<'static>>,
|
||||||
buf: Vec<u8>,
|
buf: Vec<u8>,
|
||||||
}
|
}
|
||||||
impl<T: IRead> Reader<T> {
|
impl<T: IRead> Reader<T> {
|
||||||
pub async fn new(mut rdr: NsReader<T>) -> Result<Self, ParsingError> {
|
pub 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 cur = rdr.read_event_into_async(&mut buf).await?.into_owned();
|
||||||
|
let parents = vec![];
|
||||||
buf.clear();
|
buf.clear();
|
||||||
Ok(Self { evt, rdr, buf })
|
Ok(Self { cur, parents, rdr, buf })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn peek(&self) -> &Event<'static> {
|
/// read one more tag
|
||||||
&self.evt
|
/// 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> {
|
pub async fn skip(&mut self) -> Result<Event<'static>, ParsingError> {
|
||||||
println!("skip on {:?}", &self.evt);
|
println!("skipping inside node {:?}", self.parents.last());
|
||||||
match &self.evt {
|
match &self.cur {
|
||||||
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
|
||||||
|
@ -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
|
/// 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() {
|
let qname = match self.peek() {
|
||||||
Event::Start(bs) | Event::Empty(bs) => bs.name(),
|
Event::Start(bs) | Event::Empty(bs) => bs.name(),
|
||||||
Event::End(be) => be.name(),
|
Event::End(be) => be.name(),
|
||||||
|
@ -111,43 +110,25 @@ impl<T: IRead> Reader<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
fn parent_has_child(&self) -> bool {
|
||||||
* Disabled
|
matches!(self.parents.last(), Some(Event::Start(_)) | None)
|
||||||
/// 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);
|
fn ensure_parent_has_child(&self) -> Result<(), ParsingError> {
|
||||||
let peek = self.peek();
|
match self.parent_has_child() {
|
||||||
match peek {
|
true => Ok(()),
|
||||||
Event::Start(_) | Event::Empty(_) if self.is_tag(ns, key) => Ok(Some(self.next().await?)),
|
false => Err(ParsingError::Recoverable),
|
||||||
_ => Ok(None),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// find start tag
|
pub fn peek(&self) -> &Event<'static> {
|
||||||
pub async fn tag_start(&mut self, ns: &[u8], key: &str) -> Result<Event<'static>, ParsingError> {
|
&self.cur
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NEW API
|
||||||
pub async fn tag_string(&mut self) -> Result<String, ParsingError> {
|
pub async fn tag_string(&mut self) -> Result<String, ParsingError> {
|
||||||
|
self.ensure_parent_has_child()?;
|
||||||
|
|
||||||
let mut acc = String::new();
|
let mut acc = String::new();
|
||||||
loop {
|
loop {
|
||||||
match self.peek() {
|
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> {
|
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 {
|
match N::qread(self).await {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
*t = Some(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> {
|
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 {
|
match N::qread(self).await {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
t.push(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> {
|
pub async fn find<N: Node<N>>(&mut self) -> Result<N, ParsingError> {
|
||||||
|
self.ensure_parent_has_child()?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Try parse
|
// Try parse
|
||||||
match N::qread(self).await {
|
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> {
|
pub async fn maybe_find<N: Node<N>>(&mut self) -> Result<Option<N>, ParsingError> {
|
||||||
|
self.ensure_parent_has_child()?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Try parse
|
// Try parse
|
||||||
match N::qread(self).await {
|
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> {
|
pub async fn collect<N: Node<N>>(&mut self) -> Result<Vec<N>, ParsingError> {
|
||||||
|
self.ensure_parent_has_child()?;
|
||||||
let mut acc = Vec::new();
|
let mut acc = Vec::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match N::qread(self).await {
|
match N::qread(self).await {
|
||||||
Err(ParsingError::Recoverable) => match self.peek() {
|
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> {
|
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 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?,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue