1
- use std:: cmp:: Ordering ;
2
1
use std:: { borrow:: Borrow , convert:: TryInto , fmt, ops:: Deref } ;
3
2
4
3
use crate :: { borrowed:: oid, Kind , SIZE_OF_SHA1_DIGEST } ;
@@ -13,86 +12,7 @@ pub struct Prefix {
13
12
}
14
13
15
14
///
16
- pub mod prefix {
17
- use quick_error:: quick_error;
18
-
19
- quick_error ! {
20
- /// The error returned by [Prefix::try_from_id()][super::Prefix::try_from_id()].
21
- #[ derive( Debug ) ]
22
- #[ allow( missing_docs) ]
23
- pub enum Error {
24
- TooShort { hex_len: usize } {
25
- display( "The minimum hex length of a short object id is 4, got {}" , hex_len)
26
- }
27
- TooLong { object_kind: crate :: Kind , hex_len: usize } {
28
- display( "An object of kind {} cannot be larger than {} in hex, but {} was requested" , object_kind, object_kind. len_in_hex( ) , hex_len)
29
- }
30
- }
31
- }
32
- }
33
-
34
- impl Prefix {
35
- /// Create a new instance by taking a full `id` as input and truncating it to `hex_len`.
36
- ///
37
- /// For instance, with `hex_len` of 7 the resulting prefix is 3.5 bytes, or 3 bytes and 4 bits
38
- /// wide, with all other bytes and bits set to zero.
39
- pub fn new ( id : impl AsRef < oid > , hex_len : usize ) -> Result < Self , prefix:: Error > {
40
- let id = id. as_ref ( ) ;
41
- if hex_len > id. kind ( ) . len_in_hex ( ) {
42
- Err ( prefix:: Error :: TooLong {
43
- object_kind : id. kind ( ) ,
44
- hex_len,
45
- } )
46
- } else if hex_len < 4 {
47
- Err ( prefix:: Error :: TooShort { hex_len } )
48
- } else {
49
- let mut prefix = ObjectId :: null ( id. kind ( ) ) ;
50
- let b = prefix. as_mut_slice ( ) ;
51
- let copy_len = ( hex_len + 1 ) / 2 ;
52
- b[ ..copy_len] . copy_from_slice ( & id. as_bytes ( ) [ ..copy_len] ) ;
53
- if hex_len % 2 == 1 {
54
- b[ hex_len / 2 ] &= 0xf0 ;
55
- }
56
-
57
- Ok ( Prefix { bytes : prefix, hex_len } )
58
- }
59
- }
60
-
61
- /// Returns the prefix as object id.
62
- ///
63
- /// Note that it may be deceptive to use given that it looks like a full
64
- /// object id, even though its post-prefix bytes/bits are set to zero.
65
- pub fn as_oid ( & self ) -> & oid {
66
- & self . bytes
67
- }
68
-
69
- /// Return the amount of hexadecimal characters that are set in the prefix.
70
- ///
71
- /// This gives the prefix a granularity of 4 bits.
72
- pub fn hex_len ( & self ) -> usize {
73
- self . hex_len
74
- }
75
-
76
- /// Provided with candidate id which is a full hash, determine how this prefix compares to it,
77
- /// only looking at the prefix bytes, ignoring everything behind that.
78
- pub fn cmp_oid ( & self , candidate : & oid ) -> Ordering {
79
- let common_len = self . hex_len / 2 ;
80
-
81
- self . bytes . as_bytes ( ) [ ..common_len]
82
- . cmp ( & candidate. as_bytes ( ) [ ..common_len] )
83
- . then ( if self . hex_len % 2 == 1 {
84
- let half_byte_idx = self . hex_len / 2 ;
85
- self . bytes . as_bytes ( ) [ half_byte_idx] . cmp ( & ( candidate. as_bytes ( ) [ half_byte_idx] & 0xf0 ) )
86
- } else {
87
- Ordering :: Equal
88
- } )
89
- }
90
-
91
- /// Create an instance from the given hexadecimal prefix, e.g. `35e77c16` would yield a `Prefix` with `hex_len()` = 8.
92
- pub fn from_hex ( _hex : & str ) -> Self {
93
- todo ! ( "Prefix::from_hex()" )
94
- }
95
- }
15
+ pub mod prefix;
96
16
97
17
/// An owned hash identifying objects, most commonly Sha1
98
18
#[ derive( PartialEq , Eq , Hash , Ord , PartialOrd , Clone , Copy ) ]
0 commit comments