1+ /// Returned by [`Tree::traverse()`]
2+ #[ derive( thiserror:: Error , Debug ) ]
3+ #[ allow( missing_docs) ]
4+ pub enum Error {
5+ #[ error( "Encountered unsupported command code: 0" ) ]
6+ UnsupportedCommandCode ,
7+ #[ error( "Delta copy from base: byte slices must match" ) ]
8+ DeltaCopyBaseSliceMismatch ,
9+ #[ error( "Delta copy data: byte slices must match" ) ]
10+ DeltaCopyDataSliceMismatch ,
11+ }
12+
113/// Given the decompressed pack delta `d`, decode a size in bytes (either the base object size or the result object size)
214/// Equivalent to [this canonical git function](https://github.com/git/git/blob/311531c9de557d25ac087c1637818bd2aad6eb3a/delta.h#L89)
315pub fn decode_header_size ( d : & [ u8 ] ) -> ( u64 , usize ) {
@@ -15,7 +27,7 @@ pub fn decode_header_size(d: &[u8]) -> (u64, usize) {
1527 ( size, consumed)
1628}
1729
18- pub fn apply ( base : & [ u8 ] , mut target : & mut [ u8 ] , data : & [ u8 ] ) {
30+ pub fn apply ( base : & [ u8 ] , mut target : & mut [ u8 ] , data : & [ u8 ] ) -> Result < ( ) , Error > {
1931 let mut i = 0 ;
2032 while let Some ( cmd) = data. get ( i) {
2133 i += 1 ;
@@ -55,16 +67,18 @@ pub fn apply(base: &[u8], mut target: &mut [u8], data: &[u8]) {
5567 }
5668 let ofs = ofs as usize ;
5769 std:: io:: Write :: write ( & mut target, & base[ ofs..ofs + size as usize ] )
58- . expect ( "delta copy from base: byte slices must match" ) ;
70+ . map_err ( |_e| Error :: DeltaCopyBaseSliceMismatch ) ? ;
5971 }
60- 0 => panic ! ( "encountered unsupported command code: 0" ) ,
72+ 0 => return Err ( Error :: UnsupportedCommandCode ) ,
6173 size => {
6274 std:: io:: Write :: write ( & mut target, & data[ i..i + * size as usize ] )
63- . expect ( "delta copy data: slice sizes to match up" ) ;
75+ . map_err ( |_e| Error :: DeltaCopyDataSliceMismatch ) ? ;
6476 i += * size as usize ;
6577 }
6678 }
6779 }
6880 assert_eq ! ( i, data. len( ) ) ;
6981 assert_eq ! ( target. len( ) , 0 ) ;
82+
83+ Ok ( ( ) )
7084}
0 commit comments