SubOpts

Struct SubOpts 

Source
pub struct SubOpts { /* private fields */ }
Expand description

MQTT Subscription Options

Represents the subscription options byte used in SUBSCRIBE packets as defined in MQTT v5.0 specification. This single byte contains multiple bit fields that control various aspects of subscription behavior including QoS level, retain handling, and MQTT v5.0 specific flags.

§Bit Layout

The subscription options byte is structured as follows:

Bit:  7  6  5  4  3  2  1  0
     [Reserved] [RH] [RAP][NL][QoS]

Where:

  • Bits 0-1: QoS level (0, 1, or 2)
  • Bit 2: No Local flag (NL)
  • Bit 3: Retain As Published flag (RAP)
  • Bits 4-5: Retain Handling option (RH)
  • Bits 6-7: Reserved (must be 0)

§Examples

use mqtt_protocol_core::mqtt;

// Create default subscription options (QoS 0, all flags false)
let opts = mqtt::packet::SubOpts::new();

// Configure specific options
let opts = mqtt::packet::SubOpts::new()
    .set_qos(mqtt::packet::Qos::AtLeastOnce)
    .set_nl(true)  // No Local flag
    .set_rap(true) // Retain As Published flag
    .set_rh(mqtt::packet::RetainHandling::DoNotSendRetained);

// Parse from byte value
let opts = mqtt::packet::SubOpts::from_u8(0x25).unwrap();

Implementations§

Source§

impl SubOpts

Source

pub fn new() -> Self

Create new subscription options with default values

Creates a SubOpts instance with all options set to their default values:

  • QoS: AtMostOnce (0)
  • No Local flag: false
  • Retain As Published flag: false
  • Retain Handling: SendRetained (0)
§Returns

A new SubOpts instance with default settings

§Examples
use mqtt_protocol_core::mqtt;

let opts = mqtt::packet::SubOpts::new();
assert_eq!(opts.qos(), mqtt::packet::Qos::AtMostOnce);
assert_eq!(opts.nl(), false);
assert_eq!(opts.rap(), false);
Source

pub fn from_u8(value: u8) -> Result<Self, MqttError>

Create subscription options from a byte value

Parses a subscription options byte and validates that all fields contain valid values according to the MQTT v5.0 specification. This method performs comprehensive validation to ensure protocol compliance.

§Parameters
  • value - The subscription options byte to parse
§Returns
  • Ok(SubOpts) - Successfully parsed and validated subscription options
  • Err(MqttError::MalformedPacket) - If any field contains invalid values
§Validation Rules
  1. Reserved bits (6-7) must be 0
  2. QoS value (bits 0-1) must be 0, 1, or 2
  3. Retain Handling value (bits 4-5) must be 0, 1, or 2
§Examples
use mqtt_protocol_core::mqtt;

// Valid subscription options byte
let opts = mqtt::packet::SubOpts::from_u8(0x25).unwrap();

// Invalid: reserved bits set
assert!(mqtt::packet::SubOpts::from_u8(0xC0).is_err());

// Invalid: QoS value 3
assert!(mqtt::packet::SubOpts::from_u8(0x03).is_err());
Source

pub fn qos(&self) -> Qos

Get the QoS level from subscription options

Extracts and returns the Quality of Service level from bits 0-1 of the subscription options byte. The QoS level determines the delivery guarantee for messages matching this subscription.

§Returns

The QoS level as a Qos enum value:

  • Qos::AtMostOnce for value 0
  • Qos::AtLeastOnce for value 1
  • Qos::ExactlyOnce for value 2
§Examples
use mqtt_protocol_core::mqtt;

let opts = mqtt::packet::SubOpts::new().set_qos(mqtt::packet::Qos::AtLeastOnce);
assert_eq!(opts.qos(), mqtt::packet::Qos::AtLeastOnce);
Source

pub fn set_qos(self, qos: Qos) -> Self

Set the QoS level in subscription options

Updates bits 0-1 of the subscription options byte with the specified QoS level. This method uses a builder pattern, consuming and returning the SubOpts instance to allow method chaining.

§Parameters
  • qos - The QoS level to set
§Returns

The updated SubOpts instance with the new QoS level

§Examples
use mqtt_protocol_core::mqtt;

let opts = mqtt::packet::SubOpts::new()
    .set_qos(mqtt::packet::Qos::ExactlyOnce);
assert_eq!(opts.qos(), mqtt::packet::Qos::ExactlyOnce);
Source

pub fn nl(&self) -> bool

Get the No Local flag from subscription options

Extracts the No Local flag from bit 2 of the subscription options byte. When set to true, messages published by this client will not be forwarded back to it, even if it has a matching subscription.

This flag is useful for preventing message loops in scenarios where a client both publishes and subscribes to the same topics.

§Returns

true if the No Local flag is set, false otherwise

§Examples
use mqtt_protocol_core::mqtt;

let opts = mqtt::packet::SubOpts::new().set_nl(true);
assert_eq!(opts.nl(), true);
Source

pub fn set_nl(self, nl: bool) -> Self

Set the No Local flag in subscription options

Updates bit 2 of the subscription options byte with the specified No Local flag value. This method uses a builder pattern, consuming and returning the SubOpts instance to allow method chaining.

§Parameters
  • nl - The No Local flag value to set
§Returns

The updated SubOpts instance with the new No Local flag

§Examples
use mqtt_protocol_core::mqtt;

// Enable No Local to prevent message loops
let opts = mqtt::packet::SubOpts::new().set_nl(true);
assert_eq!(opts.nl(), true);
Source

pub fn rap(&self) -> bool

Get the Retain As Published flag from subscription options

Extracts the Retain As Published flag from bit 3 of the subscription options byte. When set to true, messages forwarded to this subscription will keep their original RETAIN flag value. When false, forwarded messages will have their RETAIN flag set to 0.

This flag affects how the broker handles the RETAIN flag when forwarding messages to this specific subscription.

§Returns

true if the Retain As Published flag is set, false otherwise

§Examples
use mqtt_protocol_core::mqtt;

let opts = mqtt::packet::SubOpts::new().set_rap(true);
assert_eq!(opts.rap(), true);
Source

pub fn set_rap(self, rap: bool) -> Self

Set the Retain As Published flag in subscription options

Updates bit 3 of the subscription options byte with the specified Retain As Published flag value. This method uses a builder pattern, consuming and returning the SubOpts instance to allow method chaining.

§Parameters
  • rap - The Retain As Published flag value to set
§Returns

The updated SubOpts instance with the new Retain As Published flag

§Examples
use mqtt_protocol_core::mqtt;

// Preserve original RETAIN flag in forwarded messages
let opts = mqtt::packet::SubOpts::new().set_rap(true);
assert_eq!(opts.rap(), true);
Source

pub fn rh(&self) -> RetainHandling

Get the Retain Handling option from subscription options

Extracts and returns the Retain Handling option from bits 4-5 of the subscription options byte. This option controls how retained messages are handled when the subscription is established.

§Returns

The retain handling option as a RetainHandling enum value:

  • RetainHandling::SendRetained for value 0
  • RetainHandling::SendRetainedIfNotExists for value 1
  • RetainHandling::DoNotSendRetained for value 2
§Examples
use mqtt_protocol_core::mqtt;

let opts = mqtt::packet::SubOpts::new()
    .set_rh(mqtt::packet::RetainHandling::DoNotSendRetained);
assert_eq!(opts.rh(), mqtt::packet::RetainHandling::DoNotSendRetained);
Source

pub fn set_rh(self, rh: RetainHandling) -> Self

Set the Retain Handling option in subscription options

Updates bits 4-5 of the subscription options byte with the specified retain handling option. This method uses a builder pattern, consuming and returning the SubOpts instance to allow method chaining.

§Parameters
  • rh - The retain handling option to set
§Returns

The updated SubOpts instance with the new retain handling option

§Examples
use mqtt_protocol_core::mqtt;

let opts = mqtt::packet::SubOpts::new()
    .set_rh(mqtt::packet::RetainHandling::SendRetainedIfNotExists);
assert_eq!(opts.rh(), mqtt::packet::RetainHandling::SendRetainedIfNotExists);
Source

pub fn to_buffer(&self) -> &[u8; 1]

Get the raw subscription options byte buffer

Returns a reference to the internal byte buffer containing the encoded subscription options. This can be used for direct serialization to the MQTT wire format.

§Returns

A reference to the single-byte buffer containing the encoded options

§Examples
use mqtt_protocol_core::mqtt;

let opts = mqtt::packet::SubOpts::new().set_qos(mqtt::packet::Qos::AtLeastOnce);
let buffer = opts.to_buffer();
assert_eq!(buffer[0] & 0x03, 1); // QoS bits should be 01

Trait Implementations§

Source§

impl Clone for SubOpts

Source§

fn clone(&self) -> SubOpts

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SubOpts

Source§

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

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

impl Default for SubOpts

Implementation of Default for SubOpts

Creates subscription options with all default values. This is equivalent to calling SubOpts::new().

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for SubOpts

Implementation of Display for SubOpts

Formats the subscription options as a JSON string for human-readable output. This is particularly useful for logging and debugging purposes. If JSON serialization fails, an error message is displayed instead.

Source§

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

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

impl Ord for SubOpts

Source§

fn cmp(&self, other: &SubOpts) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SubOpts

Source§

fn eq(&self, other: &SubOpts) -> 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 PartialOrd for SubOpts

Source§

fn partial_cmp(&self, other: &SubOpts) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for SubOpts

Implementation of Serialize for SubOpts

Serializes the subscription options to a structured format with individual fields for each option. This allows the options to be serialized to JSON format with clear field names and values.

§Serialized Fields

  • qos: Quality of Service level as a string
  • nl: No Local flag as a boolean
  • rap: Retain As Published flag as a boolean
  • rh: Retain Handling option as a string
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for SubOpts

Source§

impl Eq for SubOpts

Source§

impl StructuralPartialEq for SubOpts

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.