refactor(sorting): replace a lot of ifs with an array
This commit is contained in:
parent
3d9c39c872
commit
b7fc64a3a5
1 changed files with 29 additions and 32 deletions
61
src/main.rs
61
src/main.rs
|
@ -119,42 +119,39 @@ fn sort_files(selection: String, _recursive: bool) -> Result<(), Box<dyn std::er
|
|||
|
||||
let dir = fs::read_dir(search_path)?;
|
||||
|
||||
// TODO: Simplyfy by using an array of tulpes instead of a bucket of ifs
|
||||
let file_types = [
|
||||
(
|
||||
"Picture",
|
||||
filesorters::PICTURE_EXTENTIONS.to_vec(),
|
||||
CONFIG.get().unwrap().pictures_dir.clone(),
|
||||
),
|
||||
(
|
||||
"Book",
|
||||
filesorters::BOOK_EXTENTIONS.to_vec(),
|
||||
CONFIG.get().unwrap().books_dir.clone(),
|
||||
),
|
||||
(
|
||||
"Video",
|
||||
filesorters::VIDEO_EXTENTIONS.to_vec(),
|
||||
CONFIG.get().unwrap().videos_dir.clone(),
|
||||
),
|
||||
(
|
||||
"Music",
|
||||
filesorters::SOUND_EXTENTIONS.to_vec(),
|
||||
CONFIG.get().unwrap().music_dir.clone(),
|
||||
),
|
||||
];
|
||||
|
||||
for entry in dir.flatten().filter(|e| e.metadata().unwrap().is_file()) {
|
||||
if let Some(extension) = entry.path().extension().and_then(|e| e.to_str()) {
|
||||
let config = CONFIG.get().unwrap();
|
||||
let path = entry.path();
|
||||
|
||||
// Book files
|
||||
if config.books_dir.is_some() && filesorters::BOOK_EXTENTIONS.contains(&extension) {
|
||||
println!("Moved to Books: {}", path.display());
|
||||
if let Some(books_dir) = &config.books_dir {
|
||||
move_file_to_directory(&path, books_dir);
|
||||
}
|
||||
}
|
||||
|
||||
// Picture files
|
||||
if config.pictures_dir.is_some() && filesorters::PICTURE_EXTENTIONS.contains(&extension)
|
||||
{
|
||||
println!("Moved to Pictures: {}", path.display());
|
||||
if let Some(pictures_dir) = &config.pictures_dir {
|
||||
move_file_to_directory(&path, pictures_dir);
|
||||
}
|
||||
}
|
||||
|
||||
// Video files
|
||||
if config.videos_dir.is_some() && filesorters::VIDEO_EXTENTIONS.contains(&extension) {
|
||||
println!("Moved to Videos: {}", path.display());
|
||||
if let Some(videos_dir) = &config.videos_dir {
|
||||
move_file_to_directory(&path, videos_dir);
|
||||
}
|
||||
}
|
||||
|
||||
// Music files
|
||||
if config.music_dir.is_some() && filesorters::SOUND_EXTENTIONS.contains(&extension) {
|
||||
println!("Moved to Music: {}", path.display());
|
||||
if let Some(music_dir) = &config.music_dir {
|
||||
move_file_to_directory(&path, music_dir);
|
||||
for (label, valid_extensions, target_dir_option) in &file_types {
|
||||
if let Some(target_dir) = target_dir_option {
|
||||
if valid_extensions.contains(&extension) {
|
||||
println!("{}: {}", label, path.display());
|
||||
move_file_to_directory(&path, target_dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue