coreon-eip 0.1.0

Enterprise Integration Pattern processors for camel-rs.
Documentation
//! Transform — apply a pure function to the in-message body.

use async_trait::async_trait;
use coreon_core::{message::Body, Exchange, Processor, Result};
use std::sync::Arc;

pub type BodyFn = dyn Fn(Body) -> Body + Send + Sync;

pub struct Transform {
    f: Arc<BodyFn>,
}

impl Transform {
    pub fn new<F>(f: F) -> Arc<Self>
    where
        F: Fn(Body) -> Body + Send + Sync + 'static,
    {
        Arc::new(Self { f: Arc::new(f) })
    }
}

#[async_trait]
impl Processor for Transform {
    async fn process(&self, exchange: &mut Exchange) -> Result<()> {
        let body = std::mem::take(&mut exchange.r#in.body);
        exchange.r#in.body = (self.f)(body);
        Ok(())
    }
}