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
use crate::codec::types::QoS;
/// Options for connecting to an MQTT broker.
#[derive(Debug, Clone)]
pub struct ConnectOptions {
pub client_id: String,
pub keep_alive_secs: u16,
pub clean_start: bool,
pub username: Option<String>,
pub password: Option<Vec<u8>>,
/// When true, the `replyTo` field in request envelopes uses dots instead
/// of slashes (e.g. `egress.reply.{id}` instead of `egress/reply/{id}`).
///
/// Enable this when the reply publisher is an AMQP consumer (e.g. behind
/// RabbitMQ's MQTT plugin), since AMQP routing keys use dots while MQTT
/// topics use slashes. The MQTT plugin bridges between the two formats.
pub amqp_reply_format: bool,
}
impl Default for ConnectOptions {
fn default() -> Self {
Self {
client_id: String::new(), // empty = broker assigns one
keep_alive_secs: 60,
clean_start: true,
username: None,
password: None,
amqp_reply_format: false,
}
}
}
impl ConnectOptions {
/// Create options with the given client ID. Empty string lets the broker assign one.
pub fn new(client_id: impl Into<String>) -> Self {
Self {
client_id: client_id.into(),
..Default::default()
}
}
/// Set the keep-alive interval in seconds (default: 60).
pub fn with_keep_alive(mut self, secs: u16) -> Self {
self.keep_alive_secs = secs;
self
}
/// Set username and password for broker authentication.
pub fn with_credentials(mut self, user: impl Into<String>, pass: impl Into<Vec<u8>>) -> Self {
self.username = Some(user.into());
self.password = Some(pass.into());
self
}
pub fn with_clean_start(mut self, clean: bool) -> Self {
self.clean_start = clean;
self
}
/// Enable AMQP-compatible reply routing for RabbitMQ MQTT plugin.
///
/// When the reply publisher is an AMQP consumer (not an MQTT client),
/// `replyTo` must use dots (AMQP routing key format) while the MQTT
/// subscription uses slashes. RabbitMQ's MQTT plugin bridges between them.
pub fn with_amqp_reply_format(mut self, enabled: bool) -> Self {
self.amqp_reply_format = enabled;
self
}
}
/// Options for publishing a message.
#[derive(Debug, Clone)]
pub struct PublishOptions {
pub qos: QoS,
pub retain: bool,
}
impl Default for PublishOptions {
fn default() -> Self {
Self {
qos: QoS::AtMostOnce,
retain: false,
}
}
}