@@ -28,11 +28,8 @@ pub enum Error<E: std::error::Error + 'static> {
2828 Filename ( String ) ,
2929 #[ error( "commit {id} has invalid generation {generation}" ) ]
3030 Generation { generation : u32 , id : gix_hash:: ObjectId } ,
31- #[ error( "checksum mismatch: expected {expected}, got {actual}" ) ]
32- Mismatch {
33- actual : gix_hash:: ObjectId ,
34- expected : gix_hash:: ObjectId ,
35- } ,
31+ #[ error( transparent) ]
32+ Checksum ( #[ from] checksum:: Error ) ,
3633 #[ error( "{0}" ) ]
3734 Processor ( #[ source] E ) ,
3835 #[ error( "commit {id} has invalid root tree ID {root_tree_id}" ) ]
@@ -42,6 +39,17 @@ pub enum Error<E: std::error::Error + 'static> {
4239 } ,
4340}
4441
42+ ///
43+ pub mod checksum {
44+ /// The error used in [`super::File::verify_checksum()`].
45+ #[ derive( thiserror:: Error , Debug ) ]
46+ #[ allow( missing_docs) ]
47+ pub enum Error {
48+ #[ error( transparent) ]
49+ Verify ( #[ from] gix_hash:: verify:: Error ) ,
50+ }
51+ }
52+
4553/// The positive result of [`File::traverse()`] providing some statistical information.
4654#[ derive( Clone , Debug , Eq , PartialEq ) ]
4755#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize , serde:: Serialize ) ) ]
@@ -73,8 +81,7 @@ impl File {
7381 E : std:: error:: Error + ' static ,
7482 Processor : FnMut ( & file:: Commit < ' a > ) -> Result < ( ) , E > ,
7583 {
76- self . verify_checksum ( )
77- . map_err ( |( actual, expected) | Error :: Mismatch { actual, expected } ) ?;
84+ self . verify_checksum ( ) ?;
7885 verify_split_chain_filename_hash ( & self . path , self . checksum ( ) ) . map_err ( Error :: Filename ) ?;
7986
8087 let null_id = self . object_hash ( ) . null_ref ( ) ;
@@ -138,23 +145,18 @@ impl File {
138145 /// Assure the [`checksum`][File::checksum()] matches the actual checksum over all content of this file, excluding the trailing
139146 /// checksum itself.
140147 ///
141- /// Return the actual checksum on success or `(actual checksum, expected checksum)` if there is a mismatch.
142- pub fn verify_checksum ( & self ) -> Result < gix_hash:: ObjectId , ( gix_hash :: ObjectId , gix_hash :: ObjectId ) > {
143- // Even though we could use gix_hash::bytes_of_file(…), this would require using our own
144- // Error type to support io::Error and Mismatch . As we only gain progress, there probably isn't much value
148+ /// Return the actual checksum on success or [` checksum::Error`] if there is a mismatch.
149+ pub fn verify_checksum ( & self ) -> Result < gix_hash:: ObjectId , checksum :: Error > {
150+ // Even though we could use gix_hash::bytes_of_file(…), this would require extending our
151+ // Error type to support io::Error. As we only gain progress, there probably isn't much value
145152 // as these files are usually small enough to process them in less than a second, even for the large ones.
146153 // But it's possible, once a progress instance is passed.
147154 let data_len_without_trailer = self . data . len ( ) - self . hash_len ;
148155 let mut hasher = gix_hash:: hasher ( self . object_hash ( ) ) ;
149156 hasher. update ( & self . data [ ..data_len_without_trailer] ) ;
150157 let actual = hasher. finalize ( ) ;
151-
152- let expected = self . checksum ( ) ;
153- if actual == expected {
154- Ok ( actual)
155- } else {
156- Err ( ( actual, expected. into ( ) ) )
157- }
158+ actual. verify ( self . checksum ( ) ) ?;
159+ Ok ( actual)
158160 }
159161}
160162
0 commit comments