Skip to main content

coreon_eip/
to.rs

1//! To — send the current Exchange to another endpoint.
2
3use async_trait::async_trait;
4use coreon_core::{CamelContext, Exchange, Processor, Result};
5use std::sync::{Arc, Weak};
6
7/// Sends the exchange to a named endpoint resolved from the CamelContext.
8///
9/// Holds a `Weak<CamelContext>` to avoid a cycle between context → route →
10/// processor → context. The context outlives routes, so the weak reference
11/// is only None during teardown.
12pub struct To {
13    uri: String,
14    ctx: Weak<CamelContext>,
15}
16
17impl To {
18    pub fn new(uri: impl Into<String>, ctx: &Arc<CamelContext>) -> Arc<Self> {
19        Arc::new(Self {
20            uri: uri.into(),
21            ctx: Arc::downgrade(ctx),
22        })
23    }
24}
25
26#[async_trait]
27impl Processor for To {
28    async fn process(&self, exchange: &mut Exchange) -> Result<()> {
29        let ctx = self
30            .ctx
31            .upgrade()
32            .ok_or_else(|| coreon_core::CamelError::NotRunning)?;
33        ctx.send(&self.uri, exchange).await
34    }
35}