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