1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ParseError {
8 pub msg: String,
10 pub line: u32,
12 pub col: u32,
15 pub pos: usize,
17 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
54pub type Result<T> = std::result::Result<T, ParseError>;