use crate::Decorator;
pub struct ConditionDecorator<D, P> {
decorator: D,
pred: P,
}
impl<T, D, P> Decorator<T, T> for ConditionDecorator<D, P>
where
D: Decorator<T, T>,
P: FnMut(&T) -> bool,
{
fn produce(&mut self, input: T) -> T {
if (self.pred)(&input) {
self.decorator.produce(input)
} else {
input
}
}
}
pub trait DecoratorExt<T>: Decorator<T, T> {
fn when<P>(self, pred: P) -> ConditionDecorator<Self, P>
where
P: FnMut(&T) -> bool,
Self: Sized,
{
ConditionDecorator {
decorator: self,
pred,
}
}
}
impl<F: Decorator<T, T>, T> DecoratorExt<T> for F {}