md-tmpl-macros 0.9.3

Proc macros for build-time template validation and pre-parsing
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
use std::path::PathBuf;

use hashbrown::{HashMap, HashSet};

/// Extract the file stem from a template path, stripping `.tmpl.md` or
/// `.tmpl` suffixes.
pub(crate) fn stem_from_path(path: &str) -> String {
    let filename = std::path::Path::new(path)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or(path);
    // Strip known double extensions first.
    filename
        .strip_suffix(".tmpl.md")
        .or_else(|| filename.strip_suffix(".tmpl"))
        .unwrap_or_else(|| {
            // Fallback: strip last extension.
            filename.rsplit_once('.').map_or(filename, |(stem, _)| stem)
        })
        .to_string()
}

pub(crate) fn hash_source(source: &str) -> u64 {
    md_tmpl_core::__private::fnv1a_hash(source.as_bytes())
}

/// Result of compiling a template at macro expansion time.
pub(crate) struct CompiledTemplateAst {
    pub(crate) frontmatter: md_tmpl_core::Frontmatter,
    pub(crate) segments: Vec<md_tmpl_core::compiled::Segment>,
    pub(crate) inline_templates: HashMap<String, md_tmpl_core::compiled::CompiledInlineTemplate>,
    pub(crate) source_hash: u64,
    /// Absolute paths of every file read while compiling this template
    /// (imported and `{% include %}`d templates, transitively). The generated
    /// code emits an `include_str!` for each so Cargo rebuilds when any of
    /// them changes.
    pub(crate) dependency_paths: Vec<PathBuf>,
}

/// Read a template file relative to `CARGO_MANIFEST_DIR`, compile it,
/// and return both the resolved full path and the compiled AST.
pub(crate) fn load_and_compile(
    rel_path: &str,
    env_values: &[(&str, md_tmpl_core::Value)],
) -> Result<(std::path::PathBuf, CompiledTemplateAst), String> {
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
    let full_path = std::path::Path::new(&manifest_dir).join(rel_path);
    let source = std::fs::read_to_string(&full_path)
        .map_err(|e| format!("failed to read template '{}': {e}", full_path.display()))?;
    let base_dir = full_path.parent().unwrap_or(std::path::Path::new("."));
    let ast = compile_template_to_ast(&source, base_dir, env_values)?;
    Ok((full_path, ast))
}

pub(crate) fn compile_template_to_ast(
    source: &str,
    base_dir: &std::path::Path,
    env_values: &[(&str, md_tmpl_core::Value)],
) -> Result<CompiledTemplateAst, String> {
    let source_hash = hash_source(source);
    let (mut fm, body) =
        md_tmpl_core::parse_frontmatter_with_base_dir(source, base_dir, env_values)
            .map_err(|e| e.to_string())?;

    // Track every file read at macro-expansion time so the generated code can
    // emit `include_str!` for each. Without this, Cargo would not rebuild when
    // an imported or `{% include %}`d dependency changes, silently embedding
    // stale content and skipping build-time validation of the edited file.
    let mut dependency_paths: Vec<PathBuf> = Vec::new();
    collect_import_deps(&fm, base_dir, &mut dependency_paths);

    let (mut segments, inline_templates) =
        md_tmpl_core::compiled::compile(body, &fm.type_aliases).map_err(|e| e.to_string())?;

    // Static analysis: Enforce that all parameters referenced in the body are declared.
    check_undeclared_variables(&fm, &inline_templates, &segments)?;

    // Recursively resolve includes at compile time.
    resolve_compile_time_includes(
        &mut segments,
        base_dir,
        &fm,
        &inline_templates,
        &mut dependency_paths,
    )?;

    // Flow-sensitive type check: validate variant names and field access.
    validate_types(&fm, &segments)?;

    // Inject enum type alias constants so that kind(TypeName.Variant)
    // and kinds(TypeName) work at render time. Reuses the same function
    // as the runtime `from_source` path (DRY).
    md_tmpl_core::__private::inject_enum_type_constants(&fm.type_aliases, &mut fm.imported_consts);

    dependency_paths.sort();
    dependency_paths.dedup();

    Ok(CompiledTemplateAst {
        frontmatter: fm,
        segments,
        inline_templates,
        source_hash,
        dependency_paths,
    })
}

/// Record the on-disk paths of a frontmatter's `imports:` entries as build
/// dependencies. Import paths are resolved relative to `base_dir` (matching the
/// core import resolver); absolute paths are recorded as-is.
fn collect_import_deps(
    fm: &md_tmpl_core::Frontmatter,
    base_dir: &std::path::Path,
    deps: &mut Vec<PathBuf>,
) {
    for import in &fm.imports {
        let resolved = if import.path.is_absolute() {
            import.path.clone()
        } else {
            base_dir.join(&import.path)
        };
        deps.push(resolved);
    }
}

/// Check that all parameters referenced in the template body are declared
/// in the frontmatter (params, consts, env, imports, inline templates,
/// or type aliases).
fn check_undeclared_variables(
    fm: &md_tmpl_core::Frontmatter,
    inline_templates: &HashMap<String, md_tmpl_core::compiled::CompiledInlineTemplate>,
    segments: &[md_tmpl_core::compiled::Segment],
) -> Result<(), String> {
    let referenced = md_tmpl_core::compiled::collect_referenced_params(segments);
    let mut declared: HashSet<String> = fm.params.iter().cloned().collect();
    for c in &fm.consts {
        declared.insert(c.name.clone());
    }
    for e in &fm.env {
        declared.insert(e.name.clone());
    }
    for import in &fm.imports {
        declared.insert(import.stem.clone());
    }
    // Inline template names ({% tmpl NAME %}) are valid targets for
    // {% include NAME %} and should not be flagged as undeclared variables.
    for inline_name in inline_templates.keys() {
        declared.insert(inline_name.clone());
    }
    // Type alias names are valid references in kind()/kinds() expressions
    // (e.g., `kind(Status.Active)`, `kinds(Role)`).
    // Enum variant names (e.g. `Active`, `Paused`) appear as unquoted labels
    // in match arms and are collected by the analysis — they must not be
    // flagged as undeclared variables.  We only add top-level enum variants
    // (not recursively nested ones) to avoid masking real typos.
    for (type_name, var_type) in &fm.type_aliases {
        if let md_tmpl_core::VarType::Enum(variants) = var_type {
            declared.insert(type_name.clone());
            for variant in variants {
                declared.insert(variant.name.clone());
            }
        }
    }
    // Variant names from inline enum types on params/consts
    // (e.g. `status = enum(Open, Closed)` without a type alias).
    for decl in fm.declarations.iter().chain(fm.consts.iter()) {
        if let md_tmpl_core::VarType::Enum(variants) = &decl.var_type {
            for variant in variants {
                declared.insert(variant.name.clone());
            }
        }
    }
    // The wildcard `_` in `{% case _ %}` and boolean literals `true`/`false`
    // in `{% case true %}` are pattern syntax, not variable references.
    declared.insert("_".to_string());
    declared.insert("true".to_string());
    declared.insert("false".to_string());
    // `Some` and `None` are option-type sentinels used in `{% case Some %}`
    // and `{% case None %}` arms.
    declared.insert("Some".to_string());
    declared.insert("None".to_string());
    let undeclared: Vec<&String> = referenced
        .iter()
        .filter(|v| !declared.contains(v.as_str()))
        .collect();
    if !undeclared.is_empty() {
        let mut names: Vec<&str> = undeclared.iter().map(|s| s.as_str()).collect();
        names.sort_unstable();
        return Err(format!(
            "undeclared variable(s) referenced in body: {}",
            names.join(", ")
        ));
    }
    Ok(())
}

/// Recursively resolve includes at compile time, collecting declared
/// `tmpl()` parameter names to skip dynamic includes.
fn resolve_compile_time_includes(
    segments: &mut [md_tmpl_core::compiled::Segment],
    base_dir: &std::path::Path,
    fm: &md_tmpl_core::Frontmatter,
    inline_templates: &HashMap<String, md_tmpl_core::compiled::CompiledInlineTemplate>,
    deps: &mut Vec<PathBuf>,
) -> Result<(), String> {
    let tmpl_params: HashSet<String> = fm
        .declarations
        .iter()
        .filter(|d| matches!(d.var_type, md_tmpl_core::VarType::Tmpl(_)))
        .map(|d| d.name.clone())
        .collect();
    let mut visited_paths = HashSet::new();
    resolve_includes_recursive(
        segments,
        base_dir,
        &mut visited_paths,
        inline_templates,
        &tmpl_params,
        deps,
        0,
    )
}

/// Flow-sensitive type check: validate variant names and field access
/// using the full type alias map.
///
/// Delegates to [`md_tmpl_core::Frontmatter::validate_field_types`], the single
/// source of truth shared with the runtime and cross-backend test runners.
fn validate_types(
    fm: &md_tmpl_core::Frontmatter,
    segments: &[md_tmpl_core::compiled::Segment],
) -> Result<(), String> {
    let type_errors = fm.validate_field_types(segments);
    if !type_errors.is_empty() {
        return Err(type_errors.join("\n"));
    }
    Ok(())
}

/// Maximum compile-time include depth. Prevents pathological non-circular
/// chains from causing excessive compilation time. Override with the
/// `MD_TMPL_MAX_INCLUDE_DEPTH` environment variable.
const DEFAULT_MAX_COMPILE_INCLUDE_DEPTH: usize = 64;

pub(crate) fn max_compile_include_depth() -> usize {
    std::env::var("MD_TMPL_MAX_INCLUDE_DEPTH")
        // NOLINT: missing or invalid env var is expected — fall back to compiled default
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(DEFAULT_MAX_COMPILE_INCLUDE_DEPTH)
}

pub(crate) fn resolve_includes_recursive(
    segments: &mut [md_tmpl_core::compiled::Segment],
    base_dir: &std::path::Path,
    visited_paths: &mut HashSet<PathBuf>,
    inline_templates: &HashMap<String, md_tmpl_core::compiled::CompiledInlineTemplate>,
    tmpl_params: &HashSet<String>,
    deps: &mut Vec<PathBuf>,
    depth: usize,
) -> Result<(), String> {
    let max_depth = max_compile_include_depth();
    if depth > max_depth {
        return Err(format!(
            "compile-time include depth ({depth}) exceeds maximum ({max_depth}). \
             Set MD_TMPL_MAX_INCLUDE_DEPTH to increase the limit"
        ));
    }

    for seg in segments {
        match seg {
            md_tmpl_core::compiled::Segment::Include(inc) => {
                // Dynamic tmpl() parameter — resolved at runtime, not compile time.
                if tmpl_params.contains(inc.path.as_ref()) {
                    continue;
                }

                // Check inline templates (scoped to THIS file).
                if let Some(compiled) = inline_templates.get(inc.path.as_ref()) {
                    inc.inline_compiled = Some(compiled.clone());
                    continue;
                }

                let include_path = base_dir.join(inc.path.as_ref());
                let canonical = include_path
                    .canonicalize()
                    .unwrap_or_else(|_| include_path.clone());

                if !visited_paths.insert(canonical.clone()) {
                    // Cycle detected — load declarations for boundary checking
                    // but don't recurse into the body.
                    load_include_declarations(inc, &include_path, deps)?;
                    continue;
                }

                resolve_single_include(inc, base_dir, visited_paths, deps, depth + 1)?;
                visited_paths.remove(&canonical);
            }
            md_tmpl_core::compiled::Segment::ForLoop { body, .. } => {
                resolve_includes_recursive(
                    body,
                    base_dir,
                    visited_paths,
                    inline_templates,
                    tmpl_params,
                    deps,
                    depth,
                )?;
            }
            md_tmpl_core::compiled::Segment::If {
                branches,
                else_body,
            } => {
                for (_, branch_body) in branches {
                    resolve_includes_recursive(
                        branch_body,
                        base_dir,
                        visited_paths,
                        inline_templates,
                        tmpl_params,
                        deps,
                        depth,
                    )?;
                }
                resolve_includes_recursive(
                    else_body,
                    base_dir,
                    visited_paths,
                    inline_templates,
                    tmpl_params,
                    deps,
                    depth,
                )?;
            }
            md_tmpl_core::compiled::Segment::Match { arms, .. } => {
                for arm in arms {
                    resolve_includes_recursive(
                        &mut arm.body,
                        base_dir,
                        visited_paths,
                        inline_templates,
                        tmpl_params,
                        deps,
                        depth,
                    )?;
                }
            }
            md_tmpl_core::compiled::Segment::Static(_)
            | md_tmpl_core::compiled::Segment::Expr { .. }
            | md_tmpl_core::compiled::Segment::Raw(_)
            | md_tmpl_core::compiled::Segment::Comment(_)
            | md_tmpl_core::compiled::Segment::Panic(_) => {}
        }
    }
    Ok(())
}

/// Load and compile an included template file into its `inline_compiled` field.
///
/// Used for the cycle case: we need the declarations for boundary type checking
/// but don't recurse into the body's own includes.
pub(crate) fn load_include_declarations(
    inc: &mut md_tmpl_core::compiled::CompiledInclude,
    include_path: &std::path::Path,
    deps: &mut Vec<PathBuf>,
) -> Result<(), String> {
    if inc.inline_compiled.is_some() {
        return Ok(());
    }
    let included_source = std::fs::read_to_string(include_path)
        .map_err(|e| format!("cannot read include {}: {e}", include_path.display()))?;
    deps.push(include_path.to_path_buf());
    let included_base_dir = include_path.parent().unwrap_or(std::path::Path::new("."));
    let (included_fm, included_body) =
        md_tmpl_core::parse_frontmatter_with_base_dir(&included_source, included_base_dir, &[])
            .map_err(|e| format!("syntax error in include {}: {e}", include_path.display()))?;
    collect_import_deps(&included_fm, included_base_dir, deps);
    let (included_segments, _) =
        md_tmpl_core::compiled::compile(included_body, &included_fm.type_aliases).map_err(|e| {
            format!(
                "compilation error in include {}: {e}",
                include_path.display()
            )
        })?;
    // Build const values map from included file's own consts.
    let mut included_consts = hashbrown::HashMap::new();
    for d in &included_fm.consts {
        if let Some(ref v) = d.default_value {
            included_consts.insert(d.name.clone(), v.clone());
        }
    }
    inc.inline_compiled = Some(md_tmpl_core::compiled::CompiledInlineTemplate {
        segments: std::sync::Arc::from(included_segments),
        declarations: std::sync::Arc::from(included_fm.declarations),
        consts: std::sync::Arc::new(included_consts),
        imported_consts: std::sync::Arc::new(included_fm.imported_consts),
    });
    Ok(())
}

/// Process a single include directive: load, compile, and recurse into
/// the included template's own includes.
///
/// Contract and type checking is now handled by `validate_field_accesses`
/// after all includes are resolved.
pub(crate) fn resolve_single_include(
    inc: &mut md_tmpl_core::compiled::CompiledInclude,
    base_dir: &std::path::Path,
    visited_paths: &mut HashSet<PathBuf>,
    deps: &mut Vec<PathBuf>,
    depth: usize,
) -> Result<(), String> {
    let include_path = base_dir.join(inc.path.as_ref());
    let included_source = std::fs::read_to_string(&include_path)
        .map_err(|e| format!("cannot read include {}: {e}", include_path.display()))?;
    deps.push(include_path.clone());

    let included_base_dir = include_path.parent().unwrap_or(base_dir);
    let (included_fm, included_body) =
        md_tmpl_core::parse_frontmatter_with_base_dir(&included_source, included_base_dir, &[])
            .map_err(|e| format!("syntax error in include {}: {e}", include_path.display()))?;
    collect_import_deps(&included_fm, included_base_dir, deps);

    // Compile the included file and extract ITS OWN inline templates.
    // Each file has its own {% tmpl %} namespace — parent templates do NOT
    // leak into includes, and included file templates don't leak to parents.
    let (mut included_segments, included_inline_templates) =
        md_tmpl_core::compiled::compile(included_body, &included_fm.type_aliases).map_err(|e| {
            format!(
                "compilation error in include {}: {e}",
                include_path.display()
            )
        })?;

    let child_base_dir = include_path.parent().unwrap_or(base_dir);
    // Use the INCLUDED FILE'S own inline templates, not the parent's.
    // Block scope ensures `child_tmpl_params` is dropped before
    // `included_fm.declarations` is moved into an Arc.
    {
        let child_tmpl_params: HashSet<String> = included_fm
            .declarations
            .iter()
            .filter(|d| matches!(d.var_type, md_tmpl_core::VarType::Tmpl(_)))
            .map(|d| d.name.clone())
            .collect();
        resolve_includes_recursive(
            &mut included_segments,
            child_base_dir,
            visited_paths,
            &included_inline_templates,
            &child_tmpl_params,
            deps,
            depth,
        )?;
    }

    // Build const values map from included file's own consts.
    let mut included_consts = hashbrown::HashMap::new();
    for d in &included_fm.consts {
        if let Some(ref v) = d.default_value {
            included_consts.insert(d.name.clone(), v.clone());
        }
    }
    inc.inline_compiled = Some(md_tmpl_core::compiled::CompiledInlineTemplate {
        segments: std::sync::Arc::from(included_segments),
        declarations: std::sync::Arc::from(included_fm.declarations),
        consts: std::sync::Arc::new(included_consts),
        imported_consts: std::sync::Arc::new(included_fm.imported_consts),
    });
    Ok(())
}