#![allow(dead_code)]
use core::fmt;
#[derive(Debug, PartialEq)]
pub struct Error {
kind: ErrorKind,
}
impl Error {
pub(crate) const fn new(kind: ErrorKind) -> Error {
Error { kind }
}
}
impl From<ErrorKind> for Error {
#[inline]
fn from(kind: ErrorKind) -> Error {
Error { kind }
}
}
impl From<core::array::TryFromSliceError> for Error {
#[inline]
fn from(_: core::array::TryFromSliceError) -> Error {
Error::incompatible_types()
}
}
impl From<core::str::Utf8Error> for Error {
#[inline]
fn from(_: core::str::Utf8Error) -> Error {
Error::incompatible_types()
}
}
impl From<core::convert::Infallible> for Error {
#[inline]
fn from(_: core::convert::Infallible) -> Error {
Error::internal_failure()
}
}
impl From<&'static str> for Error {
#[inline]
fn from(message: &'static str) -> Error {
Error::verbose(message)
}
}
#[derive(Debug, Default, PartialEq)]
pub(crate) enum ErrorKind {
DecodeFailure {
message: &'static str,
},
EncodeFailure {
message: &'static str,
},
IncompatibleTypes,
InvalidCodec {
reason: &'static str,
},
MisalignedAccess,
NullReference,
OutOfBounds {
needed: usize,
available: usize,
},
SizeMismatch {
needed: usize,
available: usize,
},
#[default]
InternalFailure,
Verbose {
message: &'static str,
},
}
impl Error {
pub(crate) const fn decode_failed() -> Error {
Error::new(ErrorKind::DecodeFailure {
message:
"Decoder failed; cannot write malformed bytes due to size or alignment requirements",
})
}
pub(crate) const fn encode_failed() -> Error {
Error::new(ErrorKind::EncodeFailure {
message:
"Encoder failed; cannot write malformed bytes due to size or alignment requirements",
})
}
pub(crate) const fn out_of_bounds(needed: usize, available: usize) -> Error {
Error::new(ErrorKind::OutOfBounds { needed, available })
}
pub(crate) const fn misaligned_access() -> Error {
Error::new(ErrorKind::MisalignedAccess)
}
pub(crate) const fn size_mismatch(needed: usize, available: usize) -> Error {
Error::new(ErrorKind::SizeMismatch { needed, available })
}
pub(crate) const fn internal_failure() -> Error {
Error::new(ErrorKind::InternalFailure)
}
pub(crate) const fn incompatible_types() -> Error {
Error::new(ErrorKind::IncompatibleTypes)
}
pub(crate) const fn null_reference() -> Error {
Error::new(ErrorKind::NullReference)
}
pub(crate) const fn verbose(message: &'static str) -> Error {
Error::new(ErrorKind::Verbose { message })
}
pub(crate) const fn invalid_codec(reason: &'static str) -> Error {
Error::new(ErrorKind::InvalidCodec { reason })
}
}
impl fmt::Display for Error {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.kind {
ErrorKind::OutOfBounds { needed: required, available: actual } => {
write!(f, "Out of bounds error. Required {required} bytes, but buffer size can only hold {actual} bytes")
}
ErrorKind::IncompatibleTypes => {
write!(f, "Failed to convert one type to another due to incompatible layouts")
}
ErrorKind::InternalFailure => write!(
f,
"Entered unrecoverable failure state with an unknown or unexpected origin"
),
ErrorKind::MisalignedAccess => {
write!(f, "Misaligned memory access caused by misaligned pointer")
}
ErrorKind::NullReference => {
write!(f, "Invalid pointer dereferenced to null",)
}
ErrorKind::SizeMismatch { needed: expected, available: actual } => {
write!(f, "Size mismatch error (Required {expected} bytes, got {actual}")
}
ErrorKind::Verbose { message } => write!(f, "[ERROR]: {message}"),
ErrorKind::DecodeFailure { message } => write!(f, "Decode failed: {message}"),
ErrorKind::EncodeFailure { message } => write!(f, "Encode failed: {message}"),
ErrorKind::InvalidCodec { reason } => {
write!(f, "Invalid codec configuration: {reason}")
}
}
}
}
pub type Result<T, E = Error> = core::result::Result<T, E>;