1
+ use bstr:: ByteSlice ;
2
+ use nom:: lib:: std:: fmt:: Formatter ;
1
3
use quick_error:: quick_error;
2
4
3
5
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
@@ -19,12 +21,36 @@ pub struct Time {
19
21
pub const SHA1_SIZE : usize = 20 ;
20
22
21
23
/// A SHA1 identifying objects
22
- pub type Id = [ u8 ; SHA1_SIZE ] ;
24
+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
25
+ pub struct Id ( pub [ u8 ; SHA1_SIZE ] ) ;
26
+
27
+ impl Id {
28
+ pub fn encode_to_40_bytes_slice ( & self , out : & mut [ u8 ] ) -> Result < ( ) , hex:: FromHexError > {
29
+ hex:: encode_to_slice ( self . 0 , out)
30
+ }
23
31
24
- pub fn id_from_20_bytes ( b : & [ u8 ] ) -> Id {
25
- let mut id = [ 0 ; SHA1_SIZE ] ;
26
- id. copy_from_slice ( b) ;
27
- id
32
+ pub fn from_20_bytes ( b : & [ u8 ] ) -> Id {
33
+ let mut id = [ 0 ; SHA1_SIZE ] ;
34
+ id. copy_from_slice ( b) ;
35
+ Id ( id)
36
+ }
37
+
38
+ pub fn from_hex ( buf : & [ u8 ] ) -> Result < Id , hex:: FromHexError > {
39
+ use hex:: FromHex ;
40
+ Ok ( Id ( <[ u8 ; 20 ] >:: from_hex ( buf) ?) )
41
+ }
42
+
43
+ pub fn null ( ) -> Id {
44
+ Id ( [ 0u8 ; 20 ] )
45
+ }
46
+ }
47
+
48
+ impl std:: fmt:: Display for Id {
49
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
50
+ let mut buf = [ 0u8 ; 40 ] ;
51
+ self . encode_to_40_bytes_slice ( & mut buf) . unwrap ( ) ;
52
+ write ! ( f, "{}" , & buf. as_bstr( ) )
53
+ }
28
54
}
29
55
30
56
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
0 commit comments