@@ -15,7 +15,7 @@ use hex::{FromHex, FromHexError};
15
15
#[ serde( transparent) ]
16
16
pub struct ChannelId (
17
17
#[ serde(
18
- deserialize_with = "channel_id_from_str " ,
18
+ deserialize_with = "deserialize_channel_id " ,
19
19
serialize_with = "SerHex::<StrictPfx>::serialize"
20
20
) ]
21
21
[ u8 ; 32 ] ,
@@ -27,16 +27,21 @@ impl fmt::Debug for ChannelId {
27
27
}
28
28
}
29
29
30
- fn channel_id_from_str < ' de , D > ( deserializer : D ) -> Result < [ u8 ; 32 ] , D :: Error >
30
+ fn deserialize_channel_id < ' de , D > ( deserializer : D ) -> Result < [ u8 ; 32 ] , D :: Error >
31
31
where
32
32
D : Deserializer < ' de > ,
33
33
{
34
34
let channel_id = String :: deserialize ( deserializer) ?;
35
- if channel_id. is_empty ( ) || channel_id. len ( ) != 66 {
36
- return Err ( serde:: de:: Error :: custom ( "invalid channel id" . to_string ( ) ) ) ;
37
- }
35
+ let channel_id = validate_channel_id ( channel_id) . map_err ( serde :: de :: Error :: custom ) ? ;
36
+ < [ u8 ; 32 ] as FromHex > :: from_hex ( channel_id ) . map_err ( serde:: de:: Error :: custom)
37
+ }
38
38
39
- <[ u8 ; 32 ] as FromHex >:: from_hex ( & channel_id[ 2 ..] ) . map_err ( serde:: de:: Error :: custom)
39
+ fn validate_channel_id ( s : String ) -> Result < String , FromHexError > {
40
+ if s. is_empty ( ) || s. len ( ) != 66 || & s[ 0 ..2 ] != "0x" {
41
+ Err ( FromHexError :: InvalidStringLength )
42
+ } else {
43
+ Ok ( s[ 2 ..] . into ( ) )
44
+ }
40
45
}
41
46
42
47
impl Deref for ChannelId {
@@ -79,11 +84,7 @@ impl FromStr for ChannelId {
79
84
type Err = FromHexError ;
80
85
81
86
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
82
- if s. len ( ) != 66 || & s[ 0 ..2 ] != "0x" {
83
- Err ( FromHexError :: InvalidStringLength )
84
- } else {
85
- Self :: from_hex ( & s[ 2 ..] )
86
- }
87
+ ChannelId :: from_hex ( validate_channel_id ( s. into ( ) ) ?)
87
88
}
88
89
}
89
90
0 commit comments