From 21e5bb07228869dd98ba51b9b9e59c144813506a Mon Sep 17 00:00:00 2001 From: Nereuxofficial <37740907+Nereuxofficial@users.noreply.github.com> Date: Thu, 23 Feb 2023 13:38:55 +0100 Subject: [PATCH] Changed Will topic field to use Topic (#53) * Changed Will topic field to use Topic (cherry picked from commit 6579ad30f859f6cad9cad68c2931c76650d173cb) * Build fix * Fixed unnecessary borrow * Right Error Mapping Co-authored-by: Brian Schwind * Removed unnecessary import --------- Co-authored-by: Brian Schwind --- mqtt-v5/src/decoder.rs | 24 ++++++++++++++---------- mqtt-v5/src/encoder.rs | 2 +- mqtt-v5/src/types.rs | 4 ++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/mqtt-v5/src/decoder.rs b/mqtt-v5/src/decoder.rs index 015b993..cc057c9 100644 --- a/mqtt-v5/src/decoder.rs +++ b/mqtt-v5/src/decoder.rs @@ -1,14 +1,17 @@ -use crate::types::{ - properties::*, AuthenticatePacket, AuthenticateReason, ConnectAckPacket, ConnectPacket, - ConnectReason, DecodeError, DisconnectPacket, DisconnectReason, FinalWill, Packet, PacketType, - ProtocolVersion, PublishAckPacket, PublishAckReason, PublishCompletePacket, - PublishCompleteReason, PublishPacket, PublishReceivedPacket, PublishReceivedReason, - PublishReleasePacket, PublishReleaseReason, QoS, RetainHandling, SubscribeAckPacket, - SubscribeAckReason, SubscribePacket, SubscriptionTopic, UnsubscribeAckPacket, - UnsubscribeAckReason, UnsubscribePacket, VariableByteInt, +use crate::{ + topic::Topic, + types::{ + properties::*, AuthenticatePacket, AuthenticateReason, ConnectAckPacket, ConnectPacket, + ConnectReason, DecodeError, DisconnectPacket, DisconnectReason, FinalWill, Packet, + PacketType, ProtocolVersion, PublishAckPacket, PublishAckReason, PublishCompletePacket, + PublishCompleteReason, PublishPacket, PublishReceivedPacket, PublishReceivedReason, + PublishReleasePacket, PublishReleaseReason, QoS, RetainHandling, SubscribeAckPacket, + SubscribeAckReason, SubscribePacket, SubscriptionTopic, UnsubscribeAckPacket, + UnsubscribeAckReason, UnsubscribePacket, VariableByteInt, + }, }; use bytes::{Buf, Bytes, BytesMut}; -use std::{convert::TryFrom, io::Cursor}; +use std::{convert::TryFrom, io::Cursor, str::FromStr}; macro_rules! return_if_none { ($x: expr) => {{ @@ -405,7 +408,8 @@ fn decode_connect(bytes: &mut Cursor<&mut BytesMut>) -> Result, D })?); } - let topic = read_string!(bytes); + let topic = + Topic::from_str(read_string!(bytes).as_str()).map_err(DecodeError::InvalidTopic)?; let payload = read_binary_data!(bytes); Some(FinalWill { diff --git a/mqtt-v5/src/encoder.rs b/mqtt-v5/src/encoder.rs index 0589b10..acf48a3 100644 --- a/mqtt-v5/src/encoder.rs +++ b/mqtt-v5/src/encoder.rs @@ -266,7 +266,7 @@ fn encode_connect(packet: &ConnectPacket, bytes: &mut BytesMut, protocol_version will.user_properties.encode(bytes); } - encode_string(&will.topic, bytes); + encode_string(will.topic.topic_name(), bytes); encode_binary_data(&will.payload, bytes); } diff --git a/mqtt-v5/src/types.rs b/mqtt-v5/src/types.rs index 11fe4c5..61b296a 100644 --- a/mqtt-v5/src/types.rs +++ b/mqtt-v5/src/types.rs @@ -747,7 +747,7 @@ pub enum AuthenticateReason { // Payloads #[derive(Debug, Clone, PartialEq, Eq)] pub struct FinalWill { - pub topic: String, // TODO(bschwind) - Use Topic type here. + pub topic: Topic, pub payload: Bytes, pub qos: QoS, pub should_retain: bool, @@ -962,7 +962,7 @@ impl From for PublishPacket { retain: will.should_retain, // Variable header - topic: will.topic.parse().unwrap(), // TODO(bschwind) - Add a Topic type directly to FinalWill + topic: will.topic, packet_id: None, // Properties