rust_mqtt/utils/
types.rs

1/*
2 * MIT License
3 *
4 * Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25use 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/// Encoded string provides structure representing UTF-8 encoded string in MQTTv5 packets
59#[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    /// Return length of string
71    pub fn encoded_len(&self) -> u16 {
72        self.len + 2
73    }
74}
75
76/// Binary data represents `Binary data` in MQTTv5 protocol
77#[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    /// Returns length of Byte array
88    pub fn encoded_len(&self) -> u16 {
89        self.len + 2
90    }
91}
92
93/// String pair struct represents `String pair` in MQTTv5 (2 UTF-8 encoded strings name-value)
94#[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    /// Returns length which is equal to sum of the lenghts of UTF-8 encoded strings in pair
108    pub fn encoded_len(&self) -> u16 {
109        self.name.encoded_len() + self.value.encoded_len()
110    }
111}
112
113/// Topic filter serves as bound for topic selection and subscription options for `SUBSCRIPTION` packet
114#[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}