parsers: add streamable parsing
This adds shims which provide FromXml and IntoXml implementations to *all* macro-generated types in `xmpp_parsers`. Mind that this does not cover all types in `xmpp_parsers`, but a good share of them. This is another first step toward real, fully streamed parsing.
This commit is contained in:
parent
14a1d66bf8
commit
cb3da52ba2
7 changed files with 131 additions and 0 deletions
|
|
@ -21,6 +21,9 @@ pub enum Error {
|
|||
/// Attempt to parse text data failed with the provided nested error.
|
||||
TextParseError(Box<dyn std::error::Error + Send + Sync + 'static>),
|
||||
|
||||
/// Generic, unspecified other error.
|
||||
Other(Box<str>),
|
||||
|
||||
/// An element header did not match an expected element.
|
||||
///
|
||||
/// This is only rarely generated: most of the time, a mismatch of element
|
||||
|
|
@ -35,6 +38,7 @@ impl fmt::Display for Error {
|
|||
Self::XmlError(ref e) => write!(f, "xml parse error: {}", e),
|
||||
Self::TextParseError(ref e) => write!(f, "text parse error: {}", e),
|
||||
Self::TypeMismatch => f.write_str("mismatch between expected and actual XML data"),
|
||||
Self::Other(msg) => f.write_str(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -61,6 +65,12 @@ impl From<rxml::strings::Error> for Error {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<std::convert::Infallible> for Error {
|
||||
fn from(other: std::convert::Infallible) -> Self {
|
||||
match other {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Error returned from
|
||||
/// [`FromXml::from_events`][`crate::FromXml::from_events`].
|
||||
#[derive(Debug)]
|
||||
|
|
@ -87,6 +97,12 @@ impl From<Error> for FromEventsError {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<std::convert::Infallible> for FromEventsError {
|
||||
fn from(other: std::convert::Infallible) -> Self {
|
||||
match other {}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FromEventsError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,11 @@ use of this library in parsing XML streams like specified in RFC 6120.
|
|||
pub mod error;
|
||||
pub mod minidom_compat;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod exports {
|
||||
pub use rxml;
|
||||
}
|
||||
|
||||
/// Trait allowing to consume a struct and iterate its contents as
|
||||
/// serialisable [`rxml::Event`] items.
|
||||
pub trait IntoXml {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
use std::marker::PhantomData;
|
||||
use std::vec::IntoIter;
|
||||
|
||||
use minidom::{Element, Node};
|
||||
|
|
@ -235,6 +236,73 @@ impl FromXml for Element {
|
|||
}
|
||||
}
|
||||
|
||||
/// Helper struct to streamingly parse a struct which implements conversion
|
||||
/// from [`minidom::Element`].
|
||||
pub struct FromEventsViaElement<T> {
|
||||
inner: ElementFromEvents,
|
||||
// needed here because we need to keep the type `T` around until
|
||||
// `FromEventsBuilder` is done and it must always be the same type, so we
|
||||
// have to nail it down in the struct's type, and to do that we need to
|
||||
// bind it to a field. that's what PhantomData is for.
|
||||
_phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<E, T: TryFrom<minidom::Element, Error = E>> FromEventsViaElement<T>
|
||||
where
|
||||
Error: From<E>,
|
||||
{
|
||||
/// Create a new streaming parser for `T`.
|
||||
pub fn new(qname: rxml::QName, attrs: rxml::AttrMap) -> Result<Self, FromEventsError> {
|
||||
Ok(Self {
|
||||
_phantom: PhantomData,
|
||||
inner: Element::from_events(qname, attrs)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, T: TryFrom<minidom::Element, Error = E>> FromEventsBuilder for FromEventsViaElement<T>
|
||||
where
|
||||
Error: From<E>,
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn feed(&mut self, ev: Event) -> Result<Option<Self::Output>, Error> {
|
||||
match self.inner.feed(ev) {
|
||||
Ok(Some(v)) => Ok(Some(v.try_into()?)),
|
||||
Ok(None) => Ok(None),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper struct to stream a struct which implements conversion
|
||||
/// to [`minidom::Element`].
|
||||
pub struct IntoEventsViaElement {
|
||||
inner: IntoEvents,
|
||||
}
|
||||
|
||||
impl IntoEventsViaElement {
|
||||
/// Create a new streaming parser for `T`.
|
||||
pub fn new<E, T>(value: T) -> Result<Self, crate::error::Error>
|
||||
where
|
||||
Error: From<E>,
|
||||
minidom::Element: TryFrom<T, Error = E>,
|
||||
{
|
||||
let element: minidom::Element = value.try_into()?;
|
||||
Ok(Self {
|
||||
inner: element.into_event_iter()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for IntoEventsViaElement {
|
||||
type Item = Result<Event, Error>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.inner.next()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
Loading…
Reference in a new issue