use core::fmt;
use forma_core::error::{DeError, SerError};
use std::io;
#[derive(Debug)]
pub enum Error {
Message(String),
Io(io::Error),
Eof,
Syntax(String, usize, usize),
ExpectedColon(usize, usize),
ExpectedCommaOrEnd(usize, usize),
TrailingData,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Message(msg) => f.write_str(msg),
Error::Io(e) => write!(f, "I/O error: {e}"),
Error::Eof => f.write_str("unexpected end of input"),
Error::Syntax(msg, line, col) => write!(f, "{msg} at line {line} column {col}"),
Error::ExpectedColon(line, col) => {
write!(f, "expected `:` at line {line} column {col}")
}
Error::ExpectedCommaOrEnd(line, col) => {
write!(f, "expected `,` or closing bracket at line {line} column {col}")
}
Error::TrailingData => f.write_str("trailing data after JSON value"),
}
}
}
impl std::error::Error for Error {}
impl SerError for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl DeError for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}