use crate::*;
use std::any::Any;
#[must_use]
#[derive(Debug)]
pub enum TestPanicResult<R> {
Cool(R),
Panic(Box<dyn Any + Send>),
}
impl<R> TestPanicResult<R> {
#[must_use]
pub fn is_cool(&self) -> bool {
matches!(*self, Self::Cool(_))
}
#[must_use]
pub fn is_panic(&self) -> bool {
matches!(*self, Self::Panic(_))
}
pub fn cool(self) -> Option<R> {
match self {
Self::Cool(x) => Some(x),
Self::Panic(_) => None,
}
}
pub fn panic(self) -> Option<Box<dyn Any + Send>> {
match self {
Self::Cool(_) => None,
Self::Panic(x) => Some(x),
}
}
#[must_use]
pub fn value(&self) -> &R {
match self {
Self::Cool(x) => x,
Self::Panic(_) => panic!("`self` is panic."),
}
}
#[must_use]
pub fn payload(&self) -> &Box<dyn Any + Send> {
match self {
Self::Cool(_) => panic!("`self` is cool."),
Self::Panic(x) => x,
}
}
#[must_use]
pub fn message(&self) -> String {
if self.is_cool() {
panic!("`self` is cool.");
}
match self.get_message() {
None => panic!("Panic payload is not string like."),
Some(x) => x,
}
}
#[must_use]
pub fn get_message(&self) -> Option<String> {
if self.is_cool() {
return None;
}
util::string_like_to_string(self.payload().as_ref())
}
#[must_use]
pub fn eq_almost(&self, other: &Self) -> bool
where
R: PartialEq,
{
if other.get_message().is_none() {
self.eq_nearly(other)
} else {
self.eq(other)
}
}
#[must_use]
pub fn eq_nearly(&self, other: &Self) -> bool
where
R: PartialEq,
{
match (self, other) {
(Self::Cool(x), Self::Cool(y)) => x == y,
(Self::Panic(_), Self::Panic(_)) => true,
_ => false,
}
}
}
impl<R> Eq for TestPanicResult<R>
where
R: Eq,
{
}
impl<R> PartialEq for TestPanicResult<R>
where
R: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Cool(l0), Self::Cool(r0)) => l0 == r0,
(Self::Panic(_), Self::Panic(_)) => self.get_message() == other.get_message(),
_ => false,
}
}
}