strut_rabbitmq/repr/ingress/
exchange.rs

1use lapin::ExchangeKind as LapinExchangeKind;
2use std::fmt::{Display, Formatter};
3use strut_factory::Deserialize as StrutDeserialize;
4
5/// Name of the RabbitMQ built-in default exchange
6pub const EXCHANGE_DEFAULT: &str = "";
7
8/// Name of the RabbitMQ built-in `amq.direct` exchange
9pub const EXCHANGE_AMQ_DIRECT: &str = "amq.direct";
10
11/// Name of the RabbitMQ built-in `amq.fanout` exchange
12pub const EXCHANGE_AMQ_FANOUT: &str = "amq.fanout";
13
14/// Name of the RabbitMQ built-in `amq.headers` exchange
15pub const EXCHANGE_AMQ_HEADERS: &str = "amq.headers";
16
17/// Name of the RabbitMQ built-in `amq.match` exchange
18pub const EXCHANGE_AMQ_MATCH: &str = "amq.match";
19
20/// Name of the RabbitMQ built-in `amq.topic` exchange
21pub const EXCHANGE_AMQ_TOPIC: &str = "amq.topic";
22
23/// Represents the supported kinds of RabbitMQ exchanges.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, StrutDeserialize)]
25#[strut(eq_fn = strut_deserialize::Slug::eq_as_slugs)]
26pub enum ExchangeKind {
27    /// A **direct** exchange delivers messages to queues based on the message
28    /// routing key.
29    Direct,
30
31    /// A **fanout** exchange routes messages to all the queues that are bound
32    /// to it, and the routing key is ignored.
33    #[strut(alias = "fan")]
34    Fanout,
35
36    /// A **headers** exchange is designed for routing on multiple attributes
37    /// that are more easily expressed as message headers than a routing key.
38    #[strut(alias = "header")]
39    Headers,
40
41    /// **Topic** exchanges route messages to one or many queues based on matching
42    /// between a message routing key and the pattern that was used to bind a
43    /// queue to an exchange.
44    Topic,
45
46    /// The **consistent-hash** exchange type uses consistent hashing to distribute
47    /// messages between the bound queues.
48    ///
49    /// This subtype applies the hashing to the **routing key** on the message
50    /// (not the routing key used when binding a queue to an exchange).
51    #[strut(
52        alias = "hash_keys",
53        alias = "hash_routing_key",
54        alias = "hash_routing_keys"
55    )]
56    HashKey,
57
58    /// The **consistent-hash** exchange type uses consistent hashing to distribute
59    /// messages between the bound queues.
60    ///
61    /// This subtype applies the hashing to the **message ID**.
62    #[strut(
63        alias = "hash_ids",
64        alias = "hash_message_id",
65        alias = "hash_message_ids"
66    )]
67    HashId,
68}
69
70impl ExchangeKind {
71    /// Returns the [`lapin::ExchangeKind`] value corresponding to this exchange
72    /// kind.
73    pub fn lapin_value(&self) -> LapinExchangeKind {
74        match self {
75            Self::Direct => LapinExchangeKind::Direct,
76            Self::Fanout => LapinExchangeKind::Fanout,
77            Self::Headers => LapinExchangeKind::Headers,
78            Self::Topic => LapinExchangeKind::Topic,
79            Self::HashKey | Self::HashId => LapinExchangeKind::Custom("x-consistent-hash".into()),
80        }
81    }
82}
83
84impl Display for ExchangeKind {
85    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
86        f.write_str(match self {
87            ExchangeKind::Direct => "direct",
88            ExchangeKind::Fanout => "fanout",
89            ExchangeKind::Headers => "headers",
90            ExchangeKind::Topic => "topic",
91            ExchangeKind::HashKey => "x-consistent-hash",
92            ExchangeKind::HashId => "x-consistent-hash",
93        })
94    }
95}