@@ -5,6 +5,7 @@ crate::utils::enum_wrapper!(uds, UdsCommand, UdsCommandByte);
5
5
#[ derive( strum:: FromRepr , Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
6
6
#[ cfg_attr( feature = "iter" , derive( strum:: EnumIter ) ) ]
7
7
#[ cfg_attr( feature = "display" , derive( displaydoc:: Display ) ) ]
8
+ #[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
8
9
pub enum UdsCommand {
9
10
/// The client requests to control a diagnostic session with a server(s).
10
11
DiagnosticSessionControl = 0x10 ,
@@ -59,3 +60,33 @@ pub enum UdsCommand {
59
60
/// The client requests the negotiation of a file transfer between server and client.
60
61
RequestFileTransfer = 0x38 ,
61
62
}
63
+
64
+ #[ cfg( all( test, feature = "serde" ) ) ]
65
+ mod tests {
66
+ use super :: * ;
67
+
68
+ #[ derive( serde:: Serialize , serde:: Deserialize ) ]
69
+ struct TestStruct {
70
+ command : UdsCommand ,
71
+ command_byte : UdsCommandByte ,
72
+ }
73
+
74
+ #[ test]
75
+ fn test_serde ( ) {
76
+ let test = TestStruct {
77
+ command : UdsCommand :: DiagnosticSessionControl ,
78
+ command_byte : UdsCommandByte :: from ( UdsCommand :: DiagnosticSessionControl ) ,
79
+ } ;
80
+
81
+ let json = serde_json:: to_string ( & test) . unwrap ( ) ;
82
+ assert_eq ! (
83
+ json,
84
+ r###"{"command":"DiagnosticSessionControl","command_byte":{"Standard":"DiagnosticSessionControl"}}"###
85
+ ) ;
86
+
87
+ let deserialized: TestStruct = serde_json:: from_str ( & json) . unwrap ( ) ;
88
+
89
+ assert_eq ! ( test. command, deserialized. command) ;
90
+ assert_eq ! ( test. command_byte, deserialized. command_byte) ;
91
+ }
92
+ }
0 commit comments