@@ -154,7 +154,7 @@ fn process_png(
154154
155155/// Prints a success message to `stdout`.
156156///
157- /// Varies the message output depending on whether the source and
157+ /// Varies the message contents depending on whether the source and
158158/// destination paths are same or different.
159159#[ inline]
160160fn print_success ( src_path : & Path , dst_path : & Path , different_paths : bool , lock : & mut StdoutLock ) {
@@ -227,7 +227,6 @@ fn copy_or_skip(
227227 }
228228}
229229
230- // TODO: Get file size and compare. Then copy_or_skip(), but without `err`. It can be Optional. Pass `None`.
231230/// The main business logic.
232231/// Loops over files and calls appropriate functions for processing images.
233232/// Processing consists of optional resizing first, and of optimizing images
@@ -250,7 +249,7 @@ pub fn process_images(
250249 recursive : bool ,
251250 resize : bool ,
252251 quality : i32 ,
253- _size : i32 ,
252+ size : u64 ,
254253) -> bool {
255254 let mut has_error = false ;
256255
@@ -262,58 +261,67 @@ pub fn process_images(
262261
263262 for src_path in get_file_list ( & src_dir, recursive) {
264263 let src_path = src_path. path ( ) ;
265- if let Some ( extension) = src_path. extension ( ) {
266- let mut dst_path = PathBuf :: from ( src_path) ;
267-
268- // TODO: Extract this into a function? See at the end. The `continue`s can be problematic.
269- // TODO: Return a Boolean flag? Or Option<PathBuf>, for dst_path?
270- // TODO: But, we shouldn't skip a file! We should copy it. Alright, but what if `mkdir` fails?
271- // TODO: Well, we can then abort the program. Currently, we are reporting the failure, which could be not only okay, but perhaps a better option.
272- if different_paths {
273- dst_path = dst_dir. as_path ( ) . join (
274- diff_paths (
275- src_path. to_str ( ) . expect ( "Expected some src_path." ) ,
276- src_dir. to_str ( ) . expect ( "Expected some src_dir." ) ,
277- )
278- . expect ( "Expected diff_paths() to work." ) ,
279- ) ;
280-
281- if let Some ( parent) = dst_path. parent ( ) {
282- match fs:: create_dir_all ( parent) {
283- Ok ( _) => { }
284- Err ( err) => {
285- let err = format ! (
286- "\n \t Failed to create the subdirectory {:?} with the following error: {}" ,
287- parent, err
288- ) ;
289- set_and_print_error (
290- src_path,
291- Box :: from ( err) ,
292- & mut lock,
293- & mut has_error,
294- ) ;
295- continue ;
296- }
297- } ;
298- } else {
299- let err_msg = format ! ( "Destination path {:?} doesn't have a parent." , dst_path) ;
300- set_and_print_error ( src_path, Box :: from ( err_msg) , & mut lock, & mut has_error) ;
301- continue ;
264+
265+ let mut dst_path = PathBuf :: from ( src_path) ;
266+
267+ if different_paths {
268+ dst_path = dst_dir. as_path ( ) . join (
269+ diff_paths (
270+ src_path. to_str ( ) . expect ( "Expected some src_path." ) ,
271+ src_dir. to_str ( ) . expect ( "Expected some src_dir." ) ,
272+ )
273+ . expect ( "Expected diff_paths() to work." ) ,
274+ ) ;
275+
276+ if let Some ( parent) = dst_path. parent ( ) {
277+ match fs:: create_dir_all ( parent) {
278+ Ok ( _) => { }
279+ Err ( err) => {
280+ let err = format ! (
281+ "\n \t Failed to create the subdirectory {:?} with the following error: {}" ,
282+ parent, err
283+ ) ;
284+ set_and_print_error ( src_path, Box :: from ( err) , & mut lock, & mut has_error) ;
285+ continue ;
286+ }
302287 } ;
303- }
288+ } else {
289+ let err_msg = format ! ( "Destination path {:?} doesn't have a parent." , dst_path) ;
290+ set_and_print_error ( src_path, Box :: from ( err_msg) , & mut lock, & mut has_error) ;
291+ continue ;
292+ } ;
293+ }
294+
295+ let file_size = src_path. metadata ( ) . expect ( "Expected file metadata." ) . len ( ) ;
296+ let extension = src_path. extension ( ) ;
304297
305- match extension. to_string_lossy ( ) . to_lowercase ( ) . as_str ( ) {
298+ // Copy or skip a file if it is not big enough, or has no extension, or if its extension is not supported.
299+ if file_size >= size && extension. is_some ( ) {
300+ match extension. unwrap ( ) . to_string_lossy ( ) . to_lowercase ( ) . as_str ( ) {
306301 "jpg" | "jpeg" => {
307302 match process_jpeg ( src_path, & dst_path, resize, quality, & mut lock) {
308303 Ok ( _) => print_success ( src_path, & dst_path, different_paths, & mut lock) ,
309- Err ( err) => set_and_print_error ( src_path, err, & mut lock, & mut has_error) , // TODO: Or copy_or_skip()? Pass `err` in and print it first, like in Python.
304+ Err ( err) => copy_or_skip (
305+ src_path,
306+ & dst_path,
307+ different_paths,
308+ & mut lock,
309+ Some ( err) ,
310+ & mut has_error,
311+ ) ,
310312 }
311313 }
312314 "png" => match process_png ( src_path, & dst_path, resize, & mut lock) {
313315 Ok ( _) => print_success ( src_path, & dst_path, different_paths, & mut lock) ,
314- Err ( err) => set_and_print_error ( src_path, err, & mut lock, & mut has_error) , // TODO: Or copy_or_skip()? Pass `err` in and print it first, like in Python.
316+ Err ( err) => copy_or_skip (
317+ src_path,
318+ & dst_path,
319+ different_paths,
320+ & mut lock,
321+ Some ( err) ,
322+ & mut has_error,
323+ ) ,
315324 } ,
316- // _ => (), // TODO: copy_or_skip(), but without `err`? Pass `None`.
317325 _ => copy_or_skip (
318326 src_path,
319327 & dst_path,
@@ -323,6 +331,15 @@ pub fn process_images(
323331 & mut has_error,
324332 ) ,
325333 }
334+ } else {
335+ copy_or_skip (
336+ src_path,
337+ & dst_path,
338+ different_paths,
339+ & mut lock,
340+ None ,
341+ & mut has_error,
342+ ) ;
326343 }
327344 }
328345
0 commit comments