coreon-eip 0.1.0

Enterprise Integration Pattern processors for camel-rs.
Documentation
//! To — send the current Exchange to another endpoint.

use async_trait::async_trait;
use coreon_core::{CamelContext, Exchange, Processor, Result};
use std::sync::{Arc, Weak};

/// Sends the exchange to a named endpoint resolved from the CamelContext.
///
/// Holds a `Weak<CamelContext>` to avoid a cycle between context → route →
/// processor → context. The context outlives routes, so the weak reference
/// is only None during teardown.
pub struct To {
    uri: String,
    ctx: Weak<CamelContext>,
}

impl To {
    pub fn new(uri: impl Into<String>, ctx: &Arc<CamelContext>) -> Arc<Self> {
        Arc::new(Self {
            uri: uri.into(),
            ctx: Arc::downgrade(ctx),
        })
    }
}

#[async_trait]
impl Processor for To {
    async fn process(&self, exchange: &mut Exchange) -> Result<()> {
        let ctx = self
            .ctx
            .upgrade()
            .ok_or_else(|| coreon_core::CamelError::NotRunning)?;
        ctx.send(&self.uri, exchange).await
    }
}