Skip to main content

coreon_core/
exchange.rs

1//! Exchange — the per-message state that flows through a route.
2
3use crate::message::Message;
4use std::collections::HashMap;
5
6/// Message Exchange Pattern. Mirrors Camel's MEP but we only implement
7/// InOnly and InOut for MVP.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
9pub enum ExchangePattern {
10    #[default]
11    InOnly,
12    InOut,
13}
14
15#[derive(Clone, Debug, Default)]
16pub struct Exchange {
17    pub pattern: ExchangePattern,
18    pub r#in: Message,
19    pub out: Option<Message>,
20    /// Free-form properties attached to this exchange (route-scoped).
21    pub properties: HashMap<String, String>,
22}
23
24impl Exchange {
25    pub fn new(body: impl Into<crate::message::Body>) -> Self {
26        Self {
27            r#in: Message::new(body),
28            ..Self::default()
29        }
30    }
31
32    pub fn with_pattern(mut self, p: ExchangePattern) -> Self {
33        self.pattern = p;
34        self
35    }
36
37    /// Convenience accessor: the "effective" message is `out` if present,
38    /// otherwise `in`. EIPs write to `out`; the pipeline swaps it into `in`
39    /// before the next step if needed.
40    pub fn effective_message(&self) -> &Message {
41        self.out.as_ref().unwrap_or(&self.r#in)
42    }
43
44    /// Promote `out` → `in` between pipeline steps so the next Processor
45    /// reads a single canonical input.
46    pub fn advance(&mut self) {
47        if let Some(out) = self.out.take() {
48            self.r#in = out;
49        }
50    }
51}