use std::fmt;
#[derive(Debug, Clone)]
pub enum HedgeError<E> {
AllAttemptsFailed(E),
Inner(E),
}
impl<E: fmt::Display> fmt::Display for HedgeError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HedgeError::AllAttemptsFailed(e) => {
write!(f, "all hedged attempts failed: {}", e)
}
HedgeError::Inner(e) => write!(f, "{}", e),
}
}
}
impl<E: std::error::Error + 'static> std::error::Error for HedgeError<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
HedgeError::AllAttemptsFailed(e) => Some(e),
HedgeError::Inner(e) => Some(e),
}
}
}
impl<E> HedgeError<E> {
pub fn is_all_attempts_failed(&self) -> bool {
matches!(self, HedgeError::AllAttemptsFailed(_))
}
pub fn is_inner(&self) -> bool {
matches!(self, HedgeError::Inner(_))
}
pub fn inner(&self) -> &E {
match self {
HedgeError::AllAttemptsFailed(e) => e,
HedgeError::Inner(e) => e,
}
}
pub fn into_inner(self) -> E {
match self {
HedgeError::AllAttemptsFailed(e) => e,
HedgeError::Inner(e) => e,
}
}
}