use displaydoc::Display;
use std::{fmt, io};
pub type Result<T> = ::core::result::Result<T, Error>;
#[derive(Debug, Display)]
pub enum Error {
WriteSerialized(io::Error),
ReadSerialized(io::Error),
UnexpectedEof,
Unexpected {
expected: char,
actual: char,
},
ExpectedDigit {
actual: char,
},
Utf8Error(std::str::Utf8Error),
CharConversionFailed(std::char::CharTryFromError),
NotAValidNumber(Box<dyn std::error::Error + Send + Sync>),
InvalidBooleanValue(char),
UnsupportedArrayKeyType(char),
InvalidTypeIndicator(char),
MissingFeature(&'static str),
IndexMismatch {
expected: usize,
actual: usize,
},
LengthRequired,
SerializationFailed(String),
DeserializationFailed(String),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::WriteSerialized(ref err) => Some(err),
Error::ReadSerialized(ref err) => Some(err),
Error::Utf8Error(ref err) => Some(err),
Error::CharConversionFailed(ref err) => Some(err),
Error::NotAValidNumber(ref err) => Some(err.as_ref()),
_ => None,
}
}
}
impl serde::ser::Error for Error {
#[inline]
fn custom<T>(msg: T) -> Self
where
T: fmt::Display,
{
Error::SerializationFailed(msg.to_string())
}
}
impl serde::de::Error for Error {
#[inline]
fn custom<T>(msg: T) -> Self
where
T: fmt::Display,
{
Error::DeserializationFailed(msg.to_string())
}
}