use std::convert::TryFrom;
use crate::{
ByteArray, DecodeError, DecodePacket, EncodeError, EncodePacket, FixedHeader, Packet, PacketId,
PacketType, QoS, SubTopic, VarIntError,
};
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SubscribeTopic {
topic: SubTopic,
qos: QoS,
}
impl SubscribeTopic {
pub fn new(topic: &str, qos: QoS) -> Result<Self, EncodeError> {
let topic = SubTopic::new(topic)?;
Ok(Self { topic, qos })
}
pub fn topic(&self) -> &str {
self.topic.as_ref()
}
#[must_use]
pub const fn qos(&self) -> QoS {
self.qos
}
#[must_use]
pub fn bytes(&self) -> usize {
1 + self.topic.bytes()
}
}
impl EncodePacket for SubscribeTopic {
fn encode(&self, buf: &mut Vec<u8>) -> Result<usize, EncodeError> {
self.topic.encode(buf)?;
let qos: u8 = 0b0000_0011 & (self.qos as u8);
buf.push(qos);
Ok(self.bytes())
}
}
impl DecodePacket for SubscribeTopic {
fn decode(ba: &mut ByteArray) -> Result<Self, DecodeError> {
let topic = SubTopic::decode(ba)?;
let qos_flag = ba.read_byte()?;
if qos_flag & 0b1111_0000 != 0b0000_0000 {
return Err(DecodeError::InvalidQoS);
}
let qos = QoS::try_from(qos_flag & 0b0000_0011)?;
Ok(Self { topic, qos })
}
}
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct SubscribePacket {
packet_id: PacketId,
topics: Vec<SubscribeTopic>,
}
impl SubscribePacket {
pub fn new(topic: &str, qos: QoS, packet_id: PacketId) -> Result<Self, EncodeError> {
let topic = SubscribeTopic::new(topic, qos)?;
Ok(Self {
packet_id,
topics: vec![topic],
})
}
pub fn set_packet_id(&mut self, packet_id: PacketId) -> &mut Self {
self.packet_id = packet_id;
self
}
#[inline]
#[must_use]
pub const fn packet_id(&self) -> PacketId {
self.packet_id
}
pub fn set_topics(&mut self, topics: &[SubscribeTopic]) -> &mut Self {
self.topics.clear();
self.topics.extend_from_slice(topics);
self
}
#[must_use]
pub fn topics(&self) -> &[SubscribeTopic] {
&self.topics
}
pub fn mut_topics(&mut self) -> &mut Vec<SubscribeTopic> {
&mut self.topics
}
fn get_fixed_header(&self) -> Result<FixedHeader, VarIntError> {
let mut remaining_length = PacketId::bytes();
for topic in &self.topics {
remaining_length += topic.bytes();
}
FixedHeader::new(PacketType::Subscribe, remaining_length)
}
}
impl DecodePacket for SubscribePacket {
fn decode(ba: &mut ByteArray) -> Result<Self, DecodeError> {
let fixed_header = FixedHeader::decode(ba)?;
if fixed_header.packet_type() != PacketType::Subscribe {
return Err(DecodeError::InvalidPacketType);
}
let packet_id = PacketId::decode(ba)?;
if packet_id.value() == 0 {
return Err(DecodeError::InvalidPacketId);
}
let mut topics = Vec::new();
let mut remaining_length = PacketId::bytes();
while remaining_length < fixed_header.remaining_length() {
let topic = SubscribeTopic::decode(ba)?;
remaining_length += topic.bytes();
topics.push(topic);
}
if topics.is_empty() {
return Err(DecodeError::EmptyTopicFilter);
}
Ok(Self { packet_id, topics })
}
}
impl EncodePacket for SubscribePacket {
fn encode(&self, buf: &mut Vec<u8>) -> Result<usize, EncodeError> {
let old_len = buf.len();
let fixed_header = self.get_fixed_header()?;
fixed_header.encode(buf)?;
self.packet_id.encode(buf)?;
for topic in &self.topics {
topic.encode(buf)?;
}
Ok(buf.len() - old_len)
}
}
impl Packet for SubscribePacket {
fn packet_type(&self) -> PacketType {
PacketType::Subscribe
}
fn bytes(&self) -> Result<usize, VarIntError> {
let fixed_header = self.get_fixed_header()?;
Ok(fixed_header.bytes() + fixed_header.remaining_length())
}
}