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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//! # About
//! t4rust is a minimal templating engine, inspired by the [T4](https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates) syntax.
//!
//! # Example
//! A simple example how to create a template.
//!
//! ```
//! use t4rust_derive::Template;
//!
//! // Add this attribute to use a template
//! #[derive(Template)]
//! // Specify the path to the template file here
//! #[TemplatePath = "./examples/doc_example1.tt"]
//! // Add this attribute if you want to get debug parsing information
//! // This also enables writing temporary files, you might get better error messages.
//! //#[TemplateDebug]
//! struct Example {
//!     // Add fields to the struct you want to use in the template
//!     name: String,
//!     food: String,
//!     num: i32,
//! }
//!
//! fn main() {
//!     // Generate your template by formating it.
//!     let result = format!("{}", Example { name: "Splamy".into(), food: "Cake".into(), num: 3 });
//!     println!("{}", result);
//!#    assert_eq!(result, "Hello From Template!\nMy Name is: Splamy\nI like to eat Cake.\nNum:1\nNum:2\nNum:3\n\n");
//! }
//! ```
//!
//! `doc_example1.tt`:
//! ```text
//! Hello From Template!
//! My Name is: <# write!(_fmt, "{}", self.name)?; #>
//! I like to eat <#= self.food #>.
//! <# for num in 0..self.num { #>Num:<#= num + 1 #>
//! <# } #>
//! ```
//!
//! Output:
//! ```text
//! Hello From Template!
//! My Name is: Splamy
//! I like to eat Cake.
//! Num:1
//! Num:2
//! Num:3
//! ```
//!
//! # Syntax
//!
//! You can simply write rust code within code blocks.
//!
//! Code is written within `<#` and `#>` blocks.
//! If you want to write a `<#` in template text without starting a code block
//! simply write it twice: `<#<#`. Same goes for the `#>` in code blocks.
//! You dont need to duplicate the `<#` within code blocks and `#>` not in
//! template text blocks.
//!
//! You can use `<#= expr #>` to print out a single expression.
//!
//! Maybe you noticed the magical `_fmt` in the template. This variable gives you
//! access to the formatter and e.g. enables you to write functions in your
//! template. `<# write!(_fmt, "{}", self.name)?; #>` is equal to `<#= self.name #>`.
//!
//! **Warning**: Make sure to never create a variable called `_fmt`! You will get
//! weird compiler errors.
//!
//! # Features
//!
//! ## Auto-escaping
//!
//! Use the `escape` directive in your .tt file:
//! ```text
//! <#@ escape function="escape_html" #>`
//! ```
//!
//! And a function with this signature in your code:
//! ```rust
//! fn escape_html(s: &str) -> String {
//!     todo!(); /* Your escaping code here */
//! }
//! ```
//!
//! All expression blocks (e.g. `<#= self.name #>`) will call the escape
//! function before inserted.
//!
//! You can redeclare this directive as many times and where you want in your
//! template to change or disable (with `function=""`) the escape function.

extern crate proc_macro;

use std::collections::hash_map::DefaultHasher;
use std::fs::File;
use std::hash::Hasher;
use std::io::prelude::*;
use std::option::Option;
use std::path::Path;
use std::path::PathBuf;
use std::result::Result;
use std::vec::Vec;

use nom::{
	branch::alt,
	bytes::complete::{
		escaped_transform, is_not, tag, take, take_until, take_while,
	},
	character::complete::{alphanumeric1, line_ending, space0},
	combinator::{map, not, opt, peek},
	multi::many0,
	sequence::tuple,
	IResult,
};
use quote::quote;
use syn::Meta::*;
use syn::*;

use crate::TemplatePart::*;

macro_rules! dbg_println {
	($inf:ident) => { if $inf.debug_print { println!(); } };
	($inf:ident, $fmt:expr) => { if $inf.debug_print { println!($fmt); } };
	($inf:ident, $fmt:expr, $($arg:tt)*) => { if $inf.debug_print { println!($fmt, $($arg)*); } };
}

macro_rules! dbg_print {
	($inf:ident) => { if $inf.debug_print { print!(); } };
	($inf:ident, $fmt:expr) => { if $inf.debug_print { print!($fmt); } };
	($inf:ident, $fmt:expr, $($arg:tt)*) => { if $inf.debug_print { print!($fmt, $($arg)*); } };
}

const TEMPLATE_PATH_MACRO: &str = "TemplatePath";
const TEMPLATE_DEBUG_MACRO: &str = "TemplateDebug";

#[proc_macro_derive(Template, attributes(TemplatePath, TemplateDebug))]
pub fn transform_template(
	input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
	let macro_input = parse_macro_input!(input as DeriveInput);

	let mut path: Option<String> = None;
	let mut info = TemplateInfo::default();

	for attr in macro_input.attrs {
		let meta = attr.parse_meta().expect("Failed to parse t4 attribute");
		match &meta {
			NameValue(MetaNameValue {
				path: p,
				lit: Lit::Str(lit_str),
				..
			}) => {
				if p.get_ident().expect("Attribute with no name")
					== TEMPLATE_PATH_MACRO
				{
					path = Some(lit_str.value());
				}
			}
			Path(name) => {
				if name.get_ident().expect("Attribute with no name")
					== TEMPLATE_DEBUG_MACRO
				{
					info.debug_print = true;
				}
			}
			_ => {}
		}
	}

	// Get template path
	let mut path_absolute =
		PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
	path_absolute.push(&path.unwrap_or_else(|| {
		panic!(
			"Please specify a #[{}=\"<path>\"] atribute with the template \
			 file path.",
			TEMPLATE_PATH_MACRO
		)
	}));
	let path =
		&path_absolute.canonicalize().expect("Could not canonicalize path");
	dbg_println!(
		info,
		"Looking for template in \"{}\"",
		path.to_str().unwrap()
	);

	// Read template file
	let read = read_from_file(path).expect("Could not read file");

	// Parse template file
	let mut data = parse_all(&mut info, &read).expect("Parse failed!");

	if info.debug_print {
		debug_to_file(path, &data);
	}

	parse_postprocess(&mut data);

	let data = parse_optimize(data);

	// Build code from template
	info = TemplateInfo::default();
	let mut builder = String::new();
	for part in data {
		match part {
			Text(x) => {
				builder.push_str(generate_save_str_print(&x).as_ref());
			}
			Code(x) => {
				builder.push_str(x.as_ref());
			}
			Expr(x) => {
				builder.push_str(generate_expression_print(&x, &info).as_ref());
			}
			Directive(dir) => {
				apply_directive(&mut info, &dir);
			}
		}
	}

	dbg_println!(info, "Generated Code:\n{}", builder);

	let tokens: proc_macro2::TokenStream =
		builder.parse().expect("Parsing template code failed!");

	// Build frame and insert
	let (impl_generics, ty_generics, where_clause) =
		macro_input.generics.split_for_impl();
	let name = &macro_input.ident;
	let path_str = path.to_str().expect("Invalid path");

	let frame = quote! {
		impl #impl_generics ::std::fmt::Display for #name #ty_generics #where_clause {
			fn fmt(&self, _fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
				let _ = include_bytes!(#path_str);
				#tokens
				Ok(())
			}
		}
	};

	// We could return the code now. The problem is that span information are
	// missing and the error messages are awful.
	// So instead, we write to a file and include! this file, which still does
	// not give us nice errors but at least includes source code.
	if !info.debug_print {
		proc_macro::TokenStream::from(frame)
	} else {
		// Unfortunately we have no access to OUT_DIR like build scripts so we
		// try to emulate that partially.

		// Use hash of template path as filename
		let mut hasher = DefaultHasher::new();
		hasher.write(path_str.as_bytes());

		let out_dir = if let Ok(target_dir) = std::env::var("CARGO_TARGET_DIR")
		{
			PathBuf::from(target_dir)
		} else {
			let dir = std::env::var("CARGO_MANIFEST_DIR")
				.expect("CARGO_MANIFEST_DIR not set");
			PathBuf::from(dir).join("target")
		};

		let code_path = out_dir
			.join("t4rust")
			.join(&hasher.finish().to_string())
			.with_extension("rs");

		std::fs::create_dir_all(code_path.parent().unwrap())
			.expect("Failed to create output path");

		// Write file
		std::fs::write(&code_path, frame.to_string().as_bytes())
			.expect("Failed to write compiled template");

		let code_path_str = code_path.to_str();
		proc_macro::TokenStream::from(quote! { include!(#code_path_str); })
	}
}

fn generate_expression_print(print_expr: &str, info: &TemplateInfo) -> String {
	if info.print_postprocessor.is_empty() {
		format!("write!(_fmt, \"{{}}\", {})?;\n", print_expr)
	} else {
		format!(
			"{{
			let _s = format!(\"{{}}\", {});
			let _s_transfomed = {}(&_s);
			_fmt.write_str(&_s_transfomed)?;
			}}\n",
			print_expr, info.print_postprocessor
		)
	}
}

fn generate_save_str_print(print_str: &str) -> String {
	let mut max_sharp_count = 0;
	let mut cur_sharp_count = 0;

	for c in print_str.chars() {
		if c == '#' {
			cur_sharp_count += 1;
			max_sharp_count = std::cmp::max(max_sharp_count, cur_sharp_count);
		} else {
			cur_sharp_count = 0;
		}
	}

	let sharps = "#".repeat(max_sharp_count + 1);
	format!("_fmt.write_str(r{1}\"{0}\"{1})?;\n", print_str, sharps)
}

fn read_from_file(path: &Path) -> Result<String, std::io::Error> {
	let mut file = File::open(path)?;
	let mut contents = String::new();
	file.read_to_string(&mut contents)?;
	Ok(contents)
}

fn debug_to_file(path: &Path, data: &[TemplatePart]) {
	let mut pathbuf = PathBuf::new();
	pathbuf.push(path);
	pathbuf.set_extension("tt.out");
	let writepath = pathbuf.as_path();
	if let Ok(mut file) = File::create(writepath) {
		for var in data {
			match *var {
				Code(ref x) => {
					write!(file, "Code:").unwrap();
					file.write_all(x.as_bytes()).unwrap();
				}
				Text(ref x) => {
					write!(file, "Text:").unwrap();
					file.write_all(x.as_bytes()).unwrap();
				}
				Expr(ref x) => {
					write!(file, "Expr:").unwrap();
					file.write_all(x.as_bytes()).unwrap();
				}
				Directive(ref dir) => {
					write!(file, "Dir:{:?}", dir).unwrap();
				}
			}
			writeln!(file).unwrap();
		}
	}
}

/// Transforms template code into an intermediate representation
fn parse_all(
	info: &mut TemplateInfo,
	input: &str,
) -> Result<Vec<TemplatePart>, TemplateError>
{
	let mut builder: Vec<TemplatePart> = Vec::new();
	let mut cur = input;

	dbg_println!(info, "Reading template");

	while !cur.is_empty() {
		let (crest, content) = parse_text(info, cur)?;
		builder.push(Text(content));
		cur = crest;
		dbg_println!(info, "");

		// Read code block
		if let Ok((rest, _)) = expression_start(cur) {
			dbg_print!(info, " expression start");
			let (crest, content) = parse_code(info, rest)?;
			builder.push(Expr(content));
			cur = crest;
		} else if let Ok((rest, _)) = template_directive_start(cur) {
			dbg_print!(info, " directive start");
			let (crest, content) = parse_code(info, rest)?;
			let dir = parse_directive(&content);
			dbg_println!(info, " Directive: {:?}", dir);
			match dir {
				Ok((_, dir)) => {
					apply_directive(info, &dir);
					builder.push(Directive(dir));
				}
				Err(_) => {
					println!("Malformed directive: {}", &content);
					return Err(TemplateError {
						index: 0,
						reason: format!(
							"Could not understand the directive: {}",
							&content
						),
					});
				}
			}
			cur = crest;
		} else if let Ok((rest, _)) = code_start(cur) {
			dbg_print!(info, " code start");
			let (crest, content) = parse_code(info, rest)?;
			builder.push(Code(content));
			cur = crest;
		}

		dbg_println!(info, " Rest: {:?}", &cur);
	}

	dbg_println!(info, "\nTemplate ok!");

	Result::Ok(builder)
}

fn parse_text<'a>(
	info: &TemplateInfo,
	input: &'a str,
) -> Result<(&'a str, String), TemplateError>
{
	let mut content = String::new();
	let mut cur = input;

	loop {
		let read = read_text(cur);
		match read {
			Ok((rest, done)) => {
				content.push_str(&done);
				if rest.is_empty() {
					return Ok((rest, content));
				}
				cur = rest;
				dbg_print!(info, " take text: {:?}", &done);

				if let Ok((rest, _)) = double_code_start(cur) {
					dbg_print!(info, " double-escape");
					content.push_str("<#");

					if rest.is_empty() {
						return Ok((rest, content));
					}
					cur = rest;
				} else if done.is_empty() {
					return Ok((rest, content));
				}
			}
			Err(_) => {
				if let Ok((rest, done)) = till_end(cur) {
					if rest.is_empty() {
						content.push_str(&done);
						return Ok((rest, content));
					}
				}
				panic!(
					"Reached unknown parsing state (!read_text > !till_end)"
				);
			}
		}

		dbg_println!(info, " Rest: {:?}", &cur);
	}
}

fn parse_code<'a>(
	info: &TemplateInfo,
	input: &'a str,
) -> Result<(&'a str, String), TemplateError>
{
	let mut content = String::new();
	let mut cur = input;

	loop {
		match read_code(cur) {
			Ok((rest, done)) => {
				dbg_print!(info, " take code: {:?}", &done);
				content.push_str(&done);
				cur = rest;

				if let Ok((rest, _)) = code_end(cur) {
					dbg_print!(info, " code end");
					return Ok((rest, content));
				} else if let Ok((rest, _)) = double_code_end(cur) {
					dbg_print!(info, " double-escape");
					content.push_str("#>");
					cur = rest;
				} else {
					panic!("Nothing, i guess?");
				}
			}
			Err(err) => {
				dbg_println!(info, "Error at code {:?}", err);
				return Err(TemplateError {
					index: 0,
					reason: "Unclosed code or expression block".into(),
				});
			}
		}
	}
}

/// Merges multiple identical Parts into one
fn parse_optimize(data: Vec<TemplatePart>) -> Vec<TemplatePart> {
	let mut last_type = TemplatePartType::None;
	let mut combined = Vec::<TemplatePart>::new();
	let mut tmp_build = String::new();
	for item in data {
		match item {
			Code(u) => {
				if u.is_empty() {
					continue;
				}
				if last_type != TemplatePartType::Code {
					if !tmp_build.is_empty() {
						match last_type {
							TemplatePartType::None | TemplatePartType::Code => {
								panic!()
							}
							TemplatePartType::Text => {
								combined.push(Text(tmp_build))
							}
							TemplatePartType::Expr => {
								combined.push(Expr(tmp_build))
							}
						}
					}
					tmp_build = String::new();
					last_type = TemplatePartType::Code;
				}
				tmp_build.push_str(&u);
			}
			Text(u) => {
				if u.is_empty() {
					continue;
				}
				if last_type != TemplatePartType::Text {
					if !tmp_build.is_empty() {
						match last_type {
							TemplatePartType::None | TemplatePartType::Text => {
								panic!()
							}
							TemplatePartType::Code => {
								combined.push(Code(tmp_build))
							}
							TemplatePartType::Expr => {
								combined.push(Expr(tmp_build))
							}
						}
					}
					tmp_build = String::new();
					last_type = TemplatePartType::Text;
				}
				tmp_build.push_str(&u);
			}
			Expr(u) => {
				if !tmp_build.is_empty() {
					match last_type {
						TemplatePartType::None => panic!(),
						TemplatePartType::Code => {
							combined.push(Code(tmp_build))
						}
						TemplatePartType::Text => {
							combined.push(Text(tmp_build))
						}
						TemplatePartType::Expr => {
							combined.push(Expr(tmp_build))
						}
					}
				}
				tmp_build = String::new();
				last_type = TemplatePartType::Expr;
				tmp_build.push_str(&u);
			}
			Directive(d) => {
				combined.push(Directive(d));
			}
		}
	}
	if !tmp_build.is_empty() {
		match last_type {
			TemplatePartType::None => {}
			TemplatePartType::Code => combined.push(Code(tmp_build)),
			TemplatePartType::Text => combined.push(Text(tmp_build)),
			TemplatePartType::Expr => combined.push(Expr(tmp_build)),
		}
	}
	combined
}

/// Applies template directives like 'cleanws' and modifies the input
/// accordingly.
fn parse_postprocess(data: &mut Vec<TemplatePart>) {
	let mut info = TemplateInfo::default();
	let mut was_b_clean = None;
	let mut clean_index = 0;

	// if there are less than 3 blocks available we can't do any transformations
	if data.len() < 3 {
		return;
	}

	for i in 0..(data.len() - 2) {
		let tri = data[i..(i + 3)].as_mut();
		if let Directive(ref dir) = tri[1] {
			apply_directive(&mut info, dir);
		}

		if !info.clean_whitespace
			|| !tri[0].is_text()
			|| !tri[1].should_trim_whitespace()
			|| !tri[2].is_text()
		{
			continue;
		}

		let mut res_a = None;
		if clean_index == i && was_b_clean.is_some() {
			res_a = was_b_clean;
		} else if let Text(ref text_a) = tri[0] {
			let rev_txt: String = text_a.chars().rev().collect();
			if let Ok((_, a_len)) = is_ws_till_newline(&rev_txt) {
				res_a = Some(a_len);
			} else if i == 0 && text_a.is_empty() {
				// Start of file
				res_a = Some((0, 0));
			} else {
				continue;
			}
		}

		let mut res_b = None;
		if let Text(ref text_b) = tri[2] {
			if let Ok((_, b_len)) = is_ws_till_newline(&text_b) {
				res_b = Some(b_len);
			} else {
				continue;
			}
		}

		// start trimming

		if let Text(ref mut text_a) = tri[0] {
			let res_a = res_a.unwrap();
			let len = text_a.len();
			text_a.drain((len - (res_a.0))..len);
		}

		if let Text(ref mut text_b) = tri[2] {
			let rev_txt: String = text_b.chars().rev().collect();
			if let Ok((_, b_len)) = is_ws_till_newline(&rev_txt) {
				was_b_clean = Some(b_len);
				clean_index = i + 2;
			}

			let res_b = res_b.unwrap();
			text_b.drain(0..(res_b.0 + res_b.1));
		}
	}
}

fn apply_directive(info: &mut TemplateInfo, directive: &TemplateDirective) {
	for (key, value) in directive
		.params
		.iter()
		.map(|p| ((directive.name.as_str(), p.0.as_str()), p.1.as_str()))
	{
		match key {
			("template", "debug") => {
				info.debug_print = value.parse::<bool>().unwrap()
			}
			("template", "cleanws") | ("template", "clean_whitespace") => {
				info.clean_whitespace = value.parse::<bool>().unwrap()
			}
			("escape", "function") => {
				info.print_postprocessor = value.to_string()
			}
			_ => println!(
				"Unrecognized template parameter \"{}\" in \"{}\"",
				key.0, key.1
			),
		}
	}
}

// NOM DECLARATIONS ===========================================================

fn expression_start(s: &str) -> IResult<&str, &str> { tag("<#=")(s) }
fn template_directive_start(s: &str) -> IResult<&str, &str> { tag("<#@")(s) }
fn read_text(s: &str) -> IResult<&str, &str> { take_until("<#")(s) }

fn code_start(s: &str) -> IResult<&str, &str> {
	let (s, r) = tag("<#")(s)?;
	not(tag("<#"))(s)?;
	Ok((s, r))
}
fn double_code_start(s: &str) -> IResult<&str, &str> { tag("<#<#")(s) }

fn code_end(s: &str) -> IResult<&str, &str> {
	let (s, r) = tag("#>")(s)?;
	not(tag("#>"))(s)?;
	Ok((s, r))
}
fn double_code_end(s: &str) -> IResult<&str, &str> { tag("#>#>")(s) }

fn read_code(s: &str) -> IResult<&str, &str> { take_until("#>")(s) }

fn till_end(s: &str) -> IResult<&str, &str> { take_while(|_| true)(s) }

fn parse_directive(s: &str) -> IResult<&str, TemplateDirective> {
	map(
		tuple((space0, alphanumeric1, many0(parse_directive_param), at_end)),
		|t| TemplateDirective { name: t.1.to_string(), params: t.2 },
	)(s)
}

fn at_end(s: &str) -> IResult<&str, ()> { not(peek(take(1usize)))(s) }

fn parse_directive_param(s: &str) -> IResult<&str, (String, String)> {
	map(
		tuple((
			space0,
			alphanumeric1,
			space0,
			tag("="),
			space0,
			tag("\""),
			opt(escaped_transform(
				is_not("\\\""),
				'\\',
				alt((tag_transform("\\", "\\"), tag_transform("\"", "\""))),
			)),
			tag("\""),
			space0,
		)),
		|t| (t.1.to_string(), t.6.unwrap_or_else(|| "".to_string())),
	)(s)
}

fn is_ws_till_newline(s: &str) -> IResult<&str, (usize, usize)> {
	map(
		tuple((space0, line_ending)),
		|t: (&str, &str)| (t.0.len(), t.1.len()),
	)(s)
}

fn tag_transform<'a>(
	s: &'a str,
	t: &'a str,
) -> impl Fn(&'a str) -> IResult<&str, &str>
{
	move |i: &'a str| {
		let (r, _) = tag(s)(i)?;
		Ok((r, t))
	}
}

// NOM END ====================================================================

#[derive(Debug)]
struct TemplateError {
	reason: String,
	index: usize,
}

#[derive(Debug)]
struct TemplateDirective {
	name: String,
	params: Vec<(String, String)>,
}

#[derive(Debug)]
enum TemplatePart {
	Text(String),
	Code(String),
	Expr(String),
	Directive(TemplateDirective),
}

impl TemplatePart {
	fn is_text(&self) -> bool { matches!(self, Text(_)) }

	/// Whitespace should only be trimmed for code and directive blocks, we want to keep it for
	/// expressions.
	fn should_trim_whitespace(&self) -> bool { matches!(self, Code(_) | Directive(_)) }
}

#[derive(PartialEq)]
enum TemplatePartType {
	None,
	Code,
	Text,
	Expr,
}

#[derive(Debug)]
struct TemplateInfo {
	debug_print: bool,
	clean_whitespace: bool,
	print_postprocessor: String,
}

impl TemplateInfo {
	fn default() -> Self {
		Self {
			debug_print: false,
			clean_whitespace: false,
			print_postprocessor: "".into(),
		}
	}
}