@@ -64,82 +64,67 @@ fn process_checksum(
6464 }
6565 } ;
6666
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 => {
7171 let mut hasher = Sha256 :: new ( ) ;
7272 hasher. update ( b) ;
7373 encode ( hasher. finalize ( ) )
74- } else {
75- bail ! ( "Error opening file" ) ;
7674 }
77- }
78- ChecksumType :: Sha512 => {
79- let bytes = fs:: read ( file_path) ;
80- if let Ok ( b) = bytes {
75+ ChecksumType :: Sha512 => {
8176 let mut hasher = Sha512 :: new ( ) ;
8277 hasher. update ( b) ;
8378 encode ( hasher. finalize ( ) )
84- } else {
85- bail ! ( "Error opening file" ) ;
8679 }
87- }
88- ChecksumType :: Sha1 => {
89- let bytes = fs:: read ( file_path) ;
90- if let Ok ( b) = bytes {
80+ ChecksumType :: Sha1 => {
9181 let mut hasher = Sha1 :: new ( ) ;
9282 hasher. update ( b) ;
9383 encode ( hasher. finalize ( ) )
94- } else {
95- bail ! ( "Error opening file" ) ;
9684 }
97- }
98- ChecksumType :: Md5 => {
99- let bytes = fs:: read ( file_path) ;
100- if let Ok ( b) = bytes {
85+ ChecksumType :: Md5 => {
10186 format ! ( "{:?}" , md5:: compute( b) )
102- } else {
103- bail ! ( "Error opening file" ) ;
10487 }
105- }
106- } ;
88+ } ;
10789
108- let checksum_output = format ! ( "{checksum_type} checksum: {hash} - {file_name}" ) ;
90+ let checksum_output = format ! ( "{checksum_type} checksum: {hash} - {file_name}" ) ;
10991
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+ }
11598 }
116- }
11799
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) ;
120102
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" ) ;
124109 }
125110 } else {
126- bail ! ( "Error opening file" ) ;
127- }
128- } else {
129- let file = File :: create ( o) ;
111+ let file = File :: create ( o) ;
130112
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" ) ;
134119 }
135- } else {
136- bail ! ( "Error writing file" ) ;
137120 }
138121 }
139- }
140122
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" ) ;
143128 }
144129
145130 Ok ( ( ) )
@@ -238,27 +223,24 @@ mod tests {
238223 }
239224
240225 fn get_checksum ( file : & Path , checksum_type : ChecksumType ) -> String {
226+ let bytes = fs:: read ( file) . unwrap ( ) ;
241227 match checksum_type {
242228 ChecksumType :: Sha256 => {
243- let bytes = fs:: read ( file) . unwrap ( ) ;
244229 let mut hasher = Sha256 :: new ( ) ;
245230 hasher. update ( bytes) ;
246231 encode ( hasher. finalize ( ) )
247232 }
248233 ChecksumType :: Sha512 => {
249- let bytes = fs:: read ( file) . unwrap ( ) ;
250234 let mut hasher = Sha512 :: new ( ) ;
251235 hasher. update ( bytes) ;
252236 encode ( hasher. finalize ( ) )
253237 }
254238 ChecksumType :: Sha1 => {
255- let bytes = fs:: read ( file) . unwrap ( ) ;
256239 let mut hasher = Sha1 :: new ( ) ;
257240 hasher. update ( bytes) ;
258241 encode ( hasher. finalize ( ) )
259242 }
260243 ChecksumType :: Md5 => {
261- let bytes = fs:: read ( file) . unwrap ( ) ;
262244 format ! ( "{:?}" , md5:: compute( bytes) )
263245 }
264246 }
0 commit comments