feat: edit item keys and values and write to file (IT WAS A PAIN)
This commit is contained in:
+20
-2
@@ -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::collections::HashMap;
|
||||||
use std::env::var;
|
use std::env::var;
|
||||||
use std::io::Write;
|
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 editor = var("EDITOR").unwrap();
|
||||||
let mut file = File::create(&tmp_file)?;
|
let mut file = File::create(&tmp_file)?;
|
||||||
for item in tag.items() {
|
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)
|
Command::new(editor)
|
||||||
@@ -23,6 +33,14 @@ pub fn edit(file_path: PathBuf, mut tag: Tag) -> std::io::Result<()> {
|
|||||||
.expect("Could not open file")
|
.expect("Could not open file")
|
||||||
.read_to_string(&mut editable)?;
|
.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user