mq_bridge/endpoints/
response.rs1use crate::traits::{MessagePublisher, PublisherError, Sent, SentBatch};
3use crate::CanonicalMessage;
4use async_trait::async_trait;
5use std::any::Any;
6
7#[derive(Clone)]
11pub struct ResponsePublisher;
12
13#[async_trait]
14impl MessagePublisher for ResponsePublisher {
15 async fn send(&self, message: CanonicalMessage) -> Result<Sent, PublisherError> {
16 Ok(Sent::Response(message))
17 }
18
19 async fn send_batch(
20 &self,
21 messages: Vec<CanonicalMessage>,
22 ) -> Result<SentBatch, PublisherError> {
23 Ok(SentBatch::Partial {
25 responses: Some(messages),
26 failed: Vec::new(),
27 })
28 }
29
30 fn as_any(&self) -> &dyn Any {
31 self
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38 use crate::command_handler::CommandPublisher;
39 use crate::traits::Handled;
40
41 #[tokio::test]
42 async fn test_response_publisher_echo() {
43 let publisher = ResponsePublisher;
44 let msg = CanonicalMessage::new(b"test".to_vec(), None);
45 let result = publisher.send(msg).await.unwrap();
46
47 match result {
48 Sent::Response(r) => assert_eq!(&r.payload[..], b"test"),
49 _ => panic!("Expected Sent::Response"),
50 }
51 }
52
53 #[tokio::test]
54 async fn test_response_publisher_with_handler() {
55 let response_pub = ResponsePublisher;
57
58 let handler = |mut msg: CanonicalMessage| async move {
60 msg.payload = [b"handled: ", &msg.payload[..]].concat().into();
61 Ok(Handled::Publish(msg))
62 };
63
64 let publisher = CommandPublisher::new(response_pub, handler);
65
66 let msg = CanonicalMessage::new(b"input".to_vec(), None);
67 let result = publisher.send(msg).await.unwrap();
68
69 match result {
70 Sent::Response(r) => assert_eq!(&r.payload[..], b"handled: input"),
71 _ => panic!("Expected Sent::Response"),
72 }
73 }
74}