use thiserror in tokio-xmpp and minidom

This commit is contained in:
lemonsh 2025-10-12 00:31:48 +02:00
commit de564a53b0
No known key found for this signature in database
4 changed files with 53 additions and 186 deletions

View file

@ -13,13 +13,14 @@
use std::io;
use core::{error::Error as StdError, fmt};
use thiserror::Error;
/// Our main error type.
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum Error {
/// Error from rxml parsing or writing
XmlError(rxml::Error),
#[error("XML error: {0}")]
XmlError(#[from] rxml::Error),
/// I/O error from accessing the source or destination.
///
@ -27,35 +28,27 @@ pub enum Error {
/// [`io::Error`] when using it with [`BufRead`][`io::BufRead`],
/// any rxml errors will still be reported through the
/// [`XmlError`][`Self::XmlError`] variant.
#[error("I/O error: {0}")]
Io(io::Error),
/// An error which is returned when the end of the document was reached prematurely.
#[error("the end of the document has been reached prematurely")]
EndOfDocument,
/// An error which is returned when an element being serialized doesn't contain a prefix
/// (be it None or Some(_)).
#[error("the prefix is invalid")]
InvalidPrefix,
/// An error which is returned when an element doesn't contain a namespace
#[error("the XML element is missing a namespace")]
MissingNamespace,
/// An error which is returned when a prefixed is defined twice
#[error("the prefix is already defined")]
DuplicatePrefix,
}
impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> {
match self {
Error::XmlError(e) => Some(e),
Error::Io(e) => Some(e),
Error::EndOfDocument
| Error::InvalidPrefix
| Error::MissingNamespace
| Error::DuplicatePrefix => None,
}
}
}
impl From<io::Error> for Error {
fn from(other: io::Error) -> Self {
match other.downcast::<rxml::Error>() {
@ -65,27 +58,6 @@ impl From<io::Error> for Error {
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> 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")
}
Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
Error::MissingNamespace => write!(fmt, "the XML element is missing a namespace",),
Error::DuplicatePrefix => write!(fmt, "the prefix is already defined"),
}
}
}
impl From<rxml::Error> for Error {
fn from(err: rxml::Error) -> Error {
Error::XmlError(err)
}
}
impl From<rxml::strings::Error> for Error {
fn from(err: rxml::strings::Error) -> Error {
rxml::error::Error::from(err).into()