use std::fmt::{self, Display};
use crate::{position::Position, range::Range};
#[derive(Debug, PartialEq, Clone)]
pub enum AsonError {
Message(String),
UnexpectedEndOfDocument(String),
MessageWithPosition(String, Position),
MessageWithRange(String, Range),
}
impl Display for AsonError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AsonError::Message(msg) => f.write_str(msg),
AsonError::UnexpectedEndOfDocument(detail) => {
writeln!(f, "Unexpected to reach the end of document.")?;
write!(f, "{}", detail)
}
AsonError::MessageWithPosition(detail, position) => {
writeln!(
f,
"Error at line: {} column: {}",
position.line + 1,
position.column + 1
)?;
write!(f, "{}", detail)
}
AsonError::MessageWithRange(detail, range) => {
writeln!(
f,
"Error from line: {} column: {}, to line: {} column: {}",
range.start.line + 1,
range.start.column + 1,
range.end_included.line + 1,
range.end_included.column + 1
)?;
write!(f, "{}", detail)
}
}
}
}
impl std::error::Error for AsonError {}
impl serde::de::Error for AsonError {
fn custom<T: Display>(msg: T) -> Self {
AsonError::Message(msg.to_string())
}
}
impl serde::ser::Error for AsonError {
fn custom<T: Display>(msg: T) -> Self {
AsonError::Message(msg.to_string())
}
}