use super::Refused;
#[derive(Debug, PartialEq, Eq)]
struct Closed;
#[derive(Debug, PartialEq, Eq)]
struct Corrupt(u8);
impl core::fmt::Display for Closed {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("the index is closed")
}
}
impl core::fmt::Display for Corrupt {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "segment {} is corrupt", self.0)
}
}
impl core::error::Error for Closed {}
impl core::error::Error for Corrupt {}
#[test]
fn each_leg_keeps_its_own_error_type() {
let first: Refused<Closed, Corrupt> = Refused::First(Closed);
let second: Refused<Closed, Corrupt> = Refused::Second(Corrupt(3));
assert!(matches!(first, Refused::First(Closed)));
assert!(matches!(second, Refused::Second(Corrupt(3))));
}
#[test]
fn a_third_leg_is_the_second_nested() {
let third: Refused<Closed, Refused<Closed, Corrupt>> =
Refused::Second(Refused::Second(Corrupt(9)));
let middle: Refused<Closed, Refused<Closed, Corrupt>> = Refused::Second(Refused::First(Closed));
assert!(matches!(
third,
Refused::Second(Refused::Second(Corrupt(9)))
));
assert!(matches!(middle, Refused::Second(Refused::First(Closed))));
}
#[test]
fn the_display_names_the_fan_once() {
let first: Refused<Closed, Corrupt> = Refused::First(Closed);
let second: Refused<Closed, Corrupt> = Refused::Second(Corrupt(3));
assert_eq!(
first.to_string(),
"the fan's first engine refused: the index is closed"
);
assert_eq!(second.to_string(), "segment 3 is corrupt");
}
#[test]
fn the_source_is_the_leg_that_refused() {
use core::error::Error as _;
let second: Refused<Closed, Corrupt> = Refused::Second(Corrupt(3));
let source = second.source().expect("a fan's refusal has a source");
assert_eq!(source.to_string(), "segment 3 is corrupt");
}
#[test]
fn a_fan_over_one_error_type_collapses() {
let second: Refused<Closed, Closed> = Refused::Second(Closed);
assert_eq!(second.into_inner(), Closed);
}