xmpp-rs/src/error.rs

65 lines
2 KiB
Rust
Raw Normal View History

//! Provides an error type for this crate.
2017-02-19 20:46:44 +01:00
use std::convert::From;
/// Our main error type.
2019-09-05 20:06:17 +02:00
#[derive(Debug)]
pub enum Error {
/// An error from quick_xml.
2019-09-05 20:06:17 +02:00
XmlError(::quick_xml::Error),
/// An UTF-8 conversion error.
2019-09-05 20:06:17 +02:00
Utf8Error(::std::str::Utf8Error),
/// An I/O error, from std::io.
2019-09-05 20:06:17 +02:00
IoError(::std::io::Error),
/// An error which is returned when the end of the document was reached prematurely.
EndOfDocument,
/// An error which is returned when an element is closed when it shouldn't be
InvalidElementClosed,
/// An error which is returned when an elemet's name contains more than one colon
InvalidElement,
/// An error which is returned when a comment is to be parsed by minidom
#[cfg(not(comments))]
CommentsDisabled,
}
2019-09-05 20:06:17 +02:00
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::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
Error::IoError(e) => write!(fmt, "IO error: {}", e),
Error::EndOfDocument => write!(fmt, "the end of the document has been reached prematurely"),
Error::InvalidElementClosed => write!(fmt, "the XML is invalid, an element was wrongly closed"),
Error::InvalidElement => write!(fmt, "the XML element is invalid"),
#[cfg(not(comments))]
Error::CommentsDisabled => write!(fmt, "a comment has been found even though comments are disabled by feature"),
}
}
}
impl From<::quick_xml::Error> for Error {
fn from(err: ::quick_xml::Error) -> Error {
Error::XmlError(err)
}
}
impl From<::std::str::Utf8Error> for Error {
fn from(err: ::std::str::Utf8Error) -> Error {
Error::Utf8Error(err)
2017-02-19 20:46:44 +01:00
}
}
2017-02-19 20:46:44 +01:00
impl From<::std::io::Error> for Error {
fn from(err: ::std::io::Error) -> Error {
Error::IoError(err)
2017-02-19 20:46:44 +01:00
}
}
/// Our simplified Result type.
pub type Result<T> = ::std::result::Result<T, Error>;