use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum QoS {
AtMostOnce = 0,
AtLeastOnce = 1,
ExactlyOnce = 2,
}
impl QoS {
#[cfg(feature = "rumqttc")]
pub fn to_rumqttc(self) -> rumqttc::QoS {
match self {
| QoS::AtMostOnce => rumqttc::QoS::AtMostOnce,
| QoS::AtLeastOnce => rumqttc::QoS::AtLeastOnce,
| QoS::ExactlyOnce => rumqttc::QoS::ExactlyOnce,
}
}
#[cfg(feature = "rumqttc")]
pub fn to_rumqttc_v5(self) -> rumqttc::v5::mqttbytes::QoS {
use rumqttc::v5::mqttbytes::QoS as V5;
match self {
| QoS::AtMostOnce => V5::AtMostOnce,
| QoS::AtLeastOnce => V5::AtLeastOnce,
| QoS::ExactlyOnce => V5::ExactlyOnce,
}
}
#[cfg(feature = "paho-mqtt")]
pub fn to_paho_mqtt(self) -> paho_mqtt::QoS {
match self {
| QoS::AtMostOnce => paho_mqtt::QoS::AtMostOnce,
| QoS::AtLeastOnce => paho_mqtt::QoS::AtLeastOnce,
| QoS::ExactlyOnce => paho_mqtt::QoS::ExactlyOnce,
}
}
#[cfg(feature = "ntex-mqtt")]
pub fn to_ntex_mqtt(self) -> ntex_mqtt::QoS {
match self {
| QoS::AtMostOnce => ntex_mqtt::QoS::AtMostOnce,
| QoS::AtLeastOnce => ntex_mqtt::QoS::AtLeastOnce,
| QoS::ExactlyOnce => ntex_mqtt::QoS::ExactlyOnce,
}
}
}
#[cfg(feature = "rumqttc")]
impl From<rumqttc::QoS> for QoS {
fn from(qos: rumqttc::QoS) -> Self {
match qos {
| rumqttc::QoS::AtMostOnce => QoS::AtMostOnce,
| rumqttc::QoS::AtLeastOnce => QoS::AtLeastOnce,
| rumqttc::QoS::ExactlyOnce => QoS::ExactlyOnce,
}
}
}
#[cfg(feature = "rumqttc")]
impl From<rumqttc::v5::mqttbytes::QoS> for QoS {
fn from(qos: rumqttc::v5::mqttbytes::QoS) -> Self {
use rumqttc::v5::mqttbytes::QoS as V5;
match qos {
| V5::AtMostOnce => QoS::AtMostOnce,
| V5::AtLeastOnce => QoS::AtLeastOnce,
| V5::ExactlyOnce => QoS::ExactlyOnce,
}
}
}
#[cfg(feature = "paho-mqtt")]
impl From<paho_mqtt::QoS> for QoS {
fn from(qos: paho_mqtt::QoS) -> Self {
match qos {
| paho_mqtt::QoS::AtMostOnce => QoS::AtMostOnce,
| paho_mqtt::QoS::AtLeastOnce => QoS::AtLeastOnce,
| paho_mqtt::QoS::ExactlyOnce => QoS::ExactlyOnce,
}
}
}
#[cfg(feature = "ntex-mqtt")]
impl From<ntex_mqtt::QoS> for QoS {
fn from(qos: ntex_mqtt::QoS) -> Self {
match qos {
| ntex_mqtt::QoS::AtMostOnce => QoS::AtMostOnce,
| ntex_mqtt::QoS::AtLeastOnce => QoS::AtLeastOnce,
| ntex_mqtt::QoS::ExactlyOnce => QoS::ExactlyOnce,
}
}
}
impl fmt::Display for QoS {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
| QoS::AtMostOnce => write!(f, "QoS0"),
| QoS::AtLeastOnce => write!(f, "QoS1"),
| QoS::ExactlyOnce => write!(f, "QoS2"),
}
}
}
#[cfg(all(test, feature = "rumqttc"))]
mod rumqttc_v5_tests {
use super::QoS;
#[test]
fn v5_round_trip_all_levels() {
for qos in [QoS::AtMostOnce, QoS::AtLeastOnce, QoS::ExactlyOnce] {
assert_eq!(QoS::from(qos.to_rumqttc_v5()), qos);
}
}
#[test]
fn v5_is_a_distinct_enum_from_v4() {
use rumqttc::v5::mqttbytes::QoS as V5;
assert_eq!(QoS::AtLeastOnce.to_rumqttc_v5(), V5::AtLeastOnce);
assert_eq!(QoS::from(V5::ExactlyOnce), QoS::ExactlyOnce);
}
}