Update to rxml 0.12.0

This commit is contained in:
Jonas Schäfer 2024-08-09 14:48:40 +02:00
commit f77c21f0fc
9 changed files with 51 additions and 43 deletions

View file

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

View file

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

View file

@ -11,6 +11,8 @@
//! Provides an error type for this crate. //! Provides an error type for this crate.
use std::io;
use std::error::Error as StdError; use std::error::Error as StdError;
/// Our main error type. /// Our main error type.
@ -19,6 +21,14 @@ pub enum Error {
/// Error from rxml parsing or writing /// Error from rxml parsing or writing
XmlError(rxml::Error), 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. /// An error which is returned when the end of the document was reached prematurely.
EndOfDocument, EndOfDocument,
@ -37,6 +47,7 @@ impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> { fn cause(&self) -> Option<&dyn StdError> {
match self { match self {
Error::XmlError(e) => Some(e), Error::XmlError(e) => Some(e),
Error::Io(e) => Some(e),
Error::EndOfDocument => None, Error::EndOfDocument => None,
Error::InvalidPrefix => None, Error::InvalidPrefix => None,
Error::MissingNamespace => 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 { impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
Error::XmlError(e) => write!(fmt, "XML error: {}", e), Error::XmlError(e) => write!(fmt, "XML error: {}", e),
Error::Io(e) => write!(fmt, "I/O error: {}", e),
Error::EndOfDocument => { Error::EndOfDocument => {
write!(fmt, "the end of the document has been reached prematurely") 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 { impl From<rxml::strings::Error> for Error {
fn from(err: rxml::strings::Error) -> Error { fn from(err: rxml::strings::Error) -> Error {
rxml::error::XmlError::from(err).into() rxml::error::Error::from(err).into()
} }
} }

View file

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

View file

@ -19,7 +19,7 @@ tokio = { version = "1", features = ["net", "rt", "rt-multi-thread", "macros"] }
tokio-stream = { version = "0.1", features = [] } tokio-stream = { version = "0.1", features = [] }
tokio-util = { version = "0.7", features = ["codec"] } tokio-util = { version = "0.7", features = ["codec"] }
webpki-roots = { version = "0.26", optional = true } 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" rand = "0.8"
syntect = { version = "5", optional = true } syntect = { version = "5", optional = true }
# same repository dependencies # same repository dependencies

View file

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

View file

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

View file

@ -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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use core::fmt; use core::fmt;
use rxml::error::XmlError; use rxml::Error as XmlError;
/// Opaque string error. /// Opaque string error.
/// ///
@ -100,8 +100,8 @@ impl std::error::Error for Error {
} }
} }
impl From<rxml::error::XmlError> for Error { impl From<rxml::Error> for Error {
fn from(other: rxml::error::XmlError) -> Error { fn from(other: rxml::Error) -> Error {
Error::XmlError(other) Error::XmlError(other)
} }
} }

View file

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