use std::fmt;
#[derive(Debug, PartialEq, Eq)]
pub enum CircuitCallError<E> {
Open,
Inner(E),
}
impl<E> CircuitCallError<E> {
pub fn is_open(&self) -> bool {
matches!(self, Self::Open)
}
pub fn into_inner(self) -> Option<E> {
match self {
Self::Open => None,
Self::Inner(e) => Some(e),
}
}
}
impl<E: fmt::Display> fmt::Display for CircuitCallError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Open => write!(f, "circuit breaker is open"),
Self::Inner(e) => write!(f, "{e}"),
}
}
}
impl<E: fmt::Debug + fmt::Display> std::error::Error for CircuitCallError<E> {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_circuit_call_error_helpers() {
assert!(CircuitCallError::<&str>::Open.is_open());
assert_eq!(CircuitCallError::Inner("x").into_inner(), Some("x"));
assert!(CircuitCallError::<&str>::Open.into_inner().is_none());
assert_eq!(
CircuitCallError::<&str>::Open.to_string(),
"circuit breaker is open"
);
assert_eq!(CircuitCallError::Inner("boom").to_string(), "boom");
}
}