use crate::{
MatchType::{self, To},
Matcher, TypedMatcher,
};
pub fn or<T>(matchers: Vec<Box<dyn TypedMatcher<T>>>) -> Or<T> {
Or { matchers }
}
pub struct Or<T> {
matchers: Vec<Box<dyn TypedMatcher<T>>>,
}
impl<T> Or<T> {
pub fn new(matchers: Vec<Box<dyn TypedMatcher<T>>>) -> Self {
Or { matchers }
}
}
impl<T> Matcher<T> for Or<T> {
fn matches(&self, value: &T) -> bool {
self.matchers
.iter()
.any(|m| m.matches(value))
}
fn description(&self) -> String {
self.matchers
.iter()
.map(|m| m.description())
.collect::<Vec<_>>()
.join(" or ")
}
}
impl<T> TypedMatcher<T> for Or<T> {
fn matcher_type(&self) -> MatchType {
self.matchers
.first()
.map(|m| TypedMatcher::<T>::matcher_type(m.as_ref()))
.unwrap_or(To)
}
}