use core::fmt;
#[cfg(feature = "std")]
pub use std::error::Error as StdError;
#[cfg(all(not(feature = "std"), feature = "newer-rust"))]
if_rust_version::if_rust_version! {
>= 1.81 {
pub use core::error::Error as StdError;
}
}
macro_rules! if_std_error {
($($t:tt)*) => {
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", all(feature = "newer-rust", rust_version = ">= 1.81")))))]
$($t)*
#[cfg(all(not(feature = "std"), feature = "newer-rust"))]
if_rust_version::if_rust_version! {
>= 1.81 {
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", all(feature = "newer-rust", rust_version = ">= 1.81")))))]
$($t)*
}
}
};
}
pub(crate) use if_std_error;
#[derive(Debug)]
#[non_exhaustive]
pub struct EmptyCollectionError;
impl fmt::Display for EmptyCollectionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "the collection is empty")
}
}
if_std_error! {
impl StdError for EmptyCollectionError {}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum CollectionError<E> {
Other(E),
Empty,
}
impl<E: fmt::Display> fmt::Display for CollectionError<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CollectionError::Other(error) => fmt::Display::fmt(error, f),
CollectionError::Empty => write!(f, "the value is empty"),
}
}
}
if_std_error! {
impl<E: StdError> StdError for CollectionError<E> {
#[inline]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
CollectionError::Other(error) => error.source(),
CollectionError::Empty => None,
}
}
}
}