11use crate :: client:: { FlushClient , PublishClient , RequestClient , SubscribeClient } ;
2+ use async_nats:: subject:: ToSubject ;
23use std:: sync:: { Arc , Mutex } ;
34
45#[ derive( Debug , Clone ) ]
@@ -118,18 +119,24 @@ impl Default for AdvancedMockNatsClient {
118119impl SubscribeClient for MockNatsClient {
119120 type SubscribeError = MockError ;
120121
121- async fn subscribe ( & self , subject : String ) -> Result < async_nats:: Subscriber , MockError > {
122- self . subscribed_subjects . lock ( ) . unwrap ( ) . push ( subject) ;
122+ async fn subscribe < S : ToSubject + Send > (
123+ & self ,
124+ subject : S ,
125+ ) -> Result < async_nats:: Subscriber , MockError > {
126+ self . subscribed_subjects
127+ . lock ( )
128+ . unwrap ( )
129+ . push ( subject. to_subject ( ) . to_string ( ) ) ;
123130 Err ( MockError ( "mock: subscribe not implemented" . to_string ( ) ) )
124131 }
125132}
126133
127134impl RequestClient for MockNatsClient {
128135 type RequestError = MockError ;
129136
130- async fn request_with_headers (
137+ async fn request_with_headers < S : ToSubject + Send > (
131138 & self ,
132- _subject : String ,
139+ _subject : S ,
133140 _headers : async_nats:: HeaderMap ,
134141 _payload : bytes:: Bytes ,
135142 ) -> Result < async_nats:: Message , MockError > {
@@ -140,16 +147,16 @@ impl RequestClient for MockNatsClient {
140147impl PublishClient for MockNatsClient {
141148 type PublishError = MockError ;
142149
143- async fn publish_with_headers (
150+ async fn publish_with_headers < S : ToSubject + Send > (
144151 & self ,
145- subject : String ,
152+ subject : S ,
146153 _headers : async_nats:: HeaderMap ,
147154 payload : bytes:: Bytes ,
148155 ) -> Result < ( ) , MockError > {
149- self . published
150- . lock ( )
151- . unwrap ( )
152- . push ( PublishedMessage { subject , payload } ) ;
156+ self . published . lock ( ) . unwrap ( ) . push ( PublishedMessage {
157+ subject : subject . to_subject ( ) . to_string ( ) ,
158+ payload ,
159+ } ) ;
153160 Ok ( ( ) )
154161 }
155162}
@@ -165,20 +172,24 @@ impl FlushClient for MockNatsClient {
165172impl SubscribeClient for AdvancedMockNatsClient {
166173 type SubscribeError = MockError ;
167174
168- async fn subscribe ( & self , subject : String ) -> Result < async_nats:: Subscriber , MockError > {
175+ async fn subscribe < S : ToSubject + Send > (
176+ & self ,
177+ subject : S ,
178+ ) -> Result < async_nats:: Subscriber , MockError > {
169179 self . base . subscribe ( subject) . await
170180 }
171181}
172182
173183impl RequestClient for AdvancedMockNatsClient {
174184 type RequestError = MockError ;
175185
176- async fn request_with_headers (
186+ async fn request_with_headers < S : ToSubject + Send > (
177187 & self ,
178- subject : String ,
188+ subject : S ,
179189 _headers : async_nats:: HeaderMap ,
180190 _payload : bytes:: Bytes ,
181191 ) -> Result < async_nats:: Message , MockError > {
192+ let subject = subject. to_subject ( ) . to_string ( ) ;
182193 let should_fail = * self . should_fail_request . lock ( ) . unwrap ( ) ;
183194 if should_fail {
184195 * self . should_fail_request . lock ( ) . unwrap ( ) = false ;
@@ -207,9 +218,9 @@ impl RequestClient for AdvancedMockNatsClient {
207218impl PublishClient for AdvancedMockNatsClient {
208219 type PublishError = MockError ;
209220
210- async fn publish_with_headers (
221+ async fn publish_with_headers < S : ToSubject + Send > (
211222 & self ,
212- subject : String ,
223+ subject : S ,
213224 headers : async_nats:: HeaderMap ,
214225 payload : bytes:: Bytes ,
215226 ) -> Result < ( ) , MockError > {
@@ -242,15 +253,19 @@ mod tests {
242253 async fn mock_client_tracks_publish ( ) {
243254 let mock = MockNatsClient :: new ( ) ;
244255 let _ = mock
245- . publish_with_headers ( "foo" . into ( ) , async_nats:: HeaderMap :: new ( ) , "bar" . into ( ) )
256+ . publish_with_headers (
257+ "foo" ,
258+ async_nats:: HeaderMap :: new ( ) ,
259+ bytes:: Bytes :: from ( "bar" ) ,
260+ )
246261 . await ;
247262 assert_eq ! ( mock. published_messages( ) , vec![ "foo" ] ) ;
248263 }
249264
250265 #[ tokio:: test]
251266 async fn mock_client_tracks_subscribe ( ) {
252267 let mock = MockNatsClient :: new ( ) ;
253- let _ = mock. subscribe ( "test.sub" . into ( ) ) . await ;
268+ let _ = mock. subscribe ( "test.sub" ) . await ;
254269 assert_eq ! ( mock. subscribed_to( ) , vec![ "test.sub" ] ) ;
255270 }
256271
@@ -273,7 +288,11 @@ mod tests {
273288 async fn advanced_mock_request_no_response_configured ( ) {
274289 let mock = AdvancedMockNatsClient :: new ( ) ;
275290 let result = mock
276- . request_with_headers ( "missing" . into ( ) , async_nats:: HeaderMap :: new ( ) , "x" . into ( ) )
291+ . request_with_headers (
292+ "missing" ,
293+ async_nats:: HeaderMap :: new ( ) ,
294+ bytes:: Bytes :: from ( "x" ) ,
295+ )
277296 . await ;
278297 assert ! ( result. is_err( ) ) ;
279298 assert ! ( result. unwrap_err( ) . 0 . contains( "no response configured" ) ) ;
0 commit comments