Skip to main content

coreon_eip/
transform.rs

1//! Transform — apply a pure function to the in-message body.
2
3use async_trait::async_trait;
4use coreon_core::{message::Body, Exchange, Processor, Result};
5use std::sync::Arc;
6
7pub type BodyFn = dyn Fn(Body) -> Body + Send + Sync;
8
9pub struct Transform {
10    f: Arc<BodyFn>,
11}
12
13impl Transform {
14    pub fn new<F>(f: F) -> Arc<Self>
15    where
16        F: Fn(Body) -> Body + Send + Sync + 'static,
17    {
18        Arc::new(Self { f: Arc::new(f) })
19    }
20}
21
22#[async_trait]
23impl Processor for Transform {
24    async fn process(&self, exchange: &mut Exchange) -> Result<()> {
25        let body = std::mem::take(&mut exchange.r#in.body);
26        exchange.r#in.body = (self.f)(body);
27        Ok(())
28    }
29}