@@ -18,7 +18,7 @@ pub(crate) struct CommandMetadata {
18
18
19
19
impl fmt:: Display for CommandMetadata {
20
20
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
21
- write ! ( f, "{:? } " , self . ty) ?;
21
+ write ! ( f, "{} " , self . ty) ?;
22
22
if let Some ( name) = & self . name {
23
23
write ! ( f, "[{}]" , name) ?;
24
24
} else {
@@ -171,3 +171,133 @@ impl From<CommandType> for MessageType {
171
171
}
172
172
}
173
173
}
174
+
175
+ impl TryFrom < MessageType > for CommandType {
176
+ type Error = MessageType ;
177
+
178
+ fn try_from ( value : MessageType ) -> Result < Self , Self :: Error > {
179
+ match value {
180
+ MessageType :: InputCommand => Ok ( CommandType :: Input ) ,
181
+ MessageType :: OutputCommand => Ok ( CommandType :: Output ) ,
182
+ MessageType :: GetLazyStateCommand | MessageType :: GetEagerStateCommand => {
183
+ Ok ( CommandType :: GetState )
184
+ }
185
+ MessageType :: GetLazyStateKeysCommand | MessageType :: GetEagerStateKeysCommand => {
186
+ Ok ( CommandType :: GetStateKeys )
187
+ }
188
+ MessageType :: SetStateCommand => Ok ( CommandType :: SetState ) ,
189
+ MessageType :: ClearStateCommand => Ok ( CommandType :: ClearState ) ,
190
+ MessageType :: ClearAllStateCommand => Ok ( CommandType :: ClearAllState ) ,
191
+ MessageType :: GetPromiseCommand => Ok ( CommandType :: GetPromise ) ,
192
+ MessageType :: PeekPromiseCommand => Ok ( CommandType :: PeekPromise ) ,
193
+ MessageType :: CompletePromiseCommand => Ok ( CommandType :: CompletePromise ) ,
194
+ MessageType :: SleepCommand => Ok ( CommandType :: Sleep ) ,
195
+ MessageType :: CallCommand => Ok ( CommandType :: Call ) ,
196
+ MessageType :: OneWayCallCommand => Ok ( CommandType :: OneWayCall ) ,
197
+ MessageType :: SendSignalCommand => Ok ( CommandType :: SendSignal ) ,
198
+ MessageType :: RunCommand => Ok ( CommandType :: Run ) ,
199
+ MessageType :: AttachInvocationCommand => Ok ( CommandType :: AttachInvocation ) ,
200
+ MessageType :: GetInvocationOutputCommand => Ok ( CommandType :: GetInvocationOutput ) ,
201
+ MessageType :: CompleteAwakeableCommand => Ok ( CommandType :: CompleteAwakeable ) ,
202
+ _ => Err ( value) ,
203
+ }
204
+ }
205
+ }
206
+
207
+ #[ cfg( test) ]
208
+ mod tests {
209
+ use super :: * ;
210
+
211
+ #[ test]
212
+ fn test_message_type_to_command_type_conversion ( ) {
213
+ // Test successful conversions
214
+ assert_eq ! (
215
+ CommandType :: try_from( MessageType :: InputCommand ) . unwrap( ) ,
216
+ CommandType :: Input
217
+ ) ;
218
+ assert_eq ! (
219
+ CommandType :: try_from( MessageType :: OutputCommand ) . unwrap( ) ,
220
+ CommandType :: Output
221
+ ) ;
222
+ assert_eq ! (
223
+ CommandType :: try_from( MessageType :: GetLazyStateCommand ) . unwrap( ) ,
224
+ CommandType :: GetState
225
+ ) ;
226
+ assert_eq ! (
227
+ CommandType :: try_from( MessageType :: GetLazyStateKeysCommand ) . unwrap( ) ,
228
+ CommandType :: GetStateKeys
229
+ ) ;
230
+ assert_eq ! (
231
+ CommandType :: try_from( MessageType :: SetStateCommand ) . unwrap( ) ,
232
+ CommandType :: SetState
233
+ ) ;
234
+ assert_eq ! (
235
+ CommandType :: try_from( MessageType :: ClearStateCommand ) . unwrap( ) ,
236
+ CommandType :: ClearState
237
+ ) ;
238
+ assert_eq ! (
239
+ CommandType :: try_from( MessageType :: ClearAllStateCommand ) . unwrap( ) ,
240
+ CommandType :: ClearAllState
241
+ ) ;
242
+ assert_eq ! (
243
+ CommandType :: try_from( MessageType :: GetPromiseCommand ) . unwrap( ) ,
244
+ CommandType :: GetPromise
245
+ ) ;
246
+ assert_eq ! (
247
+ CommandType :: try_from( MessageType :: PeekPromiseCommand ) . unwrap( ) ,
248
+ CommandType :: PeekPromise
249
+ ) ;
250
+ assert_eq ! (
251
+ CommandType :: try_from( MessageType :: CompletePromiseCommand ) . unwrap( ) ,
252
+ CommandType :: CompletePromise
253
+ ) ;
254
+ assert_eq ! (
255
+ CommandType :: try_from( MessageType :: SleepCommand ) . unwrap( ) ,
256
+ CommandType :: Sleep
257
+ ) ;
258
+ assert_eq ! (
259
+ CommandType :: try_from( MessageType :: CallCommand ) . unwrap( ) ,
260
+ CommandType :: Call
261
+ ) ;
262
+ assert_eq ! (
263
+ CommandType :: try_from( MessageType :: OneWayCallCommand ) . unwrap( ) ,
264
+ CommandType :: OneWayCall
265
+ ) ;
266
+ assert_eq ! (
267
+ CommandType :: try_from( MessageType :: SendSignalCommand ) . unwrap( ) ,
268
+ CommandType :: SendSignal
269
+ ) ;
270
+ assert_eq ! (
271
+ CommandType :: try_from( MessageType :: RunCommand ) . unwrap( ) ,
272
+ CommandType :: Run
273
+ ) ;
274
+ assert_eq ! (
275
+ CommandType :: try_from( MessageType :: AttachInvocationCommand ) . unwrap( ) ,
276
+ CommandType :: AttachInvocation
277
+ ) ;
278
+ assert_eq ! (
279
+ CommandType :: try_from( MessageType :: GetInvocationOutputCommand ) . unwrap( ) ,
280
+ CommandType :: GetInvocationOutput
281
+ ) ;
282
+ assert_eq ! (
283
+ CommandType :: try_from( MessageType :: CompleteAwakeableCommand ) . unwrap( ) ,
284
+ CommandType :: CompleteAwakeable
285
+ ) ;
286
+
287
+ // Test failed conversions
288
+ assert_eq ! (
289
+ CommandType :: try_from( MessageType :: Start ) . err( ) . unwrap( ) ,
290
+ MessageType :: Start
291
+ ) ;
292
+ assert_eq ! (
293
+ CommandType :: try_from( MessageType :: End ) . err( ) . unwrap( ) ,
294
+ MessageType :: End
295
+ ) ;
296
+ assert_eq ! (
297
+ CommandType :: try_from( MessageType :: GetLazyStateCompletionNotification )
298
+ . err( )
299
+ . unwrap( ) ,
300
+ MessageType :: GetLazyStateCompletionNotification
301
+ ) ;
302
+ }
303
+ }
0 commit comments