feat: edit item keys and values and write to file (IT WAS A PAIN)

This commit is contained in:
2026-09-03 20:39:51 +02:00
parent 0b586ecef0
commit 052c5bd1bc
+20 -2
View File
@@ -1,4 +1,9 @@
use lofty::tag::Tag;
use lofty::config::WriteOptions;
use lofty::prelude::ItemKey::AlbumTitle;
use lofty::tag::TagItem;
use lofty::tag::TagType::VorbisComments;
use lofty::tag::{ItemKey, ItemValue};
use lofty::tag::{Tag, TagExt};
use std::collections::HashMap;
use std::env::var;
use std::io::Write;
@@ -10,7 +15,12 @@ pub fn edit(file_path: PathBuf, mut tag: Tag) -> std::io::Result<()> {
let editor = var("EDITOR").unwrap();
let mut file = File::create(&tmp_file)?;
for item in tag.items() {
writeln!(file, "{:?} = {}", item.key(), item.value().text().unwrap())?;
writeln!(
file,
"{} = {}",
item.key().map_key(VorbisComments).unwrap(),
item.value().text().unwrap()
)?;
}
Command::new(editor)
@@ -23,6 +33,14 @@ pub fn edit(file_path: PathBuf, mut tag: Tag) -> std::io::Result<()> {
.expect("Could not open file")
.read_to_string(&mut editable)?;
let parsed = parse(&editable);
for (key, value) in parsed {
let item_key = ItemKey::from_key(VorbisComments, &key).expect("Failed to get item key");
let item = TagItem::new(item_key, ItemValue::Text(value));
tag.insert(item.clone());
}
tag.save_to_path(file_path, WriteOptions::default())
.expect("ERROR: Failed to write tag");
Ok(())
}