@@ -9,7 +9,7 @@ use std::{
99} ;
1010
1111use diffy:: {
12- binary:: BinaryPatch ,
12+ binary:: { BinaryPatch , BinaryPatchParseError } ,
1313 patch_set:: { FileOperation , ParseOptions , PatchKind , PatchSet , PatchSetParseError } ,
1414} ;
1515
@@ -268,13 +268,15 @@ fn print_patch_version() {
268268pub enum TestError {
269269 Parse ( PatchSetParseError ) ,
270270 Apply ( diffy:: ApplyError ) ,
271+ Binary ( BinaryPatchParseError ) ,
271272}
272273
273274impl std:: fmt:: Display for TestError {
274275 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
275276 match self {
276277 TestError :: Parse ( e) => write ! ( f, "parse error: {e}" ) ,
277278 TestError :: Apply ( e) => write ! ( f, "apply error: {e}" ) ,
279+ TestError :: Binary ( e) => write ! ( f, "binary patch error: {e}" ) ,
278280 }
279281 }
280282}
@@ -352,31 +354,40 @@ pub fn apply_diffy(
352354 }
353355 } ;
354356
357+ let read_original = || {
358+ if let Some ( name) = original_name {
359+ let original_path = in_dir. join ( name) ;
360+ fs:: read ( & original_path) . unwrap_or_default ( )
361+ } else {
362+ Vec :: new ( )
363+ }
364+ } ;
365+
366+ let write_modified = |result : & [ u8 ] | {
367+ let result_path = output_dir. join ( target_name) ;
368+ if let Some ( parent) = result_path. parent ( ) {
369+ fs:: create_dir_all ( parent) . unwrap ( ) ;
370+ }
371+ fs:: write ( & result_path, result) . unwrap ( ) ;
372+ } ;
373+
355374 match file_patch. patch ( ) {
356375 PatchKind :: Text ( patch) => {
357- let original = if let Some ( name) = original_name {
358- let original_path = in_dir. join ( name) ;
359- fs:: read_to_string ( & original_path) . unwrap_or_else ( |e| {
360- panic ! ( "failed to read {}: {e}" , original_path. display( ) )
361- } )
362- } else {
363- String :: new ( )
364- } ;
376+ let original = String :: from_utf8 ( read_original ( ) ) . unwrap ( ) ;
365377
366378 let result = diffy:: apply ( & original, patch) . map_err ( TestError :: Apply ) ?;
367379
368- let result_path = output_dir. join ( target_name) ;
369- if let Some ( parent) = result_path. parent ( ) {
370- fs:: create_dir_all ( parent) . unwrap ( ) ;
371- }
372- fs:: write ( & result_path, result. as_bytes ( ) ) . unwrap ( ) ;
380+ write_modified ( result. as_bytes ( ) ) ;
373381 }
374382 PatchKind :: Binary ( BinaryPatch :: Marker ) => {
375- // Dont do anything if it is just a binary patch marker.[
383+ // Dont do anything if it is just a binary patch marker.
376384 }
377- PatchKind :: Binary ( _) => {
378- // Binary patch application requires the `binary` feature.
379- // Will be wired up when that feature is added.
385+ PatchKind :: Binary ( patch) => {
386+ let original = read_original ( ) ;
387+
388+ let result = patch. apply ( & original) . map_err ( TestError :: Binary ) ?;
389+
390+ write_modified ( & result) ;
380391 }
381392 }
382393 }
0 commit comments