use crate::{implementation, private, Asserter};
use std::fmt::{self, Debug};
pub trait ResultAssertion<OkValue, ErrValue>: private::Sealed {
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_ok(self) -> OkAsserter<OkValue>
where
ErrValue: Debug;
#[track_caller]
#[allow(clippy::wrong_self_convention)]
fn is_err(self) -> ErrAsserter<ErrValue>
where
OkValue: Debug;
}
struct ErrWrapper<'a, E>(&'a E);
impl<E: Debug> Debug for ErrWrapper<'_, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Err({:?})", self.0)
}
}
struct OkWrapper<'a, O>(&'a O);
impl<O: Debug> Debug for OkWrapper<'_, O> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Ok({:?})", self.0)
}
}
impl<OkValue, ErrValue> ResultAssertion<OkValue, ErrValue> for Asserter<Result<OkValue, ErrValue>> {
fn is_ok(self) -> OkAsserter<OkValue>
where
ErrValue: Debug,
{
if let Err(ref e) = self.value {
implementation::assert_no_expected(false, ErrWrapper(e), "to be Ok");
}
#[allow(clippy::unwrap_used)]
let value = self.value.unwrap();
OkAsserter { value }
}
fn is_err(self) -> ErrAsserter<ErrValue>
where
OkValue: Debug,
{
if let Ok(ref v) = self.value {
implementation::assert_no_expected(false, OkWrapper(v), "to be Err");
}
#[allow(clippy::unwrap_used)]
let value = self.value.err().unwrap();
ErrAsserter { value }
}
}
pub struct ErrAsserter<ErrValue> {
value: ErrValue,
}
impl<ErrValue> ErrAsserter<ErrValue> {
#[track_caller]
#[must_use = "Transforming the asserted value does not assert anything"]
pub fn and_error(self) -> Asserter<ErrValue> {
Asserter { value: self.value }
}
}
pub struct OkAsserter<OkValue> {
value: OkValue,
}
impl<OkValue> OkAsserter<OkValue> {
#[track_caller]
#[must_use = "Transforming the asserted value does not assert anything"]
pub fn and_value(self) -> Asserter<OkValue> {
Asserter { value: self.value }
}
}