1717
1818use std:: cmp:: Ordering ;
1919use std:: fmt;
20- use std:: hash:: Hash ;
21- use std:: hash:: Hasher ;
2220
2321use serde:: Deserialize ;
2422use serde:: Serialize ;
2523
26- #[ derive( Clone , Debug ) ]
24+ #[ derive( Debug , Clone , Hash , Serialize , Deserialize , Eq , Ord , PartialEq , PartialOrd ) ]
2725pub struct MessageQueue {
2826 topic : String ,
2927 broker_name : String ,
3028 queue_id : i32 ,
3129}
32-
3330impl MessageQueue {
3431 pub fn new ( ) -> Self {
3532 MessageQueue {
@@ -47,7 +44,7 @@ impl MessageQueue {
4744 }
4845 }
4946
50- pub fn with_params ( topic : & str , broker_name : & str , queue_id : i32 ) -> Self {
47+ pub fn from_parts ( topic : & str , broker_name : & str , queue_id : i32 ) -> Self {
5148 MessageQueue {
5249 topic : topic. to_string ( ) ,
5350 broker_name : broker_name. to_string ( ) ,
@@ -59,16 +56,16 @@ impl MessageQueue {
5956 & self . topic
6057 }
6158
62- pub fn set_topic ( & mut self , topic : & str ) {
63- self . topic = topic. to_string ( ) ;
59+ pub fn set_topic ( & mut self , topic : String ) {
60+ self . topic = topic;
6461 }
6562
6663 pub fn get_broker_name ( & self ) -> & str {
6764 & self . broker_name
6865 }
6966
70- pub fn set_broker_name ( & mut self , broker_name : & str ) {
71- self . broker_name = broker_name. to_string ( ) ;
67+ pub fn set_broker_name ( & mut self , broker_name : String ) {
68+ self . broker_name = broker_name;
7269 }
7370
7471 pub fn get_queue_id ( & self ) -> i32 {
@@ -80,116 +77,18 @@ impl MessageQueue {
8077 }
8178}
8279
83- impl PartialEq for MessageQueue {
84- fn eq ( & self , other : & Self ) -> bool {
85- self . topic == other. topic
86- && self . broker_name == other. broker_name
87- && self . queue_id == other. queue_id
88- }
89- }
90-
91- impl Eq for MessageQueue { }
92-
93- impl Hash for MessageQueue {
94- fn hash < H : Hasher > ( & self , state : & mut H ) {
95- self . topic . hash ( state) ;
96- self . broker_name . hash ( state) ;
97- self . queue_id . hash ( state) ;
98- }
99- }
100-
101- impl Ord for MessageQueue {
102- fn cmp ( & self , other : & Self ) -> Ordering {
103- self . topic
104- . cmp ( & other. topic )
105- . then_with ( || self . broker_name . cmp ( & other. broker_name ) )
106- . then_with ( || self . queue_id . cmp ( & other. queue_id ) )
107- }
108- }
109-
110- impl PartialOrd for MessageQueue {
111- fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
112- Some ( self . cmp ( other) )
113- }
114- }
115-
11680impl fmt:: Display for MessageQueue {
11781 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
11882 write ! (
11983 f,
120- "MessageQueue [topic={}, broker_name ={}, queue_id ={}]" ,
84+ "MessageQueue [topic={}, brokerName ={}, queueId ={}]" ,
12185 self . topic, self . broker_name, self . queue_id
12286 )
12387 }
12488}
12589
126- #[ cfg( test) ]
127- mod message_queue_tests {
128- use super :: * ;
129-
130- #[ test]
131- fn new_creates_empty_message_queue ( ) {
132- let mq = MessageQueue :: new ( ) ;
133- assert_eq ! ( mq. topic, "" ) ;
134- assert_eq ! ( mq. broker_name, "" ) ;
135- assert_eq ! ( mq. queue_id, 0 ) ;
136- }
137-
138- #[ test]
139- fn from_other_copies_all_fields ( ) {
140- let original = MessageQueue :: with_params ( "topic1" , "broker1" , 1 ) ;
141- let copy = MessageQueue :: from_other ( & original) ;
142- assert_eq ! ( copy. topic, "topic1" ) ;
143- assert_eq ! ( copy. broker_name, "broker1" ) ;
144- assert_eq ! ( copy. queue_id, 1 ) ;
145- }
146-
147- #[ test]
148- fn with_params_sets_all_fields ( ) {
149- let mq = MessageQueue :: with_params ( "topic2" , "broker2" , 2 ) ;
150- assert_eq ! ( mq. topic, "topic2" ) ;
151- assert_eq ! ( mq. broker_name, "broker2" ) ;
152- assert_eq ! ( mq. queue_id, 2 ) ;
153- }
154-
155- #[ test]
156- fn set_topic_updates_topic ( ) {
157- let mut mq = MessageQueue :: new ( ) ;
158- mq. set_topic ( "new_topic" ) ;
159- assert_eq ! ( mq. topic, "new_topic" ) ;
160- }
161-
162- #[ test]
163- fn set_broker_name_updates_broker_name ( ) {
164- let mut mq = MessageQueue :: new ( ) ;
165- mq. set_broker_name ( "new_broker" ) ;
166- assert_eq ! ( mq. broker_name, "new_broker" ) ;
167- }
168-
169- #[ test]
170- fn set_queue_id_updates_queue_id ( ) {
171- let mut mq = MessageQueue :: new ( ) ;
172- mq. set_queue_id ( 3 ) ;
173- assert_eq ! ( mq. queue_id, 3 ) ;
174- }
175-
176- #[ test]
177- fn equals_self ( ) {
178- let mq1 = MessageQueue :: with_params ( "topic" , "broker" , 1 ) ;
179- assert ! ( mq1. eq( & mq1) ) ;
180- }
181-
182- #[ test]
183- fn not_equal_different_topic ( ) {
184- let mq1 = MessageQueue :: with_params ( "topic1" , "broker" , 1 ) ;
185- let mq2 = MessageQueue :: with_params ( "topic2" , "broker" , 1 ) ;
186- assert ! ( !mq1. eq( & mq2) ) ;
187- }
188-
189- #[ test]
190- fn not_equal_different_broker_name ( ) {
191- let mq1 = MessageQueue :: with_params ( "topic" , "broker1" , 1 ) ;
192- let mq2 = MessageQueue :: with_params ( "topic" , "broker2" , 1 ) ;
193- assert ! ( !mq1. eq( & mq2) ) ;
90+ impl Default for MessageQueue {
91+ fn default ( ) -> Self {
92+ MessageQueue :: new ( )
19493 }
19594}
0 commit comments