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
use core::fmt::{Display, Formatter};
#[derive(core::fmt::Debug, Clone, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum BufferError {
Utf8Error,
InsufficientBufferSize,
VariableByteIntegerError,
IdNotFound,
EncodingError,
DecodingError,
PacketTypeMismatch,
WrongPacketToDecode,
WrongPacketToEncode,
PropertyNotFound,
}
impl Display for BufferError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match *self {
BufferError::Utf8Error => write!(f, "Error encountered during UTF8 decoding!"),
BufferError::InsufficientBufferSize => write!(f, "Buffer size is not sufficient for packet!"),
BufferError::VariableByteIntegerError => write!(f, "Error encountered during variable byte integer decoding / encoding!"),
BufferError::IdNotFound => write!(f, "Packet identifier not found!"),
BufferError::EncodingError => write!(f, "Error encountered during packet encoding!"),
BufferError::DecodingError => write!(f, "Error encountered during packet decoding!"),
BufferError::PacketTypeMismatch => write!(f, "Packet type not matched during decoding (Received different packet type than encode type)!"),
BufferError::WrongPacketToDecode => write!(f, "Not able to decode packet, this packet is used just for sending to broker, not receiving by client!"),
BufferError::WrongPacketToEncode => write!(f, "Not able to encode packet, this packet is used only from server to client not the opposite way!"),
BufferError::PropertyNotFound => write!(f, "Property with ID not found!")
}
}
}
#[derive(Debug, Clone, Default)]
pub struct EncodedString<'a> {
pub string: &'a str,
pub len: u16,
}
impl EncodedString<'_> {
pub fn new() -> Self {
Self { string: "", len: 0 }
}
pub fn encoded_len(&self) -> u16 {
self.len + 2
}
}
#[derive(Debug, Clone, Default)]
pub struct BinaryData<'a> {
pub bin: &'a [u8],
pub len: u16,
}
impl BinaryData<'_> {
pub fn new() -> Self {
Self { bin: &[0], len: 0 }
}
pub fn encoded_len(&self) -> u16 {
self.len + 2
}
}
#[derive(Debug, Clone, Default)]
pub struct StringPair<'a> {
pub name: EncodedString<'a>,
pub value: EncodedString<'a>,
}
impl StringPair<'_> {
pub fn new() -> Self {
Self {
name: EncodedString::new(),
value: EncodedString::new(),
}
}
pub fn encoded_len(&self) -> u16 {
self.name.encoded_len() + self.value.encoded_len()
}
}
#[derive(Debug, Default)]
pub struct TopicFilter<'a> {
pub filter: EncodedString<'a>,
pub sub_options: u8,
}
impl TopicFilter<'_> {
pub fn new() -> Self {
Self {
filter: EncodedString::new(),
sub_options: 0,
}
}
pub fn encoded_len(&self) -> u16 {
self.filter.len + 3
}
}