better logging and error handling

This commit is contained in:
Max Bradbury 2022-01-04 17:59:16 +00:00
parent 8d584d2af1
commit ef4d2816b3
1 changed files with 21 additions and 13 deletions

View File

@ -33,9 +33,11 @@ fn create_dir_if_not_exists(path: Vec<&str>) -> PathBuf {
} }
fn move_file(file: PathBuf, mut destination: 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()) fs_extra::file::move_file(file, destination, &Default::default())
.expect("Couldn't move file"); .expect("Couldn't move file");
@ -64,9 +66,13 @@ fn handle_gif(file: DirEntry) {
} }
fn handle_mp3(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!("----------------------");
println!("artist: {}", tag.artist.trim()); println!("artist: {}", tag.artist.trim());
println!("album: {}", tag.album.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"); let dir = std::fs::read_dir(path).expect("Couldn't read dir");
for inode in dir { for inode in dir {
let inode = inode.unwrap(); let inode = inode.expect("Couldn't read inode");
if inode.metadata().unwrap().is_dir() { if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path()); handle_dir(inode.path());
} }
match inode.path().extension().unwrap_or("none".as_ref()).to_str() { if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() {
Some("gif") => { handle_gif( inode) } match extension.to_string().to_lowercase().as_ref() {
Some("jpg") => { handle_image(inode) } "gif" => { handle_gif( inode) }
Some("jpeg") => { handle_image(inode) } "jpg" => { handle_image(inode) }
Some("png") => { handle_image(inode) } "jpeg" => { handle_image(inode) }
Some("mp3") => { handle_mp3( inode) } "png" => { handle_image(inode) }
// todo m4a, flac, etc? "mp3" => { handle_mp3( inode) }
_ => { /*println!("Here's where we would do nothing.");*/ } // todo m4a, flac, etc?
_ => { /*println!("Here's where we would do nothing.");*/ }
}
} }
} }
} }