Move some conversions to impl_tag macro

This commit is contained in:
Serial 2021-04-20 23:14:17 -04:00
parent 253609cc4b
commit 297db6fbac
6 changed files with 59 additions and 299 deletions

View file

@ -28,65 +28,6 @@ impl ApeTag {
}
}
impl<'a> From<&'a ApeTag> for AnyTag<'a> {
fn from(inp: &'a ApeTag) -> Self {
Self {
title: inp.title(),
artists: inp.artists_vec(),
year: inp.year().map(|y| y as i32),
album: Album::new(
inp.album_title(),
inp.album_artists_vec(),
inp.album_cover(),
),
track_number: inp.track_number(),
total_tracks: inp.total_tracks(),
disc_number: inp.disc_number(),
total_discs: inp.total_discs(),
comments: None,
date: None, // TODO
#[cfg(feature = "duration")]
duration: inp.duration,
}
}
}
impl<'a> From<AnyTag<'a>> for ApeTag {
fn from(inp: AnyTag<'a>) -> Self {
let mut tag = ApeTag::new();
if let Some(v) = inp.title() {
tag.set_title(v)
}
if let Some(v) = inp.artists_as_string() {
tag.set_artist(v.as_str())
}
if let Some(v) = inp.year {
tag.set_year(v)
}
if let Some(v) = inp.album().title {
tag.set_album_title(v)
}
if let Some(v) = inp.album().artists {
tag.set_album_artist(&v.join("/"))
}
if let Some(v) = inp.track_number() {
tag.set_track(v)
}
if let Some(v) = inp.total_tracks() {
tag.set_total_tracks(v)
}
if let Some(v) = inp.disc_number() {
tag.set_disc(v)
}
if let Some(v) = inp.total_discs() {
tag.set_total_discs(v)
}
tag
}
}
impl ApeTag {
fn get_value(&self, key: &str) -> Option<&str> {
if let Some(item) = self.inner.item(key) {

View file

@ -28,65 +28,6 @@ impl Id3v2Tag {
}
}
impl<'a> From<&'a Id3v2Tag> for AnyTag<'a> {
fn from(inp: &'a Id3v2Tag) -> Self {
Self {
title: inp.title(),
artists: inp.artists_vec(),
year: inp.year().map(|y| y as i32),
album: Album::new(
inp.album_title(),
inp.album_artists_vec(),
inp.album_cover(),
),
track_number: inp.track_number(),
total_tracks: inp.total_tracks(),
disc_number: inp.disc_number(),
total_discs: inp.total_discs(),
comments: None,
date: None, // TODO
#[cfg(feature = "duration")]
duration: inp.duration,
}
}
}
impl<'a> From<AnyTag<'a>> for Id3v2Tag {
fn from(inp: AnyTag<'a>) -> Self {
let mut tag = Id3v2Tag::new();
if let Some(v) = inp.title() {
tag.set_title(v)
}
if let Some(v) = inp.artists_as_string() {
tag.set_artist(v.as_str())
}
if let Some(v) = inp.year {
tag.set_year(v)
}
if let Some(v) = inp.album().title {
tag.set_album_title(v)
}
if let Some(v) = inp.album().artists {
tag.set_album_artist(&v.join("/"))
}
if let Some(v) = inp.track_number() {
tag.set_track(v)
}
if let Some(v) = inp.total_tracks() {
tag.set_total_tracks(v)
}
if let Some(v) = inp.disc_number() {
tag.set_disc(v)
}
if let Some(v) = inp.total_discs() {
tag.set_total_discs(v)
}
tag
}
}
impl<'a> std::convert::TryFrom<&'a id3::frame::Picture> for Picture<'a> {
type Error = Error;
fn try_from(inp: &'a id3::frame::Picture) -> Result<Self> {

View file

@ -27,71 +27,6 @@ impl Mp4Tag {
}
}
impl<'a> From<&'a Mp4Tag> for AnyTag<'a> {
fn from(inp: &'a Mp4Tag) -> Self {
let title = inp.title();
let artists = inp.artists_vec().map(|i| i.into_iter().collect::<Vec<_>>());
let year = inp.year().map(|y| y as i32);
let album = Album::new(
inp.album_title(),
inp.album_artists_vec(),
inp.album_cover(),
);
let (track_number, total_tracks) = inp.track();
let (disc_number, total_discs) = inp.disc();
Self {
title,
artists,
year,
album,
track_number,
total_tracks,
disc_number,
total_discs,
comments: None,
date: None,
#[cfg(feature = "duration")]
duration: inp.duration, // TODO?
}
}
}
impl<'a> From<AnyTag<'a>> for Mp4Tag {
fn from(inp: AnyTag<'a>) -> Self {
let mut tag = Mp4Tag::new();
if let Some(v) = inp.title() {
tag.set_title(v)
}
if let Some(i) = inp.artists_as_string() {
tag.set_artist(&*i)
}
if let Some(v) = inp.year {
tag.set_year(v)
}
if let Some(v) = inp.album().title {
tag.set_album_title(v)
}
if let Some(i) = inp.album().artists_as_string() {
tag.set_album_artist(&*i)
}
if let Some(v) = inp.track_number() {
tag.set_track_number(v)
}
if let Some(v) = inp.total_tracks() {
tag.set_total_tracks(v)
}
if let Some(v) = inp.disc_number() {
tag.set_disc_number(v)
}
if let Some(v) = inp.total_discs() {
tag.set_total_discs(v)
}
tag
}
}
impl<'a> std::convert::TryFrom<&'a mp4ameta::Data> for Picture<'a> {
type Error = Error;
fn try_from(inp: &'a mp4ameta::Data) -> Result<Self> {

View file

@ -128,64 +128,6 @@ impl VorbisTag {
}
}
impl<'a> From<AnyTag<'a>> for VorbisTag {
fn from(inp: AnyTag<'a>) -> Self {
let mut tag = VorbisTag::default();
if let Some(v) = inp.title() {
tag.set_title(v)
}
if let Some(v) = inp.artists_as_string() {
tag.set_artist(&v)
}
if let Some(v) = inp.year {
tag.set_year(v)
}
if let Some(v) = inp.album().title {
tag.set_album_title(v)
}
if let Some(v) = inp.album().artists {
tag.set_album_artist(&v.join("/"))
}
if let Some(v) = inp.track_number() {
tag.set_track_number(v)
}
if let Some(v) = inp.total_tracks() {
tag.set_total_tracks(v)
}
if let Some(v) = inp.disc_number() {
tag.set_disc_number(v)
}
if let Some(v) = inp.total_discs() {
tag.set_total_discs(v)
}
tag
}
}
impl<'a> From<&'a VorbisTag> for AnyTag<'a> {
fn from(inp: &'a VorbisTag) -> Self {
Self {
title: inp.title(),
artists: inp.artists_vec(),
year: inp.year().map(|y| y as i32),
album: Album::new(
inp.album_title(),
inp.album_artists_vec(),
inp.album_cover(),
),
track_number: inp.track_number(),
total_tracks: inp.total_tracks(),
disc_number: inp.disc_number(),
total_discs: inp.total_discs(),
comments: None,
date: None,
duration: inp.duration
}
}
}
impl From<metaflac::Tag> for VorbisTag {
fn from(inp: metaflac::Tag) -> Self {
let mut tag = Self::default();

View file

@ -41,64 +41,6 @@ impl WavTag {
impl_tag!(WavTag, WavInnerTag, TagType::Wav);
impl<'a> From<AnyTag<'a>> for WavTag {
fn from(inp: AnyTag<'a>) -> Self {
let mut tag = WavTag::default();
if let Some(v) = inp.title() {
tag.set_title(v)
}
if let Some(v) = inp.artists_as_string() {
tag.set_artist(&v)
}
if let Some(v) = inp.year {
tag.set_year(v)
}
if let Some(v) = inp.album().title {
tag.set_album_title(v)
}
if let Some(v) = inp.album().artists {
tag.set_album_artist(&v.join("/"))
}
if let Some(v) = inp.track_number() {
tag.set_track_number(v)
}
if let Some(v) = inp.total_tracks() {
tag.set_total_tracks(v)
}
if let Some(v) = inp.disc_number() {
tag.set_disc_number(v)
}
if let Some(v) = inp.total_discs() {
tag.set_total_discs(v)
}
tag
}
}
impl<'a> From<&'a WavTag> for AnyTag<'a> {
fn from(inp: &'a WavTag) -> Self {
Self {
title: inp.title(),
artists: inp.artists_vec(),
year: inp.year().map(|y| y as i32),
album: Album::new(
inp.album_title(),
inp.album_artists_vec(),
inp.album_cover(),
),
track_number: inp.track_number(),
total_tracks: inp.total_tracks(),
disc_number: inp.disc_number(),
total_discs: inp.total_discs(),
comments: None,
date: None,
duration: inp.duration
}
}
}
impl WavTag {
fn get_value(&self, key: &str) -> Option<&str> {
self.inner

View file

@ -62,6 +62,65 @@ macro_rules! impl_tag {
}
}
impl<'a> From<&'a $tag> for AnyTag<'a> {
fn from(inp: &'a $tag) -> Self {
Self {
title: inp.title(),
artists: inp.artists_vec(),
year: inp.year().map(|y| y as i32),
album: Album::new(
inp.album_title(),
inp.album_artists_vec(),
inp.album_cover(),
),
track_number: inp.track_number(),
total_tracks: inp.total_tracks(),
disc_number: inp.disc_number(),
total_discs: inp.total_discs(),
comments: None,
date: None, // TODO
#[cfg(feature = "duration")]
duration: inp.duration,
}
}
}
impl<'a> From<AnyTag<'a>> for $tag {
fn from(inp: AnyTag<'a>) -> Self {
let mut tag = $tag::default();
if let Some(v) = inp.title() {
tag.set_title(v)
}
if let Some(v) = inp.artists_as_string() {
tag.set_artist(&v)
}
if let Some(v) = inp.year {
tag.set_year(v)
}
if let Some(v) = inp.album().title {
tag.set_album_title(v)
}
if let Some(v) = inp.album().artists {
tag.set_album_artist(&v.join("/"))
}
if let Some(v) = inp.track_number() {
tag.set_track_number(v)
}
if let Some(v) = inp.total_tracks() {
tag.set_total_tracks(v)
}
if let Some(v) = inp.disc_number() {
tag.set_disc_number(v)
}
if let Some(v) = inp.total_discs() {
tag.set_total_discs(v)
}
tag
}
}
// From dyn AudioTag to wrapper (any type)
impl From<Box<dyn AudioTag>> for $tag {
fn from(inp: Box<dyn AudioTag>) -> Self {