Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the concept of packet payloads from the public API #53

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/packet/connack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl ConnackPacket {
}

impl DecodablePacket for ConnackPacket {
type Payload = ();
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let flags: ConnackFlags = Decodable::decode(reader)?;
Expand Down
68 changes: 23 additions & 45 deletions src/packet/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,7 @@ impl ConnectPacket {
pub fn set_will(&mut self, topic_message: Option<(TopicName, Vec<u8>)>) {
self.flags.will_flag = topic_message.is_some();

match topic_message {
Some((topic, msg)) => {
self.payload.will_topic = Some(topic);
self.payload.will_message = Some(VarBytes(msg));
}
None => {
self.payload.will_topic = None;
self.payload.will_message = None;
}
}
self.payload.will = topic_message.map(|(t, m)| (t, VarBytes(m)));

self.fix_header_remaining_len();
}
Expand Down Expand Up @@ -112,12 +103,8 @@ impl ConnectPacket {
self.payload.password.as_ref().map(|x| &x[..])
}

pub fn will(&self) -> Option<(&str, &Vec<u8>)> {
self.payload
.will_topic
.as_ref()
.map(|x| &x[..])
.and_then(|topic| self.payload.will_message.as_ref().map(|msg| (topic, &msg.0)))
pub fn will(&self) -> Option<(&str, &[u8])> {
self.payload.will.as_ref().map(|(topic, msg)| (&topic[..], &*msg.0))
}

pub fn will_retain(&self) -> bool {
Expand Down Expand Up @@ -152,7 +139,7 @@ impl ConnectPacket {
}

impl DecodablePacket for ConnectPacket {
type Payload = ConnectPacketPayload;
type DecodePacketError = ConnectPacketError;

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let protoname: ProtocolName = Decodable::decode(reader)?;
Expand All @@ -175,10 +162,9 @@ impl DecodablePacket for ConnectPacket {

/// Payloads for connect packet
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct ConnectPacketPayload {
struct ConnectPacketPayload {
client_identifier: String,
will_topic: Option<TopicName>,
will_message: Option<VarBytes>,
will: Option<(TopicName, VarBytes)>,
user_name: Option<String>,
password: Option<String>,
}
Expand All @@ -187,8 +173,7 @@ impl ConnectPacketPayload {
pub fn new(client_identifier: String) -> ConnectPacketPayload {
ConnectPacketPayload {
client_identifier,
will_topic: None,
will_message: None,
will: None,
user_name: None,
password: None,
}
Expand All @@ -199,11 +184,8 @@ impl Encodable for ConnectPacketPayload {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), io::Error> {
self.client_identifier.encode(writer)?;

if let Some(ref will_topic) = self.will_topic {
if let Some((will_topic, will_message)) = &self.will {
will_topic.encode(writer)?;
}

if let Some(ref will_message) = self.will_message {
will_message.encode(writer)?;
}

Expand All @@ -220,45 +202,42 @@ impl Encodable for ConnectPacketPayload {

fn encoded_length(&self) -> u32 {
self.client_identifier.encoded_length()
+ self.will_topic.as_ref().map(|t| t.encoded_length()).unwrap_or(0)
+ self.will_message.as_ref().map(|t| t.encoded_length()).unwrap_or(0)
+ self
.will
.as_ref()
.map(|(a, b)| a.encoded_length() + b.encoded_length())
.unwrap_or(0)
+ self.user_name.as_ref().map(|t| t.encoded_length()).unwrap_or(0)
+ self.password.as_ref().map(|t| t.encoded_length()).unwrap_or(0)
}
}

impl Decodable for ConnectPacketPayload {
type Error = ConnectPacketPayloadError;
type Error = ConnectPacketError;
type Cond = Option<ConnectFlags>;

fn decode_with<R: Read>(
reader: &mut R,
rest: Option<ConnectFlags>,
) -> Result<ConnectPacketPayload, ConnectPacketPayloadError> {
let mut need_will_topic = false;
let mut need_will_message = false;
) -> Result<ConnectPacketPayload, ConnectPacketError> {
let mut need_will = false;
let mut need_user_name = false;
let mut need_password = false;

if let Some(r) = rest {
need_will_topic = r.will_flag;
need_will_message = r.will_flag;
need_will = r.will_flag;
need_user_name = r.user_name;
need_password = r.password;
}

let ident = String::decode(reader)?;
let topic = if need_will_topic {
let will = if need_will {
let topic = TopicName::decode(reader).map_err(|e| match e {
TopicNameDecodeError::IoError(e) => ConnectPacketPayloadError::from(e),
TopicNameDecodeError::IoError(e) => ConnectPacketError::from(e),
TopicNameDecodeError::InvalidTopicName(e) => e.into(),
})?;
Some(topic)
} else {
None
};
let msg = if need_will_message {
Some(VarBytes::decode(reader)?)
let msg = VarBytes::decode(reader)?;
Some((topic, msg))
} else {
None
};
Expand All @@ -275,8 +254,7 @@ impl Decodable for ConnectPacketPayload {

Ok(ConnectPacketPayload {
client_identifier: ident,
will_topic: topic,
will_message: msg,
will,
user_name: uname,
password: pwd,
})
Expand All @@ -285,7 +263,7 @@ impl Decodable for ConnectPacketPayload {

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub enum ConnectPacketPayloadError {
pub enum ConnectPacketError {
IoError(#[from] io::Error),
TopicNameError(#[from] TopicNameError),
}
Expand Down
2 changes: 1 addition & 1 deletion src/packet/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Default for DisconnectPacket {
}

impl DecodablePacket for DisconnectPacket {
type Payload = ();
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(_reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
Ok(DisconnectPacket { fixed_header })
Expand Down
6 changes: 3 additions & 3 deletions src/packet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Specific packets

use std::error::Error;
use std::fmt::{self, Debug};
use std::io::{self, Read, Write};

Expand Down Expand Up @@ -105,7 +106,7 @@ impl<T: EncodablePacket> Encodable for T {
}

pub trait DecodablePacket: EncodablePacket + Sized {
type Payload: Decodable + 'static;
type DecodePacketError: Error + 'static;

/// Decode packet given a `FixedHeader`
fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>>;
Expand Down Expand Up @@ -135,15 +136,14 @@ where
{
FixedHeaderError(#[from] FixedHeaderError),
VariableHeaderError(#[from] VariableHeaderError),
PayloadError(<<P as DecodablePacket>::Payload as Decodable>::Error),
PayloadError(<P as DecodablePacket>::DecodePacketError),
IoError(#[from] io::Error),
TopicNameError(#[from] TopicNameError),
}

impl<P> Debug for PacketError<P>
where
P: DecodablePacket,
<P::Payload as Decodable>::Error: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down
2 changes: 1 addition & 1 deletion src/packet/pingreq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Default for PingreqPacket {
}

impl DecodablePacket for PingreqPacket {
type Payload = ();
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(_reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
Ok(PingreqPacket { fixed_header })
Expand Down
2 changes: 1 addition & 1 deletion src/packet/pingresp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Default for PingrespPacket {
}

impl DecodablePacket for PingrespPacket {
type Payload = ();
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(_reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
Ok(PingrespPacket { fixed_header })
Expand Down
2 changes: 1 addition & 1 deletion src/packet/puback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl PubackPacket {
}

impl DecodablePacket for PubackPacket {
type Payload = ();
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?;
Expand Down
2 changes: 1 addition & 1 deletion src/packet/pubcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl PubcompPacket {
}

impl DecodablePacket for PubcompPacket {
type Payload = ();
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?;
Expand Down
2 changes: 1 addition & 1 deletion src/packet/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl PublishPacket {
}

impl DecodablePacket for PublishPacket {
type Payload = Vec<u8>;
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let topic_name = TopicName::decode(reader)?;
Expand Down
2 changes: 1 addition & 1 deletion src/packet/pubrec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl PubrecPacket {
}

impl DecodablePacket for PubrecPacket {
type Payload = ();
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?;
Expand Down
2 changes: 1 addition & 1 deletion src/packet/pubrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl PubrelPacket {
}

impl DecodablePacket for PubrelPacket {
type Payload = ();
type DecodePacketError = std::convert::Infallible;

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?;
Expand Down
20 changes: 10 additions & 10 deletions src/packet/suback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ impl SubackPacket {
pub fn set_packet_identifier(&mut self, pkid: u16) {
self.packet_identifier.0 = pkid;
}

pub fn subscribes(&self) -> &[SubscribeReturnCode] {
&self.payload.subscribes[..]
}
}

impl DecodablePacket for SubackPacket {
type Payload = SubackPacketPayload;
type DecodePacketError = SubackPacketError;

fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<Self>> {
let packet_identifier = PacketIdentifier::decode(reader)?;
Expand All @@ -98,18 +102,14 @@ impl DecodablePacket for SubackPacket {
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct SubackPacketPayload {
struct SubackPacketPayload {
subscribes: Vec<SubscribeReturnCode>,
}

impl SubackPacketPayload {
pub fn new(subs: Vec<SubscribeReturnCode>) -> SubackPacketPayload {
SubackPacketPayload { subscribes: subs }
}

pub fn subscribes(&self) -> &[SubscribeReturnCode] {
&self.subscribes[..]
}
}

impl Encodable for SubackPacketPayload {
Expand All @@ -127,10 +127,10 @@ impl Encodable for SubackPacketPayload {
}

impl Decodable for SubackPacketPayload {
type Error = SubackPacketPayloadError;
type Error = SubackPacketError;
type Cond = u32;

fn decode_with<R: Read>(reader: &mut R, payload_len: u32) -> Result<SubackPacketPayload, SubackPacketPayloadError> {
fn decode_with<R: Read>(reader: &mut R, payload_len: u32) -> Result<SubackPacketPayload, SubackPacketError> {
let mut subs = Vec::new();

for _ in 0..payload_len {
Expand All @@ -139,7 +139,7 @@ impl Decodable for SubackPacketPayload {
0x01 => SubscribeReturnCode::MaximumQoSLevel1,
0x02 => SubscribeReturnCode::MaximumQoSLevel2,
0x80 => SubscribeReturnCode::Failure,
code => return Err(SubackPacketPayloadError::InvalidSubscribeReturnCode(code)),
code => return Err(SubackPacketError::InvalidSubscribeReturnCode(code)),
};

subs.push(retcode);
Expand All @@ -150,7 +150,7 @@ impl Decodable for SubackPacketPayload {
}

#[derive(Debug, thiserror::Error)]
pub enum SubackPacketPayloadError {
pub enum SubackPacketError {
#[error(transparent)]
IoError(#[from] io::Error),
#[error("invalid subscribe return code {0}")]
Expand Down
Loading