Skip to main content

mq_bridge/endpoints/
null.rs

1use crate::traits::{MessagePublisher, PublisherError, Sent, SentBatch};
2use crate::CanonicalMessage;
3use async_trait::async_trait;
4use std::any::Any;
5
6#[derive(Clone)]
7pub struct NullPublisher;
8
9#[async_trait]
10impl MessagePublisher for NullPublisher {
11    async fn send(&self, _message: CanonicalMessage) -> Result<Sent, PublisherError> {
12        Ok(Sent::Ack)
13    }
14
15    async fn send_batch(
16        &self,
17        _messages: Vec<CanonicalMessage>,
18    ) -> Result<SentBatch, PublisherError> {
19        Ok(SentBatch::Ack)
20    }
21
22    fn as_any(&self) -> &dyn Any {
23        self
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use crate::traits::Sent;
31
32    #[tokio::test]
33    async fn test_null_publisher_acks_single_and_batch_messages() {
34        let publisher = NullPublisher;
35
36        assert!(matches!(
37            publisher
38                .send(CanonicalMessage::from("ignored"))
39                .await
40                .unwrap(),
41            Sent::Ack
42        ));
43        assert!(matches!(
44            publisher
45                .send_batch(vec![
46                    CanonicalMessage::from("one"),
47                    CanonicalMessage::from("two")
48                ])
49                .await
50                .unwrap(),
51            SentBatch::Ack
52        ));
53        assert!(publisher.as_any().is::<NullPublisher>());
54    }
55}