1use core::fmt::{Display, Formatter};
26
27#[derive(core::fmt::Debug, Clone, PartialEq)]
28#[cfg_attr(feature = "defmt", derive(defmt::Format))]
29pub enum BufferError {
30 Utf8Error,
31 InsufficientBufferSize,
32 VariableByteIntegerError,
33 IdNotFound,
34 EncodingError,
35 DecodingError,
36 PacketTypeMismatch,
37 WrongPacketToDecode,
38 WrongPacketToEncode,
39 PropertyNotFound,
40}
41
42impl Display for BufferError {
43 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
44 match *self {
45 BufferError::Utf8Error => write!(f, "Error encountered during UTF8 decoding!"),
46 BufferError::InsufficientBufferSize => write!(f, "Buffer size is not sufficient for packet!"),
47 BufferError::VariableByteIntegerError => write!(f, "Error encountered during variable byte integer decoding / encoding!"),
48 BufferError::IdNotFound => write!(f, "Packet identifier not found!"),
49 BufferError::EncodingError => write!(f, "Error encountered during packet encoding!"),
50 BufferError::DecodingError => write!(f, "Error encountered during packet decoding!"),
51 BufferError::PacketTypeMismatch => write!(f, "Packet type not matched during decoding (Received different packet type than encode type)!"),
52 BufferError::WrongPacketToDecode => write!(f, "Not able to decode packet, this packet is used just for sending to broker, not receiving by client!"),
53 BufferError::WrongPacketToEncode => write!(f, "Not able to encode packet, this packet is used only from server to client not the opposite way!"),
54 BufferError::PropertyNotFound => write!(f, "Property with ID not found!")
55 }
56 }
57}
58#[derive(Debug, Clone, Default)]
60pub struct EncodedString<'a> {
61 pub string: &'a str,
62 pub len: u16,
63}
64
65impl EncodedString<'_> {
66 pub fn new() -> Self {
67 Self { string: "", len: 0 }
68 }
69
70 pub fn encoded_len(&self) -> u16 {
72 self.len + 2
73 }
74}
75
76#[derive(Debug, Clone, Default)]
78pub struct BinaryData<'a> {
79 pub bin: &'a [u8],
80 pub len: u16,
81}
82
83impl BinaryData<'_> {
84 pub fn new() -> Self {
85 Self { bin: &[0], len: 0 }
86 }
87 pub fn encoded_len(&self) -> u16 {
89 self.len + 2
90 }
91}
92
93#[derive(Debug, Clone, Default)]
95pub struct StringPair<'a> {
96 pub name: EncodedString<'a>,
97 pub value: EncodedString<'a>,
98}
99
100impl StringPair<'_> {
101 pub fn new() -> Self {
102 Self {
103 name: EncodedString::new(),
104 value: EncodedString::new(),
105 }
106 }
107 pub fn encoded_len(&self) -> u16 {
109 self.name.encoded_len() + self.value.encoded_len()
110 }
111}
112
113#[derive(Debug, Default)]
115pub struct TopicFilter<'a> {
116 pub filter: EncodedString<'a>,
117 pub sub_options: u8,
118}
119
120impl TopicFilter<'_> {
121 pub fn new() -> Self {
122 Self {
123 filter: EncodedString::new(),
124 sub_options: 0,
125 }
126 }
127
128 pub fn encoded_len(&self) -> u16 {
129 self.filter.len + 3
130 }
131}