pub enum DataType {
Byte(u8),
TwoByteInteger(u16),
FourByteInteger(u32),
VariableByteInteger(VariableByte),
Utf8EncodedString(String),
BinaryData(Vec<u8>),
Utf8StringPair(String, String),
}
Expand description
Data types defined by the MQTT v5 spec.
Variants§
Byte(u8)
TwoByteInteger(u16)
FourByteInteger(u32)
VariableByteInteger(VariableByte)
Utf8EncodedString(String)
BinaryData(Vec<u8>)
Utf8StringPair(String, String)
Implementations§
Source§impl DataType
impl DataType
Sourcepub fn parse_byte<R: Read>(reader: &mut R) -> Result<Self, Error>
pub fn parse_byte<R: Read>(reader: &mut R) -> Result<Self, Error>
Reads one byte from the reader and attempts to convert the byte to DataType::Byte (u8).
Bits in a byte are labelled 7 to 0. Bit number 7 is the most significant bit, the least significant bit is assigned bit number 0.
§Examples
use mqtt_packet::DataType;
use std::io;
let data: Vec<u8> = vec![0xFF, 0x02];
let mut reader = io::BufReader::new(&data[..]);
let byte = DataType::parse_byte(&mut reader).unwrap();
assert_eq!(byte, DataType::Byte(255));
Sourcepub fn parse_two_byte_int<R: Read>(reader: &mut R) -> Result<Self, Error>
pub fn parse_two_byte_int<R: Read>(reader: &mut R) -> Result<Self, Error>
Reads two bytes from the reader and attempts to convert the bytes to DataType::TwoByteInteger (u16).
Two Byte Integer data values are 16-bit unsigned integers in big-endian order: the high order byte precedes the lower order byte. This means that a 16-bit word is presented on the network as Most Significant Byte (MSB), followed by Least Significant Byte (LSB).
§Examples
use mqtt_packet::DataType;
use std::io;
let data: Vec<u8> = vec![0x01, 0x02, 0x03];
let mut reader = io::BufReader::new(&data[..]);
let two = DataType::parse_two_byte_int(&mut reader).unwrap();
assert_eq!(two, DataType::TwoByteInteger(258));
Sourcepub fn parse_four_byte_int<R: Read>(reader: &mut R) -> Result<Self, Error>
pub fn parse_four_byte_int<R: Read>(reader: &mut R) -> Result<Self, Error>
Reads four bytes from the reader and attempts to convert the bytes to DataType::FourByteInteger (u32).
Four Byte Integer data values are 32-bit unsigned integers in big-endian order: the high order byte precedes the successively lower order bytes. This means that a 32-bit word is presented on the network as Most Significant Byte (MSB), followed by the next most Significant Byte (MSB), followed by the next most Significant Byte (MSB), followed by Least Significant Byte (LSB).
§Examples
use mqtt_packet::DataType;
use std::io;
let data: Vec<u8> = vec![0x01, 0x02, 0x03, 0x04, 0x05];
let mut reader = io::BufReader::new(&data[..]);
let four = DataType::parse_four_byte_int(&mut reader).unwrap();
assert_eq!(four, DataType::FourByteInteger(16909060));
Sourcepub fn parse_utf8_string<R: Read>(reader: &mut R) -> Result<Self, Error>
pub fn parse_utf8_string<R: Read>(reader: &mut R) -> Result<Self, Error>
Reads bytes from the reader and attempts to convert the bytes to DataType::Utf8EncodedString (String).
Text fields within the MQTT Control Packets described later are encoded as UTF-8 strings. UTF-8 is an efficient encoding of Unicode characters that optimizes the encoding of ASCII characters in support of text-based communications.
Each of these strings is prefixed with a Two Byte Integer length field that gives the number of bytes in a UTF-8 encoded string itself, as illustrated in Figure 1.1 Structure of UTF-8 Encoded Strings below. Consequently, the maximum size of a UTF-8 Encoded String is 65,535 bytes.
Unless stated otherwise all UTF-8 encoded strings can have any length in the range 0 to 65,535 bytes.
§Examples
use std::io;
use mqtt_packet::DataType;
let data: Vec<u8> = vec![
0, 11, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 100, 100, 100,
];
let mut reader = io::BufReader::new(&data[..]);
let result = DataType::parse_utf8_string(&mut reader).unwrap();
assert_eq!(
result,
DataType::Utf8EncodedString(String::from("hello world"))
);
Sourcepub fn parse_variable_byte_int<R: Read>(reader: &mut R) -> Result<Self, Error>
pub fn parse_variable_byte_int<R: Read>(reader: &mut R) -> Result<Self, Error>
Reads bytes from the reader and attempts to convert the bytes to DataType::VariableByteInteger (u8, 16, or u32).
The Variable Byte Integer is encoded using an encoding scheme which uses a single byte for values up to 127. Larger values are handled as follows. The least significant seven bits of each byte encode the data, and the most significant bit is used to indicate whether there are bytes following in the representation. Thus, each byte encodes 128 values and a “continuation bit”. The maximum number of bytes in the Variable Byte Integer field is four. The encoded value MUST use the minimum number of bytes necessary to represent the value.
The algorithm for decoding a Variable Byte Integer type is as follows:
multiplier = 1
value = 0
do
encodedByte = 'next byte from stream'
value += (encodedByte AND 127) * multiplier
if (multiplier > 128*128*128)
throw Error(Malformed Variable Byte Integer)
multiplier *= 128
while ((encodedByte AND 128) != 0)
§Examples
use mqtt_packet::{DataType, VariableByte};
use std::io;
let data: Vec<u8> = vec![0x80, 0x80, 0x80, 0x01];
let mut reader = io::BufReader::new(&data[..]);
let mut vari_type = DataType::parse_variable_byte_int(&mut reader).unwrap();
assert_eq!(
vari_type,
DataType::VariableByteInteger(VariableByte::Four(2097152))
);
Sourcepub fn parse_binary_data<R: Read>(reader: &mut R) -> Result<Self, Error>
pub fn parse_binary_data<R: Read>(reader: &mut R) -> Result<Self, Error>
Reads bytes from the reader and attempts to convert the bytes to DataType::BinaryData (Vec
Binary Data is represented by a Two Byte Integer length which indicates the number of data bytes, followed by that number of bytes. Thus, the length of Binary Data is limited to the range of 0 to 65,535 Bytes.
§Examples
use mqtt_packet::DataType;
use std::io;
let data: Vec<u8> = vec![
0, 10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
];
let mut reader = io::BufReader::new(&data[..]);
let result = DataType::parse_binary_data(&mut reader).unwrap();
let expected: Vec<u8> = vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
assert_eq!(result, DataType::BinaryData(expected));
Sourcepub fn parse_utf8_string_pair<R: Read>(reader: &mut R) -> Result<Self, Error>
pub fn parse_utf8_string_pair<R: Read>(reader: &mut R) -> Result<Self, Error>
Reads bytes from the reader and attempts to convert the bytes to DataType::Utf8StringPair (String, String).
A UTF-8 String Pair consists of two UTF-8 Encoded Strings. This data type is used to hold name-value pairs. The first string serves as the name, and the second string contains the value.
Both strings MUST comply with the requirements for UTF-8 Encoded Strings. If a receiver (Client or Server) receives a string pair which does not meet these requirements it is a Malformed Packet. Refer to section 4.13 for information about handling errors.
§Examples
use mqtt_packet::DataType;
use std::io;