@@ -20,6 +20,7 @@ use crate::{
20
20
} ,
21
21
info, oof, utils,
22
22
utils:: to_utf,
23
+ Error ,
23
24
} ;
24
25
25
26
// Used in BufReader and BufWriter to perform less syscalls
@@ -32,14 +33,16 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
32
33
let formats = extension:: extensions_from_path ( & output_path) ;
33
34
34
35
if formats. is_empty ( ) {
35
- FinalError :: with_title ( format ! ( "Cannot compress to '{}'." , to_utf( & output_path) ) )
36
+ let reason = FinalError :: with_title ( format ! ( "Cannot compress to '{}'." , to_utf( & output_path) ) )
36
37
. detail ( "You shall supply the compression format via the extension." )
37
38
. hint ( "Try adding something like .tar.gz or .zip to the output file." )
38
39
. hint ( "" )
39
40
. hint ( "Examples:" )
40
41
. hint ( format ! ( " ouch compress ... {}.tar.gz" , to_utf( & output_path) ) )
41
42
. hint ( format ! ( " ouch compress ... {}.zip" , to_utf( & output_path) ) )
42
- . display_and_crash ( ) ;
43
+ . into_owned ( ) ;
44
+
45
+ return Err ( Error :: with_reason ( reason) ) ;
43
46
}
44
47
45
48
if matches ! ( & formats[ 0 ] , Bzip | Gzip | Lzma ) && files. len ( ) > 1 {
@@ -59,35 +62,35 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
59
62
let mut suggested_output_path = output_path. clone ( ) ;
60
63
suggested_output_path. replace_range ( empty_range, ".tar" ) ;
61
64
62
- FinalError :: with_title ( format ! ( "Cannot compress to '{}'." , to_utf( & output_path) ) )
65
+ let reason = FinalError :: with_title ( format ! ( "Cannot compress to '{}'." , to_utf( & output_path) ) )
63
66
. detail ( "You are trying to compress multiple files." )
64
67
. detail ( format ! ( "The compression format '{}' cannot receive multiple files." , & formats[ 0 ] ) )
65
68
. detail ( "The only supported formats that bundle files into an archive are .tar and .zip." )
66
69
. hint ( format ! ( "Try inserting '.tar' or '.zip' before '{}'." , & formats[ 0 ] ) )
67
70
. hint ( format ! ( "From: {}" , output_path) )
68
71
. hint ( format ! ( " To : {}" , suggested_output_path) )
69
- . display_and_crash ( ) ;
72
+ . into_owned ( ) ;
73
+
74
+ return Err ( Error :: with_reason ( reason) ) ;
70
75
}
71
76
72
77
if let Some ( format) = formats. iter ( ) . skip ( 1 ) . position ( |format| matches ! ( format, Tar | Zip ) ) {
73
- FinalError :: with_title ( format ! ( "Cannot compress to '{}'." , to_utf( & output_path) ) )
78
+ let reason = FinalError :: with_title ( format ! ( "Cannot compress to '{}'." , to_utf( & output_path) ) )
74
79
. detail ( format ! ( "Found the format '{}' in an incorrect position." , format) )
75
80
. detail ( format ! ( "{} can only be used at the start of the file extension." , format) )
76
81
. hint ( format ! ( "If you wish to compress multiple files, start the extension with {}." , format) )
77
82
. hint ( format ! ( "Otherwise, remove {} from '{}'." , format, to_utf( & output_path) ) )
78
- . display_and_crash ( ) ;
83
+ . into_owned ( ) ;
84
+
85
+ return Err ( Error :: with_reason ( reason) ) ;
79
86
}
80
87
81
88
if output_path. exists ( ) && !utils:: user_wants_to_overwrite ( & output_path, flags) ? {
89
+ // User does not want to overwrite this file
82
90
return Ok ( ( ) ) ;
83
91
}
84
92
85
- let output_file = fs:: File :: create ( & output_path) . unwrap_or_else ( |err| {
86
- FinalError :: with_title ( format ! ( "Cannot compress to '{}'." , to_utf( & output_path) ) )
87
- . detail ( format ! ( "Could not open file '{}' for writing." , to_utf( & output_path) ) )
88
- . detail ( format ! ( "Error: {}." , err) )
89
- . display_and_crash ( )
90
- } ) ;
93
+ let output_file = fs:: File :: create ( & output_path) ?;
91
94
let compress_result = compress_files ( files, formats, output_file, flags) ;
92
95
93
96
// If any error occurred, delete incomplete file
0 commit comments