use crate::{error::Result, Exchange};
use std::fmt::{Debug, Formatter, Result as FmtResult};
#[async_trait::async_trait]
pub trait Processor: Send + Sync + Debug {
async fn process(&self, exchange: &mut Exchange) -> Result<()>;
}
#[derive()]
pub struct ClosureProcessor<F>
where
F: Fn(&mut Exchange) -> Result<()> + Send + Sync + 'static,
{
func: F,
}
impl<F> ClosureProcessor<F>
where
F: Fn(&mut Exchange) -> Result<()> + Send + Sync + 'static,
{
pub fn new(func: F) -> Self {
Self { func }
}
pub fn closure(func: F) -> Self {
Self { func }
}
}
#[async_trait::async_trait]
impl<F> Processor for ClosureProcessor<F>
where
F: Fn(&mut Exchange) -> Result<()> + Send + Sync + 'static,
{
async fn process(&self, exchange: &mut Exchange) -> Result<()> {
(self.func)(exchange)
}
}
impl<F> Debug for ClosureProcessor<F>
where
F: Fn(&mut Exchange) -> Result<()> + Send + Sync + 'static,
{
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str("ClosureProcessor{func=*closure*}")
}
}
pub type BoxedProcessor = Box<dyn Processor>;