lexa-framework-macro 0.2.5

Framework web backend personnel par dessus Axum.
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX)         ┃
// ┃ SPDX-License-Identifier: MPL-2.0                                          ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃                                                                           ┃
// ┃  This Source Code Form is subject to the terms of the Mozilla Public      ┃
// ┃  License, v. 2.0. If a copy of the MPL was not distributed with this      ┃
// ┃  file, You can obtain one at https://mozilla.org/MPL/2.0/.                ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

mod attrs {
	mod for_let;
	mod form_method;
	mod href_inline;
	mod href_method;
	mod if_stmt;
	mod src_inline;
}

use std::{error, fmt};

use diagnostics::SpanDiagnosticExt;
use syn::__private::quote::{quote, quote_spanned};
use syn::__private::{Span, TokenStream, TokenStream2};
use syn::parse::Parse;
use syn::Token;

// -------- //
// Constant //
// -------- //

const VOID_ELEMENTS: [&str; 13] = [
	"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta",
	"source", "track", "wbr",
];

// ---- //
// Type //
// ---- //

pub type HTMLMacroParserInput = TokenStream2;
pub(super) type Result<'err, T> = std::result::Result<T, HTMLMacroParserError>;

// --------- //
// Structure //
// --------- //

pub struct HTMLMacro {
	tokens: HTMLMacroParserInput,
	parser: rstml::Parser,
}

#[derive(Debug)]
#[derive(Clone)]
pub struct HTMLMacroParserError {
	span: syn::__private::Span,
	kind: HTMLMacroParserErrorKind,
}

// ----------- //
// Énumération //
// ----------- //

enum HTMLBlock {
	/// `unsafe for iter()` -> Iterator<Item = DangerousTextNode>
	UnsafeFor(syn::Expr),
	/// `for iter()`
	For(syn::Expr),

	/// `echo expr` -> TextNode
	Echo(syn::Expr),
	/// `json | expr`
	Json(syn::Expr),
	/// `json_pretty | expr`
	JsonPretty(syn::Expr),

	/// `unsafe expr` -> DangerousTextNode
	UnsafeExpr(syn::Expr),
	/// `expr`
	Expr(syn::Expr),
}

pub enum HTMLCustomAttribute {
	/// Syntaxe de l'attribut : `for-let:<identifier>="<iterable>"`
	ForLet,

	/// Syntaxe de l'attribut : `if="bool_expr"`
	If,

	/// Attribut `href:inline`.
	///
	/// Utilisé pour les éléments `LINK`.
	HrefInline,

	/// Attribut  `href:delete`, `href:patch`, `href:post`, `href:put`.
	///
	/// Utilisé pour les éléments `A`.
	HrefDelete,
	HrefPatch,
	HrefPost,
	HrefPut,

	/// Attribut  `method`.
	///
	/// Utilisé pour les éléments `FORM`.
	FormMethod,

	/// Attribut `src:inline`.
	///
	/// Utilisé pour les éléments `SCRIPT`.
	SrcInline,
}

#[derive(Debug)]
#[derive(Clone)]
pub enum HTMLMacroParserErrorKind {
	AttributesRequired {
		attributes: &'static [(&'static str, Option<&'static str>)],
	},

	InvalidTag {
		expected: &'static str,
		found: String,
	},

	BooleanExprExpected,
	AttributeValueIsRequired,
}

// -------------- //
// Implémentation //
// -------------- //

impl HTMLMacro {
	pub fn parse(
		&self,
		nodes: &[rstml::node::Node],
	) -> Result<Vec<TokenStream2>> {
		let mut tokens = vec![];

		for node in nodes {
			use rstml::node::Node;

			let token = match node {
				| Node::Comment(comment_node) => {
					self.parse_comment(comment_node)
				}
				| Node::Doctype(doctype_node) => {
					self.parse_doctype(doctype_node)
				}
				| Node::Fragment(fragment_node) => {
					self.parse_fragment(fragment_node)
				}
				| Node::Element(element_node) => {
					Ok(self.parse_element(element_node)?.into_iter().collect())
				}
				| Node::Block(block_node) => self.parse_block(block_node),
				| Node::Text(text_node) => self.parse_text(text_node),
				| Node::RawText(raw_text) => self.parse_raw_text(raw_text),
			};

			tokens.push(token?);
		}

		Ok(tokens)
	}

	fn parse_comment(
		&self,
		node: &rstml::node::NodeComment,
	) -> Result<TokenStream2> {
		let comment = &node.value;
		Ok(quote! {
			Node::create_comment(#comment)
		})
	}

	fn parse_doctype(
		&self,
		node: &rstml::node::NodeDoctype,
	) -> Result<TokenStream2> {
		let public_identifier = &node.value.to_string_best();
		Ok(quote! {
			Node::create_doctype(#public_identifier)
		})
	}

	fn parse_fragment(
		&self,
		node: &rstml::node::NodeFragment,
	) -> Result<TokenStream2> {
		let html_nodes = self.parse(&node.children)?;
		Ok(tmp::create_fragment(&html_nodes))
	}

	fn parse_element(
		&self,
		node: &rstml::node::NodeElement,
	) -> Result<Vec<TokenStream2>> {
		use rstml::node::NodeAttribute;

		let tag_name = node.open_tag.name.to_string();
		let is_void_element = VOID_ELEMENTS.contains(&tag_name.as_str());

		let children = if is_void_element {
			vec![]
		} else {
			self.parse(&node.children)?
		};

		fn filter_custom_attribute(attr: &NodeAttribute) -> bool {
			match attr {
				| NodeAttribute::Block(_) => false,
				| NodeAttribute::Attribute(attr) => {
					let key = attr.key.to_string();
					key.parse::<HTMLCustomAttribute>().is_ok()
				}
			}
		}

		fn filter_custom_attribute_m(
			attr: &NodeAttribute,
		) -> Option<(&rstml::node::KeyedAttribute, HTMLCustomAttribute)> {
			match attr {
				| NodeAttribute::Block(_) => None,
				| NodeAttribute::Attribute(attr) => {
					let key = attr.key.to_string();
					key.parse::<HTMLCustomAttribute>()
						.map(|custom| (attr, custom))
						.ok()
				}
			}
		}

		let mut tag_attrs: Vec<_> = node
			.attributes()
			.iter()
			.filter(|attr| !filter_custom_attribute(attr))
			.map(|attribute| {
				match attribute {
					| NodeAttribute::Block(block) => {
						quote! {
							Node::from(#[allow(unused_braces)] #block)
						}
					}

					| NodeAttribute::Attribute(attr) => {
						let key = attr.key.to_string();

						let value: Option<(
							&syn::Expr,
							Option<&Box<syn::Type>>,
						)> = attr.value().map(|value_expr| (value_expr, None));

						value.map_or_else(
							|| {
								quote! {(
									#key .to_string(),
									None
								)}
							},
							|(value, ty)| {
								ty.map_or_else(
									|| tmp::create_attribute(&key, value),
									|ty| {
										tmp::create_attribute_from(
											ty, &key, value,
										)
									},
								)
							},
						)
					}
				}
			})
			.collect();

		let contains_custom_attrs: Vec<_> = node
			.attributes()
			.iter()
			.filter_map(|attr| filter_custom_attribute_m(attr))
			.map(|(attr, custom_attr)| {
				match custom_attr {
					| HTMLCustomAttribute::ForLet => {
						self.handle_for_let_attribute(
							&tag_name, &tag_attrs, &children, attr,
						)
					}
					| HTMLCustomAttribute::If => {
						self.handle_if_attribute(
							&tag_name, &tag_attrs, &children, attr,
						)
					}

					| HTMLCustomAttribute::HrefPatch
					| HTMLCustomAttribute::HrefPost
					| HTMLCustomAttribute::HrefPut
					| HTMLCustomAttribute::HrefDelete => {
						self.handle_href_method_attribute(
							node,
							&tag_name,
							tag_attrs.as_mut(),
							attr,
							custom_attr,
						)
					}
					| HTMLCustomAttribute::HrefInline => {
						self.handle_href_inline_attribute(
							node, &tag_name, &tag_attrs, attr,
						)
					}

					| HTMLCustomAttribute::FormMethod => {
						self.handle_form_method_attribute(
							node,
							&tag_name,
							tag_attrs.as_mut(),
							attr,
						)
					}

					| HTMLCustomAttribute::SrcInline => {
						self.handle_src_inline_attribute(
							node, &tag_name, &tag_attrs, attr,
						)
					}
				}
			})
			.collect();

		if !contains_custom_attrs.is_empty() {
			let mut tokens = vec![];

			for custom_attr in contains_custom_attrs {
				tokens.push(custom_attr?);
			}

			return Ok(tokens);
		}

		let element = tmp::create_element(tag_name, &tag_attrs, &children);

		Ok(vec![element])
	}

	pub fn parse_block(
		&self,
		node: &rstml::node::NodeBlock,
	) -> Result<TokenStream2> {
		Ok(quote! {
			Node::from(#[allow(unused_braces)] #node)
		})
	}

	pub fn parse_text(
		&self,
		node: &rstml::node::NodeText,
	) -> Result<TokenStream2> {
		let text = &node.value;

		Ok(quote! {
			Node::create_text(#text)
		})
	}

	pub fn parse_raw_text(
		&self,
		raw_text: &rstml::node::RawText,
	) -> Result<TokenStream2> {
		let text = raw_text.to_string_best();

		Ok(quote! {
			Node::create_text(#text)
		})
	}

	fn transform_block(
		parser: syn::parse::ParseStream,
	) -> core::result::Result<Option<TokenStream2>, syn::Error> {
		let block = parser.parse::<HTMLBlock>()?;

		let tokens = match block {
			| HTMLBlock::UnsafeFor(for_expr) => {
				quote! {
					#for_expr.iter().map(|node| {
						Node::create_unsafe_html(node)
					})
				}
			}
			| HTMLBlock::For(for_expr) => {
				quote! {
					#for_expr . into_iter()
				}
			}

			| HTMLBlock::Echo(text_expr) => {
				quote! {
					std::iter::once(Node::create_text(#text_expr))
				}
			}

			| HTMLBlock::Json(json_expr) => {
				quote! {
					std::iter::once(Node::create_json(
						serde_json::json!(#json_expr)
					))
				}
			}
			| HTMLBlock::JsonPretty(json_expr) => {
				quote! {
					std::iter::once(Node::create_json(
						serde_json::to_string_pretty(&#json_expr)
							.expect("Pretty JSON")
					))
				}
			}

			| HTMLBlock::UnsafeExpr(expr) => {
				quote! {
					std::iter::once(Node::create_unsafe_html(&#expr))
				}
			}
			| HTMLBlock::Expr(expr) => {
				quote! {
					std::iter::once(Node::create_text(#expr))
				}
			}
		};

		Ok(Some(tokens))
	}
}

// -------------- //
// Implémentation // -> Interface
// -------------- //

impl lexa_syn::Parser for HTMLMacro {
	type Err<'err> = HTMLMacroParserError;
	type Input = HTMLMacroParserInput;

	fn new(input: Self::Input) -> Self {
		let config = rstml::ParserConfig::new()
			.always_self_closed_elements({
				<_ as Into<std::collections::HashSet<_>>>::into(VOID_ELEMENTS)
			})
			.transform_block(Self::transform_block);
		let parser = rstml::Parser::new(config);
		Self {
			tokens: input,
			parser,
		}
	}

	fn analyze(&self) -> Result<'_, TokenStream> {
		let (nodes, mut diags) = self
			.parser
			.parse_recoverable(self.tokens.clone())
			.split_vec();

		let maybe_html_nodes = self.parse(&nodes);

		let html_node = match maybe_html_nodes.as_deref() {
			| Ok([html_node]) => quote! { #html_node },
			| Ok(html_nodes) => tmp::create_fragment(html_nodes),
			| Err(err) => {
				diags.push(err.span.error(err.to_string()));
				quote! {}
			}
		};

		let errors = diags
			.into_iter()
			.map(diagnostics::Diagnostic::emit_as_expr_tokens);

		let output = quote! {
			{
				#(#errors;)*
				#html_node
			}
		};

		let output = <TokenStream2 as Into<TokenStream>>::into(output);
		Ok(output)
	}
}

impl Parse for HTMLBlock {
	fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
		// expr
		if input.peek(Token![unsafe]) && input.peek2(Token![for]) {
			input.parse::<Token![unsafe]>()?;
			input.parse::<Token![for]>()?;

			let for_expr = input.parse::<syn::Expr>()?;
			Ok(Self::UnsafeFor(for_expr))
		} else if input.peek(Token![for]) {
			input.parse::<Token![for]>()?;
			let for_expr = input.parse::<syn::Expr>()?;
			Ok(Self::For(for_expr))
		} else if input.peek(Token![unsafe]) {
			input.parse::<Token![unsafe]>()?;
			let expr = input.parse::<syn::Expr>()?;
			Ok(Self::UnsafeExpr(expr))
		} else if input.peek(kw::echo) {
			input.parse::<kw::echo>()?;
			let text_expr = input.parse::<syn::Expr>()?;
			Ok(Self::Echo(text_expr))
		} else if input.peek(kw::json) {
			input.parse::<kw::json>()?;
			input.parse::<Token![|]>()?;
			let json_expr = input.parse::<syn::Expr>()?;
			Ok(Self::Json(json_expr))
		} else if input.peek(kw::json_pretty) {
			input.parse::<kw::json_pretty>()?;
			input.parse::<Token![|]>()?;
			let json_expr = input.parse::<syn::Expr>()?;
			Ok(Self::JsonPretty(json_expr))
		} else {
			let expr = input.parse::<syn::Expr>()?;
			Ok(Self::Expr(expr))
		}
	}
}

// -------------- //
// Implémentation // -> Error
// -------------- //

impl<'err> lexa_syn::ParserError<'err> for HTMLMacroParserError {
	fn compile_error(self) -> TokenStream {
		let err_s = self.to_string();
		let tokens = quote_spanned! {
			self.span() => compile_error!(#err_s);
		};
		TokenStream::from(tokens)
	}

	fn span(self) -> Span {
		self.span
	}
}

impl std::str::FromStr for HTMLCustomAttribute {
	// Osef de l'erreur pour le moment.
	type Err = &'static str;

	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
		Ok(match s.to_ascii_lowercase().as_str() {
			| s if s.contains("for-let:") => Self::ForLet,
			| "if" => Self::If,
			| "href:delete" => Self::HrefDelete,
			| "href:inline" => Self::HrefInline,
			| "href:patch" => Self::HrefPatch,
			| "href:post" => Self::HrefPost,
			| "href:put" => Self::HrefPut,
			| "method" => Self::FormMethod,
			| "src:inline" => Self::SrcInline,
			| _ => return Err("attribut HTML non pris en charge"),
		})
	}
}

// -------------- //
// Implémentation // -> Error
// -------------- //

impl error::Error for HTMLMacroParserError {}

impl fmt::Display for HTMLMacroParserError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let err_s = match &self.kind {
			| HTMLMacroParserErrorKind::AttributesRequired { attributes } => {
				let attrs: Vec<_> = attributes
					.iter()
					.map(|(k, v)| {
						if let Some(v) = v {
							format!("{k}={v}")
						} else {
							k.to_string()
						}
					})
					.collect();
				format!(
					"Les attributs « `{}` » sont requis.",
					attrs.join("`, `")
				)
			}
			| HTMLMacroParserErrorKind::InvalidTag { expected, found } => {
				format!(
					"Une balise « {} » est attendue; trouvée: « {} »",
					expected, found
				)
			}
			| HTMLMacroParserErrorKind::BooleanExprExpected => {
				String::from("Une expression booléenne est attendue.")
			}
			| HTMLMacroParserErrorKind::AttributeValueIsRequired => {
				String::from("Une valeur est obligatoire pour l'attribut.")
			}
		};

		write!(f, "`html!` macro: {}", err_s)
	}
}

mod kw {
	syn::custom_keyword!(echo);
	syn::custom_keyword!(json);
	syn::custom_keyword!(json_pretty);
}

mod tmp {
	use super::*;

	#[inline(always)]
	pub fn create_fragment(
		elements: &[impl syn::__private::ToTokens],
	) -> TokenStream2 {
		quote! {
			Node::create_fragment(vec![#(#elements),*])
		}
	}

	#[inline(always)]
	pub fn create_element(
		tag_name: impl syn::__private::ToTokens,
		tag_attrs: &[impl syn::__private::ToTokens],
		children: &[impl syn::__private::ToTokens],
	) -> TokenStream2 {
		quote! {
			Node::create_element(
				#tag_name,
				vec![#(#tag_attrs),*],
				vec![#(#children),*],
			)
		}
	}

	#[inline(always)]
	pub fn create_void_element(
		tag_name: impl syn::__private::ToTokens,
		tag_attrs: &[impl syn::__private::ToTokens],
	) -> TokenStream2 {
		quote! {
			Node::create_void_element(
				#tag_name,
				vec![#(#tag_attrs),*],
			)
		}
	}

	#[inline(always)]
	pub fn create_attribute(
		key: impl syn::__private::ToTokens,
		value: impl syn::__private::ToTokens,
	) -> TokenStream2 {
		quote! {
			(
				#key .to_string(),
				Some( #value .to_string() )
			)
		}
	}

	#[inline(always)]
	pub fn create_attribute_from(
		ty: impl syn::__private::ToTokens,
		key: impl syn::__private::ToTokens,
		value: impl syn::__private::ToTokens,
	) -> TokenStream2 {
		quote! {
			(
				#key .to_string(),
				Option::<#ty>::from( #value .to_string() )
			)
		}
	}
}