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)
}
}
}
}