Skip to main content

rdom_parser/
error.rs

1//! `ParseError` — human-friendly error reporting for malformed templates.
2
3use std::fmt;
4
5/// Parse error with position + optional hint.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ParseError {
8    /// Human-readable error message.
9    pub msg: String,
10    /// 1-indexed line number where the error was detected.
11    pub line: u32,
12    /// 1-indexed column number (in bytes, not graphemes — sufficient
13    /// for source-error purposes; human debugging uses the source text).
14    pub col: u32,
15    /// Byte offset into the input for tooling.
16    pub pos: usize,
17    /// Optional suggestion for how to fix.
18    pub hint: Option<String>,
19}
20
21impl ParseError {
22    pub fn new(msg: impl Into<String>, line: u32, col: u32, pos: usize) -> Self {
23        Self {
24            msg: msg.into(),
25            line,
26            col,
27            pos,
28            hint: None,
29        }
30    }
31
32    pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
33        self.hint = Some(hint.into());
34        self
35    }
36}
37
38impl fmt::Display for ParseError {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        write!(
41            f,
42            "parse error at line {}, col {}: {}",
43            self.line, self.col, self.msg
44        )?;
45        if let Some(hint) = &self.hint {
46            write!(f, " (hint: {})", hint)?;
47        }
48        Ok(())
49    }
50}
51
52impl std::error::Error for ParseError {}
53
54/// `Result` alias for parser operations.
55pub type Result<T> = std::result::Result<T, ParseError>;