Enum DataType

Source
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

Source

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).

1.5.1 Bits

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));
Source

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).

1.5.2 Two Byte Integer

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));
Source

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).

1.5.3 Four Byte Integer

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));
Source

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).

1.5.4 UTF-8 Encoded 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"))
);
Source

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).

1.5.5 Variable Byte Integer

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))
);
Source

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).

1.5.6 Binary Data

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));
Source

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).

1.5.7 UTF-8 String Pair

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;
Source

pub fn into_bytes(&self) -> Result<Vec<u8>, Error>

Convert DataType variants into u8 vectors.

Trait Implementations§

Source§

impl Debug for DataType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<DataType> for u16

Source§

fn from(t: DataType) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for DataType

Source§

fn eq(&self, other: &DataType) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for DataType

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.