@@ -5,6 +5,8 @@ use quick_error::quick_error;
5
5
6
6
use crate :: { oid, ObjectId , Prefix } ;
7
7
8
+ const MIN_HEX_LEN : usize = 4 ;
9
+
8
10
quick_error ! {
9
11
/// The error returned by [Prefix::try_from_id()][super::Prefix::try_from_id()].
10
12
#[ derive( Debug ) ]
@@ -28,7 +30,7 @@ pub mod from_hex {
28
30
#[ allow( missing_docs) ]
29
31
pub enum Error {
30
32
TooShort { hex_len: usize } {
31
- display( "The minimum hex length of a short object id is 4 , got {}" , hex_len)
33
+ display( "The minimum hex length of a short object id is {} , got {}" , super :: MIN_HEX_LEN , hex_len)
32
34
}
33
35
TooLong { hex_len: usize } {
34
36
display( "An id cannot be larger than {} chars in hex, but {} was requested" , crate :: Kind :: longest( ) . len_in_hex( ) , hex_len)
@@ -52,7 +54,7 @@ impl Prefix {
52
54
object_kind : id. kind ( ) ,
53
55
hex_len,
54
56
} )
55
- } else if hex_len < 4 {
57
+ } else if hex_len < MIN_HEX_LEN {
56
58
Err ( Error :: TooShort { hex_len } )
57
59
} else {
58
60
let mut prefix = ObjectId :: null ( id. kind ( ) ) ;
@@ -102,14 +104,12 @@ impl Prefix {
102
104
use hex:: FromHex ;
103
105
let hex_len = value. len ( ) ;
104
106
105
- // validate
106
107
if hex_len > crate :: Kind :: longest ( ) . len_in_hex ( ) {
107
108
return Err ( from_hex:: Error :: TooLong { hex_len } ) ;
108
- } else if hex_len < 4 {
109
+ } else if hex_len < MIN_HEX_LEN {
109
110
return Err ( from_hex:: Error :: TooShort { hex_len } ) ;
110
111
} ;
111
112
112
- // prepare
113
113
let src = if value. len ( ) % 2 == 0 {
114
114
Vec :: from_hex ( value)
115
115
} else {
@@ -119,11 +119,10 @@ impl Prefix {
119
119
hex:: FromHexError :: InvalidHexCharacter { c, index } => from_hex:: Error :: Invalid { c, index } ,
120
120
hex:: FromHexError :: OddLength | hex:: FromHexError :: InvalidStringLength => panic ! ( "This is already checked" ) ,
121
121
} ) ?;
122
- let copy_len = src. len ( ) ;
123
122
124
- // patch an ObjectId
125
123
let mut bytes = ObjectId :: null ( crate :: Kind :: Sha1 ) ;
126
124
let dst = bytes. as_mut_slice ( ) ;
125
+ let copy_len = src. len ( ) ;
127
126
dst[ ..copy_len] . copy_from_slice ( & src) ;
128
127
129
128
Ok ( Prefix { bytes, hex_len } )
0 commit comments