Skip to main content

camel_processor/
enrichment_strategy.rs

1use camel_api::Exchange;
2
3/// Merges the original Exchange with the enriched/polled Exchange in the EIP-7
4/// `enrich` and `pollEnrich` verbs.
5///
6/// Distinct from the EIP-22 `AggregationStrategy` family (which combines many
7/// Exchanges into one); `EnrichmentStrategy` is always 2→1 (original + enriched).
8pub trait EnrichmentStrategy: Send + Sync {
9    fn aggregate(&self, original: Exchange, enriched: Exchange) -> Exchange;
10}
11
12/// Default strategy: discard the original body, replace with the enriched body.
13/// Headers and properties from the original are preserved; enriched headers
14/// are discarded. Implement a custom `EnrichmentStrategy` if you need to merge
15/// headers from the enriched exchange.
16pub struct UseEnrichedBody;
17
18impl EnrichmentStrategy for UseEnrichedBody {
19    fn aggregate(&self, mut original: Exchange, enriched: Exchange) -> Exchange {
20        // Replace the body from the enriched exchange, but keep original headers/properties.
21        original.input.body = enriched.input.body;
22        original
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29    use camel_api::{Body, Message};
30
31    fn exchange(body: &str) -> Exchange {
32        Exchange::new(Message::new(body))
33    }
34
35    #[test]
36    fn use_enriched_body_replaces_body() {
37        let strategy = UseEnrichedBody;
38        let result = strategy.aggregate(exchange("original"), exchange("enriched"));
39        match &result.input.body {
40            Body::Text(s) => assert_eq!(s, "enriched"),
41            other => panic!("unexpected body {other:?}"),
42        }
43    }
44}