11use std:: ffi:: OsStr ;
2+ use std:: convert:: TryFrom ;
23use std:: path:: { Component , Path } ;
34
45use crate :: prelude:: * ;
@@ -35,6 +36,33 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
3536 }
3637}
3738
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+
3866fn line_program_add_file (
3967 line_program : & mut LineProgram ,
4068 line_strings : & mut LineStringTable ,
@@ -58,20 +86,13 @@ fn line_program_add_file(
5886 line_strings,
5987 ) ;
6088
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 ) ;
7090
91+ line_program. file_has_md5 = file_hash. is_ok ( ) ;
7192 line_program. add_file ( file_name, dir_id, Some ( FileInfo {
7293 timestamp : 0 ,
7394 size : 0 ,
74- md5 : md5 . unwrap_or_default ( ) ,
95+ md5 : file_hash . unwrap_or_default ( ) . inner ( ) ,
7596 } ) )
7697 }
7798 // FIXME give more appropriate file names
0 commit comments