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:
Jonas Schäfer 2024-06-16 10:14:18 +02:00
parent 14a1d66bf8
commit cb3da52ba2
7 changed files with 131 additions and 0 deletions

View file

@ -17,3 +17,4 @@ sasl = { path = "sasl" }
tokio-xmpp = { path = "tokio-xmpp" } tokio-xmpp = { path = "tokio-xmpp" }
xmpp-parsers = { path = "parsers" } xmpp-parsers = { path = "parsers" }
xmpp = { path = "xmpp" } xmpp = { path = "xmpp" }
xso = { path = "xso" }

View file

@ -24,6 +24,7 @@ chrono = { version = "0.4.5", default-features = false, features = ["std"] }
# same repository dependencies # same repository dependencies
jid = { version = "0.10", features = ["minidom"], path = "../jid" } jid = { version = "0.10", features = ["minidom"], path = "../jid" }
minidom = { version = "0.15", path = "../minidom" } minidom = { version = "0.15", path = "../minidom" }
xso = { version = "0.0.2" }
[features] [features]
# Build xmpp-parsers to make components instead of clients. # Build xmpp-parsers to make components instead of clients.

View file

@ -131,3 +131,18 @@ impl From<chrono::ParseError> for Error {
Error::ChronoParseError(err) Error::ChronoParseError(err)
} }
} }
impl From<Error> for xso::error::Error {
fn from(other: Error) -> Self {
match other {
Error::ParseError(e) => Self::Other(e.to_string().into()),
Error::TypeMismatch { .. } => Self::TypeMismatch,
Error::Base64Error(e) => Self::TextParseError(Box::new(e)),
Error::ParseIntError(e) => Self::TextParseError(Box::new(e)),
Error::ParseStringError(e) => Self::TextParseError(Box::new(e)),
Error::ParseAddrError(e) => Self::TextParseError(Box::new(e)),
Error::JidParseError(e) => Self::TextParseError(Box::new(e)),
Error::ChronoParseError(e) => Self::TextParseError(Box::new(e)),
}
}
}

View file

@ -676,6 +676,23 @@ macro_rules! generate_element {
)* )*
} }
impl ::xso::FromXml for $elem {
type Builder = ::xso::minidom_compat::FromEventsViaElement<$elem>;
fn from_events(
qname: ::xso::exports::rxml::QName,
attrs: ::xso::exports::rxml::AttrMap,
) -> Result<Self::Builder, ::xso::error::FromEventsError> {
if qname.0 != crate::ns::$ns || qname.1 != $name {
return Err(::xso::error::FromEventsError::Mismatch {
name: qname,
attrs,
})
}
Self::Builder::new(qname, attrs)
}
}
impl ::std::convert::TryFrom<crate::Element> for $elem { impl ::std::convert::TryFrom<crate::Element> for $elem {
type Error = crate::util::error::Error; type Error = crate::util::error::Error;
@ -748,6 +765,14 @@ macro_rules! generate_element {
builder.build() builder.build()
} }
} }
impl ::xso::IntoXml for $elem {
type EventIter = ::xso::minidom_compat::IntoEventsViaElement;
fn into_event_iter(self) -> Result<Self::EventIter, ::xso::error::Error> {
Self::EventIter::new(self)
}
}
); );
} }

View file

@ -21,6 +21,9 @@ pub enum Error {
/// Attempt to parse text data failed with the provided nested error. /// Attempt to parse text data failed with the provided nested error.
TextParseError(Box<dyn std::error::Error + Send + Sync + 'static>), 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. /// An element header did not match an expected element.
/// ///
/// This is only rarely generated: most of the time, a mismatch of 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::XmlError(ref e) => write!(f, "xml parse error: {}", e),
Self::TextParseError(ref e) => write!(f, "text 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::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 /// Error returned from
/// [`FromXml::from_events`][`crate::FromXml::from_events`]. /// [`FromXml::from_events`][`crate::FromXml::from_events`].
#[derive(Debug)] #[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 { impl fmt::Display for FromEventsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {

View file

@ -20,6 +20,11 @@ use of this library in parsing XML streams like specified in RFC 6120.
pub mod error; pub mod error;
pub mod minidom_compat; pub mod minidom_compat;
#[doc(hidden)]
pub mod exports {
pub use rxml;
}
/// Trait allowing to consume a struct and iterate its contents as /// Trait allowing to consume a struct and iterate its contents as
/// serialisable [`rxml::Event`] items. /// serialisable [`rxml::Event`] items.
pub trait IntoXml { pub trait IntoXml {

View file

@ -4,6 +4,7 @@
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::marker::PhantomData;
use std::vec::IntoIter; use std::vec::IntoIter;
use minidom::{Element, Node}; 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;