md-tmpl-macros 0.5.2

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
use quote::{format_ident, quote};

use crate::{
    codegen::{codegen_value_as_rust_literal, is_scalar},
    crate_path,
    type_gen::{typed_dict_codegen, typed_enum_codegen, typed_list_codegen, typed_option_codegen},
};

/// A closure that generates a context-setter statement from a value expression.
pub(crate) type SetterFn = Box<dyn Fn(proc_macro2::TokenStream, &str) -> proc_macro2::TokenStream>;

/// Where the template source came from — determines doc strings and the shape
/// of the generated `render()` method.
pub(crate) enum StructGenSource<'a> {
    /// Struct lives inside a module that has a `template()` accessor.
    ///
    /// `render()` takes no template argument and calls `super::template()`
    /// internally. A separate `render_reloaded(tmpl)` method is emitted for
    /// hot-reload use cases.
    ///
    /// Dep tracking is handled by the enclosing module — `generate_struct_tokens`
    /// does NOT emit `include_str!` for this variant.
    Module {
        /// Path shown in doc comments (may be a file path or `<inline>`).
        doc_path: &'a str,
    },
}

/// Generate the `render` / `render_reloaded` method tokens for the impl block.
fn render_method_tokens() -> proc_macro2::TokenStream {
    let cp = crate_path();
    quote! {
        /// Render using the embedded compile-time template.
        ///
        /// This calls the sibling [`template()`] function internally.
        /// For hot-reload scenarios where you load a template from disk
        /// at runtime, use [`render_reloaded()`](Self::render_reloaded)
        /// instead.
        ///
        /// # Errors
        ///
        /// Returns [`TemplateError`] if rendering fails.
        pub fn render(&self) -> ::core::result::Result<#cp::__private::String, #cp::TemplateError> {
            self.render_reloaded(template())
        }

        /// Validate a reloaded template and render with this struct's fields.
        ///
        /// Use this when hot-reloading a template from disk to ensure the
        /// reloaded version is still compatible with this compiled struct.
        ///
        /// # Errors
        ///
        /// Returns [`TemplateError`] if validation or rendering fails.
        pub fn render_reloaded(&self, tmpl: &#cp::Template) -> ::core::result::Result<#cp::__private::String, #cp::TemplateError> {
            Self::validate_template(tmpl)?;
            let ctx = self.to_context();
            tmpl.render_ctx(&ctx)
        }
    }
}

/// Generate the struct definition and impl block from frontmatter declarations.
pub(crate) fn generate_struct_tokens(
    frontmatter: &md_tmpl_core::Frontmatter,
    struct_name: &syn::Ident,
    source: &StructGenSource<'_>,
) -> proc_macro2::TokenStream {
    let struct_name_str = struct_name.to_string();
    let mut sub_structs = Vec::new();
    let mut fields = Vec::new();
    let mut set_stmts = Vec::new();
    let mut expected_vars = Vec::new();

    for decl in &frontmatter.declarations {
        let field_name = format_ident!("{}", decl.name);
        let var_name_str = &decl.name;
        let (field_type, field_set) = var_type_to_rust(
            &decl.var_type,
            &struct_name_str,
            &decl.name,
            &mut sub_structs,
        );

        let builder_attrs = builder_field_attrs(&decl.var_type);
        fields.push(quote! { #builder_attrs pub #field_name: #field_type });
        set_stmts.push(field_set(quote! { self.#field_name }, var_name_str));
        expected_vars.push(var_name_str.clone());
    }

    let const_decls =
        generate_const_decl_tokens(&frontmatter.consts, &struct_name_str, &mut sub_structs);

    let expected_var_lits: Vec<_> = expected_vars.iter().map(|s| quote! { #s }).collect();
    let expected_count = expected_vars.len();

    let StructGenSource::Module { doc_path } = source;
    let path_for_docs = (*doc_path).to_string();
    let doc_attrs = build_struct_docs(frontmatter, &path_for_docs);

    let has_tmpl_fields = frontmatter
        .declarations
        .iter()
        .any(|d| matches!(d.var_type, md_tmpl_core::VarType::Tmpl(_)));
    let derive_attrs = struct_derive_attrs(has_tmpl_fields);

    let render_methods = render_method_tokens();
    let cp = crate_path();

    quote! {

        #(#sub_structs)*

        #(#const_decls)*

        #(#doc_attrs)*
        #derive_attrs
        pub struct #struct_name {
            #(#fields),*
        }

        impl #struct_name {
            /// Expected variable names this struct was generated from.
            const EXPECTED_VARS: [&'static str; #expected_count] = [#(#expected_var_lits),*];

            /// Validate that a template's variable declarations match this struct.
            ///
            /// Use this when hot-reloading a template from disk to ensure the
            /// reloaded version is still compatible with this compiled struct.
            ///
            /// The template body can be edited freely, but the `params:`
            /// block in the frontmatter **must not be changed** — it is part
            /// of the compile-time contract.
            ///
            /// # Errors
            ///
            /// Returns [`TemplateError::DeclarationsMutated`] if the
            /// template's declared variable names don't match the expected
            /// set.
            pub fn validate_template(tmpl: &#cp::Template) -> ::core::result::Result<(), #cp::TemplateError> {
                let mut decl_names: #cp::__private::Vec<&str> = tmpl
                    .declarations()
                    .iter()
                    .map(|d| d.name.as_str())
                    .collect();
                decl_names.sort_unstable();
                let mut expected: #cp::__private::Vec<&str> = Self::EXPECTED_VARS
                    .iter()
                    .copied()
                    .collect();
                expected.sort_unstable();

                if decl_names != expected {
                    let missing: #cp::__private::Vec<&&str> = expected.iter().filter(|v| !decl_names.contains(v)).collect();
                    let extra: #cp::__private::Vec<&&str> = decl_names.iter().filter(|v| !expected.contains(v)).collect();
                    return ::core::result::Result::Err(#cp::TemplateError::DeclarationsMutated {
                        details: #cp::__private::format!(
                            "removed {:?}, added {:?}. \
                             You may edit the template body, but the \
                             frontmatter `params:` block must stay \
                             unchanged",
                            missing, extra
                        ),
                    });
                }
                ::core::result::Result::Ok(())
            }

            /// Convert this struct into a [`Context`](::md_tmpl::Context).
            #[must_use]
            pub fn to_context(&self) -> #cp::Context {
                let mut ctx = #cp::Context::new();
                #(#set_stmts)*
                ctx
            }

            #render_methods
        }
    }
}

/// Generate `const` / `static` declarations for frontmatter `consts:` entries.
pub(crate) fn generate_const_decl_tokens(
    consts: &[md_tmpl_core::VarDecl],
    struct_name_str: &str,
    sub_structs: &mut Vec<proc_macro2::TokenStream>,
) -> Vec<proc_macro2::TokenStream> {
    let cp = crate_path();
    let mut const_decls = Vec::new();
    for decl in consts {
        let const_name = decl.name.to_uppercase();
        let const_ident = format_ident!("{}", const_name);
        let (rust_type, _) =
            var_type_to_rust(&decl.var_type, struct_name_str, &decl.name, sub_structs);

        if let Some(v) = &decl.default_value {
            if is_scalar(&decl.var_type) {
                let (final_type, val_tokens) =
                    if let (md_tmpl_core::Value::Str(s), md_tmpl_core::VarType::Str) =
                        (v, &decl.var_type)
                    {
                        (quote! { &'static str }, quote! { #s })
                    } else {
                        let vt = codegen_value_as_rust_literal(
                            v,
                            &decl.var_type,
                            struct_name_str,
                            &decl.name,
                        );
                        (rust_type, vt)
                    };
                const_decls.push(quote! {
                    pub const #const_ident: #final_type = #val_tokens;
                });
            } else {
                let val_tokens =
                    codegen_value_as_rust_literal(v, &decl.var_type, struct_name_str, &decl.name);
                const_decls.push(quote! {
                    pub static #const_ident: #cp::__private::LazyLock<#rust_type> = #cp::__private::LazyLock::new(|| #val_tokens);
                });
            }
        }
    }
    const_decls
}

/// Build doc-comment attributes from frontmatter metadata.
pub(crate) fn build_struct_docs(
    frontmatter: &md_tmpl_core::Frontmatter,
    path_raw: &str,
) -> Vec<proc_macro2::TokenStream> {
    let tmpl_name = match frontmatter.name.as_deref() {
        Some(name) if !name.is_empty() => name.to_string(),
        _ => path_raw.to_string(),
    };
    let description = match frontmatter.description.as_deref() {
        Some(desc) if !desc.is_empty() => desc.to_string(),
        _ => String::from("(no description)"),
    };

    let mut doc_lines = vec![
        format!("Parameters for the **{tmpl_name}** template."),
        String::new(),
        description,
        String::new(),
        format!("Source: `{path_raw}`"),
        String::new(),
    ];

    if !frontmatter.declarations.is_empty() {
        doc_lines.push("# Variables".to_string());
        doc_lines.push(String::new());
        doc_lines.push("| Name | Type |".to_string());
        doc_lines.push("|------|------|".to_string());
        for decl in &frontmatter.declarations {
            doc_lines.push(format!("| `{}` | `{}` |", decl.name, decl.var_type));
        }
        doc_lines.push(String::new());
    }

    doc_lines.push("Fill in the fields and call [`render()`](Self::render) to produce".to_string());
    doc_lines.push("the rendered template output. Supports hot-reload via".to_string());
    doc_lines.push("[`validate_template()`](Self::validate_template).".to_string());

    doc_lines
        .iter()
        .map(|line| quote! { #[doc = #line] })
        .collect()
}

/// Choose derive attributes for generated structs based on active features.
///
/// `has_tmpl_fields` — when `true`, serde derives are omitted because
/// `Arc<Template>` does not support `Serialize` / `Deserialize`.
///
/// **Feature flag design:** `cfg!(feature = "serde")` and
/// `cfg!(feature = "typed-builder")` check the *proc-macro crate's own*
/// Cargo features, **not** the downstream user's.  These features are
/// intentionally empty in `Cargo.toml` — they carry no dependencies of their
/// own.  Instead, they act as code-generation toggles: when a user enables
/// `serde` on `md-tmpl-macros`, the proc-macro emits
/// `#[derive(Serialize, Deserialize)]` into the generated code, relying on
/// the user's own `serde` dependency to resolve the derive.
pub(crate) fn struct_derive_attrs(has_tmpl_fields: bool) -> proc_macro2::TokenStream {
    let use_serde = cfg!(feature = "serde") && !has_tmpl_fields;
    match (use_serde, cfg!(feature = "typed-builder")) {
        (true, true) => quote! {
            #[derive(Debug, Clone, PartialEq, ::serde::Serialize, ::serde::Deserialize, ::typed_builder::TypedBuilder)]
        },
        (true, false) => quote! {
            #[derive(Debug, Clone, PartialEq, ::serde::Serialize, ::serde::Deserialize)]
        },
        (false, true) => quote! {
            #[derive(Debug, Clone, PartialEq, ::typed_builder::TypedBuilder)]
        },
        (false, false) => quote! {
            #[derive(Debug, Clone, PartialEq)]
        },
    }
}

/// Generate field-level builder attributes based on the variable type.
///
/// - `String` fields get `#[builder(setter(into))]` for ergonomic `"str".into()`.
/// - `Vec<…>` fields get `#[builder(default)]` so they default to empty.
/// - All other fields have no special builder attributes.
pub(crate) fn builder_field_attrs(var_type: &md_tmpl_core::VarType) -> proc_macro2::TokenStream {
    use md_tmpl_core::VarType;
    if !cfg!(feature = "typed-builder") {
        return quote! {};
    }
    match var_type {
        VarType::Str => quote! { #[builder(setter(into))] },
        VarType::List(_) => quote! { #[builder(default, setter(into))] },
        VarType::Enum(_) if var_type.is_option() => quote! { #[builder(default)] },
        _ => quote! {},
    }
}

/// Map a `VarType` to a Rust type token and a setter closure.
pub(crate) fn var_type_to_rust(
    var_type: &md_tmpl_core::VarType,
    parent_struct: &str,
    field_name: &str,
    sub_structs: &mut Vec<proc_macro2::TokenStream>,
) -> (proc_macro2::TokenStream, SetterFn) {
    use md_tmpl_core::VarType;
    let cp = crate_path();

    match var_type {
        VarType::Str => (quote! { String }, simple_setter("as_str()")),
        VarType::Int => (quote! { i64 }, simple_setter("")),
        VarType::Float => (quote! { f64 }, simple_setter("")),
        VarType::Bool => (quote! { bool }, simple_setter("")),
        VarType::List(fields) if !fields.is_empty() => {
            if fields.len() == 1 && fields[0].name.is_empty() {
                let (inner_type, inner_set) =
                    var_type_to_rust(&fields[0].var_type, parent_struct, field_name, sub_structs);
                let cp2 = cp.clone();
                (
                    quote! { #cp::__private::Vec<#inner_type> },
                    Box::new(move |val, name| {
                        let name_lit = name.to_string();
                        let stmts = inner_set(quote! { item }, "item");
                        let cp = &cp2;
                        quote! {
                            ctx.set(#name_lit, #cp::Value::List(#cp::__private::Arc::new(
                                #val.iter().map(|item| {
                                    let mut inner_ctx = #cp::Context::new();
                                    #stmts
                                    inner_ctx.get("item").cloned().unwrap_or(#cp::Value::Str(#cp::__private::String::new()))
                                }).collect()
                            )));
                        }
                    }),
                )
            } else {
                typed_list_codegen(fields, parent_struct, field_name, sub_structs)
            }
        }
        VarType::List(_) => (quote! { Vec<#cp::Value> }, {
            let cp2 = cp.clone();
            Box::new(move |val, name| {
                let name_lit = name.to_string();
                let cp = &cp2;
                quote! { ctx.set(#name_lit, #cp::Value::List(#cp::__private::Arc::new(#val.clone()))); }
            })
        }),
        VarType::Struct(fields) if !fields.is_empty() => {
            typed_dict_codegen(fields, parent_struct, field_name, sub_structs)
        }
        VarType::Struct(_) => (quote! { #cp::Value }, {
            let cp2 = cp.clone();
            Box::new(move |val, name| {
                let name_lit = name.to_string();
                let _cp = &cp2;
                quote! { ctx.set(#name_lit, #val.clone()); }
            })
        }),
        VarType::Enum(_) if var_type.is_option() => {
            typed_option_codegen(var_type, parent_struct, field_name, sub_structs)
        }
        VarType::Enum(variants) => {
            typed_enum_codegen(variants, parent_struct, field_name, sub_structs)
        }
        VarType::Tmpl(_) => (quote! { #cp::__private::Arc<#cp::Template> }, {
            let cp2 = cp.clone();
            Box::new(move |val, name| {
                let name_lit = name.to_string();
                let cp = &cp2;
                quote! { ctx.set(#name_lit, #cp::Value::Tmpl(#val.clone())); }
            })
        }),
        VarType::Option(inner) => {
            // Desugar to enum-style option codegen
            typed_option_codegen(
                &VarType::Option(inner.clone()),
                parent_struct,
                field_name,
                sub_structs,
            )
        }
    }
}

/// Create a setter that calls `ctx.set(name, val)` or `ctx.set(name, val.as_str())`.
pub(crate) fn simple_setter(suffix: &str) -> SetterFn {
    let suffix = suffix.to_string();
    Box::new(move |val, name| {
        let name_lit = name.to_string();
        if suffix.is_empty() {
            quote! { ctx.set(#name_lit, #val); }
        } else {
            // Only case is .as_str() for String fields.
            quote! { ctx.set(#name_lit, #val.as_str()); }
        }
    })
}