ruststream_lapin/reply.rs
1//! The responder half of the direct reply-to convention, packaged as a publish transform.
2
3use ruststream::runtime::{Outgoing, PublishContext, PublishTransform};
4
5/// Redirects each reply of a `#[subscriber(.., publish(..))]` handler to the requester's
6/// private reply-to address, echoing its correlation id.
7///
8/// This is the canonical responder wiring for [request/reply over `RabbitMQ` direct
9/// reply-to](crate::LapinRequester): compose it onto the reply publisher at mount time and the
10/// handler stays a pure request-to-reply function. Requests without a `reply-to` header fall
11/// through to the mount's static destination.
12///
13/// # Examples
14///
15/// ```
16/// use ruststream::runtime::TypedPublisher;
17/// use ruststream_lapin::{DirectReplyTo, LapinPublish};
18///
19/// let replies = TypedPublisher::new(LapinPublish::default()).transform(DirectReplyTo);
20/// # let _ = replies;
21/// ```
22#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
23pub struct DirectReplyTo;
24
25// --8<-- [start:transform]
26impl<C> PublishTransform<C> for DirectReplyTo {
27 fn apply(&self, out: &mut Outgoing<'_>, cx: &PublishContext<'_, C>) {
28 if let Some(reply_to) = cx.headers().reply_to() {
29 out.set_name(reply_to.to_owned());
30 }
31 if let Some(correlation_id) = cx.headers().correlation_id() {
32 out.headers_mut()
33 .insert("correlation-id", correlation_id.as_bytes().to_vec());
34 }
35 }
36}
37// --8<-- [end:transform]