1+ // Copyright (c) 2023 Doug Hoyte
2+ // Copyright (c) 2023 Yuki Kishimoto
3+ // Distributed under the MIT software license
4+
5+ use alloc:: string:: { String , ToString } ;
6+ use core:: array:: TryFromSliceError ;
17use core:: fmt;
28
39use crate :: hex;
410
5-
611/// Error
712#[ derive( Debug , PartialEq , Eq ) ]
813pub enum Error {
@@ -32,6 +37,8 @@ pub enum Error {
3237 ParseEndsPrematurely ,
3338 /// Duplicate item added
3439 DuplicateItemAdded ,
40+ /// Protocol version not found
41+ ProtocolVersionNotFound ,
3542 /// Invalid protocol version
3643 InvalidProtocolVersion ,
3744 /// Unsupported protocol version
@@ -45,6 +52,8 @@ pub enum Error {
4552 } ,
4653 /// Hex error
4754 Hex ( hex:: Error ) ,
55+ /// Try from slice error
56+ TryFromSlice ( String ) ,
4857 /// Bad range
4958 BadRange ,
5059}
@@ -68,6 +77,7 @@ impl fmt::Display for Error {
6877 Self :: UnexpectedMode ( m) => write ! ( f, "Unexpected mode: {}" , m) ,
6978 Self :: ParseEndsPrematurely => write ! ( f, "parse ends prematurely" ) ,
7079 Self :: DuplicateItemAdded => write ! ( f, "duplicate item added" ) ,
80+ Self :: ProtocolVersionNotFound => write ! ( f, "protocol version not found" ) ,
7181 Self :: InvalidProtocolVersion => write ! ( f, "invalid negentropy protocol version byte" ) ,
7282 Self :: UnsupportedProtocolVersion => {
7383 write ! ( f, "server does not support our negentropy protocol version" )
@@ -78,6 +88,7 @@ impl fmt::Display for Error {
7888 expected, found
7989 ) ,
8090 Self :: Hex ( e) => write ! ( f, "Hex: {}" , e) ,
91+ Self :: TryFromSlice ( e) => write ! ( f, "Try from slice: {}" , e) ,
8192 Self :: BadRange => write ! ( f, "bad range" ) ,
8293 }
8394 }
@@ -88,3 +99,9 @@ impl From<hex::Error> for Error {
8899 Self :: Hex ( e)
89100 }
90101}
102+
103+ impl From < TryFromSliceError > for Error {
104+ fn from ( e : TryFromSliceError ) -> Self {
105+ Self :: TryFromSlice ( e. to_string ( ) )
106+ }
107+ }
0 commit comments