use std::borrow::Borrow;
use trailer::TrailerValue;
use trailer::accumulation::ValueAccumulator;
use trailer::spec::TrailerSpec;
pub enum ValueMatcher {
Any,
Equals(TrailerValue),
Contains(String),
}
impl ValueMatcher {
pub fn matches(&self, value: &TrailerValue) -> bool
{
match self {
&ValueMatcher::Any => true,
&ValueMatcher::Equals(ref v) => value == v,
&ValueMatcher::Contains(ref s) => value.to_string().contains(s),
}
}
pub fn matches_any<I, V>(&self, values: I) -> bool
where I: IntoIterator<Item = V>,
V: Borrow<TrailerValue>
{
values.into_iter().any(|v| self.matches(v.borrow()))
}
}
pub struct TrailerFilter<'a> {
trailer: TrailerSpec<'a>,
matcher: ValueMatcher,
}
impl<'a> TrailerFilter<'a> {
pub fn new(trailer: TrailerSpec<'a>, matcher: ValueMatcher) -> Self {
Self { trailer: trailer, matcher: matcher }
}
pub fn matches<'b>(&self, accumulator: &::std::collections::HashMap<String, ValueAccumulator>) -> bool {
let values = accumulator
.get(self.trailer.key)
.cloned()
.unwrap_or_default();
self.matcher.matches_any(values)
}
pub fn spec(&self) -> &TrailerSpec<'a> {
&self.trailer
}
}