Skip to main content

mdwright_document/
error.rs

1use std::fmt;
2
3/// Markdown source could not be parsed safely.
4///
5/// `CommonMark` accepts every byte string after mdwright's source
6/// canonicalisation, but the parser implementation can still fail on
7/// malformed edge cases. `ParseError` keeps that dependency failure at
8/// the document boundary instead of exposing parser internals.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct ParseError {
11    input_len: usize,
12}
13
14impl ParseError {
15    pub(crate) fn parser_panic(input_len: usize) -> Self {
16        Self { input_len }
17    }
18
19    /// Length in bytes of the canonical parser input.
20    #[must_use]
21    pub fn input_len(&self) -> usize {
22        self.input_len
23    }
24}
25
26impl fmt::Display for ParseError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(f, "Markdown parser failed while reading {} byte(s)", self.input_len)
29    }
30}
31
32impl std::error::Error for ParseError {}