html-cat 0.1.0

HTML5 parser: tokenizer + tree builder producing a Document tree of Element/Text/Comment nodes. No mut, no Rc/Arc, no interior mutability, no panics, exhaustive matches. First sub-crate of a Servo-replacement webview runtime targeting Tauri.
//! Parser error type.
//!
//! HTML parsing is intentionally permissive: tokenization and tree
//! construction recover from most malformed input without surfacing a
//! fatal error.  The variants here cover cases where recovery is not
//! defined (e.g. invalid UTF-8 in the input).

use crate::span::Span;

/// All errors `html-cat` can produce.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// An attribute name failed validation (e.g. contained a NUL).
    InvalidAttributeName {
        /// Where the name appeared.
        at: Span,
        /// The offending name.
        name: String,
    },
    /// A doctype declaration was malformed beyond recovery.
    InvalidDoctype {
        /// Where the doctype appeared.
        at: Span,
    },
    /// An entity reference was malformed (unterminated, unknown name,
    /// invalid numeric).  The tokenizer keeps the literal `&` in the
    /// output as recovery; this variant is reserved for the strict-mode
    /// caller who wants the error surfaced.
    InvalidEntity {
        /// Where the entity reference appeared.
        at: Span,
        /// The text between `&` and `;` (or end of input).
        text: String,
    },
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidAttributeName { at, name } => {
                write!(f, "{at}: invalid attribute name {name:?}")
            }
            Self::InvalidDoctype { at } => write!(f, "{at}: invalid doctype"),
            Self::InvalidEntity { at, text } => {
                write!(f, "{at}: invalid entity reference &{text}")
            }
        }
    }
}

impl std::error::Error for Error {}