@@ -9,88 +9,74 @@ use crate::stats::*;
99
1010use bytes:: BytesMut ;
1111use cookie_factory:: GenError ;
12- use failure :: Fail ;
12+ use thiserror :: Error ;
1313use nom:: { error:: ErrorKind , Err } ;
1414use tokio_util:: codec:: { Decoder , Encoder } ;
1515
1616/// A serialized `Packet` should be not longer than 2048 bytes.
1717pub const MAX_DHT_PACKET_SIZE : usize = 2048 ;
1818
19- error_kind ! {
20- #[ doc = "Error that can happen when decoding `Packet` from bytes." ]
21- #[ derive( Debug ) ]
22- DecodeError ,
23- #[ doc = "Error that can happen when decoding `Packet` from bytes." ]
24- #[ derive( Clone , Debug , Eq , PartialEq , Fail ) ]
25- DecodeErrorKind {
26- #[ doc = "Error indicates that we received too big packet." ]
27- #[ fail( display = "Packet should not be longer than 2048 bytes: {} bytes" , len) ]
28- TooBigPacket {
29- #[ doc = "Length of received packet." ]
30- len: usize
31- } ,
32- #[ doc = "Error indicates that received packet can't be parsed." ]
33- #[ fail( display = "Deserialize Packet error: {:?}, packet: {:?}" , error, packet) ]
34- Deserialize {
35- #[ doc = "Parsing error." ]
36- error: nom:: Err <( Vec <u8 >, ErrorKind ) >,
37- #[ doc = "Received packet." ]
38- packet: Vec <u8 >,
39- } ,
40- #[ doc = "General IO error that can happen with UDP socket." ]
41- #[ fail( display = "IO Error" ) ]
42- Io ,
43- }
19+ /// Error that can happen when decoding `Packet` from bytes.
20+ #[ derive( Debug , Error ) ]
21+ pub enum DecodeError {
22+ /// Error indicates that we received too big packet.
23+ #[ error( "Packet should not be longer than 2048 bytes: {} bytes" , len) ]
24+ TooBigPacket {
25+ /// Length of received packet.
26+ len : usize
27+ } ,
28+ /// Error indicates that received packet can't be parsed.
29+ #[ error( "Deserialize Packet error: {:?}, packet: {:?}" , error, packet) ]
30+ Deserialize {
31+ /// Parsing error.
32+ error : nom:: Err < ( Vec < u8 > , ErrorKind ) > ,
33+ /// Received packet.
34+ packet : Vec < u8 > ,
35+ } ,
36+ /// General IO error that can happen with UDP socket.
37+ #[ error( "IO Error" ) ]
38+ Io ( IoError ) ,
4439}
4540
4641impl DecodeError {
4742 pub ( crate ) fn too_big_packet ( len : usize ) -> DecodeError {
48- DecodeError :: from ( DecodeErrorKind :: TooBigPacket { len } )
43+ DecodeError :: TooBigPacket { len }
4944 }
5045
5146 pub ( crate ) fn deserialize ( e : Err < ( & [ u8 ] , ErrorKind ) > , packet : Vec < u8 > ) -> DecodeError {
52- DecodeError :: from ( DecodeErrorKind :: Deserialize { error : e. to_owned ( ) , packet } )
47+ DecodeError :: Deserialize { error : e. to_owned ( ) , packet }
5348 }
5449}
5550
56- error_kind ! {
57- #[ doc = "Error that can happen when encoding `Packet` to bytes." ]
58- #[ derive( Debug ) ]
59- EncodeError ,
60- #[ doc = "Error that can happen when encoding `Packet` to bytes." ]
61- #[ derive( Debug , Fail ) ]
62- EncodeErrorKind {
63- #[ doc = "Error indicates that `Packet` is invalid and can't be serialized." ]
64- #[ fail( display = "Serialize Packet error: {:?}" , error) ]
65- Serialize {
66- #[ doc = "Serialization error." ]
67- error: GenError
68- } ,
69- #[ doc = "General IO error that can happen with UDP socket." ]
70- #[ fail( display = "IO Error" ) ]
71- Io ,
72- }
51+ /// Error that can happen when encoding `Packet` to bytes.
52+ #[ derive( Debug , Error ) ]
53+ pub enum EncodeError {
54+ /// Error indicates that `Packet` is invalid and can't be serialized.
55+ #[ error( "Serialize Packet error: {:?}" , error) ]
56+ Serialize {
57+ /// Serialization error.
58+ error : GenError
59+ } ,
60+ /// General IO error that can happen with UDP socket.
61+ #[ error( "IO Error" ) ]
62+ Io ( IoError ) ,
7363}
7464
7565impl EncodeError {
7666 pub ( crate ) fn serialize ( error : GenError ) -> EncodeError {
77- EncodeError :: from ( EncodeErrorKind :: Serialize { error } )
67+ EncodeError :: Serialize { error }
7868 }
7969}
8070
8171impl From < IoError > for DecodeError {
8272 fn from ( error : IoError ) -> DecodeError {
83- DecodeError {
84- ctx : error. context ( DecodeErrorKind :: Io )
85- }
73+ DecodeError :: Io ( error)
8674 }
8775}
8876
8977impl From < IoError > for EncodeError {
9078 fn from ( error : IoError ) -> EncodeError {
91- EncodeError {
92- ctx : error. context ( EncodeErrorKind :: Io )
93- }
79+ EncodeError :: Io ( error)
9480 }
9581}
9682
@@ -340,9 +326,8 @@ mod tests {
340326 buf. extend_from_slice ( b"\xFF " ) ;
341327
342328 let res = codec. decode ( & mut buf) ;
343- // not enought bytes to decode EncryptedPacket
344- let error = res. err ( ) . unwrap ( ) ;
345- assert_eq ! ( * error. kind( ) , DecodeErrorKind :: Deserialize { error: Err :: Error ( ( vec![ 255 ] , ErrorKind :: Alt ) ) , packet: vec![ 0xff ] } ) ;
329+ // not enough bytes to decode EncryptedPacket
330+ assert ! ( matches!( res, Err ( DecodeError :: Deserialize { error: Err :: Error ( ( _, ErrorKind :: Alt ) ) , packet: _ } ) ) ) ;
346331 }
347332
348333 #[ test]
@@ -372,9 +357,8 @@ mod tests {
372357 let res = codec. encode ( packet, & mut buf) ;
373358 assert ! ( res. is_err( ) ) ;
374359 let error = res. err ( ) . unwrap ( ) ;
375- let error_kind = error. kind ( ) ;
376- let error_serialize = unpack ! ( error_kind, EncodeErrorKind :: Serialize , error) ;
360+ let error_serialize = unpack ! ( error, EncodeError :: Serialize , error) ;
377361 let too_small = unpack ! ( error_serialize, GenError :: BufferTooSmall ) ;
378- assert_eq ! ( * too_small, 2106 - MAX_DHT_PACKET_SIZE ) ;
362+ assert_eq ! ( too_small, 2106 - MAX_DHT_PACKET_SIZE ) ;
379363 }
380364}
0 commit comments