@@ -11,6 +11,7 @@ use byteorder::{BigEndian, ByteOrder};
1111/// A Modbus function code.
1212///
1313/// It is represented by an unsigned 8 bit integer.
14+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
1415#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
1516pub enum FunctionCode {
1617 /// Modbus Function Code: `01` (`0x01`).
@@ -157,6 +158,7 @@ pub(crate) type Quantity = u16;
157158type RawData < ' r > = & ' r [ u8 ] ;
158159
159160/// A request represents a message from the client (master) to the server (slave).
161+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
160162#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
161163pub enum Request < ' r > {
162164 ReadCoils ( Address , Quantity ) ,
@@ -191,17 +193,20 @@ pub enum Request<'r> {
191193}
192194
193195/// A server (slave) exception response.
196+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
194197#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
195198pub struct ExceptionResponse {
196199 pub function : FunctionCode ,
197200 pub exception : Exception ,
198201}
199202
200203/// Represents a message from the client (slave) to the server (master).
204+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
201205#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
202206pub struct RequestPdu < ' r > ( pub Request < ' r > ) ;
203207
204208/// Represents a message from the server (slave) to the client (master).
209+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
205210#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
206211pub struct ResponsePdu < ' r > ( pub Result < Response < ' r > , ExceptionResponse > ) ;
207212
@@ -213,6 +218,7 @@ type EventCount = u16;
213218type MessageCount = u16 ;
214219
215220/// The response data of a successfull request.
221+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
216222#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
217223pub enum Response < ' r > {
218224 ReadCoils ( Coils < ' r > ) ,
@@ -305,6 +311,7 @@ impl<'r> From<Response<'r>> for FunctionCode {
305311}
306312
307313/// A server (slave) exception.
314+
308315#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
309316pub enum Exception {
310317 IllegalFunction = 0x01 ,
@@ -318,9 +325,9 @@ pub enum Exception {
318325 GatewayTargetDevice = 0x0B ,
319326}
320327
321- impl fmt :: Display for Exception {
322- fn fmt ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
323- let desc = match * self {
328+ impl Exception {
329+ fn get_name ( & self ) -> & ' static str {
330+ match * self {
324331 Self :: IllegalFunction => "Illegal function" ,
325332 Self :: IllegalDataAddress => "Illegal data address" ,
326333 Self :: IllegalDataValue => "Illegal data value" ,
@@ -330,11 +337,20 @@ impl fmt::Display for Exception {
330337 Self :: MemoryParityError => "Memory parity error" ,
331338 Self :: GatewayPathUnavailable => "Gateway path unavailable" ,
332339 Self :: GatewayTargetDevice => "Gateway target device failed to respond" ,
333- } ;
334- write ! ( f, "{desc}" )
340+ }
335341 }
336342}
337343
344+ impl fmt:: Display for Exception {
345+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { write ! ( f, "{}" , self . get_name( ) ) }
346+ }
347+
348+ #[ cfg( feature = "defmt" ) ]
349+ impl defmt:: Format for Exception {
350+ fn format ( & self , fmt : defmt:: Formatter ) { defmt:: write!( fmt, "{}" , self . get_name( ) ) }
351+ }
352+
353+
338354impl Request < ' _ > {
339355 /// Number of bytes required for a serialized PDU frame.
340356 #[ must_use]
0 commit comments