simulacrum_mock/constraint/
result.rs

1use std::fmt;
2
3/// The Error type is a message to be printed to the user.
4pub type ConstraintResult = Result<(), ConstraintError>;
5
6#[derive(Clone, Debug, PartialEq)]
7pub enum ConstraintError {
8    AlwaysFail,
9    CalledTooFewTimes(i64),
10    CalledTooManyTimes(i64),
11    CallNotExpected,
12    Custom(String), // For custom constraints from users
13    MismatchedParams(String, String), // Expected Message, Received Message
14}
15
16impl fmt::Display for ConstraintError {
17    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18        match self {
19            &ConstraintError::AlwaysFail => {
20                write!(f, "Expectation will always fail.")
21            },
22            &ConstraintError::CalledTooFewTimes(times) => {
23                write!(f, "Called {} times fewer than expected.", times)
24            },
25            &ConstraintError::CalledTooManyTimes(times) => {
26                write!(f, "Called {} times more than expected.", times)
27            },
28            &ConstraintError::CallNotExpected => {
29                write!(f, "Called when not expected.")
30            },
31            &ConstraintError::Custom(ref msg) => {
32                write!(f, "{}", msg)
33            },
34            &ConstraintError::MismatchedParams(ref expected_msg, ref received_msg) => {
35                write!(f, "Called with unexpected parameters:\n  Expected: {}\n  Received: {}", expected_msg, received_msg)
36            },
37        }
38    }
39}