mdt_core 0.9.0

Core engine for mdt — lexer, parser, scanner, and template engine for markdown synchronization
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
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
use std::cmp::Reverse;
use std::collections::HashMap;
use std::collections::HashSet;
use std::hash::BuildHasher;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;

use tracing::debug;
use tracing::instrument;
use tracing::trace;
use tracing::warn;

use crate::Argument;
use crate::BlockType;
use crate::MdtError;
use crate::MdtResult;
use crate::Transformer;
use crate::TransformerType;
use crate::config::PaddingConfig;
use crate::parser::parse_with_diagnostics;
use crate::project::ConsumerEntry;
use crate::project::ProjectContext;
use crate::project::ProviderEntry;
use crate::project::extract_content_between_tags;
use crate::project::is_markdown_path;
use crate::project::normalize_line_endings;
use crate::source_scanner::parse_source_with_diagnostics;

/// A warning about undefined template variables in a provider block.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TemplateWarning {
	/// Path to the file containing the provider block that uses the undefined
	/// variables.
	pub provider_file: PathBuf,
	/// Name of the provider block.
	pub block_name: String,
	/// The undefined variable references found in the template (e.g.,
	/// `["pkgg.version", "typo"]`).
	pub undefined_variables: Vec<String>,
}

/// Result of checking a project for stale consumers.
#[derive(Debug)]
#[non_exhaustive]
pub struct CheckResult {
	/// Consumer entries that are out of date.
	pub stale: Vec<StaleEntry>,
	/// Files whose formatter-normalized full-file output differs even though no
	/// individual consumer content changed.
	pub stale_files: Vec<StaleFileEntry>,
	/// Errors encountered while rendering templates. These are collected
	/// instead of aborting so that the check reports all problems at once.
	pub render_errors: Vec<RenderError>,
	/// Warnings about undefined template variables in provider blocks.
	pub warnings: Vec<TemplateWarning>,
}

impl CheckResult {
	/// Returns true if all consumers are up to date and no errors occurred.
	pub fn is_ok(&self) -> bool {
		self.stale.is_empty() && self.stale_files.is_empty() && self.render_errors.is_empty()
	}

	/// Returns true if there are template render errors.
	pub fn has_errors(&self) -> bool {
		!self.render_errors.is_empty()
	}

	/// Returns true if there are warnings about undefined template variables.
	pub fn has_warnings(&self) -> bool {
		!self.warnings.is_empty()
	}
}

/// A template render error associated with a specific consumer block.
#[derive(Debug)]
#[non_exhaustive]
pub struct RenderError {
	/// Path to the file containing the consumer block.
	pub file: PathBuf,
	/// Name of the block whose template failed to render.
	pub block_name: String,
	/// The error message from the template engine.
	pub message: String,
	/// 1-indexed line number of the consumer's opening tag.
	pub line: usize,
	/// 1-indexed column number of the consumer's opening tag.
	pub column: usize,
}

/// A consumer entry that is out of date.
#[derive(Debug)]
#[non_exhaustive]
pub struct StaleEntry {
	/// Path to the file containing the stale consumer.
	pub file: PathBuf,
	/// Name of the block that is out of date.
	pub block_name: String,
	/// The current content between the consumer's tags.
	pub current_content: String,
	/// The expected content after applying provider content and transformers.
	pub expected_content: String,
	/// 1-indexed line number of the consumer's opening tag.
	pub line: usize,
	/// 1-indexed column number of the consumer's opening tag.
	pub column: usize,
}

/// <!-- {=mdtFormatterOnlyStaleDocs|trim|linePrefix:"/// ":true} -->
/// Formatter-aware checking can also report **formatter-only** drift. This happens when the formatter would rewrite the full file, but no individual managed block body is stale.
///
/// In that case mdt reports the file in `stale_files` so automation can distinguish surrounding-formatting drift from block-content drift. The CLI JSON output and MCP responses include `stale_files` for this reason.
/// <!-- {/mdtFormatterOnlyStaleDocs} -->
#[derive(Debug)]
#[non_exhaustive]
pub struct StaleFileEntry {
	/// Path to the stale file.
	pub file: PathBuf,
	/// The current full file content.
	pub current_content: String,
	/// The expected full file content after formatter normalization.
	pub expected_content: String,
}

/// Result of updating a project.
#[derive(Debug)]
#[non_exhaustive]
pub struct UpdateResult {
	/// Files that were modified and their new content.
	pub updated_files: HashMap<PathBuf, String>,
	/// Number of consumer blocks that were updated.
	pub updated_count: usize,
	/// Warnings about undefined template variables in provider blocks.
	pub warnings: Vec<TemplateWarning>,
}

/// Render provider content through minijinja using the given data context.
/// If data is empty or the content has no template syntax, returns the
/// content unchanged.
#[allow(clippy::implicit_hasher)]
#[instrument(skip(content, data), fields(content_len = content.len(), data_keys = data.len()))]
pub fn render_template(
	content: &str,
	data: &HashMap<String, serde_json::Value>,
) -> MdtResult<String> {
	trace!("rendering template");
	if data.is_empty() || !has_template_syntax(content) {
		return Ok(content.to_string());
	}

	let mut env = minijinja::Environment::new();
	env.set_keep_trailing_newline(true);
	env.set_undefined_behavior(minijinja::UndefinedBehavior::Chainable);
	env.add_template("__inline__", content)
		.map_err(|e| MdtError::TemplateRender(e.to_string()))?;

	let template = env
		.get_template("__inline__")
		.map_err(|e| MdtError::TemplateRender(e.to_string()))?;

	let ctx = minijinja::Value::from_serialize(data);
	template
		.render(ctx)
		.map_err(|e| MdtError::TemplateRender(e.to_string()))
}

/// Find template variables referenced in `content` that are not defined in
/// `data`. Returns the list of undefined variable names (with nested
/// attribute access like `"pkgg.version"`). This uses minijinja's static
/// analysis to detect undeclared variables, so it does not depend on
/// runtime control flow.
///
/// Returns an empty `Vec` when `data` is empty (no data configured means
/// template rendering is a no-op) or when the content has no template
/// syntax.
#[allow(clippy::implicit_hasher)]
pub fn find_undefined_variables(
	content: &str,
	data: &HashMap<String, serde_json::Value>,
) -> Vec<String> {
	if data.is_empty() || !has_template_syntax(content) {
		return Vec::new();
	}

	let mut env = minijinja::Environment::new();
	env.set_keep_trailing_newline(true);
	// We only need the template for static analysis, undefined behavior
	// doesn't affect undeclared_variables.
	let Ok(()) = env.add_template("__inline__", content) else {
		return Vec::new();
	};
	let Ok(template) = env.get_template("__inline__") else {
		return Vec::new();
	};

	// Get all undeclared variables with nested access (e.g., "pkg.version").
	let undeclared: HashSet<String> = template.undeclared_variables(true);

	// Also get top-level names so we can check both "pkg.version" (nested)
	// and "pkg" (top-level).
	let top_level_names: HashSet<String> = data.keys().cloned().collect();

	let mut undefined: Vec<String> = undeclared
		.into_iter()
		.filter(|var| {
			// Extract the top-level namespace from the variable reference.
			let top_level = var.split('.').next().unwrap_or(var);
			// A variable is truly undefined if its top-level namespace is
			// not present in the data context. Variables like "loop" or
			// "range" are minijinja builtins that we should not warn about.
			!top_level_names.contains(top_level) && !is_builtin_variable(top_level)
		})
		.collect();

	undefined.sort();
	undefined
}

/// Check whether a variable name is a minijinja builtin that should not
/// trigger an "undefined variable" warning.
fn is_builtin_variable(name: &str) -> bool {
	matches!(
		name,
		"loop" | "self" | "super" | "true" | "false" | "none" | "namespace" | "range" | "dict"
	)
}

/// Normalize content for lenient comparison by collapsing insignificant
/// whitespace. This makes `mdt check` tolerant of formatter rewrites that
/// only change blank lines, trailing spaces, or indentation alignment.
///
/// The normalization:
/// - trims each line of trailing whitespace
/// - collapses runs of blank lines into a single blank line
/// - trims leading/trailing blank lines from the whole string
pub fn normalize_whitespace(content: &str) -> String {
	let mut result = String::with_capacity(content.len());
	let mut prev_blank = false;

	for line in content.split('\n') {
		let trimmed = line.trim_end();
		let is_blank = trimmed.is_empty();

		if is_blank {
			if !prev_blank && !result.is_empty() {
				result.push('\n');
			}
			prev_blank = true;
		} else {
			if !result.is_empty() {
				result.push('\n');
			}
			result.push_str(trimmed);
			prev_blank = false;
		}
	}

	result
}

/// Compare two content strings, using lenient normalization when the
/// comparison mode is `Lenient`.
fn content_matches(
	actual: &str,
	expected: &str,
	comparison: &crate::config::ComparisonMode,
) -> bool {
	match comparison {
		crate::config::ComparisonMode::Strict => actual == expected,
		crate::config::ComparisonMode::Lenient => {
			normalize_whitespace(actual) == normalize_whitespace(expected)
		}
	}
}

/// Check whether content contains minijinja template syntax.
fn has_template_syntax(content: &str) -> bool {
	content.contains("{{") || content.contains("{%") || content.contains("{#")
}

/// Build a data context that merges base project data with block-specific
/// positional arguments. Consumer argument values are bound to the provider's
/// declared parameter names, with block args taking precedence over data
/// variables.
/// Build a data context that merges base project data with block-specific
/// positional arguments. Returns `None` if the argument count doesn't match.
pub fn build_render_context<S: BuildHasher + Clone>(
	base_data: &HashMap<String, serde_json::Value, S>,
	provider: &ProviderEntry,
	consumer: &ConsumerEntry,
) -> Option<HashMap<String, serde_json::Value, S>> {
	let param_count = provider.block.arguments.len();
	let arg_count = consumer.block.arguments.len();

	if param_count != arg_count && (param_count > 0 || arg_count > 0) {
		return None;
	}

	if provider.block.arguments.is_empty() {
		return Some(base_data.clone());
	}

	let mut data = base_data.clone();
	for (name, value) in provider
		.block
		.arguments
		.iter()
		.zip(consumer.block.arguments.iter())
	{
		data.insert(name.clone(), serde_json::Value::String(value.clone()));
	}
	Some(data)
}

/// Check whether all consumer blocks in the project are up to date.
/// Consumer blocks that reference non-existent providers are silently skipped.
/// Template render errors are collected rather than aborting, so the check
/// reports all problems in a single pass.
#[instrument(skip(ctx), fields(
	root = %ctx.root.display(),
	providers = ctx.project.providers.len(),
	consumers = ctx.project.consumers.len(),
	has_formatters = !ctx.formatters.is_empty(),
))]
pub fn check_project(ctx: &ProjectContext) -> MdtResult<CheckResult> {
	debug!("checking project");
	if ctx.formatters.is_empty() {
		return check_project_without_formatters(ctx);
	}

	let mut stale = Vec::new();
	let mut stale_files = Vec::new();
	let mut render_errors = Vec::new();
	let warnings = collect_template_warnings(ctx);
	debug!(warnings = warnings.len(), "collected template warnings");
	let consumers_by_file = group_consumers_by_file(&ctx.project.consumers);

	for (file, consumers) in consumers_by_file {
		trace!(file = %file.display(), consumers = consumers.len(), "checking file");
		let original = std::fs::read_to_string(&file)?;
		let ordered_consumers = sort_consumers_in_file(consumers);
		let mut candidate = original.clone();
		let mut eligible = vec![false; ordered_consumers.len()];
		let mut raw_expected: Vec<Option<String>> = vec![None; ordered_consumers.len()];

		for (index, consumer) in ordered_consumers.iter().enumerate().rev() {
			match consumer.block.r#type {
				BlockType::Consumer => {
					let Some(provider) = ctx.project.providers.get(&consumer.block.name) else {
						continue;
					};

					let Some(render_data) = build_render_context(&ctx.data, provider, consumer)
					else {
						render_errors.push(RenderError {
							file: consumer.file.clone(),
							block_name: consumer.block.name.clone(),
							message: format!(
								"argument count mismatch: provider `{}` declares {} parameter(s), \
								 but consumer passes {}",
								consumer.block.name,
								provider.block.arguments.len(),
								consumer.block.arguments.len(),
							),
							line: consumer.block.opening.start.line,
							column: consumer.block.opening.start.column,
						});
						continue;
					};
					let rendered = match render_template(&provider.content, &render_data) {
						Ok(rendered) => rendered,
						Err(error) => {
							warn!(
								file = %consumer.file.display(),
								block = %consumer.block.name,
								error = %error,
								"template render failed",
							);
							render_errors.push(RenderError {
								file: consumer.file.clone(),
								block_name: consumer.block.name.clone(),
								message: error.to_string(),
								line: consumer.block.opening.start.line,
								column: consumer.block.opening.start.column,
							});
							continue;
						}
					};
					let mut expected = apply_transformers_with_data(
						&rendered,
						&consumer.block.transformers,
						Some(&render_data),
					);
					if let Some(padding) = &ctx.padding {
						expected = pad_content_with_config(&expected, &consumer.content, padding);
					}
					eligible[index] = true;
					raw_expected[index] = Some(expected.clone());
					if consumer.content != expected {
						replace_consumer_content(&mut candidate, consumer, &expected);
					}
				}
				BlockType::Inline => {
					let Some(template) = consumer.block.arguments.first() else {
						render_errors.push(RenderError {
							file: consumer.file.clone(),
							block_name: consumer.block.name.clone(),
							message: "inline block requires one template argument, e.g. <!-- \
							          {~name:\"{{ pkg.version }}\"} -->"
								.to_string(),
							line: consumer.block.opening.start.line,
							column: consumer.block.opening.start.column,
						});
						continue;
					};
					let rendered = match render_template(template, &ctx.data) {
						Ok(rendered) => rendered,
						Err(error) => {
							render_errors.push(RenderError {
								file: consumer.file.clone(),
								block_name: consumer.block.name.clone(),
								message: error.to_string(),
								line: consumer.block.opening.start.line,
								column: consumer.block.opening.start.column,
							});
							continue;
						}
					};
					let expected = apply_transformers_with_data(
						&rendered,
						&consumer.block.transformers,
						Some(&ctx.data),
					);
					eligible[index] = true;
					raw_expected[index] = Some(expected.clone());
					if consumer.content != expected {
						replace_consumer_content(&mut candidate, consumer, &expected);
					}
				}
				BlockType::Provider => {}
			}
		}

		let (candidate, formatter_commands) = apply_formatter_pipeline(ctx, &file, &candidate)?;
		if formatter_commands.is_empty() {
			for (index, consumer) in ordered_consumers.iter().enumerate() {
				let Some(expected) = raw_expected[index].clone() else {
					continue;
				};
				if !content_matches(&consumer.content, &expected, &ctx.comparison) {
					stale.push(StaleEntry {
						file: consumer.file.clone(),
						block_name: consumer.block.name.clone(),
						current_content: consumer.content.clone(),
						expected_content: expected,
						line: consumer.block.opening.start.line,
						column: consumer.block.opening.start.column,
					});
				}
			}
			continue;
		}

		if candidate == original {
			continue;
		}

		let final_contents = parse_candidate_consumer_contents(
			ctx,
			&file,
			&candidate,
			ordered_consumers.len(),
			&formatter_commands,
		)?;
		let mut file_stale_count = 0;
		for (index, consumer) in ordered_consumers.iter().enumerate() {
			if !eligible[index] {
				continue;
			}
			let expected = final_contents[index].clone();
			if !content_matches(&consumer.content, &expected, &ctx.comparison) {
				file_stale_count += 1;
				stale.push(StaleEntry {
					file: consumer.file.clone(),
					block_name: consumer.block.name.clone(),
					current_content: consumer.content.clone(),
					expected_content: expected,
					line: consumer.block.opening.start.line,
					column: consumer.block.opening.start.column,
				});
			}
		}

		if file_stale_count == 0 {
			stale_files.push(StaleFileEntry {
				file: file.clone(),
				current_content: original,
				expected_content: candidate,
			});
		}
	}

	debug!(
		stale = stale.len(),
		stale_files = stale_files.len(),
		render_errors = render_errors.len(),
		"check complete",
	);

	Ok(CheckResult {
		stale,
		stale_files,
		render_errors,
		warnings,
	})
}

/// Compute the updated file contents for all consumer blocks.
#[instrument(skip(ctx), fields(
	root = %ctx.root.display(),
	providers = ctx.project.providers.len(),
	consumers = ctx.project.consumers.len(),
	has_formatters = !ctx.formatters.is_empty(),
))]
pub fn compute_updates(ctx: &ProjectContext) -> MdtResult<UpdateResult> {
	debug!("computing updates");
	if ctx.formatters.is_empty() {
		return compute_updates_without_formatters(ctx);
	}

	let mut file_contents: HashMap<PathBuf, String> = HashMap::new();
	let mut updated_count = 0;
	let warnings = collect_template_warnings(ctx);
	let consumers_by_file = group_consumers_by_file(&ctx.project.consumers);

	for (file, consumers) in consumers_by_file {
		trace!(file = %file.display(), "processing file for updates");
		let original = std::fs::read_to_string(&file)?;
		let ordered_consumers = sort_consumers_in_file(consumers);
		let mut candidate = original.clone();
		let mut eligible = vec![false; ordered_consumers.len()];
		let mut raw_expected: Vec<Option<String>> = vec![None; ordered_consumers.len()];

		for (index, consumer) in ordered_consumers.iter().enumerate().rev() {
			let new_content = match consumer.block.r#type {
				BlockType::Consumer => {
					let Some(provider) = ctx.project.providers.get(&consumer.block.name) else {
						continue;
					};
					let Some(render_data) = build_render_context(&ctx.data, provider, consumer)
					else {
						continue;
					};
					let rendered = render_template(&provider.content, &render_data)?;
					let mut new_content = apply_transformers_with_data(
						&rendered,
						&consumer.block.transformers,
						Some(&render_data),
					);
					if let Some(padding) = &ctx.padding {
						new_content =
							pad_content_with_config(&new_content, &consumer.content, padding);
					}
					new_content
				}
				BlockType::Inline => {
					let Some(template) = consumer.block.arguments.first() else {
						continue;
					};
					let rendered = render_template(template, &ctx.data)?;
					apply_transformers_with_data(
						&rendered,
						&consumer.block.transformers,
						Some(&ctx.data),
					)
				}
				BlockType::Provider => continue,
			};

			eligible[index] = true;
			raw_expected[index] = Some(new_content.clone());
			if consumer.content != new_content {
				replace_consumer_content(&mut candidate, consumer, &new_content);
			}
		}

		let (candidate, formatter_commands) = apply_formatter_pipeline(ctx, &file, &candidate)?;
		if candidate == original {
			continue;
		}

		if formatter_commands.is_empty() {
			updated_count += ordered_consumers
				.iter()
				.enumerate()
				.filter(|(index, consumer)| {
					raw_expected[*index]
						.as_ref()
						.is_some_and(|expected| consumer.content != *expected)
				})
				.count();
		} else {
			let final_contents = parse_candidate_consumer_contents(
				ctx,
				&file,
				&candidate,
				ordered_consumers.len(),
				&formatter_commands,
			)?;
			updated_count += ordered_consumers
				.iter()
				.enumerate()
				.filter(|(index, consumer)| {
					eligible[*index] && consumer.content != final_contents[*index]
				})
				.count();
		}

		file_contents.insert(file.clone(), candidate);
	}

	debug!(
		updated_files = file_contents.len(),
		updated_count, "updates computed"
	);

	Ok(UpdateResult {
		updated_files: file_contents,
		updated_count,
		warnings,
	})
}

#[allow(clippy::unnecessary_wraps)]
fn check_project_without_formatters(ctx: &ProjectContext) -> MdtResult<CheckResult> {
	let mut stale = Vec::new();
	let mut render_errors = Vec::new();
	let warnings = collect_template_warnings(ctx);

	for consumer in &ctx.project.consumers {
		match consumer.block.r#type {
			BlockType::Consumer => {
				let Some(provider) = ctx.project.providers.get(&consumer.block.name) else {
					continue;
				};

				let Some(render_data) = build_render_context(&ctx.data, provider, consumer) else {
					render_errors.push(RenderError {
						file: consumer.file.clone(),
						block_name: consumer.block.name.clone(),
						message: format!(
							"argument count mismatch: provider `{}` declares {} parameter(s), but \
							 consumer passes {}",
							consumer.block.name,
							provider.block.arguments.len(),
							consumer.block.arguments.len(),
						),
						line: consumer.block.opening.start.line,
						column: consumer.block.opening.start.column,
					});
					continue;
				};
				let rendered = match render_template(&provider.content, &render_data) {
					Ok(rendered) => rendered,
					Err(error) => {
						render_errors.push(RenderError {
							file: consumer.file.clone(),
							block_name: consumer.block.name.clone(),
							message: error.to_string(),
							line: consumer.block.opening.start.line,
							column: consumer.block.opening.start.column,
						});
						continue;
					}
				};
				let mut expected = apply_transformers_with_data(
					&rendered,
					&consumer.block.transformers,
					Some(&render_data),
				);
				if let Some(padding) = &ctx.padding {
					expected = pad_content_with_config(&expected, &consumer.content, padding);
				}

				if !content_matches(&consumer.content, &expected, &ctx.comparison) {
					stale.push(StaleEntry {
						file: consumer.file.clone(),
						block_name: consumer.block.name.clone(),
						current_content: consumer.content.clone(),
						expected_content: expected,
						line: consumer.block.opening.start.line,
						column: consumer.block.opening.start.column,
					});
				}
			}
			BlockType::Inline => {
				let Some(template) = consumer.block.arguments.first() else {
					render_errors.push(RenderError {
						file: consumer.file.clone(),
						block_name: consumer.block.name.clone(),
						message: "inline block requires one template argument, e.g. <!-- \
						          {~name:\"{{ pkg.version }}\"} -->"
							.to_string(),
						line: consumer.block.opening.start.line,
						column: consumer.block.opening.start.column,
					});
					continue;
				};
				let rendered = match render_template(template, &ctx.data) {
					Ok(rendered) => rendered,
					Err(error) => {
						render_errors.push(RenderError {
							file: consumer.file.clone(),
							block_name: consumer.block.name.clone(),
							message: error.to_string(),
							line: consumer.block.opening.start.line,
							column: consumer.block.opening.start.column,
						});
						continue;
					}
				};
				let expected = apply_transformers_with_data(
					&rendered,
					&consumer.block.transformers,
					Some(&ctx.data),
				);

				if !content_matches(&consumer.content, &expected, &ctx.comparison) {
					stale.push(StaleEntry {
						file: consumer.file.clone(),
						block_name: consumer.block.name.clone(),
						current_content: consumer.content.clone(),
						expected_content: expected,
						line: consumer.block.opening.start.line,
						column: consumer.block.opening.start.column,
					});
				}
			}
			BlockType::Provider => {}
		}
	}

	Ok(CheckResult {
		stale,
		stale_files: Vec::new(),
		render_errors,
		warnings,
	})
}

fn compute_updates_without_formatters(ctx: &ProjectContext) -> MdtResult<UpdateResult> {
	let mut file_contents: HashMap<PathBuf, String> = HashMap::new();
	let mut updated_count = 0;
	let warnings = collect_template_warnings(ctx);
	let consumers_by_file = group_consumers_by_file(&ctx.project.consumers);

	for (file, consumers) in &consumers_by_file {
		let original = if let Some(content) = file_contents.get(file) {
			content.clone()
		} else {
			std::fs::read_to_string(file)?
		};

		let mut result = original.clone();
		let mut had_update = false;
		let mut sorted_consumers: Vec<&&ConsumerEntry> = consumers.iter().collect();
		sorted_consumers.sort_by_key(|b| Reverse(b.block.opening.end.offset));

		for consumer in sorted_consumers {
			let new_content = match consumer.block.r#type {
				BlockType::Consumer => {
					let Some(provider) = ctx.project.providers.get(&consumer.block.name) else {
						continue;
					};

					let Some(render_data) = build_render_context(&ctx.data, provider, consumer)
					else {
						continue;
					};
					let rendered = render_template(&provider.content, &render_data)?;
					let mut new_content = apply_transformers_with_data(
						&rendered,
						&consumer.block.transformers,
						Some(&render_data),
					);
					if let Some(padding) = &ctx.padding {
						new_content =
							pad_content_with_config(&new_content, &consumer.content, padding);
					}
					new_content
				}
				BlockType::Inline => {
					let Some(template) = consumer.block.arguments.first() else {
						continue;
					};
					let rendered = render_template(template, &ctx.data)?;
					apply_transformers_with_data(
						&rendered,
						&consumer.block.transformers,
						Some(&ctx.data),
					)
				}
				BlockType::Provider => continue,
			};

			if consumer.content != new_content {
				let start = consumer.block.opening.end.offset;
				let end = consumer.block.closing.start.offset;

				if start <= end && end <= result.len() {
					let mut buf =
						String::with_capacity(result.len() - (end - start) + new_content.len());
					buf.push_str(&result[..start]);
					buf.push_str(&new_content);
					buf.push_str(&result[end..]);
					result = buf;
					had_update = true;
					updated_count += 1;
				}
			}
		}

		if had_update {
			file_contents.insert(file.clone(), result);
		}
	}

	Ok(UpdateResult {
		updated_files: file_contents,
		updated_count,
		warnings,
	})
}

fn group_consumers_by_file(consumers: &[ConsumerEntry]) -> HashMap<PathBuf, Vec<&ConsumerEntry>> {
	let mut grouped: HashMap<PathBuf, Vec<&ConsumerEntry>> = HashMap::new();
	for consumer in consumers {
		grouped
			.entry(consumer.file.clone())
			.or_default()
			.push(consumer);
	}
	grouped
}

fn sort_consumers_in_file(mut consumers: Vec<&ConsumerEntry>) -> Vec<&ConsumerEntry> {
	consumers.sort_by(|a, b| {
		a.block
			.opening
			.start
			.offset
			.cmp(&b.block.opening.start.offset)
	});
	consumers
}

fn replace_consumer_content(result: &mut String, consumer: &ConsumerEntry, new_content: &str) {
	let start = consumer.block.opening.end.offset;
	let end = consumer.block.closing.start.offset;
	if start > end || end > result.len() {
		return;
	}

	let mut buf = String::with_capacity(result.len() - (end - start) + new_content.len());
	buf.push_str(&result[..start]);
	buf.push_str(new_content);
	buf.push_str(&result[end..]);
	*result = buf;
}

fn apply_formatter_pipeline(
	ctx: &ProjectContext,
	file: &Path,
	content: &str,
) -> MdtResult<(String, Vec<String>)> {
	let matching_commands: Vec<String> = ctx
		.formatters
		.iter()
		.filter(|formatter| formatter.matches_file(&ctx.root, file))
		.map(|formatter| formatter.command.clone())
		.collect();

	let mut current = content.to_string();
	for command in &matching_commands {
		current = run_formatter_command(ctx, file, command, &current)?;
	}

	Ok((current, matching_commands))
}

fn run_formatter_command(
	ctx: &ProjectContext,
	file: &Path,
	command: &str,
	input: &str,
) -> MdtResult<String> {
	let relative_file = file.strip_prefix(&ctx.root).unwrap_or(file);
	let interpolated = interpolate_formatter_command(command, file, relative_file, &ctx.root)
		.map_err(|reason| {
			MdtError::Formatter {
				file: relative_file.display().to_string(),
				command: command.to_string(),
				reason,
			}
		})?;
	let mut command_builder = if cfg!(windows) {
		let mut command_builder = Command::new("cmd");
		command_builder.arg("/C").arg(&interpolated);
		command_builder
	} else {
		let mut command_builder = Command::new("sh");
		command_builder.arg("-c").arg(&interpolated);
		command_builder
	};
	let mut child = command_builder
		.current_dir(&ctx.root)
		.stdin(Stdio::piped())
		.stdout(Stdio::piped())
		.stderr(Stdio::piped())
		.spawn()?;

	if let Some(mut stdin) = child.stdin.take() {
		stdin.write_all(input.as_bytes())?;
	}

	let output = child.wait_with_output()?;
	if !output.status.success() {
		let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
		let reason = if stderr.is_empty() {
			format!(
				"command exited with status {}",
				output
					.status
					.code()
					.map_or_else(|| "unknown".to_string(), |code| code.to_string())
			)
		} else {
			stderr
		};
		return Err(MdtError::Formatter {
			file: relative_file.display().to_string(),
			command: interpolated,
			reason,
		});
	}

	Ok(normalize_line_endings(&String::from_utf8_lossy(
		&output.stdout,
	)))
}

fn interpolate_formatter_command(
	command: &str,
	file: &Path,
	relative_file: &Path,
	root: &Path,
) -> Result<String, String> {
	if !has_template_syntax(command) {
		return Ok(command.to_string());
	}

	let mut env = minijinja::Environment::new();
	env.set_keep_trailing_newline(true);
	env.add_template("__formatter_command__", command)
		.map_err(|error| format!("invalid formatter command template: {error}"))?;

	let template = env
		.get_template("__formatter_command__")
		.map_err(|error| format!("invalid formatter command template: {error}"))?;

	template
		.render(minijinja::context! {
			filePath => file.display().to_string(),
			relativeFilePath => relative_file.display().to_string(),
			rootDirectory => root.display().to_string(),
		})
		.map_err(|error| format!("invalid formatter command template: {error}"))
}

fn parse_candidate_consumer_contents(
	ctx: &ProjectContext,
	file: &Path,
	content: &str,
	expected_consumer_count: usize,
	formatter_commands: &[String],
) -> MdtResult<Vec<String>> {
	let normalized = normalize_line_endings(content);
	let (blocks, _) = if is_markdown_path(file) {
		parse_with_diagnostics(&normalized).map_err(|error| {
			MdtError::Formatter {
				file: file
					.strip_prefix(&ctx.root)
					.unwrap_or(file)
					.display()
					.to_string(),
				command: formatter_commands.join(" && "),
				reason: format!("formatter pipeline produced unparsable markdown: {error}"),
			}
		})?
	} else {
		parse_source_with_diagnostics(&normalized, &ctx.markdown_codeblocks).map_err(|error| {
			MdtError::Formatter {
				file: file
					.strip_prefix(&ctx.root)
					.unwrap_or(file)
					.display()
					.to_string(),
				command: formatter_commands.join(" && "),
				reason: format!("formatter pipeline produced unparsable source comments: {error}"),
			}
		})?
	};
	let consumer_contents: Vec<String> = blocks
		.into_iter()
		.filter(|block| matches!(block.r#type, BlockType::Consumer | BlockType::Inline))
		.map(|block| extract_content_between_tags(&normalized, &block))
		.collect();

	if consumer_contents.len() != expected_consumer_count {
		return Err(MdtError::Formatter {
			file: file
				.strip_prefix(&ctx.root)
				.unwrap_or(file)
				.display()
				.to_string(),
			command: formatter_commands.join(" && "),
			reason: format!(
				"formatter pipeline changed the number of consumer blocks from {} to {}",
				expected_consumer_count,
				consumer_contents.len()
			),
		});
	}

	Ok(consumer_contents)
}

/// Collect warnings about undefined template variables across all provider
/// blocks that have at least one consumer. Each provider is checked at most
/// once even if it has multiple consumers.
fn collect_template_warnings(ctx: &ProjectContext) -> Vec<TemplateWarning> {
	let mut warnings = Vec::new();
	let mut checked_providers: HashSet<String> = HashSet::new();

	// Only check providers that are actually referenced by consumers.
	for consumer in &ctx.project.consumers {
		if consumer.block.r#type != BlockType::Consumer {
			continue;
		}
		let name = &consumer.block.name;
		if checked_providers.contains(name) {
			continue;
		}
		checked_providers.insert(name.clone());

		let Some(provider) = ctx.project.providers.get(name) else {
			continue;
		};

		// Provider params are known variables — add them to the data context
		// so they don't trigger false undefined-variable warnings.
		let data_with_params = if provider.block.arguments.is_empty() {
			std::borrow::Cow::Borrowed(&ctx.data)
		} else {
			let mut data = ctx.data.clone();
			for param in &provider.block.arguments {
				data.entry(param.clone())
					.or_insert(serde_json::Value::String(String::new()));
			}
			std::borrow::Cow::Owned(data)
		};

		let undefined = find_undefined_variables(&provider.content, &data_with_params);
		if !undefined.is_empty() {
			warnings.push(TemplateWarning {
				provider_file: provider.file.clone(),
				block_name: name.clone(),
				undefined_variables: undefined,
			});
		}
	}

	warnings
}

/// Write the updated contents back to disk.
#[instrument(skip(updates), fields(file_count = updates.updated_files.len()))]
pub fn write_updates(updates: &UpdateResult) -> MdtResult<()> {
	for (path, content) in &updates.updated_files {
		trace!(path = %path.display(), "writing updated file");
		std::fs::write(path, content)?;
	}
	Ok(())
}

/// Apply a sequence of transformers to content.
#[instrument(skip(content), fields(content_len = content.len(), transformer_count = transformers.len()))]
pub fn apply_transformers(content: &str, transformers: &[Transformer]) -> String {
	apply_transformers_with_data(content, transformers, None)
}

/// Apply a sequence of transformers to content with an optional data context.
/// The data context is used by data-dependent transformers like `if`.
#[allow(clippy::implicit_hasher)]
#[instrument(skip(content, data), fields(content_len = content.len(), transformer_count = transformers.len(), has_data = data.is_some()))]
pub fn apply_transformers_with_data(
	content: &str,
	transformers: &[Transformer],
	data: Option<&HashMap<String, serde_json::Value>>,
) -> String {
	let mut result = content.to_string();

	for transformer in transformers {
		trace!(transformer = ?transformer.r#type, "applying transformer");
		result = apply_transformer(&result, transformer, data);
	}

	result
}

fn apply_transformer(
	content: &str,
	transformer: &Transformer,
	data: Option<&HashMap<String, serde_json::Value>>,
) -> String {
	match transformer.r#type {
		TransformerType::Trim => content.trim().to_string(),
		TransformerType::TrimStart => content.trim_start().to_string(),
		TransformerType::TrimEnd => content.trim_end().to_string(),
		TransformerType::Indent => {
			let indent_str = get_string_arg(&transformer.args, 0).unwrap_or_default();
			let include_empty = get_bool_arg(&transformer.args, 1).unwrap_or(false);
			content
				.lines()
				.map(|line| {
					if line.is_empty() && !include_empty {
						String::new()
					} else {
						format!("{indent_str}{line}")
					}
				})
				.collect::<Vec<_>>()
				.join("\n")
		}
		TransformerType::Prefix => {
			let prefix = get_string_arg(&transformer.args, 0).unwrap_or_default();
			format!("{prefix}{content}")
		}
		TransformerType::Wrap => {
			let wrapper = get_string_arg(&transformer.args, 0).unwrap_or_default();
			format!("{wrapper}{content}{wrapper}")
		}
		TransformerType::CodeBlock => {
			let lang = get_string_arg(&transformer.args, 0).unwrap_or_default();
			format!("```{lang}\n{content}\n```")
		}
		TransformerType::Code => {
			format!("`{content}`")
		}
		TransformerType::Replace => {
			let search = get_string_arg(&transformer.args, 0).unwrap_or_default();
			let replacement = get_string_arg(&transformer.args, 1).unwrap_or_default();
			content.replace(&search, &replacement)
		}
		TransformerType::Suffix => {
			let suffix = get_string_arg(&transformer.args, 0).unwrap_or_default();
			format!("{content}{suffix}")
		}
		TransformerType::LinePrefix => {
			let prefix = get_string_arg(&transformer.args, 0).unwrap_or_default();
			let include_empty = get_bool_arg(&transformer.args, 1).unwrap_or(false);
			content
				.lines()
				.map(|line| {
					if line.is_empty() && !include_empty {
						String::new()
					} else if line.is_empty() {
						prefix.trim_end().to_string()
					} else {
						format!("{prefix}{line}")
					}
				})
				.collect::<Vec<_>>()
				.join("\n")
		}
		TransformerType::LineSuffix => {
			let suffix = get_string_arg(&transformer.args, 0).unwrap_or_default();
			let include_empty = get_bool_arg(&transformer.args, 1).unwrap_or(false);
			content
				.lines()
				.map(|line| {
					if line.is_empty() && !include_empty {
						String::new()
					} else if line.is_empty() {
						suffix.trim_start().to_string()
					} else {
						format!("{line}{suffix}")
					}
				})
				.collect::<Vec<_>>()
				.join("\n")
		}
		TransformerType::If => {
			let path = get_string_arg(&transformer.args, 0).unwrap_or_default();
			if is_data_path_truthy(data, &path) {
				content.to_string()
			} else {
				String::new()
			}
		}
	}
}

/// Look up a dot-separated path in the data context and return whether the
/// value is "truthy". A value is truthy if it exists and is not `false`,
/// `null`, `""`, or `0`.
fn is_data_path_truthy(data: Option<&HashMap<String, serde_json::Value>>, path: &str) -> bool {
	let Some(data) = data else {
		return false;
	};

	let mut parts = path.split('.');
	let Some(root) = parts.next() else {
		return false;
	};

	let Some(mut current) = data.get(root) else {
		return false;
	};

	for part in parts {
		match current {
			serde_json::Value::Object(map) => {
				let Some(next) = map.get(part) else {
					return false;
				};
				current = next;
			}
			_ => return false,
		}
	}

	is_json_value_truthy(current)
}

/// Check whether a JSON value is truthy.
/// A value is falsy if it is `null`, `false`, `""`, `0`, or `0.0`.
/// Everything else (including non-empty arrays and objects) is truthy.
fn is_json_value_truthy(value: &serde_json::Value) -> bool {
	match value {
		serde_json::Value::Null => false,
		serde_json::Value::Bool(b) => *b,
		serde_json::Value::Number(n) => {
			// 0 and 0.0 are falsy
			if let Some(i) = n.as_i64() {
				i != 0
			} else if let Some(u) = n.as_u64() {
				u != 0
			} else if let Some(f) = n.as_f64() {
				f != 0.0
			} else {
				true
			}
		}
		serde_json::Value::String(s) => !s.is_empty(),
		serde_json::Value::Array(_) | serde_json::Value::Object(_) => true,
	}
}

/// Validate that all transformer arguments are well-formed. Returns an error
/// for the first invalid transformer found.
pub fn validate_transformers(transformers: &[Transformer]) -> MdtResult<()> {
	for t in transformers {
		let (min, max) = match t.r#type {
			TransformerType::Trim
			| TransformerType::TrimStart
			| TransformerType::TrimEnd
			| TransformerType::Code => (0, 0),
			TransformerType::Prefix
			| TransformerType::Suffix
			| TransformerType::Wrap
			| TransformerType::CodeBlock => (0, 1),
			TransformerType::Indent | TransformerType::LinePrefix | TransformerType::LineSuffix => {
				(0, 2)
			}
			TransformerType::Replace => (2, 2),
			TransformerType::If => (1, 1),
		};

		if t.args.len() < min || t.args.len() > max {
			let expected = if min == max {
				format!("{min}")
			} else {
				format!("{min}-{max}")
			};
			return Err(MdtError::InvalidTransformerArgs {
				name: t.r#type.to_string(),
				expected,
				got: t.args.len(),
			});
		}
	}
	Ok(())
}

/// Pad content according to the padding configuration while preserving the
/// trailing line prefix from the original consumer content. When the closing
/// tag is preceded by a comment prefix (e.g., `//! ` or `/// `) that prefix
/// is part of the content range and must be preserved after replacement.
///
/// The `before` value controls blank lines between the opening tag and
/// content, and `after` controls blank lines between content and the closing
/// tag. Each value can be:
///
/// - `false` — No padding; content appears inline with the tag.
/// - `0` — Content on the very next line (one newline, no blank lines).
/// - `1` — One blank line between the tag and content.
/// - `2` — Two blank lines, and so on.
fn pad_content_with_config(
	new_content: &str,
	original_content: &str,
	padding: &PaddingConfig,
) -> String {
	// Extract the trailing prefix from the original content — everything after
	// the last newline. For example, in "\n//! old\n//! " the trailing prefix
	// is "//! ".
	let trailing_prefix = original_content
		.rfind('\n')
		.map_or("", |idx| &original_content[idx + 1..]);
	// Trimmed prefix for blank padding lines — avoids trailing whitespace
	// on empty lines (e.g., "//! " becomes "//!").
	let blank_line_prefix = trailing_prefix.trim_end();

	let mut result = String::with_capacity(new_content.len() + trailing_prefix.len() * 4 + 8);

	// Before padding: lines between opening tag and content
	match padding.before.line_count() {
		None => {
			// false — content inline with opening tag
		}
		Some(0) => {
			// Content on the very next line
			if !new_content.starts_with('\n') {
				result.push('\n');
			}
		}
		Some(n) => {
			// N blank lines between opening tag and content
			if !new_content.starts_with('\n') {
				result.push('\n');
			}
			for _ in 0..n {
				result.push_str(blank_line_prefix);
				result.push('\n');
			}
		}
	}

	result.push_str(new_content);

	// After padding: lines between content and closing tag
	match padding.after.line_count() {
		None => {
			// false — closing tag inline with content
		}
		Some(0) => {
			// Closing tag on the very next line
			if !new_content.ends_with('\n') {
				result.push('\n');
			}
			result.push_str(trailing_prefix);
		}
		Some(n) => {
			if !new_content.ends_with('\n') {
				result.push('\n');
			}
			for _ in 0..n {
				result.push_str(blank_line_prefix);
				result.push('\n');
			}
			result.push_str(trailing_prefix);
		}
	}

	result
}

fn get_string_arg(args: &[Argument], index: usize) -> Option<String> {
	args.get(index).map(|arg| {
		match arg {
			Argument::String(s) => s.clone(),
			Argument::Number(n) => n.to_string(),
			Argument::Boolean(b) => b.to_string(),
		}
	})
}

fn get_bool_arg(args: &[Argument], index: usize) -> Option<bool> {
	args.get(index).map(|arg| {
		match arg {
			Argument::Boolean(b) => *b,
			Argument::String(s) => s == "true",
			Argument::Number(n) => n.0 != 0.0,
		}
	})
}