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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use proc_macro2::{Span, TokenStream};
use proc_macro2_diagnostics::{Diagnostic, Level};
use quote::quote;
use syn::{
	parse::Parse, punctuated::Punctuated, spanned::Spanned, token::Comma, Field, FnArg, GenericParam,
	Generics, Ident, Pat, Path, PathArguments, PathSegment, Token, Type, TypeImplTrait, TypeParam,
	TypePath, Visibility,
};

trait IdentPath {
	fn write_to(&self, target: &mut dyn FnMut(&str));
}

impl<'a> IdentPath for &'a str {
	fn write_to(&self, target: &mut dyn FnMut(&str)) {
		target(self);
	}
}

impl IdentPath for usize {
	fn write_to(&self, target: &mut dyn FnMut(&str)) {
		target(&self.to_string());
	}
}

impl<L, R> IdentPath for (L, R)
where
	L: IdentPath,
	R: IdentPath,
{
	fn write_to(&self, target: &mut dyn FnMut(&str)) {
		self.0.write_to(target);
		self.1.write_to(target);
	}
}

impl<'a> IdentPath for &'a dyn IdentPath {
	fn write_to(&self, target: &mut dyn FnMut(&str)) {
		(*self).write_to(target);
	}
}

#[derive(Clone)]
struct IdentParts<I>(I);

impl<'a, I> IdentPath for IdentParts<I>
where
	I: Iterator<Item = &'a str> + Clone,
{
	fn write_to(&self, target: &mut dyn FnMut(&str)) {
		for part in self.0.clone() {
			target(part);
		}
	}
}

struct ComponentAttrs {
	vis: syn::Visibility,
	name: syn::Ident,
}

impl Parse for ComponentAttrs {
	fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
		Ok(Self {
			vis: input.parse()?,
			name: input.parse()?,
		})
	}
}

struct ComponentStructBuilder {
	vis: syn::Visibility,
	ident: syn::Ident,
	generics: Generics,
	fields: Punctuated<Field, Comma>,
}

impl ComponentStructBuilder {
	fn new(attr: ComponentAttrs, generics: Generics) -> Self {
		Self {
			vis: attr.vis,
			ident: attr.name,
			generics,
			fields: Punctuated::new(),
		}
	}

	fn resolve_type(&mut self, ty: &Type, ident_hint: &dyn IdentPath) -> Type {
		match ty {
			Type::ImplTrait(inner) => self.add_generic(inner.clone(), ident_hint),

			Type::Array(inner) => {
				let elem = self.resolve_type(&inner.elem, ident_hint);
				Type::Array(syn::TypeArray {
					bracket_token: inner.bracket_token,
					elem: Box::new(elem),
					semi_token: inner.semi_token,
					len: inner.len.clone(),
				})
			}

			Type::Paren(inner) => {
				let elem = self.resolve_type(&inner.elem, ident_hint);
				Type::Paren(syn::TypeParen {
					paren_token: inner.paren_token,
					elem: Box::new(elem),
				})
			}

			Type::Ptr(inner) => {
				let elem = self.resolve_type(&inner.elem, ident_hint);
				Type::Ptr(syn::TypePtr {
					star_token: inner.star_token,
					const_token: inner.const_token,
					mutability: inner.mutability,
					elem: Box::new(elem),
				})
			}

			Type::Reference(inner) => {
				let elem = self.resolve_type(&inner.elem, ident_hint);
				Type::Reference(syn::TypeReference {
					and_token: inner.and_token,
					lifetime: inner.lifetime.clone(),
					mutability: inner.mutability,
					elem: Box::new(elem),
				})
			}

			Type::Slice(inner) => {
				let elem = self.resolve_type(&inner.elem, ident_hint);
				Type::Slice(syn::TypeSlice {
					bracket_token: inner.bracket_token,
					elem: Box::new(elem),
				})
			}

			Type::Tuple(inner) => {
				let elems = inner
					.elems
					.iter()
					.enumerate()
					.map(|(idx, elem)| self.resolve_type(elem, &(ident_hint, idx)))
					.collect();

				Type::Tuple(syn::TypeTuple {
					paren_token: inner.paren_token,
					elems,
				})
			}

			_ => ty.clone(),
		}
	}

	fn push_field(&mut self, ident: Ident, ty: Type) {
		let ty = self.resolve_type(&ty, &IdentParts(ident.to_string().split('_')));

		let field = Field {
			attrs: vec![],
			vis: Visibility::Public(Token![pub](Span::call_site())),
			mutability: syn::FieldMutability::None,
			ident: Some(ident),
			ty,
			colon_token: Some(Token![:](Span::call_site())),
		};

		self.fields.push(field);
	}

	fn add_generic(&mut self, impl_type: TypeImplTrait, ident_hint: &dyn IdentPath) -> Type {
		let mut type_ident_str = String::new();
		type_ident_str.push('T');
		ident_hint.write_to(&mut |part| {
			if part.is_empty() {
				return;
			}

			let first_char = part.chars().next().unwrap();
			type_ident_str.push(first_char.to_ascii_uppercase());
			type_ident_str.push_str(&part[first_char.len_utf8()..]);
		});

		let existing_set = self
			.generics
			.type_params()
			.map(|par| par.ident.to_string())
			.collect::<std::collections::HashSet<_>>();

		while existing_set.contains(&type_ident_str) {
			type_ident_str.push('_');
		}

		let type_ident = Ident::new(&type_ident_str, Span::call_site());
		let type_param = TypeParam {
			attrs: vec![],
			ident: type_ident.clone(),
			colon_token: Some(Token![:](Span::call_site())),
			bounds: impl_type.bounds,
			eq_token: None,
			default: None,
		};

		self.generics.params.push(GenericParam::Type(type_param));

		Type::Path(TypePath {
			qself: None,
			path: Path {
				leading_colon: None,
				segments: Punctuated::from_iter(vec![PathSegment {
					ident: type_ident,
					arguments: PathArguments::None,
				}]),
			},
		})
	}

	fn build(self) -> (TokenStream, Ident, Generics, Punctuated<Field, Comma>) {
		let Self {
			vis,
			ident,
			generics,
			fields,
		} = self;
		let (impl_generics, _, where_clause) = generics.split_for_impl();

		let generated_struct = quote! {
			#[derive(::rstml_component::HtmlComponent)]
			#[allow(non_snake_case)]
			#vis struct #ident #impl_generics #where_clause {#fields}
		};

		(generated_struct, ident, generics, fields)
	}
}

pub fn component(attr: TokenStream, input: TokenStream) -> TokenStream {
	let mut diagnostics: Vec<Diagnostic> = vec![];

	// parse input
	let input: syn::ItemFn = match syn::parse2(input) {
		Ok(input) => input,
		Err(err) => return err.to_compile_error(),
	};

	let attr: ComponentAttrs = match syn::parse2(attr) {
		Ok(attr) => attr,
		Err(err) => return err.to_compile_error(),
	};

	// check if input is valid
	if let Some(constness) = input.sig.constness {
		diagnostics.push(Diagnostic::spanned(
			constness.span(),
			Level::Error,
			"component function must not be const",
		))
	} else if let Some(asyncness) = input.sig.asyncness {
		diagnostics.push(Diagnostic::spanned(
			asyncness.span(),
			Level::Error,
			"component function must not be async",
		));
	} else if let Some(unsafety) = input.sig.unsafety {
		diagnostics.push(Diagnostic::spanned(
			unsafety.span(),
			Level::Error,
			"component function must not be unsafe",
		));
	} else if let Some(ref abi) = input.sig.abi {
		diagnostics.push(Diagnostic::spanned(
			abi.span(),
			Level::Error,
			"component function must not have an abi",
		));
	}

	let mut struct_builder = ComponentStructBuilder::new(attr, input.sig.generics.clone());

	for arg in input.sig.inputs.iter() {
		match arg {
			FnArg::Receiver(_) => {
				diagnostics.push(Diagnostic::spanned(
					arg.span(),
					Level::Error,
					"component function must not have self argument",
				));
			}

			FnArg::Typed(pat_type) => {
				let pat = *pat_type.pat.clone();
				let ty = *pat_type.ty.clone();

				match pat {
					Pat::Ident(pat) => struct_builder.push_field(pat.ident, ty),

					Pat::TupleStruct(pat) => {
						diagnostics.push(Diagnostic::spanned(
							pat.span(),
							Level::Error,
							"tuple struct pattern not supported",
						));
					}

					Pat::Struct(pat) => {
						diagnostics.push(Diagnostic::spanned(
							pat.span(),
							Level::Error,
							"struct pattern not supported",
						));
					}

					Pat::Tuple(pat) => {
						diagnostics.push(Diagnostic::spanned(
							pat.span(),
							Level::Error,
							"tuple pattern not supported",
						));
					}

					Pat::Slice(pat) => {
						diagnostics.push(Diagnostic::spanned(
							pat.span(),
							Level::Error,
							"slice pattern not supported",
						));
					}

					_ => {
						diagnostics.push(Diagnostic::spanned(
							pat.span(),
							Level::Error,
							"couldn't parse function argument",
						));
					}
				}
			}
		}
	}

	let (generated_struct, ident, generics, fields) = struct_builder.build();

	let input_ident = input.sig.ident.clone();
	let mut fn_args = Vec::new();
	for field in fields.iter() {
		// unwrap shouldn't panic since each field is generated with an ident
		let ident = field.ident.clone().unwrap();
		fn_args.push(quote!(self.#ident,));
	}

	let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

	let impl_block = quote! {
		impl #impl_generics ::rstml_component::HtmlContent for #ident #ty_generics #where_clause {
			fn fmt(self, formatter: &mut ::rstml_component::HtmlFormatter) -> std::fmt::Result {
				formatter.write_content(#input_ident (#(#fn_args)*))
			}
		}
	};

	let diagnostics = diagnostics.iter().map(|d| d.clone().emit_as_item_tokens());
	quote! {
		#(#diagnostics)*
		#input
		#generated_struct
		#impl_block
	}
}