1+ ///
2+ pub mod apply {
3+ /// Returned when failing to apply deltas.
4+ #[ derive( thiserror:: Error , Debug ) ]
5+ #[ allow( missing_docs) ]
6+ pub enum Error {
7+ #[ error( "Encountered unsupported command code: 0" ) ]
8+ UnsupportedCommandCode ,
9+ #[ error( "Delta copy from base: byte slices must match" ) ]
10+ DeltaCopyBaseSliceMismatch ,
11+ #[ error( "Delta copy data: byte slices must match" ) ]
12+ DeltaCopyDataSliceMismatch ,
13+ }
14+ }
15+
116/// Given the decompressed pack delta `d`, decode a size in bytes (either the base object size or the result object size)
217/// Equivalent to [this canonical git function](https://github.com/git/git/blob/311531c9de557d25ac087c1637818bd2aad6eb3a/delta.h#L89)
3- pub fn decode_header_size ( d : & [ u8 ] ) -> ( u64 , usize ) {
18+ pub ( crate ) fn decode_header_size ( d : & [ u8 ] ) -> ( u64 , usize ) {
419 let mut i = 0 ;
520 let mut size = 0u64 ;
621 let mut consumed = 0 ;
@@ -15,7 +30,7 @@ pub fn decode_header_size(d: &[u8]) -> (u64, usize) {
1530 ( size, consumed)
1631}
1732
18- pub fn apply ( base : & [ u8 ] , mut target : & mut [ u8 ] , data : & [ u8 ] ) {
33+ pub ( crate ) fn apply ( base : & [ u8 ] , mut target : & mut [ u8 ] , data : & [ u8 ] ) -> Result < ( ) , apply :: Error > {
1934 let mut i = 0 ;
2035 while let Some ( cmd) = data. get ( i) {
2136 i += 1 ;
@@ -55,16 +70,18 @@ pub fn apply(base: &[u8], mut target: &mut [u8], data: &[u8]) {
5570 }
5671 let ofs = ofs as usize ;
5772 std:: io:: Write :: write ( & mut target, & base[ ofs..ofs + size as usize ] )
58- . expect ( "delta copy from base: byte slices must match" ) ;
73+ . map_err ( |_e| apply :: Error :: DeltaCopyBaseSliceMismatch ) ? ;
5974 }
60- 0 => panic ! ( "encountered unsupported command code: 0" ) ,
75+ 0 => return Err ( apply :: Error :: UnsupportedCommandCode ) ,
6176 size => {
6277 std:: io:: Write :: write ( & mut target, & data[ i..i + * size as usize ] )
63- . expect ( "delta copy data: slice sizes to match up" ) ;
78+ . map_err ( |_e| apply :: Error :: DeltaCopyDataSliceMismatch ) ? ;
6479 i += * size as usize ;
6580 }
6681 }
6782 }
6883 assert_eq ! ( i, data. len( ) ) ;
6984 assert_eq ! ( target. len( ) , 0 ) ;
85+
86+ Ok ( ( ) )
7087}
0 commit comments