1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub struct ParseError {
9 pub msg: String,
11 pub line: u32,
13 pub col: u32,
16 pub pos: usize,
18 pub hint: Option<String>,
20}
21
22impl ParseError {
23 pub fn new(msg: impl Into<String>, line: u32, col: u32, pos: usize) -> Self {
24 Self {
25 msg: msg.into(),
26 line,
27 col,
28 pos,
29 hint: None,
30 }
31 }
32
33 pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
34 self.hint = Some(hint.into());
35 self
36 }
37}
38
39impl fmt::Display for ParseError {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 write!(
42 f,
43 "parse error at line {}, col {}: {}",
44 self.line, self.col, self.msg
45 )?;
46 if let Some(hint) = &self.hint {
47 write!(f, " (hint: {})", hint)?;
48 }
49 Ok(())
50 }
51}
52
53impl std::error::Error for ParseError {}
54
55pub type Result<T> = std::result::Result<T, ParseError>;