From 3091da92cd497a7d9ea6ae2247d50eaf241702b6 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Tue, 29 Jun 2021 14:44:22 -0400 Subject: [PATCH] Add attribute to lofty-attr to skip From conversion Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com> --- lofty-attr/Cargo.lock | 2 +- lofty-attr/Cargo.toml | 2 +- lofty-attr/src/lib.rs | 80 ++++++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/lofty-attr/Cargo.lock b/lofty-attr/Cargo.lock index cdd3b983..ac0c5717 100644 --- a/lofty-attr/Cargo.lock +++ b/lofty-attr/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "lofty_attr" -version = "0.1.2" +version = "0.1.3" dependencies = [ "quote", "syn", diff --git a/lofty-attr/Cargo.toml b/lofty-attr/Cargo.toml index d259ff57..887cfdfc 100644 --- a/lofty-attr/Cargo.toml +++ b/lofty-attr/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lofty_attr" -version = "0.1.2" +version = "0.1.3" authors = ["Serial <69764315+Serial-ATA@users.noreply.github.com>"] description = "Macro for Lofty tag struct creation" license = "MIT OR Apache-2.0" diff --git a/lofty-attr/src/lib.rs b/lofty-attr/src/lib.rs index 78a84473..9ae003cb 100644 --- a/lofty-attr/src/lib.rs +++ b/lofty-attr/src/lib.rs @@ -23,6 +23,50 @@ pub fn impl_tag(args: TokenStream, input: TokenStream) -> TokenStream { if let Some(inner) = inner.get_ident() { let input_ident = input.ident; + let mut anytag_convert = quote! { + impl<'a> From> for #input_ident { + fn from(inp: AnyTag<'a>) -> Self { + let mut tag = #input_ident::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 + } + } + }; + + if let Some(attr) = input.attrs.first() { + if attr.path.is_ident("custom_convert") { + anytag_convert = quote! {}; + } + } + let expanded = quote! { #[doc(hidden)] pub struct #input_ident { @@ -102,41 +146,7 @@ pub fn impl_tag(args: TokenStream, input: TokenStream) -> TokenStream { } } - impl<'a> From> for #input_ident { - fn from(inp: AnyTag<'a>) -> Self { - let mut tag = #input_ident::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 - } - } + #anytag_convert // From dyn AudioTag to wrapper (any type) impl From> for #input_ident {