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