foxtive_worker/
message.rs1use async_trait::async_trait;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4use std::fmt::Debug;
5use std::sync::Arc;
6
7use crate::MessageProperties;
8use crate::error::WorkerResult;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct Message<T> {
17 pub id: String,
19 pub payload: T,
21 pub metadata: MessageMetadata,
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct MessageMetadata {
28 pub received_at: DateTime<Utc>,
30 pub attempt: u32,
32 pub source: String,
34 pub correlation_id: Option<String>,
36 #[serde(skip_serializing_if = "Option::is_none")]
38 pub routing_key: Option<String>,
39 #[serde(skip_serializing_if = "Option::is_none")]
41 pub properties: Option<MessageProperties>,
42}
43
44impl MessageMetadata {
45 pub fn new(source: impl Into<String>) -> Self {
47 Self {
48 received_at: Utc::now(),
49 attempt: 0, source: source.into(),
51 correlation_id: None,
52 routing_key: None,
53 properties: None,
54 }
55 }
56
57 pub fn with_correlation_id(mut self, correlation_id: impl Into<String>) -> Self {
59 self.correlation_id = Some(correlation_id.into());
60 self
61 }
62
63 pub fn with_routing_key(mut self, routing_key: impl Into<String>) -> Self {
65 self.routing_key = Some(routing_key.into());
66 self
67 }
68
69 pub fn with_properties(mut self, properties: MessageProperties) -> Self {
71 self.properties = Some(properties);
72 self
73 }
74
75 pub fn increment_attempt(&mut self) {
77 self.attempt += 1;
78 }
79}
80
81#[async_trait]
87pub trait AckHandle: Send + Sync + Debug {
88 async fn ack(&self) -> WorkerResult<()>;
90
91 async fn nack(&self, requeue: bool) -> WorkerResult<()>;
97}
98
99pub struct ReceivedMessage<T> {
122 pub message: Message<T>,
124 pub ack_handle: Arc<dyn AckHandle>,
126}
127
128impl<T: Send + Sync> ReceivedMessage<T> {
129 pub fn new(message: Message<T>, ack_handle: Arc<dyn AckHandle>) -> Self {
131 Self {
132 message,
133 ack_handle,
134 }
135 }
136
137 pub async fn ack(&self) -> WorkerResult<()> {
139 self.ack_handle.ack().await
140 }
141
142 pub async fn nack(&self, requeue: bool) -> WorkerResult<()> {
144 self.ack_handle.nack(requeue).await
145 }
146
147 pub fn into_message(self) -> Message<T> {
149 self.message
150 }
151}
152
153impl<T: Clone + Send + Sync> Clone for ReceivedMessage<T> {
154 fn clone(&self) -> Self {
155 Self {
156 message: self.message.clone(),
157 ack_handle: self.ack_handle.clone(),
158 }
159 }
160}
161
162impl<T: Debug> Debug for ReceivedMessage<T> {
163 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164 f.debug_struct("ReceivedMessage")
165 .field("message", &self.message)
166 .field("ack_handle", &"<AckHandle>")
167 .finish()
168 }
169}
170
171pub type JsonMessage = Message<serde_json::Value>;
173
174pub type ReceivedJsonMessage = ReceivedMessage<serde_json::Value>;
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180 use std::sync::Arc;
181 use std::sync::atomic::{AtomicBool, Ordering};
182
183 #[derive(Debug)]
184 struct MockAckHandle {
185 acked: Arc<AtomicBool>,
186 nacked: Arc<AtomicBool>,
187 }
188
189 impl MockAckHandle {
190 fn new() -> (Self, Arc<AtomicBool>, Arc<AtomicBool>) {
191 let acked = Arc::new(AtomicBool::new(false));
192 let nacked = Arc::new(AtomicBool::new(false));
193 (
194 Self {
195 acked: acked.clone(),
196 nacked: nacked.clone(),
197 },
198 acked,
199 nacked,
200 )
201 }
202 }
203
204 #[async_trait]
205 impl AckHandle for MockAckHandle {
206 async fn ack(&self) -> WorkerResult<()> {
207 self.acked.store(true, Ordering::SeqCst);
208 Ok(())
209 }
210
211 async fn nack(&self, _requeue: bool) -> WorkerResult<()> {
212 self.nacked.store(true, Ordering::SeqCst);
213 Ok(())
214 }
215 }
216
217 #[tokio::test]
218 async fn test_message_creation() {
219 let message = Message {
220 id: "test-1".to_string(),
221 payload: "test data",
222 metadata: MessageMetadata::new("test-queue"),
223 };
224
225 assert_eq!(message.id, "test-1");
226 assert_eq!(message.payload, "test data");
227 assert_eq!(message.metadata.attempt, 0); }
229
230 #[tokio::test]
231 async fn test_received_message_ack() {
232 let (ack_handle, acked, _) = MockAckHandle::new();
233 let message = Message {
234 id: "test-1".to_string(),
235 payload: "test data",
236 metadata: MessageMetadata::new("test-queue"),
237 };
238 let received = ReceivedMessage::new(message, Arc::new(ack_handle));
239
240 received.ack().await.unwrap();
241 assert!(acked.load(Ordering::SeqCst));
242 }
243
244 #[tokio::test]
245 async fn test_received_message_nack() {
246 let (ack_handle, _, nacked) = MockAckHandle::new();
247 let message = Message {
248 id: "test-1".to_string(),
249 payload: "test data",
250 metadata: MessageMetadata::new("test-queue"),
251 };
252 let received = ReceivedMessage::new(message, Arc::new(ack_handle));
253
254 received.nack(true).await.unwrap();
255 assert!(nacked.load(Ordering::SeqCst));
256 }
257
258 #[tokio::test]
259 async fn test_metadata_with_correlation_id() {
260 let metadata = MessageMetadata::new("test-queue").with_correlation_id("corr-123");
261
262 assert_eq!(metadata.correlation_id, Some("corr-123".to_string()));
263 }
264
265 #[tokio::test]
266 async fn test_metadata_increment_attempt() {
267 let mut metadata = MessageMetadata::new("test-queue");
268 assert_eq!(metadata.attempt, 0); metadata.increment_attempt();
271 assert_eq!(metadata.attempt, 1); }
273}