Skip to main content

mq_bridge/
outcomes.rs

1use crate::errors::PublisherError;
2use crate::traits::{BatchCommitFunc, CommitFunc};
3use crate::CanonicalMessage;
4
5/// The outcome of a successful command handling operation.
6#[derive(Debug)]
7pub enum Handled {
8    /// The command was handled successfully. No further message should be sent.
9    /// This is equivalent to acknowledging the message.
10    Ack,
11    /// The command was handled successfully and produced a response to be published.
12    Publish(CanonicalMessage),
13}
14
15/// The outcome of a successful single message publishing operation.
16#[derive(Debug)]
17pub enum Sent {
18    /// Message was successfully sent, no response was generated.
19    Ack,
20    /// Message was successfully sent and a response was generated.
21    Response(CanonicalMessage),
22}
23
24/// The outcome of a successful batch message publishing operation.
25#[derive(Debug)]
26pub enum SentBatch {
27    /// All messages in the batch were sent successfully. No responses were generated.
28    Ack,
29    /// The batch operation resulted in a mix of successes and/or failures.
30    Partial {
31        responses: Option<Vec<CanonicalMessage>>,
32        failed: Vec<(CanonicalMessage, PublisherError)>,
33    },
34}
35
36/// A successfully received single message.
37pub struct Received {
38    pub message: CanonicalMessage,
39    pub commit: CommitFunc,
40}
41
42impl std::fmt::Debug for Received {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("Received")
45            .field("message", &self.message)
46            .field("commit", &"<CommitFunc>")
47            .finish()
48    }
49}
50
51/// A successfully received batch of messages.
52pub struct ReceivedBatch {
53    pub messages: Vec<CanonicalMessage>,
54    pub commit: BatchCommitFunc,
55}
56
57impl std::fmt::Debug for ReceivedBatch {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        f.debug_struct("ReceivedBatch")
60            .field("messages", &self.messages)
61            .field("commit", &"<BatchCommitFunc>")
62            .finish()
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use crate::traits::MessageDisposition;
70
71    #[test]
72    fn test_received_debug_hides_commit_implementation() {
73        let received = Received {
74            message: CanonicalMessage::from("single"),
75            commit: Box::new(|_| Box::pin(async move { Ok(()) })),
76        };
77
78        let debug = format!("{received:?}");
79        assert!(debug.contains("Received"));
80        assert!(debug.contains("<CommitFunc>"));
81        assert!(debug.contains("single"));
82    }
83
84    #[test]
85    fn test_received_batch_debug_hides_commit_implementation() {
86        let batch = ReceivedBatch {
87            messages: vec![
88                CanonicalMessage::from("first"),
89                CanonicalMessage::from("second"),
90            ],
91            commit: Box::new(|dispositions| {
92                Box::pin(async move {
93                    assert_eq!(dispositions.len(), 2);
94                    assert!(dispositions
95                        .into_iter()
96                        .all(|disposition| matches!(disposition, MessageDisposition::Ack)));
97                    Ok(())
98                })
99            }),
100        };
101
102        let debug = format!("{batch:?}");
103        assert!(debug.contains("ReceivedBatch"));
104        assert!(debug.contains("<BatchCommitFunc>"));
105        assert!(debug.contains("first"));
106        assert!(debug.contains("second"));
107    }
108}