1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, AttributeArgs, Error, ItemStruct, Meta, NestedMeta};

#[proc_macro_attribute]
#[allow(clippy::too_many_lines)]
pub fn impl_tag(args: TokenStream, input: TokenStream) -> TokenStream {
	let input = parse_macro_input!(input as ItemStruct);
	let args = parse_macro_input!(args as AttributeArgs);

	if args.len() != 2 {
		return Error::new(
			input.ident.span(),
			"impl_tag requires an inner tag and TagType",
		)
		.to_compile_error()
		.into();
	}

	if let (NestedMeta::Meta(Meta::Path(inner)), NestedMeta::Meta(tag_type)) =
		(args[0].clone(), args[1].clone())
	{
		if let Some(inner) = inner.get_ident() {
			let input_ident = input.ident;

			let expanded = quote! {
				#[doc(hidden)]
				pub struct #input_ident {
					inner: #inner,
					#[cfg(feature = "duration")]
					duration: Option<std::time::Duration>
				}

				impl Default for #input_ident {
					fn default() -> Self {
						Self {
							inner: #inner::default(),
							#[cfg(feature = "duration")]
							duration: None,
						}
					}
				}

				impl #input_ident {
					/// Creates a new default tag
					pub fn new() -> Self {
						Self::default()
					}
				}

				use std::any::Any;

				impl ToAnyTag for #input_ident {
					fn to_anytag(&self) -> AnyTag<'_> {
						self.into()
					}
				}

				impl ToAny for #input_ident {
					fn to_any(&self) -> &dyn Any {
						self
					}
					fn to_any_mut(&mut self) -> &mut dyn Any {
						self
					}
				}

				impl AudioTag for #input_ident {}

				// From wrapper to inner (same type)
				impl From<#input_ident> for #inner {
					fn from(inp: #input_ident) -> Self {
						inp.inner
					}
				}

				// From inner to wrapper (same type)
				impl From<#inner> for #input_ident {
					fn from(inp: #inner) -> Self {
						Self {
							inner: inp,
							#[cfg(feature = "duration")]
							duration: None,
						}
					}
				}

				impl<'a> From<&'a #input_ident> for AnyTag<'a> {
					fn from(inp: &'a #input_ident) -> 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_covers(),
							),
							track_number: inp.track_number(),
							total_tracks: inp.total_tracks(),
							disc_number: inp.disc_number(),
							total_discs: inp.total_discs(),
							comments: None, // TODO
							date: inp.date(),
						}
					}
				}

				impl<'a> From<AnyTag<'a>> 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
					}
				}

				// From dyn AudioTag to wrapper (any type)
				impl From<Box<dyn AudioTag>> for #input_ident {
					fn from(inp: Box<dyn AudioTag>) -> Self {
						let mut inp = inp;
						if let Some(t_refmut) = inp.to_any_mut().downcast_mut::<#input_ident>() {
							let t = std::mem::replace(t_refmut, #input_ident::new()); // TODO: can we avoid creating the dummy tag?
							t
						} else {
							let mut t = inp.to_dyn_tag(#tag_type);
							let t_refmut = t.to_any_mut().downcast_mut::<#input_ident>().unwrap();
							let t = std::mem::replace(t_refmut, #input_ident::new());
							t
						}
					}
				}

				// From dyn AudioTag to inner (any type)
				impl From<Box<dyn AudioTag>> for #inner {
					fn from(inp: Box<dyn AudioTag>) -> Self {
						let t: #input_ident = inp.into();
						t.into()
					}
				}
			};

			return TokenStream::from(expanded);
		}
	}

	Error::new(input.ident.span(), "impl_tag provided invalid arguments")
		.to_compile_error()
		.into()
}