#[derive(Debug)]
pub enum Error {
Io(::std::io::Error),
Utf8(::std::str::Utf8Error),
UnexpectedEof(String),
EndEventMismatch {
expected: String,
found: String,
},
UnexpectedToken(String),
UnexpectedBang,
TextNotFound,
XmlDeclWithoutVersion(Option<String>),
NameWithQuote(usize),
NoEqAfterName(usize),
UnquotedValue(usize),
DuplicatedAttribute(usize, usize),
EscapeError(::escape::EscapeError),
}
impl From<::std::io::Error> for Error {
#[inline]
fn from(error: ::std::io::Error) -> Error {
Error::Io(error)
}
}
impl From<::std::str::Utf8Error> for Error {
#[inline]
fn from(error: ::std::str::Utf8Error) -> Error {
Error::Utf8(error)
}
}
pub type Result<T> = ::std::result::Result<T, Error>;
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::Io(e) => write!(f, "I/O error: {}", e),
Error::Utf8(e) => write!(f, "UTF8 error: {}", e),
Error::UnexpectedEof(e) => write!(f, "Unexpected EOF during reading {}.", e),
Error::EndEventMismatch { expected, found } => {
write!(f, "Expecting </{}> found </{}>", expected, found)
}
Error::UnexpectedToken(e) => write!(f, "Unexpected token '{}'", e),
Error::UnexpectedBang => write!(
f,
"Only Comment, CDATA and DOCTYPE nodes can start with a '!'"
),
Error::TextNotFound => write!(f, "Cannot read text, expecting Event::Text"),
Error::XmlDeclWithoutVersion(e) => write!(
f,
"XmlDecl must start with 'version' attribute, found {:?}",
e
),
Error::NameWithQuote(e) => write!(
f,
"error while parsing attribute at position {}: \
Attribute key cannot contain quote.",
e
),
Error::NoEqAfterName(e) => write!(
f,
"error while parsing attribute at position {}: \
Attribute key must be directly followed by = or space",
e
),
Error::UnquotedValue(e) => write!(
f,
"error while parsing attribute at position {}: \
Attribute value must start with a quote.",
e
),
Error::DuplicatedAttribute(pos1, pos2) => write!(
f,
"error while parsing attribute at position {0}: \
Duplicate attribute at position {1} and {0}",
pos1, pos2
),
Error::EscapeError(e) => write!(f, "{}", e),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
Error::Utf8(e) => Some(e),
Error::EscapeError(e) => Some(e),
_ => None,
}
}
}