rumqttc_dev_patched/v5/mqttbytes/
mod.rsuse std::{str::Utf8Error, vec};
pub mod v5;
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
#[allow(clippy::enum_variant_names)]
pub enum QoS {
AtMostOnce = 0,
AtLeastOnce = 1,
ExactlyOnce = 2,
}
impl Default for QoS {
fn default() -> Self {
Self::AtMostOnce
}
}
pub fn qos(num: u8) -> Option<QoS> {
match num {
0 => Some(QoS::AtMostOnce),
1 => Some(QoS::AtLeastOnce),
2 => Some(QoS::ExactlyOnce),
_ => None,
}
}
pub fn has_wildcards(s: &str) -> bool {
s.contains('+') || s.contains('#')
}
pub fn valid_topic(topic: &str) -> bool {
if topic.contains('+') || topic.contains('#') {
return false;
}
true
}
pub fn valid_filter(filter: &str) -> bool {
if filter.is_empty() {
return false;
}
let mut hirerarchy = filter.split('/').rev();
let last = hirerarchy.next().unwrap();
if last.len() != 1 && (last.contains('#') || last.contains('+')) {
return false;
}
for entry in hirerarchy {
if entry.contains('#') {
return false;
}
if entry.len() > 1 && entry.contains('+') {
return false;
}
}
true
}
pub fn matches(topic: &str, filter: &str) -> bool {
if !topic.is_empty() && topic[..1].contains('$') {
return false;
}
let mut topics = topic.split('/');
let mut filters = filter.split('/');
for f in filters.by_ref() {
if f == "#" {
return true;
}
let top = topics.next();
match top {
Some("#") => return false,
Some(_) if f == "+" => continue,
Some(t) if f != t => return false,
Some(_) => continue,
None => return false,
}
}
if topics.next().is_some() {
return false;
}
true
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Invalid return code received as response for connect = {0}")]
InvalidConnectReturnCode(u8),
#[error("Invalid reason = {0}")]
InvalidReason(u8),
#[error("Invalid remaining length = {0}")]
InvalidRemainingLength(usize),
#[error("Invalid protocol used")]
InvalidProtocol,
#[error("Invalid protocol level")]
InvalidProtocolLevel(u8),
#[error("Invalid packet format")]
IncorrectPacketFormat,
#[error("Invalid packet type = {0}")]
InvalidPacketType(u8),
#[error("Invalid retain forward rule = {0}")]
InvalidRetainForwardRule(u8),
#[error("Invalid QoS level = {0}")]
InvalidQoS(u8),
#[error("Invalid subscribe reason code = {0}")]
InvalidSubscribeReasonCode(u8),
#[error("Packet received has id Zero")]
PacketIdZero,
#[error("Empty Subscription")]
EmptySubscription,
#[error("Subscription had id Zero")]
SubscriptionIdZero,
#[error("Payload size is incorrect")]
PayloadSizeIncorrect,
#[error("Payload is too long")]
PayloadTooLong,
#[error("Max Payload size of {max:?} has been exceeded by packet of {pkt_size:?} bytes")]
PayloadSizeLimitExceeded { pkt_size: usize, max: u32 },
#[error("Payload is required")]
PayloadRequired,
#[error("Payload is required = {0}")]
PayloadNotUtf8(#[from] Utf8Error),
#[error("Topic not utf-8")]
TopicNotUtf8,
#[error("Promised boundary crossed, contains {0} bytes")]
BoundaryCrossed(usize),
#[error("Packet is malformed")]
MalformedPacket,
#[error("Remaining length is malformed")]
MalformedRemainingLength,
#[error("Invalid property type = {0}")]
InvalidPropertyType(u8),
#[error("Insufficient number of bytes to frame packet, {0} more bytes required")]
InsufficientBytes(usize),
#[error("IO: {0}")]
Io(#[from] std::io::Error),
#[error("Cannot send packet of size '{pkt_size:?}'. It's greater than the broker's maximum packet size of: '{max:?}'")]
OutgoingPacketTooLarge { pkt_size: u32, max: u32 },
}