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() {
|
if let Some(sub_dir) = path.to_str() {
|
||||||
count += rename_files(sub_dir, base, args).context("Failed to rename files")?;
|
count += rename_files(sub_dir, base, args).context("Failed to rename files")?;
|
||||||
}
|
}
|
||||||
}
|
} else if path.is_file() {
|
||||||
if !path.is_file() {
|
let Some((title, artist)) = find_tag_info(&path) else {
|
||||||
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 {
|
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let (bucket, _) = artist.split_at(1);
|
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!("- title : {title}");
|
||||||
println!("> {new_name}");
|
println!("> {new_name}");
|
||||||
if args.not_dry_run() {
|
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);
|
let target_path = Path::new(&base).join(new_name);
|
||||||
if !target_path.exists() {
|
if !target_path.exists() {
|
||||||
let dir = target_path
|
let dir = target_path
|
||||||
.parent()
|
.parent()
|
||||||
.with_context(|| format!("Failed to get parent: {:#?}", target_path))?;
|
.with_context(|| format!("Failed to get parent: {:#?}", target_path))?;
|
||||||
create_dir_all(dir)
|
create_dir_all(dir).with_context(|| format!("Failed to create directory: {:#?}", dir))?;
|
||||||
.with_context(|| format!("Failed to create directory: {:#?}", dir))?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rename(&path, &target_path)
|
rename(&path, &target_path)
|
||||||
.with_context(|| format!("Failed to rename file: {:#?}", target_path))?;
|
.with_context(|| format!("Failed to rename file: {:#?}", target_path))?;
|
||||||
}
|
Ok(())
|
||||||
}
|
|
||||||
Ok(count)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_series_name(bucket: &str, artist: &str, album: &str, title: &str) -> String {
|
fn build_series_name(bucket: &str, artist: &str, album: &str, title: &str) -> String {
|
||||||
|
|
Loading…
Reference in a new issue