use core::fmt;
use forma_core::error::{DeError, SerError};
#[derive(Debug)]
pub enum Error {
Message(String),
Parse(sif::Error),
NoSchema,
UnknownField(String),
TypeMismatch { field: String, expected: String, got: String },
FieldCount { expected: usize, got: usize },
Eof,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Message(msg) => f.write_str(msg),
Error::Parse(e) => write!(f, "SIF parse error: {e}"),
Error::NoSchema => f.write_str("no schema found in SIF section"),
Error::UnknownField(name) => write!(f, "unknown field `{name}` in SIF schema"),
Error::TypeMismatch { field, expected, got } => {
write!(f, "field `{field}`: expected {expected}, got {got}")
}
Error::FieldCount { expected, got } => {
write!(f, "expected {expected} fields, got {got}")
}
Error::Eof => f.write_str("no more records"),
}
}
}
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<sif::Error> for Error {
fn from(e: sif::Error) -> Self {
Error::Parse(e)
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Message(e.to_string())
}
}