Skip to content

Commit 073d3a1

Browse files
committed
refactor (#413)
1 parent 535411f commit 073d3a1

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

git-hash/src/owned/prefix.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use quick_error::quick_error;
55

66
use crate::{oid, ObjectId, Prefix};
77

8+
const MIN_HEX_LEN: usize = 4;
9+
810
quick_error! {
911
/// The error returned by [Prefix::try_from_id()][super::Prefix::try_from_id()].
1012
#[derive(Debug)]
@@ -28,7 +30,7 @@ pub mod from_hex {
2830
#[allow(missing_docs)]
2931
pub enum Error {
3032
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)
3234
}
3335
TooLong { hex_len: usize } {
3436
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 {
5254
object_kind: id.kind(),
5355
hex_len,
5456
})
55-
} else if hex_len < 4 {
57+
} else if hex_len < MIN_HEX_LEN {
5658
Err(Error::TooShort { hex_len })
5759
} else {
5860
let mut prefix = ObjectId::null(id.kind());
@@ -102,14 +104,12 @@ impl Prefix {
102104
use hex::FromHex;
103105
let hex_len = value.len();
104106

105-
// validate
106107
if hex_len > crate::Kind::longest().len_in_hex() {
107108
return Err(from_hex::Error::TooLong { hex_len });
108-
} else if hex_len < 4 {
109+
} else if hex_len < MIN_HEX_LEN {
109110
return Err(from_hex::Error::TooShort { hex_len });
110111
};
111112

112-
// prepare
113113
let src = if value.len() % 2 == 0 {
114114
Vec::from_hex(value)
115115
} else {
@@ -119,11 +119,10 @@ impl Prefix {
119119
hex::FromHexError::InvalidHexCharacter { c, index } => from_hex::Error::Invalid { c, index },
120120
hex::FromHexError::OddLength | hex::FromHexError::InvalidStringLength => panic!("This is already checked"),
121121
})?;
122-
let copy_len = src.len();
123122

124-
// patch an ObjectId
125123
let mut bytes = ObjectId::null(crate::Kind::Sha1);
126124
let dst = bytes.as_mut_slice();
125+
let copy_len = src.len();
127126
dst[..copy_len].copy_from_slice(&src);
128127

129128
Ok(Prefix { bytes, hex_len })

0 commit comments

Comments
 (0)