From d2f8431c4db9cc755e8b323e6cd6fcd8867ffec8 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Thu, 25 Mar 2021 12:28:54 -0500 Subject: [PATCH] Remove the concept of packet payloads from the public API --- src/packet/connack.rs | 2 +- src/packet/connect.rs | 68 +++++++++++++-------------------------- src/packet/disconnect.rs | 2 +- src/packet/mod.rs | 6 ++-- src/packet/pingreq.rs | 2 +- src/packet/pingresp.rs | 2 +- src/packet/puback.rs | 2 +- src/packet/pubcomp.rs | 2 +- src/packet/publish.rs | 2 +- src/packet/pubrec.rs | 2 +- src/packet/pubrel.rs | 2 +- src/packet/suback.rs | 20 ++++++------ src/packet/subscribe.rs | 22 ++++++------- src/packet/unsuback.rs | 2 +- src/packet/unsubscribe.rs | 20 ++++++------ 15 files changed, 67 insertions(+), 89 deletions(-) diff --git a/src/packet/connack.rs b/src/packet/connack.rs index 2f68520..a64038e 100644 --- a/src/packet/connack.rs +++ b/src/packet/connack.rs @@ -36,7 +36,7 @@ impl ConnackPacket { } impl DecodablePacket for ConnackPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let flags: ConnackFlags = Decodable::decode(reader)?; diff --git a/src/packet/connect.rs b/src/packet/connect.rs index d58fd13..95efd73 100644 --- a/src/packet/connect.rs +++ b/src/packet/connect.rs @@ -66,16 +66,7 @@ impl ConnectPacket { pub fn set_will(&mut self, topic_message: Option<(TopicName, Vec)>) { 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(); } @@ -112,12 +103,8 @@ impl ConnectPacket { self.payload.password.as_ref().map(|x| &x[..]) } - pub fn will(&self) -> Option<(&str, &Vec)> { - 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 { @@ -152,7 +139,7 @@ impl ConnectPacket { } impl DecodablePacket for ConnectPacket { - type Payload = ConnectPacketPayload; + type DecodePacketError = ConnectPacketError; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let protoname: ProtocolName = Decodable::decode(reader)?; @@ -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, - will_message: Option, + will: Option<(TopicName, VarBytes)>, user_name: Option, password: Option, } @@ -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, } @@ -199,11 +184,8 @@ impl Encodable for ConnectPacketPayload { fn encode(&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)?; } @@ -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; fn decode_with( reader: &mut R, rest: Option, - ) -> Result { - let mut need_will_topic = false; - let mut need_will_message = false; + ) -> Result { + 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 }; @@ -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, }) @@ -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), } diff --git a/src/packet/disconnect.rs b/src/packet/disconnect.rs index 72aa9ac..a88f371 100644 --- a/src/packet/disconnect.rs +++ b/src/packet/disconnect.rs @@ -28,7 +28,7 @@ impl Default for DisconnectPacket { } impl DecodablePacket for DisconnectPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(_reader: &mut R, fixed_header: FixedHeader) -> Result> { Ok(DisconnectPacket { fixed_header }) diff --git a/src/packet/mod.rs b/src/packet/mod.rs index 2fd9624..36dbecc 100644 --- a/src/packet/mod.rs +++ b/src/packet/mod.rs @@ -1,5 +1,6 @@ //! Specific packets +use std::error::Error; use std::fmt::{self, Debug}; use std::io::{self, Read, Write}; @@ -105,7 +106,7 @@ impl Encodable for T { } pub trait DecodablePacket: EncodablePacket + Sized { - type Payload: Decodable + 'static; + type DecodePacketError: Error + 'static; /// Decode packet given a `FixedHeader` fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result>; @@ -135,7 +136,7 @@ where { FixedHeaderError(#[from] FixedHeaderError), VariableHeaderError(#[from] VariableHeaderError), - PayloadError(<

::Payload as Decodable>::Error), + PayloadError(

::DecodePacketError), IoError(#[from] io::Error), TopicNameError(#[from] TopicNameError), } @@ -143,7 +144,6 @@ where impl

Debug for PacketError

where P: DecodablePacket, - ::Error: Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { diff --git a/src/packet/pingreq.rs b/src/packet/pingreq.rs index ab35c0f..dad1099 100644 --- a/src/packet/pingreq.rs +++ b/src/packet/pingreq.rs @@ -28,7 +28,7 @@ impl Default for PingreqPacket { } impl DecodablePacket for PingreqPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(_reader: &mut R, fixed_header: FixedHeader) -> Result> { Ok(PingreqPacket { fixed_header }) diff --git a/src/packet/pingresp.rs b/src/packet/pingresp.rs index 2fdbacb..5a7cf47 100644 --- a/src/packet/pingresp.rs +++ b/src/packet/pingresp.rs @@ -28,7 +28,7 @@ impl Default for PingrespPacket { } impl DecodablePacket for PingrespPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(_reader: &mut R, fixed_header: FixedHeader) -> Result> { Ok(PingrespPacket { fixed_header }) diff --git a/src/packet/puback.rs b/src/packet/puback.rs index 2d6792e..d6aff56 100644 --- a/src/packet/puback.rs +++ b/src/packet/puback.rs @@ -34,7 +34,7 @@ impl PubackPacket { } impl DecodablePacket for PubackPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?; diff --git a/src/packet/pubcomp.rs b/src/packet/pubcomp.rs index 3211045..53e13d9 100644 --- a/src/packet/pubcomp.rs +++ b/src/packet/pubcomp.rs @@ -34,7 +34,7 @@ impl PubcompPacket { } impl DecodablePacket for PubcompPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?; diff --git a/src/packet/publish.rs b/src/packet/publish.rs index 3fcb320..29e6d3d 100644 --- a/src/packet/publish.rs +++ b/src/packet/publish.rs @@ -119,7 +119,7 @@ impl PublishPacket { } impl DecodablePacket for PublishPacket { - type Payload = Vec; + type DecodePacketError = std::convert::Infallible; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let topic_name = TopicName::decode(reader)?; diff --git a/src/packet/pubrec.rs b/src/packet/pubrec.rs index 2c362e1..f35ea04 100644 --- a/src/packet/pubrec.rs +++ b/src/packet/pubrec.rs @@ -34,7 +34,7 @@ impl PubrecPacket { } impl DecodablePacket for PubrecPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?; diff --git a/src/packet/pubrel.rs b/src/packet/pubrel.rs index b6be49c..b904cc6 100644 --- a/src/packet/pubrel.rs +++ b/src/packet/pubrel.rs @@ -34,7 +34,7 @@ impl PubrelPacket { } impl DecodablePacket for PubrelPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?; diff --git a/src/packet/suback.rs b/src/packet/suback.rs index 0725a70..b9af028 100644 --- a/src/packet/suback.rs +++ b/src/packet/suback.rs @@ -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(reader: &mut R, fixed_header: FixedHeader) -> Result> { let packet_identifier = PacketIdentifier::decode(reader)?; @@ -98,7 +102,7 @@ impl DecodablePacket for SubackPacket { } #[derive(Debug, Eq, PartialEq, Clone)] -pub struct SubackPacketPayload { +struct SubackPacketPayload { subscribes: Vec, } @@ -106,10 +110,6 @@ impl SubackPacketPayload { pub fn new(subs: Vec) -> SubackPacketPayload { SubackPacketPayload { subscribes: subs } } - - pub fn subscribes(&self) -> &[SubscribeReturnCode] { - &self.subscribes[..] - } } impl Encodable for SubackPacketPayload { @@ -127,10 +127,10 @@ impl Encodable for SubackPacketPayload { } impl Decodable for SubackPacketPayload { - type Error = SubackPacketPayloadError; + type Error = SubackPacketError; type Cond = u32; - fn decode_with(reader: &mut R, payload_len: u32) -> Result { + fn decode_with(reader: &mut R, payload_len: u32) -> Result { let mut subs = Vec::new(); for _ in 0..payload_len { @@ -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); @@ -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}")] diff --git a/src/packet/subscribe.rs b/src/packet/subscribe.rs index 2bec1aa..9daec55 100644 --- a/src/packet/subscribe.rs +++ b/src/packet/subscribe.rs @@ -39,10 +39,14 @@ impl SubscribePacket { pub fn set_packet_identifier(&mut self, pkid: u16) { self.packet_identifier.0 = pkid; } + + pub fn subscribes(&self) -> &[(TopicFilter, QualityOfService)] { + &self.payload.subscribes[..] + } } impl DecodablePacket for SubscribePacket { - type Payload = SubscribePacketPayload; + type DecodePacketError = SubscribePacketError; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?; @@ -61,7 +65,7 @@ impl DecodablePacket for SubscribePacket { /// Payload of subscribe packet #[derive(Debug, Eq, PartialEq, Clone)] -pub struct SubscribePacketPayload { +struct SubscribePacketPayload { subscribes: Vec<(TopicFilter, QualityOfService)>, } @@ -69,10 +73,6 @@ impl SubscribePacketPayload { pub fn new(subs: Vec<(TopicFilter, QualityOfService)>) -> SubscribePacketPayload { SubscribePacketPayload { subscribes: subs } } - - pub fn subscribes(&self) -> &[(TopicFilter, QualityOfService)] { - &self.subscribes[..] - } } impl Encodable for SubscribePacketPayload { @@ -91,13 +91,13 @@ impl Encodable for SubscribePacketPayload { } impl Decodable for SubscribePacketPayload { - type Error = SubscribePacketPayloadError; + type Error = SubscribePacketError; type Cond = u32; fn decode_with( reader: &mut R, mut payload_len: u32, - ) -> Result { + ) -> Result { let mut subs = Vec::new(); while payload_len > 0 { @@ -106,7 +106,7 @@ impl Decodable for SubscribePacketPayload { 0 => QualityOfService::Level0, 1 => QualityOfService::Level1, 2 => QualityOfService::Level2, - _ => return Err(SubscribePacketPayloadError::InvalidQualityOfService), + _ => return Err(SubscribePacketError::InvalidQualityOfService), }; payload_len -= filter.encoded_length() + 1; @@ -118,7 +118,7 @@ impl Decodable for SubscribePacketPayload { } #[derive(Debug, thiserror::Error)] -pub enum SubscribePacketPayloadError { +pub enum SubscribePacketError { #[error(transparent)] IoError(#[from] io::Error), #[error(transparent)] @@ -129,7 +129,7 @@ pub enum SubscribePacketPayloadError { TopicFilterError(#[from] TopicFilterError), } -impl From for SubscribePacketPayloadError { +impl From for SubscribePacketError { fn from(e: TopicFilterDecodeError) -> Self { match e { TopicFilterDecodeError::IoError(e) => e.into(), diff --git a/src/packet/unsuback.rs b/src/packet/unsuback.rs index 4fb3c22..be84061 100644 --- a/src/packet/unsuback.rs +++ b/src/packet/unsuback.rs @@ -34,7 +34,7 @@ impl UnsubackPacket { } impl DecodablePacket for UnsubackPacket { - type Payload = (); + type DecodePacketError = std::convert::Infallible; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?; diff --git a/src/packet/unsubscribe.rs b/src/packet/unsubscribe.rs index 860a355..81cb879 100644 --- a/src/packet/unsubscribe.rs +++ b/src/packet/unsubscribe.rs @@ -37,10 +37,14 @@ impl UnsubscribePacket { pub fn set_packet_identifier(&mut self, pkid: u16) { self.packet_identifier.0 = pkid; } + + pub fn subscribes(&self) -> &[TopicFilter] { + &self.payload.subscribes[..] + } } impl DecodablePacket for UnsubscribePacket { - type Payload = UnsubscribePacketPayload; + type DecodePacketError = UnsubscribePacketError; fn decode_packet(reader: &mut R, fixed_header: FixedHeader) -> Result> { let packet_identifier: PacketIdentifier = PacketIdentifier::decode(reader)?; @@ -58,7 +62,7 @@ impl DecodablePacket for UnsubscribePacket { } #[derive(Debug, Eq, PartialEq, Clone)] -pub struct UnsubscribePacketPayload { +struct UnsubscribePacketPayload { subscribes: Vec, } @@ -66,10 +70,6 @@ impl UnsubscribePacketPayload { pub fn new(subs: Vec) -> UnsubscribePacketPayload { UnsubscribePacketPayload { subscribes: subs } } - - pub fn subscribes(&self) -> &[TopicFilter] { - &self.subscribes[..] - } } impl Encodable for UnsubscribePacketPayload { @@ -87,13 +87,13 @@ impl Encodable for UnsubscribePacketPayload { } impl Decodable for UnsubscribePacketPayload { - type Error = UnsubscribePacketPayloadError; + type Error = UnsubscribePacketError; type Cond = u32; fn decode_with( reader: &mut R, mut payload_len: u32, - ) -> Result { + ) -> Result { let mut subs = Vec::new(); while payload_len > 0 { @@ -108,13 +108,13 @@ impl Decodable for UnsubscribePacketPayload { #[derive(Debug, thiserror::Error)] #[error(transparent)] -pub enum UnsubscribePacketPayloadError { +pub enum UnsubscribePacketError { IoError(#[from] io::Error), FromUtf8Error(#[from] FromUtf8Error), TopicFilterError(#[from] TopicFilterError), } -impl From for UnsubscribePacketPayloadError { +impl From for UnsubscribePacketError { fn from(e: TopicFilterDecodeError) -> Self { match e { TopicFilterDecodeError::IoError(e) => e.into(),