fix: empty paths are now None and not Some("")
This commit is contained in:
parent
95fe74e395
commit
3f49891dc8
2 changed files with 37 additions and 11 deletions
26
src/lib.rs
26
src/lib.rs
|
@ -8,15 +8,15 @@ use std::{
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub pictures_dir: Option<PathBuf>,
|
pub pictures_dir: Option<PathBuf>,
|
||||||
pub videos_dir: Option<PathBuf>,
|
pub video_dir: Option<PathBuf>,
|
||||||
pub music_dir: Option<PathBuf>,
|
pub music_dir: Option<PathBuf>,
|
||||||
pub books_dir: Option<PathBuf>,
|
pub books_dir: Option<PathBuf>,
|
||||||
pub sources: HashMap<String, PathBuf>,
|
pub sources: HashMap<String, PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const PICTURE_EXTENTIONS: [&str; 17] = [
|
pub const PICTURE_EXTENTIONS: [&str; 18] = [
|
||||||
"jpg", "jpeg", "png", "gif", "webp", "jfif", "bmp", "apng", "avif", "tif", "tga", "psd", "eps",
|
"jpg", "jpeg", "png", "gif", "webp", "jfif", "bmp", "apng", "avif", "tif", "tga", "psd", "eps",
|
||||||
"ai", "indd", "raw", "ico",
|
"ai", "indd", "raw", "ico", "svg",
|
||||||
];
|
];
|
||||||
pub const SOUND_EXTENTIONS: [&str; 10] = [
|
pub const SOUND_EXTENTIONS: [&str; 10] = [
|
||||||
"mp3", "wav", "flac", "ogg", "aac", "m4a", "wma", "aiff", "au", "opus",
|
"mp3", "wav", "flac", "ogg", "aac", "m4a", "wma", "aiff", "au", "opus",
|
||||||
|
@ -33,7 +33,7 @@ impl Config {
|
||||||
let mut sources: HashMap<String, PathBuf> = HashMap::new();
|
let mut sources: HashMap<String, PathBuf> = HashMap::new();
|
||||||
|
|
||||||
let mut pictures_dir = None;
|
let mut pictures_dir = None;
|
||||||
let mut videos_dir = None;
|
let mut video_dir = None;
|
||||||
let mut music_dir = None;
|
let mut music_dir = None;
|
||||||
let mut books_dir = None;
|
let mut books_dir = None;
|
||||||
|
|
||||||
|
@ -61,10 +61,10 @@ impl Config {
|
||||||
sources.insert(key_trimmed.into(), value_trimmed.into());
|
sources.insert(key_trimmed.into(), value_trimmed.into());
|
||||||
}
|
}
|
||||||
"destinations" => match key_trimmed {
|
"destinations" => match key_trimmed {
|
||||||
"music_dir" => music_dir = Some(PathBuf::from(value_trimmed)),
|
"music_dir" => set_directory(&mut music_dir, value_trimmed),
|
||||||
"video_dir" => videos_dir = Some(PathBuf::from(value_trimmed)),
|
"video_dir" => set_directory(&mut video_dir, value_trimmed),
|
||||||
"books_dir" => books_dir = Some(PathBuf::from(value_trimmed)),
|
"books_dir" => set_directory(&mut books_dir, value_trimmed),
|
||||||
"pictures_dir" => pictures_dir = Some(PathBuf::from(value_trimmed)),
|
"pictures_dir" => set_directory(&mut pictures_dir, value_trimmed),
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -74,7 +74,7 @@ impl Config {
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
pictures_dir,
|
pictures_dir,
|
||||||
videos_dir,
|
video_dir,
|
||||||
music_dir,
|
music_dir,
|
||||||
books_dir,
|
books_dir,
|
||||||
sources,
|
sources,
|
||||||
|
@ -85,7 +85,7 @@ impl Config {
|
||||||
let config_str = r#"
|
let config_str = r#"
|
||||||
[destinations]
|
[destinations]
|
||||||
pictures_dir = ""
|
pictures_dir = ""
|
||||||
videos_dir = ""
|
video_dir = ""
|
||||||
music_dir = ""
|
music_dir = ""
|
||||||
books_dir = ""
|
books_dir = ""
|
||||||
|
|
||||||
|
@ -98,3 +98,9 @@ books_dir = ""
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_directory(dir: &mut Option<PathBuf>, value: &str) {
|
||||||
|
if !value.is_empty() {
|
||||||
|
*dir = Some(PathBuf::from(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -67,6 +67,10 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
println!("config:{:#?}", CONFIG.get().unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
let mut actual_selection: Vec<String> = Vec::new();
|
let mut actual_selection: Vec<String> = Vec::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -145,6 +149,10 @@ fn main() {
|
||||||
let total_files_sorted: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
|
let total_files_sorted: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
|
||||||
|
|
||||||
for selection in actual_selection {
|
for selection in actual_selection {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
println!("spawned a thread with selection: {}", selection);
|
||||||
|
}
|
||||||
|
|
||||||
let tfs = Arc::clone(&total_files_sorted);
|
let tfs = Arc::clone(&total_files_sorted);
|
||||||
let thread = thread::spawn(move || match sort_files(selection, recursive) {
|
let thread = thread::spawn(move || match sort_files(selection, recursive) {
|
||||||
Ok(files_sorted) => {
|
Ok(files_sorted) => {
|
||||||
|
@ -189,7 +197,7 @@ fn sort_files(selection: String, recursive: bool) -> Result<usize, Box<dyn std::
|
||||||
(
|
(
|
||||||
"Video",
|
"Video",
|
||||||
filesorters::VIDEO_EXTENTIONS.to_vec(),
|
filesorters::VIDEO_EXTENTIONS.to_vec(),
|
||||||
CONFIG.get().unwrap().videos_dir.clone(),
|
CONFIG.get().unwrap().video_dir.clone(),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"Music",
|
"Music",
|
||||||
|
@ -213,6 +221,9 @@ fn sort_files(selection: String, recursive: bool) -> Result<usize, Box<dyn std::
|
||||||
|
|
||||||
for (_label, valid_extensions, target_dir_option) in file_types {
|
for (_label, valid_extensions, target_dir_option) in file_types {
|
||||||
if let Some(target_dir) = target_dir_option {
|
if let Some(target_dir) = target_dir_option {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
println!("{} - {}", file_path.display(), &extension);
|
||||||
|
}
|
||||||
if valid_extensions.contains(&extension) {
|
if valid_extensions.contains(&extension) {
|
||||||
move_file_to_directory(&file_path, target_dir);
|
move_file_to_directory(&file_path, target_dir);
|
||||||
files_sorted += 1;
|
files_sorted += 1;
|
||||||
|
@ -252,6 +263,15 @@ fn move_file_to_directory(path: &Path, dir: &Path) {
|
||||||
|
|
||||||
let destination = dir.join(path.file_name().unwrap_or_default());
|
let destination = dir.join(path.file_name().unwrap_or_default());
|
||||||
|
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
println!(
|
||||||
|
"{} - {} - {}",
|
||||||
|
path.display(),
|
||||||
|
dir.display(),
|
||||||
|
destination.display()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
match fs::rename(path, &destination) {
|
match fs::rename(path, &destination) {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
Err(err) => eprintln!("Error moving file: {}", err),
|
Err(err) => eprintln!("Error moving file: {}", err),
|
||||||
|
|
Loading…
Reference in a new issue