knien 0.0.10

Typed RabbitMQ interfacing for async Rust
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
487
488
489
490
491
492
493
494
use std::marker::PhantomData;
use std::{any::type_name, fmt::Display};

use async_trait::async_trait;
use lapin::options::QueueDeclareOptions;
use regex::Regex;
use serde::{Deserialize, Serialize};
use tracing::debug;
use uuid::Uuid;

use crate::{fmt_correlation_id, Bus, Channel, Connection, Consumer, Publisher, Result};

/// A Topic Exchange
pub trait TopicExchange: Clone + Send + Sync {
    /// The name of the Topic Exchange
    const NAME: &'static str;
}

/// A bus that is associated with a [TopicExchange], and defines a
/// pattern of topics onto which messages can be publised and consumed
pub trait TopicBus: Bus {
    /// The Topic Exchange this bus is associated with
    type Exchange: TopicExchange;
    /// The pattern of the topic that this bus publishes to or consumes from
    /// May contain `*` to indicate single-word wildcards, but not `#`
    const TOPIC_PATTERN: &'static str;
}

#[derive(Clone)]
/// A Topic Channel associated with a [TopicExchange].
pub struct TopicChannel<E> {
    inner: lapin::Channel,
    _marker: PhantomData<E>,
}

impl<E: TopicExchange> TopicChannel<E> {
    /// Create a new [TopicChannel], declaring the Topic Exchange
    /// this channel is associated with.
    pub async fn new(connection: &Connection) -> Result<Self> {
        let chan = connection.inner.create_channel().await?;
        chan.exchange_declare(
            E::NAME,
            lapin::ExchangeKind::Topic,
            Default::default(),
            Default::default(),
        )
        .await?;
        debug!("Created topic channel for exchange {}", E::NAME);
        Ok(Self {
            inner: chan,
            _marker: PhantomData,
        })
    }

    /// Create a new [Consumer] for the topic bus that consumes
    /// messages with routing keys matching the passed [ConsumerRoutingKey].
    pub async fn consumer<B: TopicBus>(
        &self,
        routing_key: ConsumerRoutingKey<B>,
        consumer_tag: &str,
    ) -> Result<Consumer<B>> {
        let queue = self
            .inner
            .queue_declare(
                "",
                QueueDeclareOptions {
                    // Clean up queue on channel disconnection
                    exclusive: true,
                    ..Default::default()
                },
                Default::default(),
            )
            .await?;
        let queue_name = queue.name().as_str();
        self.inner
            .queue_bind(
                queue_name,
                E::NAME,
                &routing_key.key,
                Default::default(),
                Default::default(),
            )
            .await?;
        let consumer = self
            .inner
            .basic_consume(
                queue_name,
                consumer_tag,
                Default::default(),
                Default::default(),
            )
            .await?;
        debug!(
            "Created consumer for topic bus {} with routing key {routing_key} and consumer tag {consumer_tag}",
            type_name::<B>()
        );
        Ok(Consumer {
            inner: consumer,
            _marker: PhantomData,
        })
    }

    /// Create a new [Publisher] that publishes onto the [TopicBus].
    pub fn publisher<B: TopicBus<Chan = Self>>(&self) -> Publisher<B> {
        debug!("Created publisher for topic bus {}", type_name::<B>());
        Publisher { chan: self.clone() }
    }
}

#[async_trait]
impl<E: TopicExchange> Channel for TopicChannel<E> {
    async fn publish_with_properties(
        &self,
        payload_bytes: &[u8],
        routing_key: &str,
        properties: lapin::BasicProperties,
        correlation_uuid: Uuid,
        reply_uuid: Option<Uuid>,
    ) -> Result<()> {
        let correlation_id = fmt_correlation_id(correlation_uuid, reply_uuid);
        let properties = properties.with_correlation_id(correlation_id.into());

        debug!("Publishing message with correlation UUID {correlation_uuid} on Topic Exchange '{}' with routing key {routing_key}", E::NAME);
        self.inner
            .basic_publish(
                E::NAME,
                routing_key,
                Default::default(),
                payload_bytes,
                properties,
            )
            .await?;

        Ok(())
    }
}

impl<'p, B> Publisher<B>
where
    B: TopicBus,
    B::PublishPayload: Deserialize<'p> + Serialize,
{
    /// Publish a message onto a topic on the exchange associated with the [TopicBus] for this [Publisher] with the passed [PublisherRoutingKey].
    pub async fn publish_topic(
        &self,
        routing_key: PublisherRoutingKey<B>,
        payload: &B::PublishPayload,
    ) -> Result<()> {
        let correlation_uuid = Uuid::new_v4();
        self.publish_with_properties(
            &routing_key.key,
            payload,
            Default::default(),
            correlation_uuid,
            None,
        )
        .await
    }
}

/// A Routing key that can be used to consume messages from a [TopicBus].
/// [ConsumerRoutingKey]s cannot contain `#` and must be at least as
/// concrete as [TopicBus::TOPIC_PATTERN].
#[derive(Debug)]
pub struct ConsumerRoutingKey<B> {
    key: String,
    _marker: PhantomData<B>,
}

impl<B: TopicBus> TryFrom<String> for ConsumerRoutingKey<B> {
    type Error = RoutingKeyError;

    fn try_from(key: String) -> std::result::Result<Self, Self::Error> {
        // Don't accept pounds
        if key.contains('#') {
            return Err(RoutingKeyError::InvalidKey(key, B::TOPIC_PATTERN));
        }
        if key.starts_with('.') || key.ends_with('.') || key.contains("..") {
            return Err(RoutingKeyError::InvalidKey(key, B::TOPIC_PATTERN));
        }
        if key.matches('.').count() != B::TOPIC_PATTERN.matches('.').count() {
            return Err(RoutingKeyError::InvalidKey(key, B::TOPIC_PATTERN));
        }

        let pattern = B::TOPIC_PATTERN
            .replace('.', r"\.")
            .replace('*', r"([[:alnum:]]*|\*)");

        let regex = Regex::new(&format!("^{pattern}$")).unwrap();
        if !regex.is_match(&key) {
            return Err(RoutingKeyError::InvalidKey(key, B::TOPIC_PATTERN));
        }
        Ok(Self {
            key,
            _marker: PhantomData,
        })
    }
}

impl<B: TopicBus> TryFrom<&str> for ConsumerRoutingKey<B> {
    type Error = <Self as TryFrom<String>>::Error;

    fn try_from(key: &str) -> std::result::Result<Self, Self::Error> {
        <Self as TryFrom<String>>::try_from(key.to_owned())
    }
}

impl<B> Display for ConsumerRoutingKey<B> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.key.fmt(f)
    }
}

/// A Routing key that can be used to publish messages on a [TopicBus].
/// Can only represent concrete routing keys, i.e. routing keys cannot contain wildcards.
#[derive(Debug)]
pub struct PublisherRoutingKey<B> {
    key: String,
    _marker: PhantomData<B>,
}

impl<B: TopicBus> TryFrom<String> for PublisherRoutingKey<B> {
    type Error = RoutingKeyError;

    fn try_from(key: String) -> std::result::Result<Self, Self::Error> {
        // Don't accept wildcards
        if key.contains('*') || key.contains('#') {
            return Err(RoutingKeyError::AbstractPublishKey(key));
        }
        if key.starts_with('.') || key.ends_with('.') || key.contains("..") {
            return Err(RoutingKeyError::InvalidKey(key, B::TOPIC_PATTERN));
        }
        if key.matches('.').count() != B::TOPIC_PATTERN.matches('.').count() {
            return Err(RoutingKeyError::InvalidKey(key, B::TOPIC_PATTERN));
        }

        let pattern = B::TOPIC_PATTERN
            .replace('.', r"\.")
            .replace('*', r"[[:alnum:]]*");

        let regex = Regex::new(&format!("^{pattern}$")).unwrap();
        if !regex.is_match(&key) {
            return Err(RoutingKeyError::InvalidKey(key, B::TOPIC_PATTERN));
        }
        Ok(Self {
            key,
            _marker: PhantomData,
        })
    }
}

impl<B: TopicBus> TryFrom<&str> for PublisherRoutingKey<B> {
    type Error = <Self as TryFrom<String>>::Error;

    fn try_from(key: &str) -> std::result::Result<Self, Self::Error> {
        <Self as TryFrom<String>>::try_from(key.to_owned())
    }
}

impl<B> Display for PublisherRoutingKey<B> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.key.fmt(f)
    }
}

#[derive(Debug)]
/// Error indicating what went wrong in setting up a [ConsumerRoutingKey] or a [PublisherRoutingKey]
pub enum RoutingKeyError {
    /// Got a routing key that does not match the topic pattern associated with the [TopicBus].
    InvalidKey(String, &'static str),
    /// Got a publish routing key that contained wildcards.
    AbstractPublishKey(String),
}

impl Display for RoutingKeyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RoutingKeyError::InvalidKey(key, topic) => {
                write!(f, "Routing key {key} is not valid for topic {topic}")
            }
            RoutingKeyError::AbstractPublishKey(key) => write!(
                f,
                "Routing key meant for publishing cannot contain wildcards: {key}"
            ),
        }
    }
}

impl std::error::Error for RoutingKeyError {}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use crate::{
        topic_bus, topic_exchange, Connection, Consumer, ConsumerRoutingKey, FramePayload,
        Publisher, PublisherRoutingKey, TopicChannel, RABBIT_MQ_URL,
    };
    use futures::StreamExt;
    use test_case::test_case;
    use tokio::{sync::oneshot, time::timeout};
    use uuid::Uuid;

    topic_exchange!(MyExchange, "the_exchange");

    topic_bus!(
        MyTopic,
        FramePayload,
        MyExchange,
        "frame.*.*",
        serde_json::to_vec,
        serde_json::from_slice
    );

    #[tokio::test]
    async fn publish() -> crate::Result<()> {
        let connection = Connection::connect(RABBIT_MQ_URL).await.unwrap();
        let uuid = Uuid::new_v4();
        let (tx, rx) = oneshot::channel();
        tokio::task::spawn({
            let channel: TopicChannel<MyExchange> = TopicChannel::new(&connection).await.unwrap();
            let mut consumer: Consumer<MyTopic> = channel
                .consumer("frame.*.*".try_into().unwrap(), &Uuid::new_v4().to_string())
                .await?;
            async move {
                let msg = consumer.next().await.unwrap().unwrap();
                msg.ack(false).await.unwrap();
                let payload = msg.get_payload().unwrap();
                assert_eq!(payload.message, uuid.to_string());
                tx.send(()).unwrap();
            }
        });

        let channel: TopicChannel<MyExchange> = TopicChannel::new(&connection).await.unwrap();
        let publisher: Publisher<MyTopic> = channel.publisher();

        publisher
            .publish_topic(
                "frame.1.2".try_into().unwrap(),
                &FramePayload {
                    message: uuid.to_string(),
                },
            )
            .await
            .unwrap();

        timeout(Duration::from_secs(3), rx).await.unwrap().unwrap();

        Ok(())
    }

    #[test_case("frame.123.456"; "1")]
    fn test_valid_publish_routing_key(key: &str) {
        PublisherRoutingKey::<MyTopic>::try_from(key.to_owned()).unwrap();
    }

    #[test_case(""; "Empty 1")]
    #[test_case("frame..#"; "Double '.' 2")]
    #[test_case("frame.**.#"; "Double '*' 3")]
    #[test_case("frame.##"; "Double '#' 4")]
    #[test_case("frame"; "Too short 5")]
    #[test_case("frame.123"; "Too short 6")]
    #[test_case("frame.123.*"; "Abstract 7")]
    #[test_case("frame.*.456"; "Abstract 8")]
    #[test_case("frame.*.*"; "Abstract 9")]
    #[test_case("#"; "Abstract 10")]
    #[test_case("test.123.456"; "Invalid prefix 11")]
    #[test_case("frame.123.456.789"; "Too long 12")]
    #[test_case("frame.@.&"; "Invalid characters 13")]
    fn test_invalid_publish_routing_key(key: &str) {
        PublisherRoutingKey::<MyTopic>::try_from(key.to_owned()).unwrap_err();
    }

    #[test_case("frame.*.*"; "1")]
    #[test_case("frame.123.*"; "2")]
    #[test_case("frame.*.456"; "3")]
    #[test_case("frame.123.456"; "4")]
    fn test_valid_consume_routing_key(key: &str) {
        ConsumerRoutingKey::<MyTopic>::try_from(key.to_owned()).unwrap();
    }

    #[test_case(""; "Empty 1")]
    #[test_case("frame..#"; "Double '.' 2")]
    #[test_case("frame.**.#"; "Double '*' 3")]
    #[test_case("frame.##"; "Double '#'4")]
    #[test_case("*"; "More abstract than pattern 5")]
    // RabbitMQ accepts this, but it would result in consumption of all messages on the topic exchange
    #[test_case("#"; "Hash 6")]
    #[test_case("*.*.*"; "More abstract than pattern 7")]
    #[test_case("frame.*.*.*"; "Too long 8")]
    #[test_case("test.*.*"; "Invalid word 9")]
    #[test_case("frame.123.*.*"; "Too long 10")]
    #[test_case("frame.*.456.*"; "Too long 11")]
    #[test_case("frame.*.*.789"; "Too long 12")]
    #[test_case("frame.123.*.789"; "Too long 13")]
    #[test_case("frame.123.456.*"; "Too long 14")]
    #[test_case("frame.*.456.789"; "Too long 15")]
    #[test_case("frame.124.456.789"; "Too long 16")]
    #[test_case("frame.#.456.789"; "Too long 17")]
    #[test_case("#.456.789"; "Not starting with 'frame' 18")]
    #[test_case("#.frame.456.789"; "Too long 19")]
    #[test_case("frame.#"; "Contains pound sign 20")]
    #[test_case("frame.#.*"; "Contains pound sign 21")]
    #[test_case("frame.*.#"; "Contains pound sign 22")]
    #[test_case("frame.#.456"; "Contains pound sign 23")]
    #[test_case("frame.123.#"; "Contains pound sign 24")]
    fn test_invalid_consume_routing_key(key: &str) {
        ConsumerRoutingKey::<MyTopic>::try_from(key.to_owned()).unwrap_err();
    }
}

#[macro_export]
/// Declare a new [TopicExchange], specifying its type identifier and name.
macro_rules! topic_exchange {
    ($doc:literal, $exchange:ident, $name:literal) => {
        #[derive(Debug, Clone)]
        #[doc = $doc]
        pub enum $exchange {}

        impl $crate::TopicExchange for $exchange {
            const NAME: &'static str = $name;
        }
    };
    (doc = $doc:literal, exchange = $exchange:ident, name = $name:literal) => {
        $crate::topic_exchange!($doc, $exchange, $name);
    };
    ($exchange:ident, $name:literal) => {
        $crate::topic_exchange!("", $exchange, $name);
    };
    (exchange = $exchange:ident, name = $name:literal) => {
        $crate::topic_exchange!($exchange, $name);
    };
}

#[macro_export]
/// Declare a new [TopicBus].
macro_rules! topic_bus {
    ($doc:literal, $bus:ident, $publish_payload:ty, $exchange:ty, $topic:literal, $serialize:expr, $deserialize:expr) => {
        $crate::bus!($doc, $bus);

        $crate::bus_impl!(
            $bus,
            $crate::TopicChannel<$exchange>,
            $publish_payload,
            $serialize,
            $deserialize
        );

        $crate::topic_bus_impl!($bus, $exchange, $topic);
    };
    (doc = $doc:literal, bus = $bus:ident, publish = $publish_payload:ty, exchange = $exchange:ty, topic = $topic:literal, serialize = $serialize:expr, deserialize = $deserialize:expr) => {
        $crate::topic_bus!(
            $doc,
            $bus,
            $publish_payload,
            $exchange,
            $topic,
            $serialize,
            $deserialize
        );
    };
    ($bus:ident, $publish_payload:ty, $exchange:ty, $topic:literal, $serialize:expr, $deserialize:expr) => {
        $crate::topic_bus!(
            "",
            $bus,
            $publish_payload,
            $exchange,
            $topic,
            $serialize,
            $deserialize
        );
    };
    (bus = $bus:ident, publish = $publish_payload:ty, exchange = $exchange:ty, topic = $topic:literal , serialize = $serialize:expr, deserialize = $deserialize:expr) => {
        $crate::topic_bus!(
            $bus,
            $publish_payload,
            $exchange,
            $topic,
            $serialize,
            $deserialize
        );
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! topic_bus_impl {
    ($bus:ident, $exchange:ty, $topic:literal) => {
        impl $crate::TopicBus for $bus {
            type Exchange = $exchange;
            const TOPIC_PATTERN: &'static str = $topic;
        }
    };
}