Enum contrafact::Check
source · pub enum Check {
Failures(Vec<Failure>),
Error(String),
}
Expand description
The result of a check operation, which contains a failure message for every constraint which was not met.
There are two levels of “error” here: the failures due to data which does not meet the constraints, and also internal errors due to a poorly written Fact.
Variants§
Failures(Vec<Failure>)
The check ran successfully, and reported these failures. An empty list of failures means the data is valid per this check.
Error(String)
There was a problem actually running the check: there is a bug in a Fact or Generator.
Implementations§
source§impl Check
impl Check
sourcepub fn map<F>(self, f: F) -> Selfwhere
F: FnMut(Failure) -> Failure,
pub fn map<F>(self, f: F) -> Selfwhere F: FnMut(Failure) -> Failure,
Map over each failure string. Useful for combinators which add additional context to errors produced by inner facts.
sourcepub fn failures(&self) -> Result<&[Failure], ContrafactError>
pub fn failures(&self) -> Result<&[Failure], ContrafactError>
Get errors if they exist
sourcepub fn result(self) -> ContrafactResult<Result<(), Vec<Failure>>>
pub fn result(self) -> ContrafactResult<Result<(), Vec<Failure>>>
Convert to a Result: No failures => Ok
all the way
The result is wrapped in another Result, in case the overall check failed for an internal reason
use contrafact::*;
assert_eq!(Check::pass().result(), Ok(Ok(())));
assert_eq!(Check::fail("message").result(), Ok(Err(vec!["message".to_string()])));
sourcepub fn result_joined(self) -> ContrafactResult<Result<(), String>>
pub fn result_joined(self) -> ContrafactResult<Result<(), String>>
If Failures, return all failures joined together in a single string
sourcepub fn check<S: ToString>(ok: bool, err: S) -> Self
pub fn check<S: ToString>(ok: bool, err: S) -> Self
Create a single-error failure if predicate is false, otherwise pass
use contrafact::*;
assert_eq!(Check::check(true, "message"), Check::pass());
assert_eq!(Check::check(false, "message"), Check::fail("message"));
sourcepub fn from_mutation<T>(res: Mutation<T>) -> Self
pub fn from_mutation<T>(res: Mutation<T>) -> Self
Create a single-error failure from a Result
use contrafact::*;
assert_eq!(Check::from_mutation(Ok(42)), Check::pass());
assert_eq!(Check::from_mutation::<()>(Err(MutationError::Check("message".to_string()))), Check::fail("message"));
sourcepub fn from_result(res: Result<Vec<Failure>, ContrafactError>) -> Self
pub fn from_result(res: Result<Vec<Failure>, ContrafactError>) -> Self
Create a check where failures are drawn from Ok, and internal errors from Err of the input Result