assertrs/matchers/
option.rs

1use std::fmt::Debug;
2use super::{Matcher, MatchOutput};
3
4pub struct SomeMatcher<Inner: Debug>(Inner);
5
6pub fn is_some<Inner: Debug>(matcher: Inner) -> SomeMatcher<Inner> {
7    SomeMatcher(matcher)
8}
9
10impl <Inner: Debug, T> Matcher<Option<T>> for SomeMatcher<Inner>
11where
12    Inner: Matcher<T>
13{
14    fn matches(&self, t: Option<T>) -> MatchOutput {
15        match t {
16            Some(value) => {
17                self.0.matches(value).wrap_with_ok("Some(", ")")
18            },
19            None => {
20                MatchOutput::expected_found("Some(_)".to_string(), "None".to_string())
21            }
22        }
23    }
24}
25
26impl <Inner: Debug> Debug for SomeMatcher<Inner> {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        f.debug_tuple("Some").field(&self.0).finish()
29    }
30}
31
32pub struct NoneMatcher;
33
34pub fn is_none() -> NoneMatcher {
35    NoneMatcher
36}
37
38impl <T> Matcher<Option<T>> for NoneMatcher {
39    fn matches(&self, t: Option<T>) -> MatchOutput {
40        match t {
41            Some(_) => {
42                MatchOutput::expected_found("None".to_string(), "Some(...)".to_string())
43            },
44            None => {
45                MatchOutput::Ok("None".to_string())
46            }
47        }
48    }
49}
50
51impl Debug for NoneMatcher {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.debug_tuple("None").finish()
54    }
55}