Cargo.toml π
@@ -17,3 +17,4 @@ sasl = { path = "sasl" }
tokio-xmpp = { path = "tokio-xmpp" }
xmpp-parsers = { path = "parsers" }
xmpp = { path = "xmpp" }
+xso = { path = "xso" }
Jonas SchΓ€fer created
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.
Cargo.toml | 1
parsers/Cargo.toml | 1
parsers/src/util/error.rs | 15 ++++++++
parsers/src/util/macros.rs | 25 ++++++++++++++
xso/src/error.rs | 16 +++++++++
xso/src/lib.rs | 5 ++
xso/src/minidom_compat.rs | 68 ++++++++++++++++++++++++++++++++++++++++
7 files changed, 131 insertions(+)
@@ -17,3 +17,4 @@ sasl = { path = "sasl" }
tokio-xmpp = { path = "tokio-xmpp" }
xmpp-parsers = { path = "parsers" }
xmpp = { path = "xmpp" }
+xso = { path = "xso" }
@@ -24,6 +24,7 @@ chrono = { version = "0.4.5", default-features = false, features = ["std"] }
# same repository dependencies
jid = { version = "0.10", features = ["minidom"], path = "../jid" }
minidom = { version = "0.15", path = "../minidom" }
+xso = { version = "0.0.2" }
[features]
# Build xmpp-parsers to make components instead of clients.
@@ -131,3 +131,18 @@ impl From<chrono::ParseError> for Error {
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)),
+ }
+ }
+}
@@ -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 {
type Error = crate::util::error::Error;
@@ -748,6 +765,14 @@ macro_rules! generate_element {
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)
+ }
+ }
);
}
@@ -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::*;