armature-messaging 0.1.0

Message broker integrations for the Armature framework - RabbitMQ, Kafka, NATS, and AWS SQS/SNS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Message types and utilities.

use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::collections::HashMap;
use std::time::SystemTime;
use uuid::Uuid;

/// A message with typed payload.
#[derive(Debug, Clone)]
pub struct Message<T> {
    /// Unique message ID
    pub id: String,
    /// Message payload
    pub payload: T,
    /// Message headers/properties
    pub headers: HashMap<String, String>,
    /// Routing key (for RabbitMQ) or topic (for Kafka/NATS)
    pub routing_key: String,
    /// Timestamp when the message was created
    pub timestamp: SystemTime,
    /// Content type
    pub content_type: String,
    /// Correlation ID for request-reply patterns
    pub correlation_id: Option<String>,
    /// Reply-to address
    pub reply_to: Option<String>,
    /// Message priority (0-9)
    pub priority: Option<u8>,
    /// Delivery tag for acknowledgment
    pub delivery_tag: Option<u64>,
    /// Whether the message was redelivered
    pub redelivered: bool,
}

impl<T> Message<T> {
    /// Create a new message with the given payload.
    pub fn new(payload: T) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            payload,
            headers: HashMap::new(),
            routing_key: String::new(),
            timestamp: SystemTime::now(),
            content_type: "application/json".to_string(),
            correlation_id: None,
            reply_to: None,
            priority: None,
            delivery_tag: None,
            redelivered: false,
        }
    }

    /// Create a new message with routing key.
    pub fn with_routing_key(payload: T, routing_key: &str) -> Self {
        let mut msg = Self::new(payload);
        msg.routing_key = routing_key.to_string();
        msg
    }

    /// Set a header.
    pub fn header(mut self, key: &str, value: &str) -> Self {
        self.headers.insert(key.to_string(), value.to_string());
        self
    }

    /// Set the correlation ID.
    pub fn with_correlation_id(mut self, id: &str) -> Self {
        self.correlation_id = Some(id.to_string());
        self
    }

    /// Set the reply-to address.
    pub fn with_reply_to(mut self, reply_to: &str) -> Self {
        self.reply_to = Some(reply_to.to_string());
        self
    }

    /// Set the priority.
    pub fn with_priority(mut self, priority: u8) -> Self {
        self.priority = Some(priority.min(9));
        self
    }

    /// Get the message age in seconds.
    pub fn age_seconds(&self) -> u64 {
        SystemTime::now()
            .duration_since(self.timestamp)
            .map(|d| d.as_secs())
            .unwrap_or(0)
    }
}

impl<T: Serialize> Message<T> {
    /// Serialize the message payload to JSON bytes.
    pub fn to_bytes(&self) -> Result<Vec<u8>, serde_json::Error> {
        serde_json::to_vec(&self.payload)
    }
}

impl<T: DeserializeOwned> Message<T> {
    /// Create a message from raw bytes.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, serde_json::Error> {
        let payload: T = serde_json::from_slice(bytes)?;
        Ok(Self::new(payload))
    }
}

/// Builder for creating publish options.
#[derive(Debug, Clone, Default)]
pub struct PublishOptions {
    /// Exchange name (RabbitMQ) or topic prefix
    pub exchange: Option<String>,
    /// Routing key (RabbitMQ) or topic (Kafka/NATS)
    pub routing_key: String,
    /// Whether the message should be mandatory (RabbitMQ)
    pub mandatory: bool,
    /// Whether the message should be persistent
    pub persistent: bool,
    /// Message expiration in milliseconds
    pub expiration: Option<u64>,
    /// Headers to include
    pub headers: HashMap<String, String>,
    /// Correlation ID
    pub correlation_id: Option<String>,
    /// Reply-to queue/topic
    pub reply_to: Option<String>,
    /// Priority (0-9)
    pub priority: Option<u8>,
    /// Partition key (Kafka)
    pub partition_key: Option<String>,
}

impl PublishOptions {
    /// Create new publish options with routing key.
    pub fn new(routing_key: &str) -> Self {
        Self {
            routing_key: routing_key.to_string(),
            persistent: true,
            ..Default::default()
        }
    }

    /// Set the exchange.
    pub fn exchange(mut self, exchange: &str) -> Self {
        self.exchange = Some(exchange.to_string());
        self
    }

    /// Set as mandatory.
    pub fn mandatory(mut self) -> Self {
        self.mandatory = true;
        self
    }

    /// Set as non-persistent.
    pub fn transient(mut self) -> Self {
        self.persistent = false;
        self
    }

    /// Set expiration.
    pub fn expires_in(mut self, ms: u64) -> Self {
        self.expiration = Some(ms);
        self
    }

    /// Add a header.
    pub fn header(mut self, key: &str, value: &str) -> Self {
        self.headers.insert(key.to_string(), value.to_string());
        self
    }

    /// Set correlation ID.
    pub fn correlation_id(mut self, id: &str) -> Self {
        self.correlation_id = Some(id.to_string());
        self
    }

    /// Set reply-to.
    pub fn reply_to(mut self, reply_to: &str) -> Self {
        self.reply_to = Some(reply_to.to_string());
        self
    }

    /// Set priority.
    pub fn priority(mut self, priority: u8) -> Self {
        self.priority = Some(priority.min(9));
        self
    }

    /// Set partition key (Kafka).
    pub fn partition(mut self, key: &str) -> Self {
        self.partition_key = Some(key.to_string());
        self
    }
}

/// Options for consuming messages.
#[derive(Debug, Clone)]
pub struct ConsumeOptions {
    /// Queue name (RabbitMQ) or topic (Kafka/NATS)
    pub queue: String,
    /// Consumer tag
    pub consumer_tag: Option<String>,
    /// Whether to auto-acknowledge messages
    pub auto_ack: bool,
    /// Whether this is an exclusive consumer
    pub exclusive: bool,
    /// Number of messages to prefetch
    pub prefetch: Option<u16>,
    /// Consumer group (Kafka)
    pub consumer_group: Option<String>,
}

impl ConsumeOptions {
    /// Create new consume options.
    pub fn new(queue: &str) -> Self {
        Self {
            queue: queue.to_string(),
            consumer_tag: None,
            auto_ack: false,
            exclusive: false,
            prefetch: None,
            consumer_group: None,
        }
    }

    /// Set consumer tag.
    pub fn tag(mut self, tag: &str) -> Self {
        self.consumer_tag = Some(tag.to_string());
        self
    }

    /// Enable auto-acknowledge.
    pub fn auto_ack(mut self) -> Self {
        self.auto_ack = true;
        self
    }

    /// Set as exclusive consumer.
    pub fn exclusive(mut self) -> Self {
        self.exclusive = true;
        self
    }

    /// Set prefetch count.
    pub fn prefetch(mut self, count: u16) -> Self {
        self.prefetch = Some(count);
        self
    }

    /// Set consumer group (Kafka).
    pub fn group(mut self, group: &str) -> Self {
        self.consumer_group = Some(group.to_string());
        self
    }
}

/// Acknowledgment result for a message.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AckResult {
    /// Message was acknowledged successfully
    Acked,
    /// Message was rejected and should be requeued
    Requeued,
    /// Message was rejected and should be discarded/dead-lettered
    Rejected,
}

/// Raw message bytes received from a broker.
#[derive(Debug, Clone)]
pub struct RawMessage {
    /// Raw message body
    pub body: Vec<u8>,
    /// Message headers
    pub headers: HashMap<String, String>,
    /// Routing key or topic
    pub routing_key: String,
    /// Delivery tag for acknowledgment
    pub delivery_tag: u64,
    /// Whether the message was redelivered
    pub redelivered: bool,
    /// Exchange/topic the message was received from
    pub exchange: String,
    /// Timestamp
    pub timestamp: Option<SystemTime>,
    /// Correlation ID
    pub correlation_id: Option<String>,
    /// Reply-to
    pub reply_to: Option<String>,
    /// Message ID
    pub message_id: Option<String>,
}

impl RawMessage {
    /// Deserialize the message body into a typed payload.
    pub fn deserialize<T: DeserializeOwned>(&self) -> Result<Message<T>, serde_json::Error> {
        let payload: T = serde_json::from_slice(&self.body)?;
        Ok(Message {
            id: self.message_id.clone().unwrap_or_else(|| Uuid::new_v4().to_string()),
            payload,
            headers: self.headers.clone(),
            routing_key: self.routing_key.clone(),
            timestamp: self.timestamp.unwrap_or_else(SystemTime::now),
            content_type: "application/json".to_string(),
            correlation_id: self.correlation_id.clone(),
            reply_to: self.reply_to.clone(),
            priority: None,
            delivery_tag: Some(self.delivery_tag),
            redelivered: self.redelivered,
        })
    }

    /// Get the body as a string.
    pub fn body_str(&self) -> Result<&str, std::str::Utf8Error> {
        std::str::from_utf8(&self.body)
    }
}

/// Event published through the messaging system.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event<T> {
    /// Event type/name
    pub event_type: String,
    /// Event payload
    pub data: T,
    /// Timestamp
    pub timestamp: i64,
    /// Event source
    pub source: String,
    /// Correlation ID
    pub correlation_id: Option<String>,
    /// Causation ID (ID of the event that caused this event)
    pub causation_id: Option<String>,
}

impl<T: Serialize> Event<T> {
    /// Create a new event.
    pub fn new(event_type: &str, data: T) -> Self {
        Self {
            event_type: event_type.to_string(),
            data,
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_millis() as i64,
            source: String::new(),
            correlation_id: None,
            causation_id: None,
        }
    }

    /// Set the source.
    pub fn from_source(mut self, source: &str) -> Self {
        self.source = source.to_string();
        self
    }

    /// Set the correlation ID.
    pub fn with_correlation(mut self, id: &str) -> Self {
        self.correlation_id = Some(id.to_string());
        self
    }

    /// Set the causation ID.
    pub fn caused_by(mut self, id: &str) -> Self {
        self.causation_id = Some(id.to_string());
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct TestPayload {
        name: String,
        value: i32,
    }

    #[test]
    fn test_message_creation() {
        let msg = Message::new(TestPayload {
            name: "test".to_string(),
            value: 42,
        });

        assert!(!msg.id.is_empty());
        assert_eq!(msg.payload.name, "test");
        assert_eq!(msg.payload.value, 42);
        assert!(!msg.redelivered);
    }

    #[test]
    fn test_message_with_routing_key() {
        let msg = Message::with_routing_key(
            TestPayload {
                name: "test".to_string(),
                value: 42,
            },
            "orders.created",
        );

        assert_eq!(msg.routing_key, "orders.created");
    }

    #[test]
    fn test_message_headers() {
        let msg = Message::new(TestPayload {
            name: "test".to_string(),
            value: 42,
        })
        .header("x-tenant", "acme")
        .header("x-version", "1.0");

        assert_eq!(msg.headers.get("x-tenant"), Some(&"acme".to_string()));
        assert_eq!(msg.headers.get("x-version"), Some(&"1.0".to_string()));
    }

    #[test]
    fn test_message_serialization() {
        let msg = Message::new(TestPayload {
            name: "test".to_string(),
            value: 42,
        });

        let bytes = msg.to_bytes().unwrap();
        let deserialized: Message<TestPayload> = Message::from_bytes(&bytes).unwrap();

        assert_eq!(deserialized.payload.name, "test");
        assert_eq!(deserialized.payload.value, 42);
    }

    #[test]
    fn test_publish_options() {
        let opts = PublishOptions::new("orders.created")
            .exchange("events")
            .mandatory()
            .expires_in(60000)
            .priority(5);

        assert_eq!(opts.routing_key, "orders.created");
        assert_eq!(opts.exchange, Some("events".to_string()));
        assert!(opts.mandatory);
        assert_eq!(opts.expiration, Some(60000));
        assert_eq!(opts.priority, Some(5));
    }

    #[test]
    fn test_consume_options() {
        let opts = ConsumeOptions::new("orders-queue")
            .tag("my-consumer")
            .prefetch(100)
            .group("order-processors");

        assert_eq!(opts.queue, "orders-queue");
        assert_eq!(opts.consumer_tag, Some("my-consumer".to_string()));
        assert_eq!(opts.prefetch, Some(100));
        assert_eq!(opts.consumer_group, Some("order-processors".to_string()));
    }

    #[test]
    fn test_event() {
        let event = Event::new(
            "user.created",
            TestPayload {
                name: "John".to_string(),
                value: 1,
            },
        )
        .from_source("user-service")
        .with_correlation("req-123");

        assert_eq!(event.event_type, "user.created");
        assert_eq!(event.source, "user-service");
        assert_eq!(event.correlation_id, Some("req-123".to_string()));
    }

    #[test]
    fn test_priority_capped_at_9() {
        let msg = Message::new("test").with_priority(15);
        assert_eq!(msg.priority, Some(9));
    }
}