use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParseError {
pub position: usize,
pub kind: ErrorKind,
}
impl ParseError {
pub(crate) fn new(position: usize, kind: ErrorKind) -> Self {
ParseError { position, kind }
}
pub fn message(&self) -> &'static str {
self.kind.message()
}
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ErrorKind {
UnexpectedEof,
UnterminatedComment,
UnterminatedCdata,
UnterminatedTag,
StrayEndTag,
MismatchedEndTag,
ImplicitlyClosed,
UnknownEntity,
BogusComment,
}
impl ErrorKind {
pub(crate) fn message(&self) -> &'static str {
match self {
ErrorKind::UnexpectedEof => "unexpected end of input",
ErrorKind::UnterminatedComment => "unterminated comment",
ErrorKind::UnterminatedCdata => "unterminated CDATA section",
ErrorKind::UnterminatedTag => "unterminated tag",
ErrorKind::StrayEndTag => "stray end tag with no matching open element",
ErrorKind::MismatchedEndTag => "end tag closed mis-nested elements",
ErrorKind::ImplicitlyClosed => "element implicitly closed",
ErrorKind::UnknownEntity => "unknown character reference left verbatim",
ErrorKind::BogusComment => "bogus comment",
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} at byte {}", self.kind.message(), self.position)
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseError {}