sigil-stitch 0.2.0

Type-safe, import-aware, width-aware code generation for multiple languages
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
//! Reusable parameterized code templates.
//!
//! `CodeTemplate` provides named parameters on top of `CodeBlock`'s positional
//! format strings. Templates use `#{name:K}` syntax where `K` is one of:
//!
//! | Kind | Specifier | Argument Type |
//! |------|-----------|---------------|
//! | `T`  | `%T`      | `TypeName<L>` |
//! | `N`  | `%N`      | `NameArg`     |
//! | `S`  | `%S`      | `StringLitArg`|
//! | `L`  | `%L`      | `&str`, `String`, or `CodeBlock<L>` |
//!
//! # Example
//!
//! ```rust
//! use sigil_stitch::code_template::CodeTemplate;
//! use sigil_stitch::code_block::NameArg;
//! use sigil_stitch::lang::typescript::TypeScript;
//! use sigil_stitch::type_name::TypeName;
//!
//! let tmpl = CodeTemplate::new("const #{var:N}: #{type:T} = #{init:L}").unwrap();
//! let user_type = TypeName::<TypeScript>::primitive("string");
//!
//! let block = tmpl.apply::<TypeScript>()
//!     .set("var", NameArg("user".into()))
//!     .set("type", user_type)
//!     .set("init", "null")
//!     .build()
//!     .unwrap();
//! ```

use std::collections::HashMap;
use std::marker::PhantomData;

use crate::code_block::{Arg, CodeBlock};
use crate::lang::CodeLang;

/// The kind of a template parameter, matching the format specifier system.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamKind {
    /// Type reference (`%T`). Expects `Arg::TypeName`.
    Type,
    /// Name identifier (`%N`). Expects `Arg::Name`.
    Name,
    /// String literal (`%S`). Expects `Arg::StringLit`.
    StringLit,
    /// Literal or nested code block (`%L`). Expects `Arg::Literal` or `Arg::Code`.
    Literal,
}

impl ParamKind {
    fn specifier(self) -> &'static str {
        match self {
            ParamKind::Type => "%T",
            ParamKind::Name => "%N",
            ParamKind::StringLit => "%S",
            ParamKind::Literal => "%L",
        }
    }

    fn from_char(c: u8) -> Option<Self> {
        match c {
            b'T' => Some(ParamKind::Type),
            b'N' => Some(ParamKind::Name),
            b'S' => Some(ParamKind::StringLit),
            b'L' => Some(ParamKind::Literal),
            _ => None,
        }
    }

    fn matches_arg<L: CodeLang>(&self, arg: &Arg<L>) -> bool {
        matches!(
            (self, arg),
            (ParamKind::Type, Arg::TypeName(_))
                | (ParamKind::Name, Arg::Name(_))
                | (ParamKind::StringLit, Arg::StringLit(_))
                | (ParamKind::Literal, Arg::Literal(_) | Arg::Code(_))
        )
    }

    fn label(self) -> &'static str {
        match self {
            ParamKind::Type => "Type",
            ParamKind::Name => "Name",
            ParamKind::StringLit => "StringLit",
            ParamKind::Literal => "Literal",
        }
    }
}

/// A parsed named parameter from the template format string.
#[derive(Debug, Clone)]
struct TemplateParam {
    name: String,
    kind: ParamKind,
}

/// A reusable, parameterized code template.
///
/// Templates are language-agnostic format string patterns using `#{name:K}`
/// syntax for named parameters. The language parameter `L` enters at
/// [`apply`](CodeTemplate::apply) time when concrete arguments are provided.
/// This allows the same template to be reused across different target languages.
///
/// Duplicate parameter names are allowed -- the same value is used at each
/// occurrence.
///
/// # Examples
///
/// ```
/// use sigil_stitch::code_template::CodeTemplate;
/// use sigil_stitch::code_block::NameArg;
/// use sigil_stitch::lang::typescript::TypeScript;
/// use sigil_stitch::type_name::TypeName;
///
/// let tmpl = CodeTemplate::new("const #{var:N}: #{type:T} = #{init:L}").unwrap();
///
/// let block = tmpl.apply::<TypeScript>()
///     .set("var", NameArg("user".into()))
///     .set("type", TypeName::<TypeScript>::primitive("string"))
///     .set("init", "null")
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct CodeTemplate {
    /// Original template format string (for error messages).
    source: String,
    /// The positional format string with `#{...}` replaced by `%T`/`%N`/`%S`/`%L`.
    positional_format: String,
    /// Ordered list of parameters as they appear in the format string.
    params: Vec<TemplateParam>,
}

impl CodeTemplate {
    /// Parse a template format string with `#{name:K}` named parameters.
    ///
    /// Returns `Err` if the syntax is invalid (unclosed `#{}`, unknown kind
    /// letter, or bare positional specifiers like `%T`).
    pub fn new(format: &str) -> Result<Self, crate::error::SigilStitchError> {
        let (positional_format, params) = parse_template(format)?;
        Ok(CodeTemplate {
            source: format.to_string(),
            positional_format,
            params,
        })
    }

    /// Begin applying this template with concrete arguments.
    pub fn apply<L: CodeLang>(&self) -> TemplateApply<'_, L> {
        TemplateApply {
            template: self,
            args: HashMap::new(),
            _phantom: PhantomData,
        }
    }

    /// Get the parameter names and their kinds (for introspection).
    pub fn param_names(&self) -> Vec<(&str, ParamKind)> {
        let mut seen = std::collections::HashSet::new();
        self.params
            .iter()
            .filter(|p| seen.insert(&p.name))
            .map(|p| (p.name.as_str(), p.kind))
            .collect()
    }
}

/// Builder for applying a [`CodeTemplate`] with concrete arguments.
///
/// Created by [`CodeTemplate::apply()`]. Set named parameters with `set()`,
/// then call `build()` to produce a `CodeBlock`.
pub struct TemplateApply<'t, L: CodeLang> {
    template: &'t CodeTemplate,
    args: HashMap<String, Arg<L>>,
    _phantom: PhantomData<L>,
}

impl<L: CodeLang> TemplateApply<'_, L> {
    /// Set a named parameter value.
    ///
    /// The argument kind is validated against the declared parameter kind
    /// in [`build`](TemplateApply::build).
    pub fn set(&mut self, name: &str, arg: impl Into<Arg<L>>) -> &mut Self {
        self.args.insert(name.to_string(), arg.into());
        self
    }

    /// Build the `CodeBlock`, validating all parameters are provided and
    /// kind-correct.
    pub fn build(&mut self) -> Result<CodeBlock<L>, crate::error::SigilStitchError> {
        let mut positional_args: Vec<Arg<L>> = Vec::with_capacity(self.template.params.len());

        for param in &self.template.params {
            let arg = self.args.get(&param.name).ok_or_else(|| {
                crate::error::SigilStitchError::Template {
                    message: format!(
                        "Template {:?}: missing parameter '{}'",
                        self.template.source, param.name
                    ),
                }
            })?;

            if !param.kind.matches_arg(arg) {
                return Err(crate::error::SigilStitchError::Template {
                    message: format!(
                        "Template {:?}: parameter '{}' declared as {} but received {:?}",
                        self.template.source,
                        param.name,
                        param.kind.label(),
                        arg_kind_label(arg),
                    ),
                });
            }

            positional_args.push(arg.clone());
        }

        CodeBlock::of(&self.template.positional_format, positional_args)
    }
}

fn arg_kind_label<L: CodeLang>(arg: &Arg<L>) -> &'static str {
    match arg {
        Arg::TypeName(_) => "TypeName",
        Arg::Name(_) => "Name",
        Arg::StringLit(_) => "StringLit",
        Arg::Literal(_) => "Literal",
        Arg::Code(_) => "Code",
    }
}

// ── Template parser ─────────────────────────────────────

fn template_err(message: String) -> crate::error::SigilStitchError {
    crate::error::SigilStitchError::Template { message }
}

/// Parse a template format string, replacing `#{name:K}` with positional
/// specifiers and collecting parameter declarations.
fn parse_template(
    format: &str,
) -> Result<(String, Vec<TemplateParam>), crate::error::SigilStitchError> {
    let mut output = String::with_capacity(format.len());
    let mut params = Vec::new();
    let bytes = format.as_bytes();
    let mut i = 0;

    while i < bytes.len() {
        // Check for bare positional arg-consuming specifiers.
        if bytes[i] == b'%' && i + 1 < bytes.len() {
            let spec = bytes[i + 1];
            match spec {
                // Arg-consuming specifiers are forbidden in templates.
                b'T' | b'N' | b'S' | b'L' => {
                    return Err(template_err(format!(
                        "Template format string contains bare positional specifier '%{}' \
                         at byte {}; use #{{name:{}}} syntax instead",
                        spec as char, i, spec as char,
                    )));
                }
                // Non-arg specifiers pass through.
                b'W' | b'>' | b'<' | b'[' | b']' | b'%' => {
                    output.push('%');
                    output.push(spec as char);
                    i += 2;
                }
                _ => {
                    // Unknown %-specifier, pass through as literal.
                    output.push('%');
                    output.push(spec as char);
                    i += 2;
                }
            }
            continue;
        }

        // Check for `#` sequences.
        if bytes[i] == b'#' {
            // `##` → literal `#`.
            if i + 1 < bytes.len() && bytes[i + 1] == b'#' {
                output.push('#');
                i += 2;
                continue;
            }

            // `#{...}` → named parameter.
            if i + 1 < bytes.len() && bytes[i + 1] == b'{' {
                let start = i;
                let close = format[i + 2..]
                    .find('}')
                    .ok_or_else(|| template_err(format!("Unclosed '#{{' at byte {start}")))?;
                let inner = &format[i + 2..i + 2 + close];

                // Parse "name:K".
                let colon = inner.find(':').ok_or_else(|| {
                    template_err(format!(
                        "Expected '#{{name:K}}' but found '#{{{}}}' at byte {} \
                         (missing ':' separator)",
                        inner, start
                    ))
                })?;
                let name = &inner[..colon];
                let kind_str = &inner[colon + 1..];

                if name.is_empty() {
                    return Err(template_err(format!(
                        "Empty parameter name in '#{{{}}}' at byte {}",
                        inner, start
                    )));
                }
                if kind_str.len() != 1 {
                    return Err(template_err(format!(
                        "Expected single kind letter (T/N/S/L) but found '{}' in '#{{{}}}' at byte {}",
                        kind_str, inner, start
                    )));
                }

                let kind = ParamKind::from_char(kind_str.as_bytes()[0]).ok_or_else(|| {
                    template_err(format!(
                        "Unknown parameter kind '{}' in '#{{{}}}' at byte {} \
                         (expected T, N, S, or L)",
                        kind_str, inner, start
                    ))
                })?;

                // Check duplicate names have consistent kinds.
                if let Some(existing) = params.iter().find(|p: &&TemplateParam| p.name == name)
                    && existing.kind != kind
                {
                    return Err(template_err(format!(
                        "Parameter '{}' declared as {} at byte {} but previously declared as {}",
                        name,
                        kind.label(),
                        start,
                        existing.kind.label(),
                    )));
                }

                output.push_str(kind.specifier());
                params.push(TemplateParam {
                    name: name.to_string(),
                    kind,
                });

                i = i + 2 + close + 1; // skip past '}'
                continue;
            }

            // Bare `#` followed by anything else → literal.
            output.push('#');
            i += 1;
            continue;
        }

        // Regular character.
        output.push(bytes[i] as char);
        i += 1;
    }

    Ok((output, params))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::code_block::NameArg;
    use crate::code_block::StringLitArg;
    use crate::import_collector;
    use crate::lang::rust_lang::RustLang;
    use crate::lang::typescript::TypeScript;
    use crate::type_name::TypeName;

    #[test]
    fn test_parse_simple_template() {
        let tmpl = CodeTemplate::new("const #{var:N}: #{type:T} = #{init:L}").unwrap();
        assert_eq!(tmpl.positional_format, "const %N: %T = %L");
        let names = tmpl.param_names();
        assert_eq!(names.len(), 3);
        assert_eq!(names[0], ("var", ParamKind::Name));
        assert_eq!(names[1], ("type", ParamKind::Type));
        assert_eq!(names[2], ("init", ParamKind::Literal));
    }

    #[test]
    fn test_apply_simple() {
        let tmpl = CodeTemplate::new("const #{var:N}: #{type:T} = #{init:L}").unwrap();
        let ty = TypeName::<TypeScript>::primitive("string");
        let block = tmpl
            .apply::<TypeScript>()
            .set("var", NameArg("user".into()))
            .set("type", ty)
            .set("init", "null")
            .build()
            .unwrap();
        assert!(!block.is_empty());
    }

    #[test]
    fn test_missing_param_error() {
        let tmpl = CodeTemplate::new("#{a:N} #{b:T}").unwrap();
        let result = tmpl
            .apply::<TypeScript>()
            .set("a", NameArg("x".into()))
            .build();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("missing parameter 'b'"),
            "got: {err}"
        );
    }

    #[test]
    fn test_kind_mismatch_error() {
        let tmpl = CodeTemplate::new("#{name:N}").unwrap();
        // Pass a TypeName where Name is expected.
        let result = tmpl
            .apply::<TypeScript>()
            .set("name", TypeName::<TypeScript>::primitive("string"))
            .build();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("declared as Name"), "got: {err}");
        assert!(err.to_string().contains("TypeName"), "got: {err}");
    }

    #[test]
    fn test_duplicate_param_name() {
        let tmpl = CodeTemplate::new("#{t:T} and #{t:T}").unwrap();
        let ty = TypeName::<TypeScript>::primitive("number");
        let block = tmpl.apply::<TypeScript>().set("t", ty).build().unwrap();
        assert!(!block.is_empty());
        // Should have 2 args (one for each occurrence).
        assert_eq!(block.args.len(), 2);
    }

    #[test]
    fn test_escaped_hash() {
        let tmpl = CodeTemplate::new("##[derive(Debug)]").unwrap();
        assert_eq!(tmpl.positional_format, "#[derive(Debug)]");
        assert!(tmpl.params.is_empty());
    }

    #[test]
    fn test_bare_hash_passthrough() {
        let tmpl = CodeTemplate::new("# comment").unwrap();
        assert_eq!(tmpl.positional_format, "# comment");
        assert!(tmpl.params.is_empty());
    }

    #[test]
    fn test_reject_bare_positional_specifiers() {
        let result = CodeTemplate::new("#{name:N} %T");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("bare positional specifier '%T'"),
            "got: {err}"
        );
    }

    #[test]
    fn test_allow_non_arg_specifiers() {
        let tmpl = CodeTemplate::new("#{a:N}%W#{b:L}").unwrap();
        assert_eq!(tmpl.positional_format, "%N%W%L");
        assert_eq!(tmpl.params.len(), 2);
    }

    #[test]
    fn test_allow_percent_escape() {
        let tmpl = CodeTemplate::new("100%%").unwrap();
        assert_eq!(tmpl.positional_format, "100%%");
    }

    #[test]
    fn test_allow_indent_dedent_specifiers() {
        let tmpl = CodeTemplate::new("%>#{body:L}%<").unwrap();
        assert_eq!(tmpl.positional_format, "%>%L%<");
    }

    #[test]
    fn test_template_with_imports() {
        let tmpl = CodeTemplate::new("const x: #{type:T} = init()").unwrap();
        let ty = TypeName::<TypeScript>::importable_type("./models", "User");
        let block = tmpl.apply::<TypeScript>().set("type", ty).build().unwrap();
        let refs = import_collector::collect_imports(&block);
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].name, "User");
    }

    #[test]
    fn test_language_agnostic() {
        let tmpl = CodeTemplate::new("#{name:N}: #{type:T}").unwrap();

        // Apply to TypeScript.
        let ts_block = tmpl
            .apply::<TypeScript>()
            .set("name", NameArg("x".into()))
            .set("type", TypeName::<TypeScript>::primitive("string"))
            .build()
            .unwrap();
        assert!(!ts_block.is_empty());

        // Same template applied to Rust.
        let rs_block = tmpl
            .apply::<RustLang>()
            .set("name", NameArg("x".into()))
            .set("type", TypeName::<RustLang>::primitive("String"))
            .build()
            .unwrap();
        assert!(!rs_block.is_empty());
    }

    #[test]
    fn test_unclosed_brace_error() {
        let result = CodeTemplate::new("#{name:T");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Unclosed"));
    }

    #[test]
    fn test_missing_colon_error() {
        let result = CodeTemplate::new("#{name}");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("missing ':'"));
    }

    #[test]
    fn test_unknown_kind_error() {
        let result = CodeTemplate::new("#{name:X}");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Unknown parameter kind")
        );
    }

    #[test]
    fn test_empty_name_error() {
        let result = CodeTemplate::new("#{:T}");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Empty parameter name")
        );
    }

    #[test]
    fn test_inconsistent_duplicate_kind_error() {
        let result = CodeTemplate::new("#{x:T} #{x:N}");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("previously declared"),
            "got: {err}"
        );
    }

    #[test]
    fn test_string_lit_param() {
        let tmpl = CodeTemplate::new("console.log(#{msg:S})").unwrap();
        let block = tmpl
            .apply::<TypeScript>()
            .set("msg", StringLitArg("hello".into()))
            .build()
            .unwrap();
        assert!(!block.is_empty());
    }

    #[test]
    fn test_code_block_param() {
        let tmpl = CodeTemplate::new("fn main() { #{body:L} }").unwrap();
        let body = CodeBlock::<RustLang>::of("println!(\"hi\")", ()).unwrap();
        let block = tmpl.apply::<RustLang>().set("body", body).build().unwrap();
        assert!(!block.is_empty());
    }
}