diff --git a/src/edit.rs b/src/edit.rs index fe3119f..803bb05 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -10,7 +10,7 @@ use std::io::Write; use std::path::PathBuf; use std::{fs::File, fs::read_dir, io::Read, process::Command}; -pub fn edit(file_path: Vec, extra_files: [String; 8]) -> std::io::Result<()> { +pub fn edit(file_path: Vec, extra_files: [String; 9]) -> std::io::Result<()> { let tmp_file_path = PathBuf::from("/tmp/keks-sort-metadata"); let mut tmp_file = File::create(&tmp_file_path)?; let editor = var("EDITOR").unwrap(); diff --git a/src/main.rs b/src/main.rs index ac2fb9a..27588dc 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ use clap::{Parser, Subcommand}; -//use ini::Ini; use lofty::file::TaggedFileExt; use lofty::probe::Probe; use std::ffi::OsStr; @@ -35,24 +34,11 @@ enum Commands { } fn main() -> Result<(), Box> { - //let mut conf = Ini::new(); - //conf.with_section(Some("General")) - // .set("target_directory", ""); - ////conf.write_to_file("conf.ini").unwrap(); - // - //let conf = Ini::load_from_file("conf.ini").unwrap(); - // - //let section = conf.section(Some("General")).unwrap(); - //println!("target_dir={:?}", section.get("target_directory")); - // - //for (_sec, prop) in &conf { - // for (key, value) in prop.iter() { - // println!("{:?}:{:?}", key, value); - // } - //} - // let args = Args::parse(); - let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string); + let extra_files = [ + "jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt", "docx", + ] + .map(str::to_string); match &args.command { Commands::Import { files, copy } => { @@ -73,7 +59,7 @@ fn main() -> Result<(), Box> { let tag = get_metadata(&path); if let Err(e) = - move_files(path.clone(), tag, extra_files.clone(), *copy, true) + move_files(path.clone(), tag?, extra_files.clone(), *copy, true) { eprintln!("Failed to import {:?}: {}", path, e); } @@ -81,7 +67,13 @@ fn main() -> Result<(), Box> { } else { println!("Importing file: {:?}", file); let tag = get_metadata(file); - move_files(file.clone(), tag.clone(), extra_files.clone(), *copy, false)?; + move_files( + file.clone(), + tag?.clone(), + extra_files.clone(), + *copy, + false, + )?; } } } @@ -91,7 +83,7 @@ fn main() -> Result<(), Box> { println!("Show file: {:?}", file); let tag = get_metadata(file); - for item in tag.items() { + for item in tag?.items() { println!("{:?}", item) } } @@ -105,16 +97,29 @@ fn main() -> Result<(), Box> { Ok(()) } -pub fn get_metadata>(file_path: P) -> lofty::tag::Tag { - let tagged_file = Probe::open(file_path.as_ref()) - .expect("ERROR: Bad path provided!") - .read() - .expect("ERROR: Failed to read file!"); +pub fn get_metadata>( + file_path: P, +) -> Result> { + let tagged_file = match Probe::open(file_path.as_ref()) { + Ok(file) => file, + Err(err) => { + eprintln!("ERROR: Bad path provided: {err}"); + return Err(err.into()); + } + }; + + let tagged_file = match tagged_file.read() { + Ok(file) => file, + Err(err) => { + eprintln!("ERROR: Failed to read file: {err}"); + return Err(err.into()); + } + }; let tag = match tagged_file.primary_tag() { Some(primary_tag) => primary_tag, None => tagged_file.first_tag().expect("ERROR: No tags found!"), }; - tag.clone() + Ok(tag.clone()) } diff --git a/src/move_files.rs b/src/move_files.rs index 771a5fe..61d7779 100644 --- a/src/move_files.rs +++ b/src/move_files.rs @@ -12,7 +12,7 @@ use std::path::PathBuf; pub fn move_files( file_path: PathBuf, tag: Tag, - extra_files: [String; 8], + extra_files: [String; 9], should_copy: bool, has_parent: bool, ) -> Result<(), Box> { @@ -60,19 +60,21 @@ pub fn move_files( } for ext in &extra_files { - let file = file_path.parent().unwrap().join(format!("*.{}", ext)); + let pattern = file_path.parent().unwrap().join(format!("*.{}", ext)); - for slice in glob(file.to_str().unwrap())? { - let path = slice?; + let paths: Vec<_> = glob(pattern.to_str().unwrap())?.collect::>()?; + + for path in paths { + let destination = target.join(path.file_name().unwrap()); let result = if should_copy { - copy(&path, target.join(path.file_name().unwrap())).map(|_| ()) + copy(&path, &destination).map(|_| ()) } else { - rename(&path, target.join(path.file_name().unwrap())) + rename(&path, &destination) }; if let Err(e) = result { - println!("Copy failed: {e}"); + println!("Copy failed for {:?}: {e}", path); } } }