use crate::{error::Result, exchange::Exchange};
use async_trait::async_trait;
use std::sync::Arc;
#[async_trait]
pub trait Processor: Send + Sync {
async fn process(&self, exchange: &mut Exchange) -> Result<()>;
}
pub struct FnProcessor<F>(F);
impl<F> FnProcessor<F>
where
F: Fn(&mut Exchange) -> Result<()> + Send + Sync + 'static,
{
pub fn new(f: F) -> Arc<Self> {
Arc::new(Self(f))
}
}
#[async_trait]
impl<F> Processor for FnProcessor<F>
where
F: Fn(&mut Exchange) -> Result<()> + Send + Sync + 'static,
{
async fn process(&self, exchange: &mut Exchange) -> Result<()> {
(self.0)(exchange)
}
}
pub trait Predicate: Send + Sync {
fn matches(&self, exchange: &Exchange) -> bool;
}
impl<F> Predicate for F
where
F: Fn(&Exchange) -> bool + Send + Sync,
{
fn matches(&self, exchange: &Exchange) -> bool {
self(exchange)
}
}