use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RetryError<E> {
Exhausted {
attempts: u32,
last_error: E,
},
}
impl<E> RetryError<E> {
pub fn attempts(&self) -> u32 {
match self {
Self::Exhausted { attempts, .. } => *attempts,
}
}
pub fn last_error(&self) -> &E {
match self {
Self::Exhausted { last_error, .. } => last_error,
}
}
pub fn into_last_error(self) -> E {
match self {
Self::Exhausted { last_error, .. } => last_error,
}
}
}
impl<E: fmt::Display> fmt::Display for RetryError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Exhausted {
attempts,
last_error,
} => write!(f, "retry gave up after {attempts} attempt(s): {last_error}"),
}
}
}
#[cfg(feature = "std")]
impl<E: std::error::Error + 'static> std::error::Error for RetryError<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Exhausted { last_error, .. } => Some(last_error),
}
}
}