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

@ -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)
}
}

View file

@ -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()
}
}

View file

@ -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),
}
}