diff --git a/src/edit.rs b/src/edit.rs index 2a5916d..42e814f 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -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(()) }