Skip to content

Commit

Permalink
Changed Will topic field to use Topic (#53)
Browse files Browse the repository at this point in the history
* Changed Will topic field to use Topic

(cherry picked from commit 6579ad3)

* Build fix

* Fixed unnecessary borrow

* Right Error Mapping

Co-authored-by: Brian Schwind <[email protected]>

* Removed unnecessary import

---------

Co-authored-by: Brian Schwind <[email protected]>
  • Loading branch information
Nereuxofficial and bschwind authored Feb 23, 2023
1 parent 7fea543 commit 21e5bb0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
24 changes: 14 additions & 10 deletions mqtt-v5/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -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) => {{
Expand Down Expand Up @@ -405,7 +408,8 @@ fn decode_connect(bytes: &mut Cursor<&mut BytesMut>) -> Result<Option<Packet>, 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 {
Expand Down
2 changes: 1 addition & 1 deletion mqtt-v5/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions mqtt-v5/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -962,7 +962,7 @@ impl From<FinalWill> 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
Expand Down

0 comments on commit 21e5bb0

Please sign in to comment.