Update to rxml 0.12.0

Jonas SchΓ€fer created

Change summary

minidom/Cargo.toml                 |  2 +-
minidom/src/element.rs             |  9 ++++-----
minidom/src/error.rs               | 29 ++++++++++++++++++++++-------
minidom/src/tests.rs               |  8 ++------
tokio-xmpp/Cargo.toml              |  2 +-
tokio-xmpp/src/proto/xmpp_codec.rs |  6 ++++--
xso/Cargo.toml                     |  2 +-
xso/src/error.rs                   |  6 +++---
xso/src/lib.rs                     | 30 +++++++++++++-----------------
9 files changed, 51 insertions(+), 43 deletions(-)

Detailed changes

minidom/Cargo.toml πŸ”—

@@ -21,4 +21,4 @@ edition = "2021"
 gitlab = { repository = "xmpp-rs/xmpp-rs" }
 
 [dependencies]
-rxml = { version = "0.11.1", default-features = false, features = ["compact_str"] }
+rxml = { version = "0.12.0", default-features = false, features = ["compact_str"] }

minidom/src/element.rs πŸ”—

@@ -19,10 +19,9 @@ use crate::node::Node;
 use crate::prefixes::{Namespace, Prefix, Prefixes};
 use crate::tree_builder::TreeBuilder;
 
-use std::collections::{btree_map, BTreeMap};
-use std::io::{BufRead, Write};
-
 use std::borrow::Cow;
+use std::collections::{btree_map, BTreeMap};
+use std::io::{self, BufRead, Write};
 use std::str;
 
 use rxml::writer::{Encoder, Item, TrackNamespace};
@@ -36,7 +35,7 @@ fn encode_and_write<W: Write, T: rxml::writer::TrackNamespace>(
     item: Item<'_>,
     enc: &mut Encoder<T>,
     mut w: W,
-) -> rxml::Result<()> {
+) -> io::Result<()> {
     let mut buf = rxml::bytes::BytesMut::new();
     enc.encode_into_bytes(item, &mut buf)
         .expect("encoder driven incorrectly");
@@ -61,7 +60,7 @@ impl<W: Write> CustomItemWriter<W, rxml::writer::SimpleNamespaces> {
 }
 
 impl<W: Write, T: rxml::writer::TrackNamespace> CustomItemWriter<W, T> {
-    pub(crate) fn write(&mut self, item: Item<'_>) -> rxml::Result<()> {
+    pub(crate) fn write(&mut self, item: Item<'_>) -> io::Result<()> {
         encode_and_write(item, &mut self.encoder, &mut self.writer)
     }
 }

minidom/src/error.rs πŸ”—

@@ -11,6 +11,8 @@
 
 //! Provides an error type for this crate.
 
+use std::io;
+
 use std::error::Error as StdError;
 
 /// Our main error type.
@@ -19,6 +21,14 @@ pub enum Error {
     /// Error from rxml parsing or writing
     XmlError(rxml::Error),
 
+    /// I/O error from accessing the source or destination.
+    ///
+    /// Even though the [`rxml`] crate emits its errors through
+    /// [`std::io::Error`] when using it with [`BufRead`][`std::io::BufRead`],
+    /// any rxml errors will still be reported through the
+    /// [`XmlError`][`Self::XmlError`] variant.
+    Io(io::Error),
+
     /// An error which is returned when the end of the document was reached prematurely.
     EndOfDocument,
 
@@ -37,6 +47,7 @@ impl StdError for Error {
     fn cause(&self) -> Option<&dyn StdError> {
         match self {
             Error::XmlError(e) => Some(e),
+            Error::Io(e) => Some(e),
             Error::EndOfDocument => None,
             Error::InvalidPrefix => None,
             Error::MissingNamespace => None,
@@ -45,10 +56,20 @@ impl StdError for Error {
     }
 }
 
+impl From<io::Error> for Error {
+    fn from(other: io::Error) -> Self {
+        match other.downcast::<rxml::Error>() {
+            Ok(e) => Self::XmlError(e),
+            Err(e) => Self::Io(e),
+        }
+    }
+}
+
 impl std::fmt::Display for Error {
     fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
         match self {
             Error::XmlError(e) => write!(fmt, "XML error: {}", e),
+            Error::Io(e) => write!(fmt, "I/O error: {}", e),
             Error::EndOfDocument => {
                 write!(fmt, "the end of the document has been reached prematurely")
             }
@@ -65,15 +86,9 @@ impl From<rxml::Error> for Error {
     }
 }
 
-impl From<rxml::error::XmlError> for Error {
-    fn from(err: rxml::error::XmlError) -> Error {
-        Error::XmlError(err.into())
-    }
-}
-
 impl From<rxml::strings::Error> for Error {
     fn from(err: rxml::strings::Error) -> Error {
-        rxml::error::XmlError::from(err).into()
+        rxml::error::Error::from(err).into()
     }
 }
 

minidom/src/tests.rs πŸ”—

@@ -431,16 +431,12 @@ fn fail_comments() {
 #[test]
 fn xml_error() {
     match "<a xmlns='ns1'></b>".parse::<Element>() {
-        Err(crate::error::Error::XmlError(rxml::Error::Xml(
-            rxml::error::XmlError::ElementMismatch,
-        ))) => (),
+        Err(crate::error::Error::XmlError(rxml::Error::ElementMismatch)) => (),
         err => panic!("No or wrong error: {:?}", err),
     }
 
     match "<a xmlns='ns1'></".parse::<Element>() {
-        Err(crate::error::Error::XmlError(rxml::Error::Xml(
-            rxml::error::XmlError::InvalidEof(_),
-        ))) => (),
+        Err(crate::error::Error::XmlError(rxml::Error::InvalidEof(_))) => (),
         err => panic!("No or wrong error: {:?}", err),
     }
 }

tokio-xmpp/Cargo.toml πŸ”—

@@ -19,7 +19,7 @@ tokio = { version = "1", features = ["net", "rt", "rt-multi-thread", "macros"] }
 tokio-stream = { version = "0.1", features = [] }
 tokio-util = { version = "0.7", features = ["codec"] }
 webpki-roots = { version = "0.26", optional = true }
-rxml = { version = "0.11.1", features = ["compact_str"] }
+rxml = { version = "0.12.0", features = ["compact_str"] }
 rand = "0.8"
 syntect = { version = "5", optional = true }
 # same repository dependencies

tokio-xmpp/src/proto/xmpp_codec.rs πŸ”—

@@ -99,8 +99,10 @@ impl Decoder for XmppCodec {
             let token = match self.driver.parse_buf(buf, false) {
                 Ok(Some(token)) => token,
                 Ok(None) => break,
-                Err(rxml::Error::IO(e)) if e.kind() == std::io::ErrorKind::WouldBlock => break,
-                Err(e) => return Err(minidom::Error::from(e).into()),
+                Err(rxml::error::EndOrError::NeedMoreData) => break,
+                Err(rxml::error::EndOrError::Error(e)) => {
+                    return Err(minidom::Error::from(e).into())
+                }
             };
 
             let had_stream_root = self.stanza_builder.depth() > 0;

xso/Cargo.toml πŸ”—

@@ -10,7 +10,7 @@ categories = ["encoding"]
 license = "MPL-2.0"
 
 [dependencies]
-rxml = { version = "0.11.1", default-features = false }
+rxml = { version = "0.12.0", default-features = false }
 minidom = { version = "0.16", path = "../minidom" }
 xso_proc = { version = "0.1", path = "../xso-proc", optional = true }
 

xso/src/error.rs πŸ”—

@@ -11,7 +11,7 @@ This module contains the error types used throughout the `xso` crate.
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 use core::fmt;
 
-use rxml::error::XmlError;
+use rxml::Error as XmlError;
 
 /// Opaque string error.
 ///
@@ -100,8 +100,8 @@ impl std::error::Error for Error {
     }
 }
 
-impl From<rxml::error::XmlError> for Error {
-    fn from(other: rxml::error::XmlError) -> Error {
+impl From<rxml::Error> for Error {
+    fn from(other: rxml::Error) -> Error {
         Error::XmlError(other)
     }
 }

xso/src/lib.rs πŸ”—

@@ -21,6 +21,9 @@ use of this library in parsing XML streams like specified in RFC 6120.
 // 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::io;
+
 pub mod asxml;
 pub mod error;
 pub mod fromxml;
@@ -310,9 +313,7 @@ pub fn transform<T: FromXml, F: AsXml>(from: F) -> Result<T, self::error::Error>
             return Ok(v);
         }
     }
-    Err(self::error::Error::XmlError(
-        rxml::error::XmlError::InvalidEof("during transform"),
-    ))
+    Err(self::error::Error::XmlError(rxml::Error::InvalidEof(None)))
 }
 
 /// Attempt to convert a [`minidom::Element`] into a type implementing
@@ -366,16 +367,13 @@ pub fn try_from_element<T: FromXml>(
     unreachable!("minidom::Element did not produce enough events to complete element")
 }
 
-fn map_nonio_error<T>(r: Result<T, rxml::Error>) -> Result<T, self::error::Error> {
+fn map_nonio_error<T>(r: Result<T, io::Error>) -> Result<T, self::error::Error> {
     match r {
         Ok(v) => Ok(v),
-        Err(rxml::Error::IO(_)) => unreachable!(),
-        Err(rxml::Error::Xml(e)) => Err(e.into()),
-        Err(rxml::Error::InvalidUtf8Byte(_)) => Err(self::error::Error::Other("invalid utf-8")),
-        Err(rxml::Error::InvalidChar(_)) => {
-            Err(self::error::Error::Other("non-character encountered"))
-        }
-        Err(rxml::Error::RestrictedXml(_)) => Err(self::error::Error::Other("restricted xml")),
+        Err(e) => match e.downcast::<rxml::Error>() {
+            Ok(e) => Err(e.into()),
+            Err(_) => unreachable!("I/O error cannot be caused by &[]"),
+        },
     }
 }
 
@@ -393,9 +391,9 @@ fn read_start_event<I: std::io::BufRead>(
             }
         }
     }
-    Err(self::error::Error::XmlError(
-        rxml::error::XmlError::InvalidEof("before start of element"),
-    ))
+    Err(self::error::Error::XmlError(rxml::Error::InvalidEof(Some(
+        rxml::error::ErrorContext::DocumentBegin,
+    ))))
 }
 
 /// Attempt to parse a type implementing [`FromXml`] from a byte buffer
@@ -415,9 +413,7 @@ pub fn from_bytes<T: FromXml>(mut buf: &[u8]) -> Result<T, self::error::Error> {
             return Ok(v);
         }
     }
-    Err(self::error::Error::XmlError(
-        rxml::error::XmlError::InvalidEof("while parsing FromXml impl"),
-    ))
+    Err(self::error::Error::XmlError(rxml::Error::InvalidEof(None)))
 }
 
 /// Return true if the string contains exclusively XML whitespace.