refactor: minor refactoring
This commit is contained in:
parent
a4693e3da9
commit
f9ea3ae1c8
1 changed files with 49 additions and 37 deletions
46
src/main.rs
46
src/main.rs
|
@ -65,17 +65,8 @@ fn rename_files(directory: &str, base: &str, args: &Arguments) -> Result<i32> {
|
|||
if let Some(sub_dir) = path.to_str() {
|
||||
count += rename_files(sub_dir, base, args).context("Failed to rename files")?;
|
||||
}
|
||||
}
|
||||
if !path.is_file() {
|
||||
continue;
|
||||
}
|
||||
let Ok(file) = taglib::File::new(&path) else {
|
||||
continue;
|
||||
};
|
||||
let Ok(tag) = file.tag() else {
|
||||
continue;
|
||||
};
|
||||
let (Some(title), Some(artist)) = (tag.title(), tag.artist()) else {
|
||||
} else if path.is_file() {
|
||||
let Some((title, artist)) = find_tag_info(&path) else {
|
||||
continue;
|
||||
};
|
||||
let (bucket, _) = artist.split_at(1);
|
||||
|
@ -92,20 +83,41 @@ fn rename_files(directory: &str, base: &str, args: &Arguments) -> Result<i32> {
|
|||
println!("- title : {title}");
|
||||
println!("> {new_name}");
|
||||
if args.not_dry_run() {
|
||||
rename_file(base, new_name, path)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
fn find_tag_info(path: &std::path::PathBuf) -> Option<(String, String)> {
|
||||
let Ok(file) = taglib::File::new(path) else {
|
||||
return None;
|
||||
};
|
||||
let Ok(tag) = file.tag() else {
|
||||
return None;
|
||||
};
|
||||
let (Some(title), Some(artist)) = (tag.title(), tag.artist()) else {
|
||||
return None;
|
||||
};
|
||||
Some((title, artist))
|
||||
}
|
||||
|
||||
fn rename_file(
|
||||
base: &str,
|
||||
new_name: String,
|
||||
path: std::path::PathBuf,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let target_path = Path::new(&base).join(new_name);
|
||||
if !target_path.exists() {
|
||||
let dir = target_path
|
||||
.parent()
|
||||
.with_context(|| format!("Failed to get parent: {:#?}", target_path))?;
|
||||
create_dir_all(dir)
|
||||
.with_context(|| format!("Failed to create directory: {:#?}", dir))?;
|
||||
create_dir_all(dir).with_context(|| format!("Failed to create directory: {:#?}", dir))?;
|
||||
}
|
||||
|
||||
rename(&path, &target_path)
|
||||
.with_context(|| format!("Failed to rename file: {:#?}", target_path))?;
|
||||
}
|
||||
}
|
||||
Ok(count)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn build_series_name(bucket: &str, artist: &str, album: &str, title: &str) -> String {
|
||||
|
|
Loading…
Reference in a new issue