refactor some stuff

This commit is contained in:
Max Bradbury 2021-11-25 23:21:26 +00:00
parent f15d0e0f76
commit 0a4dbbba81
1 changed files with 55 additions and 41 deletions

View File

@ -1,3 +1,4 @@
use std::fs::DirEntry;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
@ -40,19 +41,16 @@ fn move_file(file: PathBuf, mut destination: PathBuf) {
.expect("Couldn't move file"); .expect("Couldn't move file");
} }
fn main() { fn yes() -> bool {
let downloads = std::fs::read_dir(downloads_dir()).unwrap(); let mut answer = String::new();
// todo break this out into a "handle_dir" function, and recursively call it for child dirs io::stdin().read_line(&mut answer)
for file in downloads { .expect("Failed to read input");
let file = file.unwrap();
match file.path().extension().unwrap_or("none".as_ref()).to_str() { answer.to_lowercase().starts_with("y")
Some("gif") => { /*println!("Here's where we would move things to ~/Pictures");*/ } }
Some("jpg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpeg") => { /*println!("Here's where we would move things to ~/Pictures");*/ } fn handle_mp3(file: DirEntry) {
Some("png") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("mp3") => {
let meta = mp3_metadata::read_from_file(file.path()).unwrap(); let meta = mp3_metadata::read_from_file(file.path()).unwrap();
for tag in meta.tag { for tag in meta.tag {
@ -63,12 +61,7 @@ fn main() {
println!("move to: ~/Music/{}/{}/?", tag.artist.trim(), tag.album.trim()); println!("move to: ~/Music/{}/{}/?", tag.artist.trim(), tag.album.trim());
let mut answer = String::new(); if yes() {
io::stdin().read_line(&mut answer)
.expect("Failed to read input");
if answer.to_lowercase().starts_with("y") {
move_file( move_file(
file.path(), file.path(),
create_dir_if_not_exists( create_dir_if_not_exists(
@ -83,9 +76,30 @@ fn main() {
println!("skipping..."); println!("skipping...");
} }
} }
}
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();
if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path());
} }
match inode.path().extension().unwrap_or("none".as_ref()).to_str() {
Some("gif") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpeg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("png") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("mp3") => { handle_mp3(inode) }
// todo m4a, flac, etc? // todo m4a, flac, etc?
_ => { /*println!("Here's where we would do nothing.");*/ } _ => { /*println!("Here's where we would do nothing.");*/ }
}; }
} }
} }
fn main() {
handle_dir(downloads_dir());
}