1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use super::super::*;
#[macro_export]
macro_rules! all_of {
( $matcher: expr ) => {
Box::new(All::of($matcher))
};
( $matcher: expr, $($matchers: expr),* ) => {
Box::new(All::of($matcher)$(.and($matchers))*)
};
}
pub struct All<'a, T:'a> {
pub matcher: Box<Matcher<'a,T> + 'a>,
pub next: Option<Box<All<'a,T>>>
}
impl<'a,T:'a> All<'a, T> {
pub fn of(matcher: Box<Matcher<'a,T> + 'a>) -> All<'a,T> {
All {
matcher: matcher,
next: None
}
}
pub fn and(self, matcher: Box<Matcher<'a,T> + 'a>) -> All<'a,T> {
All {
matcher: matcher,
next: Some(Box::new(self))
}
}
}
impl<'a,T:'a> Matcher<'a,T> for All<'a,T> {
fn check(&self, actual: &'a T) -> MatchResult {
match self.matcher.check(actual) {
x@MatchResult::Matched {..} => {
match self.next {
None => x,
Some(ref next) => next.check(actual)
}
},
x@MatchResult::Failed {..} => x
}
}
}
#[macro_export]
macro_rules! any_of {
( $matcher: expr ) => {
Box::new(Any::of($matcher))
};
( $matcher: expr, $($matchers: expr),* ) => {
Box::new(Any::of($matcher)$(.or($matchers))*)
};
}
pub struct Any<'a, T:'a> {
pub matcher: Box<Matcher<'a,T> + 'a>,
pub next: Option<Box<Any<'a,T>>>
}
impl<'a,T:'a> Any<'a, T> {
pub fn of(matcher: Box<Matcher<'a,T> + 'a>) -> Any<'a,T> {
Any {
matcher: matcher,
next: None
}
}
pub fn or(self, matcher: Box<Matcher<'a,T> + 'a>) -> Any<'a,T> {
Any {
matcher: matcher,
next: Some(Box::new(self))
}
}
}
impl<'a,T:'a> Matcher<'a,T> for Any<'a,T> {
fn check(&self, actual: &'a T) -> MatchResult {
match self.matcher.check(actual) {
MatchResult::Matched {..} => MatchResult::Matched { name: "any_of".to_owned() },
x@MatchResult::Failed {..} => match self.next {
None => x,
Some(ref next) => next.check(actual)
}
}
}
}