assert-rs 0.1.1

An assertion library that uses types and data to fail tests instead of panicking.
Documentation
use super::{Assertion, Debug, Display};

/// An assertion that will fail if the given assertion was true
/// and succeed if the given assertion was false.
pub struct ShouldFail<A: Assertion>(pub A);

impl<A: Assertion> Display for ShouldFail<A> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let inner = &self.0;
        write!(f, "assertion expected to fail succeeded:\n{inner}")
    }
}

#[cfg(feature = "std")]
impl<A: Assertion> std::process::Termination for ShouldFail<A> {
    fn report(self) -> std::process::ExitCode {
        if self.0.test() {
            println!("{self}");
            std::process::ExitCode::FAILURE
        } else {
            std::process::ExitCode::SUCCESS
        }
    }
}

impl<A: Assertion> Assertion for ShouldFail<A> {
    fn test(&self) -> bool {
        !self.0.test()
    }
}

impl<A: Assertion> Debug for ShouldFail<A> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        Display::fmt(self, f)
    }
}