Remove metaflac::Tag -> OggTag conversion

This commit is contained in:
Serial 2021-07-24 15:54:51 -04:00
parent 3d6f1f846e
commit 091daefd85
2 changed files with 13 additions and 59 deletions

View file

@ -89,49 +89,6 @@ impl TryFrom<OGGTags> for OggTag {
} }
} }
#[cfg(feature = "format-flac")]
impl TryFrom<metaflac::Tag> for OggTag {
type Error = LoftyError;
fn try_from(inp: metaflac::Tag) -> Result<Self> {
if let Some(comments) = inp.vorbis_comments() {
let mut user_comments = comments.comments.clone();
let mut pictures = Vec::new();
if let Some(pics) = user_comments.remove("METADATA_BLOCK_PICTURE") {
pics.iter().for_each(|item| {
if let Ok(pic) = Picture::from_apic_bytes(item.as_bytes()) {
pictures.push(pic)
}
})
}
let mut comment_collection: HashMap<UniCase<String>, String> = HashMap::new();
for (k, v) in user_comments.clone() {
for e in v {
comment_collection.insert(UniCase::from(k.clone()), e.clone());
}
}
return Ok(Self {
inner: OggInnerTag {
vendor: comments.vendor_string.clone(),
comments: comment_collection,
pictures: Some(Cow::from(pictures)),
},
properties: FileProperties::default(), // TODO
_format: TagType::Ogg(OggFormat::Flac),
});
}
Err(LoftyError::InvalidData(
"Flac file contains no vorbis comment blocks",
))
}
}
impl OggTag { impl OggTag {
#[allow(missing_docs)] #[allow(missing_docs)]
#[allow(clippy::missing_errors_doc)] #[allow(clippy::missing_errors_doc)]

View file

@ -1,38 +1,35 @@
#![cfg(feature = "default")] #![cfg(feature = "default")]
use lofty::{Id3Format, OggTag, Tag, TagType, ToAnyTag}; use lofty::{Id3Format, OggTag, Tag, TagType, ToAnyTag, OggFormat, AudioTagEdit};
use std::convert::TryInto;
use std::io::Cursor;
#[test] #[test]
fn test_inner() { fn test_inner() {
// New flac tag // Create a new flac OggTag
let mut innertag = metaflac::Tag::new(); let mut flac_data = Cursor::new(std::fs::read("tests/assets/a.flac").unwrap());
let mut flac_tag = OggTag::read_from(&mut flac_data, &OggFormat::Flac).unwrap();
// Set the title of the flac tag // Set the title of the flac tag
innertag flac_tag.set_title("Foo title");
.vorbis_comments_mut()
.set_title(vec!["title from metaflac::Tag"]);
// Turn the flac tag into a VorbisTag // Turn the VorbisTag into an Id3v2Tag
let tag: OggTag = innertag.try_into().unwrap(); let id3tag = flac_tag.to_dyn_tag(TagType::Id3v2(Id3Format::Mp3));
// Turn the VorbisTag into a Box<dyn AudioTag> // Write the Id3v2Tag to `a.mp3`
let id3tag = tag.to_dyn_tag(TagType::Id3v2(Id3Format::Mp3));
// Write Box<dyn AudioTag> to `a.mp3`
id3tag id3tag
.write_to_path("tests/assets/a.mp3") .write_to_path("tests/assets/a.mp3")
.expect("Fail to write!"); .expect("Fail to write!");
// Read from `a.mp3` // Read from `a.mp3`
let id3tag_reload = Tag::default() let id3tag_reload = Tag::new()
.read_from_path("tests/assets/a.mp3") .read_from_path("tests/assets/a.mp3")
.expect("Fail to read!"); .expect("Fail to read!");
// Confirm title still matches // Confirm title still matches
assert_eq!(id3tag_reload.title(), Some("title from metaflac::Tag")); assert_eq!(id3tag_reload.title(), Some("Foo title"));
// Convert Box<dyn AudioTag> to id3::Tag // Convert Id3v2Tag to id3::Tag
let mut id3tag_inner: id3::Tag = id3tag_reload.into(); let mut id3tag_inner: id3::Tag = id3tag_reload.into();
// Create timestamp and change date_recorded // Create timestamp and change date_recorded