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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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,
				}

				impl Default for #input_ident {
					fn default() -> Self {
						Self {
							inner: #inner::default(),
						}
					}
				}

				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(),
							artist: inp.artist(),
							year: inp.year().map(|y| y as i32),
							album: Album::new(
								inp.album_title(),
								inp.album_artist(),
								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.artist() {
							tag.set_artist(&v)
						}
						if let Some(v) = inp.year {
							tag.set_year(v)
						}
						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)
						}

						let album = inp.album();

						if let Some(v) = album.title {
							tag.set_album_title(v)
						}
						if let Some(v) = album.artist {
							tag.set_album_artist(v)
						}
						if let Some(v) = album.covers.0 {
							tag.set_front_cover(v)
						}
						if let Some(v) = album.covers.1 {
							tag.set_back_cover(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()
}

#[proc_macro]
pub fn str_accessor(input: TokenStream) -> TokenStream {
	let input_str = input.to_string();
	let name = input_str.replace("_", " ");

	format!(
		"/// Returns the {display}
			fn {ident}(&self) -> Option<&str> {{
				None
			}}
			/// Sets the {display}
			fn set_{ident}(&mut self, _{ident}: &str) {{}}
			/// Removes the {display}
			fn remove_{ident}(&mut self) {{}}
			",
		ident = input_str,
		display = name,
	)
	.parse()
	.expect("Unable to parse str accessor:")
}

#[proc_macro]
pub fn u16_accessor(input: TokenStream) -> TokenStream {
	let input_str = input.to_string();
	let name = input_str.replace("_", " ");

	format!(
		"/// Returns the {display}
			fn {ident}(&self) -> Option<u16> {{
				None
			}}
			/// Sets the {display}
			fn set_{ident}(&mut self, _{ident}: u16) {{}}
			/// Removes the {display}
			fn remove_{ident}(&mut self) {{}}
			",
		ident = input_str,
		display = name,
	)
		.parse()
		.expect("Unable to parse u16 accessor:")
}

#[proc_macro]
pub fn u32_accessor(input: TokenStream) -> TokenStream {
	let input_str = input.to_string();
	let name = input_str.replace("_", " ");

	format!(
		"/// Returns the {display}
			fn {ident}(&self) -> Option<u32> {{
				None
			}}
			/// Sets the {display}
			fn set_{ident}(&mut self, _{ident}: u32) {{}}
			/// Removes the {display}
			fn remove_{ident}(&mut self) {{}}
			",
		ident = input_str,
		display = name,
	)
		.parse()
		.expect("Unable to parse u32 accessor:")
}

#[proc_macro]
pub fn i32_accessor(input: TokenStream) -> TokenStream {
	let input_str = input.to_string();
	let name = input_str.replace("_", " ");

	format!(
		"/// Returns the {display}
			fn {ident}(&self) -> Option<i32> {{
				None
			}}
			/// Sets the {display}
			fn set_{ident}(&mut self, _{ident}: i32) {{}}
			/// Removes the {display}
			fn remove_{ident}(&mut self) {{}}
			",
		ident = input_str,
		display = name,
	)
		.parse()
		.expect("Unable to parse i32 accessor:")
}