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
use ffi;
pub const SUBSCRIBE_NO_LOCAL: bool = true;
pub const SUBSCRIBE_LOCAL: bool = false;
pub const NO_RETAIN_AS_PUBLISHED: bool = false;
pub const RETAIN_AS_PUBLISHED: bool = true;
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum RetainHandling {
SEND_RETAINED_ON_SUBSCRIBE = 0,
SEND_RETAINED_ON_NEW = 1,
DONT_SEND_RETAINED = 2
}
#[derive(Debug, Default, Copy, Clone)]
pub struct SubscribeOptions {
pub(crate) copts: ffi::MQTTSubscribe_options,
}
impl SubscribeOptions {
pub fn new(no_local: bool) -> Self
{
SubscribeOptions {
copts: ffi::MQTTSubscribe_options {
noLocal: if no_local { 1 } else { 0 },
..ffi::MQTTSubscribe_options::default()
}
}
}
}
impl From<bool> for SubscribeOptions {
fn from(no_local: bool) -> Self {
SubscribeOptions::new(no_local)
}
}
impl From<(bool,bool)> for SubscribeOptions {
fn from((no_local, retain_as_published): (bool, bool)) -> Self {
let mut opts = SubscribeOptions::new(no_local);
opts.copts.retainAsPublished = if retain_as_published { 1 } else { 0 };
opts
}
}
impl From<(bool,bool,RetainHandling)> for SubscribeOptions {
fn from((no_local, retain_as_published, retain_handling): (bool, bool, RetainHandling)) -> Self {
let mut opts = SubscribeOptions::from((no_local, retain_as_published));
opts.copts.retainHandling = retain_handling as u8;
opts
}
}
#[derive(Debug, Default)]
pub struct SubscribeOptionsBuilder {
copts: ffi::MQTTSubscribe_options,
}
impl SubscribeOptionsBuilder {
pub fn new() -> Self {
SubscribeOptionsBuilder::default()
}
pub fn no_local(mut self, on: bool) -> Self {
self.copts.noLocal = if on { 1 } else { 0 };
self
}
pub fn retain_as_published(mut self, retain: bool) -> Self {
self.copts.retainAsPublished = if retain { 1 } else { 0 };
self
}
pub fn retain_handling(mut self, handling: RetainHandling) -> Self {
self.copts.retainHandling = handling as u8;
self
}
pub fn finalize(self) -> SubscribeOptions {
SubscribeOptions { copts: self.copts }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let opts = SubscribeOptions::new(false);
assert!(opts.copts.noLocal == 0);
let opts = SubscribeOptions::new(true);
assert!(opts.copts.noLocal != 0);
}
}