use {
alloc::{
collections::TryReserveError,
string::{String, ToString},
},
core::{convert::Infallible, fmt, num::TryFromIntError},
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: fmt::Debug> ser::Error for EncodeError<E> {
fn custom<T: fmt::Display>(msg: T) -> Self {
EncodeError::Msg(msg.to_string())
}
}
impl<E: fmt::Debug> ser::StdError for EncodeError<E> {}
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::EncodeError<E>> for EncodeError<E> {
fn from(err: cbor4ii::EncodeError<E>) -> EncodeError<E> {
match err {
cbor4ii::EncodeError::Write(e) => EncodeError::Write(e),
_ => EncodeError::Msg(err.to_string()),
}
}
}
#[derive(Debug)]
pub enum DecodeError<E> {
Msg(String),
Read(E),
Eof,
Mismatch {
expect_major: u8,
byte: u8,
},
TypeMismatch {
name: &'static str,
byte: u8,
},
CastOverflow(TryFromIntError),
Overflow {
name: &'static str,
},
RequireBorrowed {
name: &'static str,
},
RequireLength {
name: &'static str,
expect: usize,
value: usize,
},
InvalidUtf8(core::str::Utf8Error),
Unsupported {
byte: u8,
},
DepthLimit,
TrailingData,
IndefiniteSize,
}
impl<E> From<E> for DecodeError<E> {
fn from(err: E) -> DecodeError<E> {
DecodeError::Read(err)
}
}
impl<E: fmt::Debug> de::Error for DecodeError<E> {
fn custom<T: fmt::Display>(msg: T) -> Self {
DecodeError::Msg(msg.to_string())
}
}
impl<E: fmt::Debug> ser::StdError for DecodeError<E> {}
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::DecodeError<E>> for DecodeError<E> {
fn from(err: cbor4ii::DecodeError<E>) -> DecodeError<E> {
match err {
cbor4ii::DecodeError::Read(read) => DecodeError::Read(read),
cbor4ii::DecodeError::Eof => DecodeError::Eof,
cbor4ii::DecodeError::Mismatch { expect_major, byte } => {
DecodeError::Mismatch { expect_major, byte }
}
cbor4ii::DecodeError::TypeMismatch { name, byte } => {
DecodeError::TypeMismatch { name, byte }
}
cbor4ii::DecodeError::CastOverflow(overflow) => {
DecodeError::CastOverflow(overflow)
}
cbor4ii::DecodeError::Overflow { name } => DecodeError::Overflow { name },
cbor4ii::DecodeError::RequireBorrowed { name } => {
DecodeError::RequireBorrowed { name }
}
cbor4ii::DecodeError::RequireLength {
name,
expect,
value,
} => DecodeError::RequireLength {
name,
expect,
value,
},
cbor4ii::DecodeError::InvalidUtf8(invalid) => {
DecodeError::InvalidUtf8(invalid)
}
cbor4ii::DecodeError::Unsupported { byte } => {
DecodeError::Unsupported { byte }
}
cbor4ii::DecodeError::DepthLimit => DecodeError::DepthLimit,
_ => DecodeError::Msg(err.to_string()),
}
}
}
#[derive(Debug, thiserror_core2::Error)]
pub enum CodecError {
#[error("Decoding error: {0}")]
Decode(DecodeError<Infallible>),
#[error("Encoding error: {0}")]
Encode(EncodeError<TryReserveError>),
#[error("Decoding IO error: {0}")]
DecodeIo(DecodeError<core2::io::Error>),
#[error("Encoding IO error: {0}")]
EncodeIo(EncodeError<core2::io::Error>),
}
impl From<DecodeError<Infallible>> for CodecError {
fn from(error: DecodeError<Infallible>) -> Self {
Self::Decode(error)
}
}
impl From<EncodeError<TryReserveError>> for CodecError {
fn from(error: EncodeError<TryReserveError>) -> Self {
Self::Encode(error)
}
}