use anomaly::{format_err, BoxError, Context};
use serde::{de, ser};
use std::{fmt, io};
use thiserror::Error;
pub type Error = crate::Error<ErrorKind>;
#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)]
pub enum ErrorKind {
#[error("I/O error")]
Io,
#[error("parse error")]
Parse,
#[error("unexpected end of buffer")]
UnexpectedEof,
}
impl ErrorKind {
pub fn context(self, source: impl Into<BoxError>) -> Context<ErrorKind> {
Context::new(self, Some(source.into()))
}
}
impl ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
format_err!(ErrorKind::Parse, msg.to_string()).into()
}
}
impl de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
format_err!(ErrorKind::Parse, msg.to_string()).into()
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
ErrorKind::Io.context(err).into()
}
}