use std::error::Error as StdError;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::io::Error as IoError;
use std::num::ParseIntError;
use std::str::ParseBoolError;
use std::sync::Arc;
use quick_xml::events::attributes::AttrError;
use quick_xml::Error as XmlError;
#[cfg(feature = "url-links")]
use url::ParseError as ParseUrlError;
#[derive(Debug)]
pub enum Error {
Xml(XmlError),
ParseInt(ParseIntError),
ParseBool(ParseBoolError),
MissingElement(&'static str, &'static str),
MissingAttribute(&'static str, &'static str),
DuplicateElement(&'static str, &'static str),
InvalidValue(&'static str, &'static str, InvalidValue),
UnexpectedRoot(String),
#[cfg(feature = "url-links")]
ParseUrl(ParseUrlError),
#[cfg(feature = "threading")]
DisconnectedChannel,
}
impl Error {
pub fn invalid_value<S: Into<String>>(
name: &'static str,
elem: &'static str,
value: S,
) -> Self {
Error::InvalidValue(name, elem, InvalidValue(value.into()))
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
use self::Error::*;
match self {
Xml(e) => e.fmt(f),
ParseInt(e) => write!(f, "parser error: {}", e),
ParseBool(e) => write!(f, "parser error: {}", e),
#[cfg(feature = "url-links")]
ParseUrl(e) => write!(f, "parser error: {}", e),
MissingElement(x, y) => write!(f, "missing element `{}` in `{}`", x, y),
MissingAttribute(x, y) => write!(f, "missing attribute `{}` in `{}`", x, y),
DuplicateElement(x, y) => write!(f, "duplicate element `{}` in `{}`", x, y),
InvalidValue(x, y, _) => write!(f, "invalid value for attribute `{}` in `{}`", x, y),
UnexpectedRoot(root) => write!(f, "unexpected root element `{}`", root),
#[cfg(feature = "threading")]
DisconnectedChannel => write!(f, "unexpected threading channel disconnection"),
}
}
}
impl From<IoError> for Error {
fn from(e: IoError) -> Self {
Self::from(XmlError::Io(Arc::new(e)))
}
}
impl From<ParseBoolError> for Error {
fn from(e: ParseBoolError) -> Self {
Error::ParseBool(e)
}
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Self {
Error::ParseInt(e)
}
}
#[cfg(feature = "url-links")]
impl From<ParseUrlError> for Error {
fn from(e: ParseUrlError) -> Self {
Error::ParseUrl(e)
}
}
impl From<AttrError> for Error {
fn from(e: AttrError) -> Self {
Error::Xml(e.into())
}
}
impl From<XmlError> for Error {
fn from(e: XmlError) -> Self {
Error::Xml(e)
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
use self::Error::*;
match self {
Xml(e) => Some(e),
ParseInt(e) => Some(e),
ParseBool(e) => Some(e),
InvalidValue(_, _, e) => Some(e),
#[cfg(feature = "url-links")]
ParseUrl(e) => Some(e),
_ => None,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct InvalidValue(pub String);
impl Display for InvalidValue {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "invalid value: {}", &self.0)
}
}
impl StdError for InvalidValue {}
impl<S: Into<String>> From<S> for InvalidValue {
fn from(s: S) -> Self {
InvalidValue(s.into())
}
}