use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
pub msg: String,
pub line: u32,
pub col: u32,
pub pos: usize,
pub hint: Option<String>,
}
impl ParseError {
pub fn new(msg: impl Into<String>, line: u32, col: u32, pos: usize) -> Self {
Self {
msg: msg.into(),
line,
col,
pos,
hint: None,
}
}
pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
self.hint = Some(hint.into());
self
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"parse error at line {}, col {}: {}",
self.line, self.col, self.msg
)?;
if let Some(hint) = &self.hint {
write!(f, " (hint: {})", hint)?;
}
Ok(())
}
}
impl std::error::Error for ParseError {}
pub type Result<T> = std::result::Result<T, ParseError>;