diff --git a/minidom/Cargo.toml b/minidom/Cargo.toml index b8bf7d68..c142ba63 100644 --- a/minidom/Cargo.toml +++ b/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"] } diff --git a/minidom/src/element.rs b/minidom/src/element.rs index 9d690631..61fb43cb 100644 --- a/minidom/src/element.rs +++ b/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( item: Item<'_>, enc: &mut Encoder, 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 CustomItemWriter { } impl CustomItemWriter { - 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) } } diff --git a/minidom/src/error.rs b/minidom/src/error.rs index 694c766c..b0265c62 100644 --- a/minidom/src/error.rs +++ b/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 for Error { + fn from(other: io::Error) -> Self { + match other.downcast::() { + 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 for Error { } } -impl From for Error { - fn from(err: rxml::error::XmlError) -> Error { - Error::XmlError(err.into()) - } -} - impl From for Error { fn from(err: rxml::strings::Error) -> Error { - rxml::error::XmlError::from(err).into() + rxml::error::Error::from(err).into() } } diff --git a/minidom/src/tests.rs b/minidom/src/tests.rs index 160fdd30..c7d79346 100644 --- a/minidom/src/tests.rs +++ b/minidom/src/tests.rs @@ -431,16 +431,12 @@ fn fail_comments() { #[test] fn xml_error() { match "".parse::() { - 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 "() { - 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), } } diff --git a/tokio-xmpp/Cargo.toml b/tokio-xmpp/Cargo.toml index 467e48c6..bfe46656 100644 --- a/tokio-xmpp/Cargo.toml +++ b/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 diff --git a/tokio-xmpp/src/proto/xmpp_codec.rs b/tokio-xmpp/src/proto/xmpp_codec.rs index fd56a6d2..db028e7b 100644 --- a/tokio-xmpp/src/proto/xmpp_codec.rs +++ b/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; diff --git a/xso/Cargo.toml b/xso/Cargo.toml index 7f763c36..17083b25 100644 --- a/xso/Cargo.toml +++ b/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 } diff --git a/xso/src/error.rs b/xso/src/error.rs index 609d08eb..8b06a2e5 100644 --- a/xso/src/error.rs +++ b/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 for Error { - fn from(other: rxml::error::XmlError) -> Error { +impl From for Error { + fn from(other: rxml::Error) -> Error { Error::XmlError(other) } } diff --git a/xso/src/lib.rs b/xso/src/lib.rs index 6bd1c04c..34422d01 100644 --- a/xso/src/lib.rs +++ b/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(from: F) -> Result 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( unreachable!("minidom::Element did not produce enough events to complete element") } -fn map_nonio_error(r: Result) -> Result { +fn map_nonio_error(r: Result) -> Result { 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::() { + Ok(e) => Err(e.into()), + Err(_) => unreachable!("I/O error cannot be caused by &[]"), + }, } } @@ -393,9 +391,9 @@ fn read_start_event( } } } - 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(mut buf: &[u8]) -> Result { 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.