Module splitter

Module splitter 

Source
Expand description

Splitter pattern: derives multiple logical messages from a single inbound Exchange.

Implements the Enterprise Integration Pattern (EIP) “Splitter”. A splitter breaks a composite or aggregate message (e.g. a delimited string, a JSON array, a batch) into multiple individual messages. In this minimal implementation the provided closure returns a Vec<Message> representing the split parts.

§Current Behavior & Limitation

The splitter invokes the user-supplied closure and, if the returned vector is non-empty, stores ONLY the first resulting message in exchange.out_msg. The rest are discarded. This is intentionally minimal for now. Future versions may:

  • Emit all parts downstream via a Vec<Message> property.
  • Produce cloned Exchanges for each part.
  • Integrate with a downstream RecipientList or Aggregator automatically.

§Use Cases

  • Taking a CSV line and extracting the first column for downstream processing.
  • Extracting primary record from a batch for quick validation.
  • Demonstration / scaffolding before implementing full fan-out semantics.

§Example (tokenizing text payload)

use allora_core::{patterns::splitter::Splitter, route::Route, Exchange, Message};
let splitter = Splitter::new(|exchange: &Exchange| {
    exchange.in_msg.body_text()
        .map(|t| t.split_whitespace().map(Message::from_text).collect())
        .unwrap_or_else(Vec::new)
});
let route = Route::new().add(splitter).build();
let mut exchange = Exchange::new(Message::from_text("one two three"));
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { route.run(&mut exchange).await.unwrap(); });
assert_eq!(exchange.out_msg.unwrap().body_text(), Some("one"));

§Edge Cases

  • Empty returned vector => out_msg remains unchanged (None if unset).
  • Mixed payload types allowed; only first message used when non-empty.
  • Closure should be pure / side-effect free aside from constructing messages.

§Testing Strategies

  • Verify first-element extraction from multiple tokens.
  • Ensure no out_msg when closure returns empty slice.
  • Provide heterogeneous messages and confirm only first selected.

Structs§

Splitter