mqtt_codec/proto.rs
1#[macro_export]
2macro_rules! const_enum {
3 ($name:ty : $repr:ty) => {
4 impl ::std::convert::From<$repr> for $name {
5 fn from(u: $repr) -> Self {
6 unsafe { ::std::mem::transmute(u) }
7 }
8 }
9
10 impl ::std::convert::Into<$repr> for $name {
11 fn into(self) -> $repr {
12 unsafe { ::std::mem::transmute(self) }
13 }
14 }
15 };
16}
17
18pub const DEFAULT_MQTT_LEVEL: u8 = 4;
19
20#[repr(u8)]
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum Protocol {
23 MQTT(u8),
24}
25
26impl Protocol {
27 pub fn name(self) -> &'static str {
28 match self {
29 Protocol::MQTT(_) => "MQTT",
30 }
31 }
32
33 pub fn level(self) -> u8 {
34 match self {
35 Protocol::MQTT(level) => level,
36 }
37 }
38}
39
40impl Default for Protocol {
41 fn default() -> Self {
42 Protocol::MQTT(DEFAULT_MQTT_LEVEL)
43 }
44}
45
46#[repr(u8)]
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48/// Quality of Service levels
49pub enum QoS {
50 /// At most once delivery
51 ///
52 /// The message is delivered according to the capabilities of the underlying network.
53 /// No response is sent by the receiver and no retry is performed by the sender.
54 /// The message arrives at the receiver either once or not at all.
55 AtMostOnce = 0,
56 /// At least once delivery
57 ///
58 /// This quality of service ensures that the message arrives at the receiver at least once.
59 /// A QoS 1 PUBLISH Packet has a Packet Identifier in its variable header
60 /// and is acknowledged by a PUBACK Packet.
61 AtLeastOnce = 1,
62 /// Exactly once delivery
63 ///
64 /// This is the highest quality of service,
65 /// for use when neither loss nor duplication of messages are acceptable.
66 /// There is an increased overhead associated with this quality of service.
67 ExactlyOnce = 2,
68}
69
70const_enum!(QoS: u8);