use crate::pack_error;
use std::error;
use std::fmt::{self, Display};
use serde;
#[derive(Debug)]
pub enum SerError {
MustHaveLength,
PackError(pack_error::PackError),
Custom(String),
}
impl From<pack_error::PackError> for SerError {
fn from(err: pack_error::PackError) -> SerError {
SerError::PackError(err)
}
}
impl error::Error for SerError {
fn description(&self) -> &str {
use SerError::*;
match *self {
MustHaveLength => "must have lenght",
Custom(ref s) => s,
PackError(ref e) => e.description(),
}
}
fn cause(&self) -> Option<&dyn error::Error> {
use SerError::*;
match *self {
MustHaveLength => None,
PackError(ref s) => Some(s),
Custom(_) => None,
}
}
}
impl Display for SerError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
error::Error::description(self).fmt(f)
}
}
impl serde::ser::Error for SerError {
fn custom<T: Display>(msg: T) -> SerError {
SerError::Custom(msg.to_string())
}
}