use core::fmt;
use crate::alloc::AllocError;
#[derive(Debug)]
pub enum CustomError<E> {
Custom(E),
Error(Error),
}
impl<E> From<Error> for CustomError<E> {
fn from(error: Error) -> Self {
CustomError::Error(error)
}
}
impl<E> From<AllocError> for CustomError<E> {
fn from(error: AllocError) -> Self {
CustomError::Error(Error::from(error))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
#[doc(hidden)]
CapacityOverflow,
#[doc(hidden)]
LayoutError,
#[doc(hidden)]
FormatError,
#[doc(hidden)]
AllocError {
error: AllocError,
},
}
impl From<AllocError> for Error {
#[inline]
fn from(error: AllocError) -> Self {
Error::AllocError { error }
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::CapacityOverflow => write!(f, "Capacity overflow"),
Error::LayoutError => write!(f, "Layout error"),
Error::FormatError => write!(f, "Format error"),
Error::AllocError { error } => error.fmt(f),
}
}
}
#[cfg(feature = "std")]
impl ::std::error::Error for Error {
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
match self {
Error::AllocError { error } => Some(error),
_ => None,
}
}
}