use serde::{de, ser};
use std::fmt::Display;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Message {0}")]
Message(String),
#[error("IO {0}")]
Io(std::io::Error),
#[error("Invalid format code")]
InvalidFormatCode,
#[error("Invalid value")]
InvalidValue,
#[error("Expecting non-described constructor")]
IsDescribedType,
#[error("Invalid UTF-8 encoding")]
InvalidUtf8Encoding,
#[error("Sequence length mismatch")]
SequenceLengthMismatch,
#[error("Invalid length")]
InvalidLength,
}
impl Error {
pub(crate) fn too_long() -> Self {
let io_err = std::io::Error::new(std::io::ErrorKind::Other, "Too long");
Self::Io(io_err)
}
pub(crate) fn unexpected_eof(
error: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> Self {
let io_err = std::io::Error::new(std::io::ErrorKind::UnexpectedEof, error);
Self::Io(io_err)
}
}
impl ser::Error for Error {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
Self::Message(msg.to_string())
}
}
impl de::Error for Error {
fn custom<T>(msg: T) -> Self
where
T: Display,
{
Self::Message(msg.to_string())
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::Io(err)
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(_: std::string::FromUtf8Error) -> Self {
Error::InvalidUtf8Encoding
}
}
impl From<std::str::Utf8Error> for Error {
fn from(_: std::str::Utf8Error) -> Self {
Error::InvalidUtf8Encoding
}
}