use core::fmt;
use core::num::TryFromIntError;
#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};
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)
}
}
#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> ser::Error for EncodeError<E> {
fn custom<T: fmt::Display>(msg: T) -> Self {
EncodeError::Msg(msg.to_string())
}
}
#[cfg(not(feature = "std"))]
impl<E: fmt::Debug> ser::Error for EncodeError<E> {
fn custom<T: fmt::Display>(msg: T) -> Self {
EncodeError::Msg(msg.to_string())
}
}
#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> std::error::Error for EncodeError<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
EncodeError::Msg(_) => None,
EncodeError::Write(err) => Some(err),
}
}
}
#[cfg(not(feature = "std"))]
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)
}
}
#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> de::Error for DecodeError<E> {
fn custom<T: fmt::Display>(msg: T) -> Self {
DecodeError::Msg(msg.to_string())
}
}
#[cfg(not(feature = "std"))]
impl<E: fmt::Debug> de::Error for DecodeError<E> {
fn custom<T: fmt::Display>(msg: T) -> Self {
DecodeError::Msg(msg.to_string())
}
}
#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> std::error::Error for DecodeError<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
DecodeError::Msg(_) => None,
DecodeError::Read(err) => Some(err),
_ => None,
}
}
}
#[cfg(not(feature = "std"))]
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()),
}
}
}