use thiserror::Error;
pub type ParseResult<T> = std::result::Result<T, ParseError>;
#[derive(Error, Debug, Clone)]
pub enum ParseError {
#[error("Parse error: {file}:{line}:{column}: {message}")]
SyntaxError {
file: String,
line: usize,
column: usize,
message: String,
},
#[error("Pest parse error: {0}")]
PestError(String),
#[error("IO error: {0}")]
IoError(String),
#[error("Multiple parse errors: {} errors", .errors.len())]
MultipleErrors { errors: Vec<ParseErrorInfo> },
}
#[derive(Debug, Clone, PartialEq)]
pub struct ParseErrorInfo {
pub file: String,
pub line: usize,
pub column: usize,
pub message: String,
}
impl ParseError {
pub fn syntax_error(
file: impl Into<String>,
line: usize,
column: usize,
message: impl Into<String>,
) -> Self {
ParseError::SyntaxError {
file: file.into(),
line,
column,
message: message.into(),
}
}
pub fn pest_error(message: impl Into<String>) -> Self {
ParseError::PestError(message.into())
}
pub fn io_error(message: impl Into<String>) -> Self {
ParseError::IoError(message.into())
}
}
impl From<std::io::Error> for ParseError {
fn from(err: std::io::Error) -> Self {
ParseError::IoError(err.to_string())
}
}