Skip to content

Commit e8b8f31

Browse files
committed
Use new overloads instead to not repeat constructor code
1 parent 633c1c6 commit e8b8f31

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

src/mqtt/protocol/packets/publish.cr

+5-14
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,20 @@ module MQTT
99
getter topic, payload, qos, packet_id, remaining_length
1010
getter? dup, retain
1111

12-
def initialize(pub : Publish, topic = pub.topic, qos = pub.qos, payload = pub.payload, packet_id = pub.packet_id, dup = pub.dup?, retain = pub.retain?)
13-
initialize(topic, payload, packet_id, dup, qos, retain)
12+
def self.new(topic : String, payload : Bytes, packet_id : UInt16?, dup : Bool, qos : UInt8, retain : Bool)
13+
new(topic, BytesPayload.new(payload), packet_id, dup, qos, retain)
1414
end
1515

16-
def initialize(@topic : String, @payload : Payload, @packet_id : UInt16?, @dup : Bool, @qos : UInt8, @retain : Bool)
17-
raise ArgumentError.new("QoS must be 0, 1 or 2") if @qos > 2
18-
raise ArgumentError.new("Topic cannot contain wildcard") if @topic.matches?(/[#+]/)
19-
raise ArgumentError.new("Topic must be between atleast 1 char long") if @topic.size < 1
20-
raise ArgumentError.new("Topic cannot be larger than 65535 bytes") if @topic.bytesize > 65535
21-
raise ArgumentError.new("DUP must be 0 for QoS 0 messages") if dup? && qos.zero?
22-
@remaining_length = 0
23-
@remaining_length += (2 + topic.bytesize) + payload.bytesize
24-
@remaining_length += 2 if qos.positive? # packet_id
16+
def self.new(pub : Publish, topic = pub.topic, qos = pub.qos, payload = pub.payload, packet_id = pub.packet_id, dup = pub.dup?, retain = pub.retain?)
17+
new(topic, payload, packet_id, dup, qos, retain)
2518
end
2619

27-
@[Deprecated("Use Payload instead of Bytes for @payload")]
28-
def initialize(@topic : String, payload : Bytes, @packet_id : UInt16?, @dup : Bool, @qos : UInt8, @retain : Bool)
20+
def initialize(@topic : String, @payload : Payload, @packet_id : UInt16?, @dup : Bool, @qos : UInt8, @retain : Bool)
2921
raise ArgumentError.new("QoS must be 0, 1 or 2") if @qos > 2
3022
raise ArgumentError.new("Topic cannot contain wildcard") if @topic.matches?(/[#+]/)
3123
raise ArgumentError.new("Topic must be between atleast 1 char long") if @topic.size < 1
3224
raise ArgumentError.new("Topic cannot be larger than 65535 bytes") if @topic.bytesize > 65535
3325
raise ArgumentError.new("DUP must be 0 for QoS 0 messages") if dup? && qos.zero?
34-
@payload = BytesPayload.new(payload)
3526
@remaining_length = 0
3627
@remaining_length += (2 + topic.bytesize) + payload.bytesize
3728
@remaining_length += 2 if qos.positive? # packet_id

0 commit comments

Comments
 (0)