sage_mqtt/
quality_of_service.rs

1use crate::{Error as SageError, ReasonCode::MalformedPacket};
2use std::convert::TryFrom;
3
4/// Description the quality of service used in message publishing.
5#[derive(Debug, PartialEq, Clone, Copy, Eq)]
6pub enum QoS {
7    /// The message is delivered according to the capabilities of the
8    /// underlying network. No response is sent by the receiver and no retry is
9    /// performed by the sender.
10    /// The message arrives at the receiver either once or not at all.
11    AtMostOnce = 0x00,
12
13    /// This Quality of Service level ensures that the message arrives at the
14    /// receiver at least once. A QoS 1 PUBLISH packet has a Packet Identifier
15    /// and is acknowledged by a `Puback` packet.
16    AtLeastOnce = 0x01,
17
18    /// This is the highest Quality of Service level, for use when neither loss
19    /// nor duplication of messages are acceptable.
20    /// There is an increased overhead associated with QoS 2.
21    /// A QoS 2 message has a Packet Identifier. The receiver of a QoS 2
22    /// `Publish` packet acknowledges receipt with a two-step
23    /// acknowledgement process.
24    ExactlyOnce = 0x02,
25}
26
27impl TryFrom<u8> for QoS {
28    type Error = SageError;
29    fn try_from(value: u8) -> Result<Self, Self::Error> {
30        match value {
31            0x00 => Ok(QoS::AtMostOnce),
32            0x01 => Ok(QoS::AtLeastOnce),
33            0x02 => Ok(QoS::ExactlyOnce),
34            _ => Err(MalformedPacket.into()),
35        }
36    }
37}