Add methods to set and remove all pictures, remove pointless panic

Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
Serial 2021-07-09 13:10:49 -04:00
parent e6eb8d8d40
commit ac91cbfd63
5 changed files with 56 additions and 12 deletions

View file

@ -1,4 +1,4 @@
use crate::types::picture::APE_PICTYPES;
use crate::types::picture::{APE_PICTYPES, PicType};
use crate::{
Album, AnyTag, AudioTag, AudioTagEdit, AudioTagWrite, Picture, Result, TagType, ToAny, ToAnyTag,
};
@ -259,6 +259,22 @@ impl AudioTagEdit for ApeTag {
Some(Cow::from(pics))
}
}
fn set_pictures(&mut self, pictures: Vec<Picture>) {
self.remove_pictures();
for p in pictures {
let key = p.pic_type.as_ape_key();
if let Ok(item) = ape::Item::from_binary(key, p.as_ape_bytes()) {
self.inner.set_item(item)
}
}
}
fn remove_pictures(&mut self) {
for key in &APE_PICTYPES {
self.inner.remove_item(key);
}
}
// Track number and total tracks are stored together as num/total?
fn track_number(&self) -> Option<u32> {

View file

@ -310,6 +310,18 @@ impl AudioTagEdit for Id3v2Tag {
None
}
fn set_pictures(&mut self, pictures: Vec<Picture>) {
self.remove_pictures();
for p in pictures {
if let Ok(pic) = TryInto::<id3::frame::Picture>::try_into(p) {
self.inner.add_picture(pic)
}
}
}
fn remove_pictures(&mut self) {
self.inner.remove_all_pictures()
}
fn track_number(&self) -> Option<u32> {
self.inner.track()

View file

@ -184,14 +184,12 @@ impl AudioTagEdit for Mp4Tag {
}
fn set_front_cover(&mut self, cover: Picture) {
self.inner.remove_artwork();
self.inner.add_artwork(match cover.mime_type {
MimeType::Png => mp4ameta::Data::Png(Vec::from(cover.data)),
MimeType::Jpeg => mp4ameta::Data::Jpeg(Vec::from(cover.data)),
MimeType::Bmp => mp4ameta::Data::Bmp(Vec::from(cover.data)),
_ => panic!("Attempt to add an invalid image format to MP4"),
});
match cover.mime_type {
MimeType::Png => self.inner.add_artwork(mp4ameta::Data::Png(Vec::from(cover.data))),
MimeType::Jpeg => self.inner.add_artwork(mp4ameta::Data::Jpeg(Vec::from(cover.data))),
MimeType::Bmp => self.inner.add_artwork(mp4ameta::Data::Bmp(Vec::from(cover.data))),
_ => {},
}
}
fn remove_front_cover(&mut self) {
@ -204,9 +202,6 @@ impl AudioTagEdit for Mp4Tag {
fn set_back_cover(&mut self, cover: Picture) {
self.set_front_cover(cover)
}
fn remove_back_cover(&mut self) {
self.inner.remove_artwork();
}
fn pictures(&self) -> Option<Cow<'static, [Picture]>> {
let mut pictures = Vec::new();
@ -235,7 +230,17 @@ impl AudioTagEdit for Mp4Tag {
Some(Cow::from(pictures))
}
}
fn set_pictures(&mut self, pictures: Vec<Picture>) {
self.remove_pictures();
for p in pictures {
self.set_front_cover(p)
}
}
fn remove_pictures(&mut self) {
self.inner.remove_artwork()
}
fn track_number(&self) -> Option<u32> {
self.inner.track_number().map(u32::from)
}

View file

@ -377,6 +377,13 @@ impl AudioTagEdit for OggTag {
fn pictures(&self) -> Option<Cow<'static, [Picture]>> {
self.inner.pictures.clone()
}
fn set_pictures(&mut self, pictures: Vec<Picture>) {
self.remove_pictures();
self.inner.pictures = Some(Cow::from(pictures))
}
fn remove_pictures(&mut self) {
self.inner.pictures = None
}
fn track_number(&self) -> Option<u32> {
if let Some(Ok(n)) = self.inner.get_value("TRACKNUMBER").map(str::parse::<u32>) {

View file

@ -91,6 +91,10 @@ pub trait AudioTagEdit {
fn pictures(&self) -> Option<Cow<'static, [Picture]>> {
None
}
/// Replace all pictures
fn set_pictures(&mut self, _pictures: Vec<Picture>) {}
/// Remove all pictures
fn remove_pictures(&mut self) {}
/// Returns the track number and total tracks
fn track(&self) -> (Option<u32>, Option<u32>) {