@@ -64,82 +64,67 @@ fn process_checksum(
64
64
}
65
65
} ;
66
66
67
- let hash = match checksum_type {
68
- ChecksumType :: Sha256 => {
69
- let bytes = fs :: read ( file_path ) ;
70
- if let Ok ( b ) = bytes {
67
+ let bytes = fs :: read ( file_path ) ;
68
+ if let Ok ( b ) = bytes {
69
+ let hash = match checksum_type {
70
+ ChecksumType :: Sha256 => {
71
71
let mut hasher = Sha256 :: new ( ) ;
72
72
hasher. update ( b) ;
73
73
encode ( hasher. finalize ( ) )
74
- } else {
75
- bail ! ( "Error opening file" ) ;
76
74
}
77
- }
78
- ChecksumType :: Sha512 => {
79
- let bytes = fs:: read ( file_path) ;
80
- if let Ok ( b) = bytes {
75
+ ChecksumType :: Sha512 => {
81
76
let mut hasher = Sha512 :: new ( ) ;
82
77
hasher. update ( b) ;
83
78
encode ( hasher. finalize ( ) )
84
- } else {
85
- bail ! ( "Error opening file" ) ;
86
79
}
87
- }
88
- ChecksumType :: Sha1 => {
89
- let bytes = fs:: read ( file_path) ;
90
- if let Ok ( b) = bytes {
80
+ ChecksumType :: Sha1 => {
91
81
let mut hasher = Sha1 :: new ( ) ;
92
82
hasher. update ( b) ;
93
83
encode ( hasher. finalize ( ) )
94
- } else {
95
- bail ! ( "Error opening file" ) ;
96
84
}
97
- }
98
- ChecksumType :: Md5 => {
99
- let bytes = fs:: read ( file_path) ;
100
- if let Ok ( b) = bytes {
85
+ ChecksumType :: Md5 => {
101
86
format ! ( "{:?}" , md5:: compute( b) )
102
- } else {
103
- bail ! ( "Error opening file" ) ;
104
87
}
105
- }
106
- } ;
88
+ } ;
107
89
108
- let checksum_output = format ! ( "{checksum_type} checksum: {hash} - {file_name}" ) ;
90
+ let checksum_output = format ! ( "{checksum_type} checksum: {hash} - {file_name}" ) ;
109
91
110
- if let Some ( o) = output_file {
111
- let output_path = o. parent ( ) ;
112
- if let Some ( path) = output_path {
113
- if !path. exists ( ) && fs:: create_dir_all ( path) . is_err ( ) {
114
- bail ! ( "Error creating directory" ) ;
92
+ if let Some ( o) = output_file {
93
+ let output_path = o. parent ( ) ;
94
+ if let Some ( path) = output_path {
95
+ if !path. exists ( ) && fs:: create_dir_all ( path) . is_err ( ) {
96
+ bail ! ( "Error creating directory" ) ;
97
+ }
115
98
}
116
- }
117
99
118
- if !overwrite && o. exists ( ) {
119
- let file_result = OpenOptions :: new ( ) . append ( true ) . open ( o) ;
100
+ if !overwrite && o. exists ( ) {
101
+ let file_result = OpenOptions :: new ( ) . append ( true ) . open ( o) ;
120
102
121
- if let Ok ( mut file) = file_result {
122
- if let Err ( e) = writeln ! ( file, "{}" , & checksum_output) {
123
- bail ! ( "Couldn't write to file: {}" , e) ;
103
+ if let Ok ( mut file) = file_result {
104
+ if let Err ( e) = writeln ! ( file, "{}" , & checksum_output) {
105
+ bail ! ( "Couldn't write to file: {}" , e) ;
106
+ }
107
+ } else {
108
+ bail ! ( "Error opening file" ) ;
124
109
}
125
110
} else {
126
- bail ! ( "Error opening file" ) ;
127
- }
128
- } else {
129
- let file = File :: create ( o) ;
111
+ let file = File :: create ( o) ;
130
112
131
- if let Ok ( mut f) = file {
132
- if let Err ( e) = writeln ! ( f, "{}" , & checksum_output) {
133
- bail ! ( "Error writing file: {}" , e) ;
113
+ if let Ok ( mut f) = file {
114
+ if let Err ( e) = writeln ! ( f, "{}" , & checksum_output) {
115
+ bail ! ( "Error writing file: {}" , e) ;
116
+ }
117
+ } else {
118
+ bail ! ( "Error writing file" ) ;
134
119
}
135
- } else {
136
- bail ! ( "Error writing file" ) ;
137
120
}
138
121
}
139
- }
140
122
141
- if output_file. is_none ( ) || verbose {
142
- println ! ( "{checksum_output}" ) ;
123
+ if output_file. is_none ( ) || verbose {
124
+ println ! ( "{checksum_output}" ) ;
125
+ }
126
+ } else {
127
+ bail ! ( "Error opening file" ) ;
143
128
}
144
129
145
130
Ok ( ( ) )
@@ -238,27 +223,24 @@ mod tests {
238
223
}
239
224
240
225
fn get_checksum ( file : & Path , checksum_type : ChecksumType ) -> String {
226
+ let bytes = fs:: read ( file) . unwrap ( ) ;
241
227
match checksum_type {
242
228
ChecksumType :: Sha256 => {
243
- let bytes = fs:: read ( file) . unwrap ( ) ;
244
229
let mut hasher = Sha256 :: new ( ) ;
245
230
hasher. update ( bytes) ;
246
231
encode ( hasher. finalize ( ) )
247
232
}
248
233
ChecksumType :: Sha512 => {
249
- let bytes = fs:: read ( file) . unwrap ( ) ;
250
234
let mut hasher = Sha512 :: new ( ) ;
251
235
hasher. update ( bytes) ;
252
236
encode ( hasher. finalize ( ) )
253
237
}
254
238
ChecksumType :: Sha1 => {
255
- let bytes = fs:: read ( file) . unwrap ( ) ;
256
239
let mut hasher = Sha1 :: new ( ) ;
257
240
hasher. update ( bytes) ;
258
241
encode ( hasher. finalize ( ) )
259
242
}
260
243
ChecksumType :: Md5 => {
261
- let bytes = fs:: read ( file) . unwrap ( ) ;
262
244
format ! ( "{:?}" , md5:: compute( bytes) )
263
245
}
264
246
}
0 commit comments