From ef4d2816b3165ec23bcf255509ab4c26d158c105 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 4 Jan 2022 17:59:16 +0000 Subject: [PATCH] better logging and error handling --- src/main.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 59fe259..17b1075 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,9 +33,11 @@ fn create_dir_if_not_exists(path: Vec<&str>) -> PathBuf { } fn move_file(file: PathBuf, mut destination: PathBuf) { - println!("Moving to {:?}", destination); + let file_name = file.file_name().expect("No file name").to_str().unwrap(); - destination.push(file.file_name().unwrap().to_str().unwrap()); + println!("Moving {:?} to {:?}", file_name, destination); + + destination.push(file_name); fs_extra::file::move_file(file, destination, &Default::default()) .expect("Couldn't move file"); @@ -64,9 +66,13 @@ fn handle_gif(file: DirEntry) { } fn handle_mp3(file: DirEntry) { - let meta = mp3_metadata::read_from_file(file.path()).unwrap(); + let meta = mp3_metadata::read_from_file(file.path()); - if let Some(tag) = meta.tag { + if meta.is_err() { + println!("Couldn't read ID3 metadata for file {:?}", file.path()); + } + + if let Some(tag) = meta.unwrap().tag { println!("----------------------"); println!("artist: {}", tag.artist.trim()); println!("album: {}", tag.album.trim()); @@ -95,20 +101,22 @@ fn handle_dir(path: PathBuf) { let dir = std::fs::read_dir(path).expect("Couldn't read dir"); for inode in dir { - let inode = inode.unwrap(); + let inode = inode.expect("Couldn't read inode"); if inode.metadata().unwrap().is_dir() { handle_dir(inode.path()); } - match inode.path().extension().unwrap_or("none".as_ref()).to_str() { - Some("gif") => { handle_gif( inode) } - Some("jpg") => { handle_image(inode) } - Some("jpeg") => { handle_image(inode) } - Some("png") => { handle_image(inode) } - Some("mp3") => { handle_mp3( inode) } - // todo m4a, flac, etc? - _ => { /*println!("Here's where we would do nothing.");*/ } + if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() { + match extension.to_string().to_lowercase().as_ref() { + "gif" => { handle_gif( inode) } + "jpg" => { handle_image(inode) } + "jpeg" => { handle_image(inode) } + "png" => { handle_image(inode) } + "mp3" => { handle_mp3( inode) } + // todo m4a, flac, etc? + _ => { /*println!("Here's where we would do nothing.");*/ } + } } } }