use super::{either::Either, IdentityPlugin};
use crate::operation::{Operation, OperationShape};
use super::Plugin;
pub struct FilterByOperationName<Inner, F> {
inner: Inner,
predicate: F,
}
pub fn filter_by_operation_name<Inner, F>(plugins: Inner, predicate: F) -> FilterByOperationName<Inner, F>
where
F: Fn(&str) -> bool,
{
FilterByOperationName::new(plugins, predicate)
}
impl<Inner, F> FilterByOperationName<Inner, F> {
fn new(inner: Inner, predicate: F) -> Self {
Self { inner, predicate }
}
}
impl<P, Op, S, L, Inner, F> Plugin<P, Op, S, L> for FilterByOperationName<Inner, F>
where
F: Fn(&str) -> bool,
Inner: Plugin<P, Op, S, L>,
Op: OperationShape,
{
type Service = Either<Inner::Service, S>;
type Layer = Either<Inner::Layer, L>;
fn map(&self, input: Operation<S, L>) -> Operation<Self::Service, Self::Layer> {
let either_plugin = if (self.predicate)(Op::NAME) {
Either::Left { value: &self.inner }
} else {
Either::Right { value: IdentityPlugin }
};
either_plugin.map(input)
}
}