1
1
use std:: ffi:: OsStr ;
2
+ use std:: convert:: TryFrom ;
2
3
use std:: path:: { Component , Path } ;
3
4
4
5
use crate :: prelude:: * ;
@@ -35,6 +36,33 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
35
36
}
36
37
}
37
38
39
+ pub ( crate ) const MD5_LEN : usize = 16 ;
40
+
41
+ #[ derive( Default , Clone , Copy ) ]
42
+ pub struct FileHash ( [ u8 ; MD5_LEN ] ) ;
43
+
44
+ impl FileHash {
45
+ pub fn inner ( self ) -> [ u8 ; MD5_LEN ] {
46
+ self . 0
47
+ }
48
+ }
49
+
50
+ pub struct UnsupportedHashType ;
51
+
52
+ impl TryFrom < SourceFileHash > for FileHash {
53
+ type Error = UnsupportedHashType ;
54
+
55
+ fn try_from ( hash : SourceFileHash ) -> Result < Self , Self :: Error > {
56
+ if hash. kind == SourceFileHashAlgorithm :: Md5 {
57
+ let mut buf = [ 0u8 ; MD5_LEN ] ;
58
+ buf. copy_from_slice ( hash. hash_bytes ( ) ) ;
59
+ Ok ( Self ( buf) )
60
+ } else {
61
+ Err ( UnsupportedHashType )
62
+ }
63
+ }
64
+ }
65
+
38
66
fn line_program_add_file (
39
67
line_program : & mut LineProgram ,
40
68
line_strings : & mut LineStringTable ,
@@ -58,20 +86,13 @@ fn line_program_add_file(
58
86
line_strings,
59
87
) ;
60
88
61
- let md5 = Some ( file. src_hash )
62
- . filter ( |h| matches ! ( h, SourceFileHash { kind: SourceFileHashAlgorithm :: Md5 , .. } ) )
63
- . map ( |h| {
64
- let mut buf = [ 0u8 ; super :: MD5_LEN ] ;
65
- buf. copy_from_slice ( h. hash_bytes ( ) ) ;
66
- buf
67
- } ) ;
68
-
69
- line_program. file_has_md5 = md5. is_some ( ) ;
89
+ let file_hash = FileHash :: try_from ( file. src_hash ) ;
70
90
91
+ line_program. file_has_md5 = file_hash. is_ok ( ) ;
71
92
line_program. add_file ( file_name, dir_id, Some ( FileInfo {
72
93
timestamp : 0 ,
73
94
size : 0 ,
74
- md5 : md5 . unwrap_or_default ( ) ,
95
+ md5 : file_hash . unwrap_or_default ( ) . inner ( ) ,
75
96
} ) )
76
97
}
77
98
// FIXME give more appropriate file names
0 commit comments