use core::{convert::Infallible, fmt};
use std::{
collections::TryReserveError,
string::{String, ToString},
};
pub use cbor4ii::core::error::Len;
use serde::{de, ser};
#[derive(Debug)]
pub enum EncodeError<E> {
Msg(String),
Write(E),
}
impl<E> From<E> for EncodeError<E> {
fn from(err: E) -> EncodeError<E> {
EncodeError::Write(err)
}
}
impl<E: core::error::Error + 'static> ser::Error for EncodeError<E> {
fn custom<T: fmt::Display>(msg: T) -> Self {
EncodeError::Msg(msg.to_string())
}
}
impl<E: core::error::Error + 'static> core::error::Error for EncodeError<E> {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
EncodeError::Msg(_) => None,
EncodeError::Write(err) => Some(err),
}
}
}
impl<E: fmt::Debug> fmt::Display for EncodeError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl<E: fmt::Debug> From<cbor4ii::core::error::EncodeError<E>> for EncodeError<E> {
fn from(err: cbor4ii::core::error::EncodeError<E>) -> EncodeError<E> {
match err {
cbor4ii::core::error::EncodeError::Write(e) => EncodeError::Write(e),
_ => EncodeError::Msg(err.to_string()),
}
}
}
#[derive(Debug)]
pub enum DecodeError<E> {
Msg(String),
Read(E),
Eof { name: &'static str, expect: Len },
Mismatch {
name: &'static str,
found: u8,
},
CastOverflow { name: &'static str },
Overflow {
name: &'static str,
},
RequireBorrowed {
name: &'static str,
},
RequireLength {
name: &'static str,
found: Len,
},
RequireUtf8 { name: &'static str },
Unsupported {
name: &'static str,
found: u8,
},
DepthOverflow { name: &'static str },
TrailingData,
IndefiniteSize,
}
impl<E> From<E> for DecodeError<E> {
fn from(err: E) -> DecodeError<E> {
DecodeError::Read(err)
}
}
impl<E: std::error::Error + 'static> de::Error for DecodeError<E> {
fn custom<T: fmt::Display>(msg: T) -> Self {
DecodeError::Msg(msg.to_string())
}
}
impl<E: core::error::Error + 'static> core::error::Error for DecodeError<E> {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
DecodeError::Msg(_) => None,
DecodeError::Read(err) => Some(err),
_ => None,
}
}
}
impl<E: fmt::Debug> fmt::Display for DecodeError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl<E: fmt::Debug> From<cbor4ii::core::error::DecodeError<E>> for DecodeError<E> {
fn from(err: cbor4ii::core::error::DecodeError<E>) -> DecodeError<E> {
use cbor4ii::core::error::DecodeError as IDecodeError;
match err {
IDecodeError::Read(read) => DecodeError::Read(read),
IDecodeError::Eof { name, expect } => DecodeError::Eof { name, expect },
IDecodeError::Mismatch { name, found } => DecodeError::Mismatch { name, found },
IDecodeError::CastOverflow { name } => DecodeError::CastOverflow { name },
IDecodeError::RequireBorrowed { name } => DecodeError::RequireBorrowed { name },
IDecodeError::RequireLength { name, found } => {
DecodeError::RequireLength { name, found }
}
IDecodeError::Unsupported { name, found } => DecodeError::Unsupported { name, found },
IDecodeError::DepthOverflow { name } => DecodeError::DepthOverflow { name },
IDecodeError::RequireUtf8 { name } => DecodeError::RequireUtf8 { name },
_ => DecodeError::Msg(err.to_string()),
}
}
}
#[derive(Debug)]
pub enum CodecError {
Decode(DecodeError<Infallible>),
Encode(EncodeError<TryReserveError>),
DecodeIo(DecodeError<std::io::Error>),
EncodeIo(EncodeError<std::io::Error>),
}
impl fmt::Display for CodecError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Decode(error) => write!(f, "decode error: {error}"),
Self::Encode(error) => write!(f, "encode error: {error}"),
Self::DecodeIo(error) => write!(f, "decode io error: {error}"),
Self::EncodeIo(error) => write!(f, "encode io error: {error}"),
}
}
}
impl std::error::Error for CodecError {}
impl From<DecodeError<Infallible>> for CodecError {
fn from(error: DecodeError<Infallible>) -> Self {
Self::Decode(error)
}
}
impl From<DecodeError<std::io::Error>> for CodecError {
fn from(error: DecodeError<std::io::Error>) -> Self {
Self::DecodeIo(error)
}
}
impl From<EncodeError<TryReserveError>> for CodecError {
fn from(error: EncodeError<TryReserveError>) -> Self {
Self::Encode(error)
}
}
impl From<EncodeError<std::io::Error>> for CodecError {
fn from(error: EncodeError<std::io::Error>) -> Self {
Self::EncodeIo(error)
}
}