sigil-stitch 0.3.2

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
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
use crate::code_node::{CodeNode, parts_args_to_nodes};
use crate::import::ImportRef;
use crate::lang::CodeLang;
use crate::type_name::TypeName;

/// A parsed format specifier from a format string.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum FormatPart {
    /// Literal text (no interpolation).
    Literal(String),
    /// `%T` - type reference (consumes an Arg::TypeName).
    Type,
    /// `%N` - name reference (consumes an Arg::Name).
    Name,
    /// `%S` - string literal (consumes an Arg::StringLit).
    StringLit,
    /// `%L` - literal/nested code block (consumes an Arg::Literal or Arg::Code).
    Literal_,
    /// `%W` - soft line break point (no argument consumed).
    Wrap,
    /// `%>` - increase indent (no argument consumed).
    Indent,
    /// `%<` - decrease indent (no argument consumed).
    Dedent,
    /// `%[` - statement begin (no argument consumed).
    StatementBegin,
    /// `%]` - statement end (no argument consumed).
    StatementEnd,
    /// Newline.
    Newline,
    /// Block open delimiter — resolved at render time via `lang.block_syntax().block_open`.
    /// Emitted by control-flow builders; braces for TS/Rust/Go, colon for Python.
    BlockOpen,
    /// Block open with an overridden delimiter (not resolved via `lang.block_syntax().block_open`).
    /// Emitted by `begin_control_flow_with_open` for constructs that need a
    /// different opener than the language default (e.g., Haskell `where` vs `=`).
    BlockOpenOverride(String),
    /// Block close delimiter (terminal) — resolved at render time via `lang.block_syntax().block_close`.
    /// Emitted by `end_control_flow`. When non-empty, also emits a trailing newline.
    /// When empty (indent-only languages like OCaml/Haskell/Python), emits nothing.
    BlockClose,
    /// Block close delimiter (transitional) — resolved at render time via
    /// `lang.block_syntax().block_close` + `" "`. Used by `next_control_flow` to emit `} else`.
    /// When `block_close()` is empty, emits nothing (Python: dedent-only transition).
    BlockCloseTransition,
}

/// An argument to a CodeBlock format string.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum Arg {
    /// A type name reference (used by `%T`).
    TypeName(TypeName),
    /// A name string (used by `%N`).
    Name(String),
    /// A string literal value (used by `%S`).
    StringLit(String),
    /// A literal string value or nested code block (used by `%L`).
    Literal(String),
    /// A nested code block (used by `%L`).
    Code(CodeBlock),
}

/// An immutable code fragment with embedded type references.
///
/// `CodeBlock` is the core composition primitive in sigil-stitch. It stores a tree
/// of [`CodeNode`] nodes — self-contained IR nodes produced from format strings
/// (`%T`, `%N`, `%S`, `%L`, etc.). CodeBlocks are produced by [`CodeBlockBuilder`]
/// and consumed by [`FileSpec`](crate::spec::file_spec::FileSpec) during rendering.
/// Type references embedded via `%T` are automatically tracked for import resolution.
///
/// Use [`CodeBlock::builder()`] to construct a block incrementally, or
/// [`CodeBlock::of()`] for simple one-liners.
///
/// # Examples
///
/// ```
/// use sigil_stitch::code_block::CodeBlock;
/// use sigil_stitch::lang::typescript::TypeScript;
/// use sigil_stitch::type_name::TypeName;
///
/// // One-liner with a type reference:
/// let user = TypeName::importable("./models", "User");
/// let block = CodeBlock::of("const u: %T = getUser()", (user,)).unwrap();
///
/// // Multi-statement block via builder:
/// let mut cb = CodeBlock::builder();
/// cb.add_statement("const x = 1", ());
/// cb.add_statement("const y = 2", ());
/// let block = cb.build().unwrap();
/// ```
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CodeBlock {
    pub(crate) nodes: Vec<CodeNode>,
}

impl CodeBlock {
    /// Create a new CodeBlockBuilder.
    pub fn builder() -> CodeBlockBuilder {
        CodeBlockBuilder::new()
    }

    /// Create a CodeBlock from a single format string and arguments.
    pub fn of(format: &str, args: impl IntoArgs) -> Result<Self, crate::error::SigilStitchError> {
        let mut builder = CodeBlockBuilder::new();
        builder.add(format, args);
        builder.build()
    }

    /// Check if this code block is empty.
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Check if this code block ends with a newline or block close.
    pub(crate) fn ends_with_newline_or_block_close(&self) -> bool {
        fn check_last(nodes: &[CodeNode]) -> bool {
            match nodes.last() {
                Some(CodeNode::Newline | CodeNode::BlockClose) => true,
                Some(CodeNode::Sequence(children)) => check_last(children),
                _ => false,
            }
        }
        check_last(&self.nodes)
    }

    /// Collect all import references from this code block.
    pub fn collect_imports(&self, out: &mut Vec<ImportRef>) {
        collect_imports_from_nodes(&self.nodes, out);
    }

    /// Render this code block to a string without import resolution.
    ///
    /// Creates a temporary empty import group and renders using the given
    /// language and target line width. Useful for quick one-off rendering
    /// in tests or when import management is not needed.
    pub fn render_standalone(
        &self,
        lang: &dyn CodeLang,
        width: usize,
    ) -> Result<String, crate::error::SigilStitchError> {
        let imports = crate::import::ImportGroup::new();
        let mut renderer = crate::code_renderer::CodeRenderer::new(lang, &imports, width);
        renderer.render(self)
    }
}

fn collect_imports_from_nodes(nodes: &[CodeNode], out: &mut Vec<ImportRef>) {
    for node in nodes {
        match node {
            CodeNode::TypeRef(tn) => tn.collect_imports(out),
            CodeNode::Nested(block) => block.collect_imports(out),
            CodeNode::Sequence(children) => collect_imports_from_nodes(children, out),
            _ => {}
        }
    }
}

/// Builder for constructing [`CodeBlock`] instances.
///
/// Provides methods for adding formatted code fragments, statements, control
/// flow blocks, and nested code blocks. Format strings use `%T`, `%N`, `%S`,
/// `%L` for type/name/string/literal substitution, and `%W`, `%>`, `%<` for
/// soft line breaks and indentation.
///
/// # Examples
///
/// ```
/// use sigil_stitch::code_block::CodeBlock;
/// use sigil_stitch::lang::typescript::TypeScript;
///
/// let mut cb = CodeBlock::builder();
/// cb.begin_control_flow("if (x > 0)", ());
/// cb.add_statement("return x", ());
/// cb.next_control_flow("else", ());
/// cb.add_statement("return -x", ());
/// cb.end_control_flow();
/// let block = cb.build().unwrap();
/// ```
#[derive(Debug)]
pub struct CodeBlockBuilder {
    nodes: Vec<CodeNode>,
    indent_depth: i32,
    errors: Vec<crate::error::SigilStitchError>,
}

impl CodeBlockBuilder {
    /// Create a new empty code block builder.
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            indent_depth: 0,
            errors: Vec::new(),
        }
    }

    /// Add a formatted code fragment.
    pub fn add(&mut self, format: &str, args: impl IntoArgs) -> &mut Self {
        let arg_vec = args.into_args();
        let parsed = match parse_format(format) {
            Ok(parts) => parts,
            Err(err) => {
                self.errors.push(err);
                return self;
            }
        };

        let consuming_specifiers: Vec<String> = parsed
            .iter()
            .filter_map(|p| match p {
                FormatPart::Type => Some("%T".to_string()),
                FormatPart::Name => Some("%N".to_string()),
                FormatPart::StringLit => Some("%S".to_string()),
                FormatPart::Literal_ => Some("%L".to_string()),
                _ => None,
            })
            .collect();

        let expected_args = consuming_specifiers.len();

        if expected_args != arg_vec.len() {
            let actual_arg_kinds: Vec<String> = arg_vec
                .iter()
                .map(|a| match a {
                    Arg::TypeName(_) => "TypeName".to_string(),
                    Arg::Name(_) => "Name".to_string(),
                    Arg::StringLit(_) => "StringLit".to_string(),
                    Arg::Literal(_) => "Literal".to_string(),
                    Arg::Code(_) => "Code".to_string(),
                })
                .collect();
            self.errors
                .push(crate::error::SigilStitchError::FormatArgCount {
                    format: format.to_string(),
                    expected: expected_args,
                    actual: arg_vec.len(),
                    expected_specifiers: consuming_specifiers,
                    actual_arg_kinds,
                });
            return self;
        }

        let new_nodes = parts_args_to_nodes(&parsed, &arg_vec);
        self.nodes.extend(new_nodes);
        self
    }

    /// Add a statement (wraps in %[...%] and appends language semicolon).
    pub fn add_statement(&mut self, format: &str, args: impl IntoArgs) -> &mut Self {
        self.nodes.push(CodeNode::StatementBegin);
        self.add(format, args);
        self.nodes.push(CodeNode::StatementEnd);
        self.nodes.push(CodeNode::Newline);
        self
    }

    /// Begin a control flow block (e.g., "if foo" -> "if foo {\n" + indent).
    pub fn begin_control_flow(&mut self, format: &str, args: impl IntoArgs) -> &mut Self {
        self.add(format, args);
        self.nodes.push(CodeNode::BlockOpen);
        self.nodes.push(CodeNode::Newline);
        self.nodes.push(CodeNode::Indent);
        self.indent_depth += 1;
        self
    }

    /// Begin a control flow block with a custom block-open string.
    ///
    /// Like [`begin_control_flow`](Self::begin_control_flow), but uses
    /// `custom_open` instead of the language's `block_open()`. Pass `""`
    /// to suppress the block opener entirely (e.g., OCaml `match x with`).
    pub fn begin_control_flow_with_open(
        &mut self,
        format: &str,
        args: impl IntoArgs,
        custom_open: &str,
    ) -> &mut Self {
        self.add(format, args);
        if !custom_open.is_empty() {
            self.nodes
                .push(CodeNode::BlockOpenOverride(custom_open.to_string()));
        }
        self.nodes.push(CodeNode::Newline);
        self.nodes.push(CodeNode::Indent);
        self.indent_depth += 1;
        self
    }

    /// Add an else/else-if clause (e.g., "} else {" or "elif ...:" for Python).
    pub fn next_control_flow(&mut self, format: &str, args: impl IntoArgs) -> &mut Self {
        self.nodes.push(CodeNode::Dedent);
        self.indent_depth -= 1;
        self.nodes.push(CodeNode::BlockCloseTransition);
        self.add(format, args);
        self.nodes.push(CodeNode::BlockOpen);
        self.nodes.push(CodeNode::Newline);
        self.nodes.push(CodeNode::Indent);
        self.indent_depth += 1;
        self
    }

    /// End a control flow block (emits "}" or nothing for Python, and decreases indent).
    pub fn end_control_flow(&mut self) -> &mut Self {
        self.nodes.push(CodeNode::Dedent);
        self.indent_depth -= 1;
        self.nodes.push(CodeNode::BlockClose);
        self
    }

    /// Add a blank line.
    pub fn add_line(&mut self) -> &mut Self {
        self.nodes.push(CodeNode::Newline);
        self
    }

    /// Add an inline comment.
    pub fn add_comment(&mut self, text: &str) -> &mut Self {
        self.nodes.push(CodeNode::Comment(text.to_string()));
        self.nodes.push(CodeNode::Newline);
        self
    }

    /// Add a nested CodeBlock inline.
    pub fn add_code(&mut self, block: CodeBlock) -> &mut Self {
        self.nodes.push(CodeNode::Nested(block));
        self
    }

    /// Build the immutable CodeBlock.
    ///
    /// Returns an error if any format string had an argument count mismatch,
    /// or if indent depth is not balanced (unmatched
    /// begin_control_flow / end_control_flow).
    pub fn build(self) -> Result<CodeBlock, crate::error::SigilStitchError> {
        if let Some(err) = self.errors.into_iter().next() {
            return Err(err);
        }
        if self.indent_depth != 0 {
            return Err(crate::error::SigilStitchError::UnbalancedIndent {
                depth: self.indent_depth,
            });
        }
        Ok(CodeBlock { nodes: self.nodes })
    }

    /// Build the CodeBlock, panicking on error.
    pub fn build_unwrap(self) -> CodeBlock {
        self.build().unwrap()
    }
}

impl Default for CodeBlockBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Parse a format string into FormatParts.
fn parse_format(format: &str) -> Result<Vec<FormatPart>, crate::error::SigilStitchError> {
    let mut parts = Vec::new();
    let mut current_literal = String::new();
    let mut chars = format.char_indices().peekable();

    while let Some(&(_, ch)) = chars.peek() {
        if ch == '%' {
            chars.next();
            if let Some(&(_, spec)) = chars.peek() {
                chars.next();
                let part = match spec {
                    'T' => Some(FormatPart::Type),
                    'N' => Some(FormatPart::Name),
                    'S' => Some(FormatPart::StringLit),
                    'L' => Some(FormatPart::Literal_),
                    'W' => Some(FormatPart::Wrap),
                    '>' => Some(FormatPart::Indent),
                    '<' => Some(FormatPart::Dedent),
                    '[' => Some(FormatPart::StatementBegin),
                    ']' => Some(FormatPart::StatementEnd),
                    '%' => {
                        current_literal.push('%');
                        continue;
                    }
                    _ => {
                        return Err(crate::error::SigilStitchError::InvalidFormatSpecifier {
                            format: format.to_string(),
                            specifier: spec,
                        });
                    }
                };
                if let Some(part) = part {
                    if !current_literal.is_empty() {
                        parts.push(FormatPart::Literal(std::mem::take(&mut current_literal)));
                    }
                    parts.push(part);
                }
            }
        } else if ch == '\n' {
            chars.next();
            if !current_literal.is_empty() {
                parts.push(FormatPart::Literal(std::mem::take(&mut current_literal)));
            }
            parts.push(FormatPart::Newline);
        } else {
            chars.next();
            current_literal.push(ch);
        }
    }

    if !current_literal.is_empty() {
        parts.push(FormatPart::Literal(current_literal));
    }

    Ok(parts)
}

// === IntoArgs trait and implementations ===

/// Trait for converting various types into a `Vec<Arg>` for format strings.
///
/// Implemented for `()` (no args), `TypeName`, `&str`, `String`, `CodeBlock`,
/// `NameArg`, `StringLitArg`, `Vec<Arg>`, and tuples up to 8 elements.
/// Bare strings convert to `Arg::Literal`; use [`NameArg`] or [`StringLitArg`]
/// wrappers to target `%N` or `%S` specifiers instead.
pub trait IntoArgs {
    /// Convert into a vector of format arguments.
    fn into_args(self) -> Vec<Arg>;
}

/// Empty args (for format strings with no specifiers).
impl IntoArgs for () {
    fn into_args(self) -> Vec<Arg> {
        Vec::new()
    }
}

/// Single TypeName arg.
impl IntoArgs for TypeName {
    fn into_args(self) -> Vec<Arg> {
        vec![Arg::TypeName(self)]
    }
}

/// Single string arg (as literal).
impl IntoArgs for &str {
    fn into_args(self) -> Vec<Arg> {
        vec![Arg::Literal(self.to_string())]
    }
}

impl IntoArgs for String {
    fn into_args(self) -> Vec<Arg> {
        vec![Arg::Literal(self)]
    }
}

/// Single CodeBlock arg.
impl IntoArgs for CodeBlock {
    fn into_args(self) -> Vec<Arg> {
        vec![Arg::Code(self)]
    }
}

/// Pre-built args vector (used by specs that dynamically build format strings).
impl IntoArgs for Vec<Arg> {
    fn into_args(self) -> Vec<Arg> {
        self
    }
}

/// A wrapper to explicitly mark a string as a Name arg (for `%N`).
///
/// By default, bare strings convert to `Arg::Literal` (for `%L`). Wrap with
/// `NameArg` when your format string uses `%N`.
///
/// # Examples
///
/// ```
/// use sigil_stitch::code_block::{CodeBlock, NameArg};
/// use sigil_stitch::lang::typescript::TypeScript;
///
/// let mut cb = CodeBlock::builder();
/// cb.add("this.%N()", (NameArg("getData".to_string()),));
/// let block = cb.build().unwrap();
/// ```
pub struct NameArg(pub String);

impl IntoArgs for NameArg {
    fn into_args(self) -> Vec<Arg> {
        vec![Arg::Name(self.0)]
    }
}

/// A wrapper to explicitly mark a string as a StringLit arg (for `%S`).
///
/// By default, bare strings convert to `Arg::Literal` (for `%L`). Wrap with
/// `StringLitArg` when your format string uses `%S` to emit a quoted string.
///
/// # Examples
///
/// ```
/// use sigil_stitch::code_block::{CodeBlock, StringLitArg};
/// use sigil_stitch::lang::typescript::TypeScript;
///
/// let mut cb = CodeBlock::builder();
/// cb.add_statement("const msg = %S", (StringLitArg("hello".to_string()),));
/// let block = cb.build().unwrap();
/// ```
pub struct StringLitArg(pub String);

impl IntoArgs for StringLitArg {
    fn into_args(self) -> Vec<Arg> {
        vec![Arg::StringLit(self.0)]
    }
}

// Individual Arg conversions.
impl From<TypeName> for Arg {
    fn from(tn: TypeName) -> Self {
        Arg::TypeName(tn)
    }
}

impl From<&str> for Arg {
    fn from(s: &str) -> Self {
        Arg::Literal(s.to_string())
    }
}

impl From<String> for Arg {
    fn from(s: String) -> Self {
        Arg::Literal(s)
    }
}

impl From<CodeBlock> for Arg {
    fn from(cb: CodeBlock) -> Self {
        Arg::Code(cb)
    }
}

impl From<NameArg> for Arg {
    fn from(n: NameArg) -> Self {
        Arg::Name(n.0)
    }
}

impl From<StringLitArg> for Arg {
    fn from(s: StringLitArg) -> Self {
        Arg::StringLit(s.0)
    }
}

// Tuple implementations for IntoArgs.
// Each element must implement Into<Arg>.

macro_rules! impl_into_args_tuple {
    ($($idx:tt $T:ident),+) => {
        impl<$($T: Into<Arg>),+> IntoArgs for ($($T,)+) {
            fn into_args(self) -> Vec<Arg> {
                vec![$(self.$idx.into()),+]
            }
        }
    };
}

impl_into_args_tuple!(0 A);
impl_into_args_tuple!(0 A, 1 B);
impl_into_args_tuple!(0 A, 1 B, 2 C);
impl_into_args_tuple!(0 A, 1 B, 2 C, 3 D);
impl_into_args_tuple!(0 A, 1 B, 2 C, 3 D, 4 E);
impl_into_args_tuple!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F);
impl_into_args_tuple!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F, 6 G);
impl_into_args_tuple!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F, 6 G, 7 H);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::code_node::CodeNode;

    #[test]
    fn test_parse_all_specifiers() {
        let parts = parse_format("hello %T world %N %S %L %W %> %< %[ %]").unwrap();
        assert!(parts.contains(&FormatPart::Type));
        assert!(parts.contains(&FormatPart::Name));
        assert!(parts.contains(&FormatPart::StringLit));
        assert!(parts.contains(&FormatPart::Literal_));
        assert!(parts.contains(&FormatPart::Wrap));
        assert!(parts.contains(&FormatPart::Indent));
        assert!(parts.contains(&FormatPart::Dedent));
        assert!(parts.contains(&FormatPart::StatementBegin));
        assert!(parts.contains(&FormatPart::StatementEnd));
    }

    #[test]
    fn test_parse_literal_percent() {
        let parts = parse_format("100%%").unwrap();
        assert_eq!(parts, vec![FormatPart::Literal("100%".to_string())]);
    }

    #[test]
    fn test_parse_empty() {
        let parts = parse_format("").unwrap();
        assert!(parts.is_empty());
    }

    #[test]
    fn test_parse_newlines() {
        let parts = parse_format("line1\nline2").unwrap();
        assert_eq!(
            parts,
            vec![
                FormatPart::Literal("line1".to_string()),
                FormatPart::Newline,
                FormatPart::Literal("line2".to_string()),
            ]
        );
    }

    #[test]
    fn test_builder_add_statement() {
        let mut b = CodeBlock::builder();
        b.add_statement("const x = %L", "42");
        let block = b.build().unwrap();

        assert!(!block.is_empty());
        let has_stmt_begin = block
            .nodes
            .iter()
            .any(|n| matches!(n, CodeNode::StatementBegin));
        let has_stmt_end = block
            .nodes
            .iter()
            .any(|n| matches!(n, CodeNode::StatementEnd));
        assert!(has_stmt_begin);
        assert!(has_stmt_end);
    }

    #[test]
    fn test_builder_control_flow() {
        let mut b = CodeBlock::builder();
        b.begin_control_flow("if (x > 0)", ());
        b.add_statement("return x", ());
        b.end_control_flow();
        let block = b.build().unwrap();

        assert!(!block.is_empty());
    }

    #[test]
    fn test_builder_unbalanced_control_flow() {
        let mut b = CodeBlock::builder();
        b.begin_control_flow("if (x)", ());
        b.add_statement("y()", ());
        // missing end_control_flow
        let result = b.build();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("unbalanced"));
    }

    #[test]
    fn test_mismatched_arg_count() {
        let mut b = CodeBlock::builder();
        b.add("%T", ());
        let result = b.build();
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("expects 1 args but got 0")
        );
    }

    #[test]
    fn test_into_args_tuple() {
        let user = TypeName::importable("./models", "User");
        let args: Vec<Arg> = (user, "hello").into_args();
        assert_eq!(args.len(), 2);
        assert!(matches!(&args[0], Arg::TypeName(_)));
        assert!(matches!(&args[1], Arg::Literal(s) if s == "hello"));
    }

    #[test]
    fn test_into_args_single_typename() {
        let user = TypeName::importable("./models", "User");
        let args: Vec<Arg> = user.into_args();
        assert_eq!(args.len(), 1);
    }

    #[test]
    fn test_into_args_single_str() {
        let args: Vec<Arg> = "hello".into_args();
        assert_eq!(args.len(), 1);
        assert!(matches!(&args[0], Arg::Literal(s) if s == "hello"));
    }

    #[test]
    fn test_collect_imports_from_codeblock() {
        let user = TypeName::importable("./models", "User");
        let tag = TypeName::importable("./models", "Tag");
        let mut b = CodeBlock::builder();
        b.add_statement("const u: %T = getUser()", (user,));
        b.add_statement("const t: %T = getTag()", (tag,));
        let block = b.build().unwrap();

        let mut imports = Vec::new();
        block.collect_imports(&mut imports);
        assert_eq!(imports.len(), 2);
        assert_eq!(imports[0].name, "User");
        assert_eq!(imports[1].name, "Tag");
    }

    #[test]
    fn test_nested_codeblock_imports() {
        let user = TypeName::importable("./models", "User");
        let mut ib = CodeBlock::builder();
        ib.add_statement("return new %T()", (user,));
        let inner = ib.build().unwrap();

        let mut ob = CodeBlock::builder();
        ob.add_code(inner);
        let outer = ob.build().unwrap();

        let mut imports = Vec::new();
        outer.collect_imports(&mut imports);
        assert_eq!(imports.len(), 1);
        assert_eq!(imports[0].name, "User");
    }

    #[test]
    fn test_name_arg() {
        let mut b = CodeBlock::builder();
        b.add("this.%N()", (NameArg("getUser".to_string()),));
        let block = b.build().unwrap();
        let has_name = block
            .nodes
            .iter()
            .any(|n| matches!(n, CodeNode::NameRef(s) if s == "getUser"));
        assert!(has_name);
    }

    #[test]
    fn test_string_lit_arg() {
        let mut b = CodeBlock::builder();
        b.add("const x = %S", (StringLitArg("hello".to_string()),));
        let block = b.build().unwrap();
        let has_str_lit = block
            .nodes
            .iter()
            .any(|n| matches!(n, CodeNode::StringLit(s) if s == "hello"));
        assert!(has_str_lit);
    }

    #[test]
    fn test_invalid_format_specifier() {
        let mut b = CodeBlock::builder();
        b.add("hello %X world", ());
        let result = b.build();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("invalid format specifier"));
        assert!(err_msg.contains("%X"));
    }

    #[test]
    fn test_parse_format_invalid_specifier_returns_error() {
        let result = parse_format("foo %Z bar");
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("invalid format specifier"));
        assert!(err_msg.contains("%Z"));
    }

    #[test]
    fn test_mismatched_arg_count_includes_specifiers_and_kinds() {
        let user = TypeName::importable("./models", "User");
        let mut b = CodeBlock::builder();
        b.add("%T %S %L", (user,));
        let result = b.build();
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("expects 3 args but got 1"));
        assert!(err_msg.contains("%T"));
        assert!(err_msg.contains("%S"));
        assert!(err_msg.contains("%L"));
        assert!(err_msg.contains("TypeName"));
    }

    #[test]
    fn test_begin_control_flow_with_open_non_empty() {
        let mut b = CodeBlock::builder();
        b.begin_control_flow_with_open("class Functor f", (), " where");
        b.add_statement("fmap :: (a -> b) -> f a -> f b", ());
        b.end_control_flow();
        let block = b.build().unwrap();
        let has_override = block
            .nodes
            .iter()
            .any(|n| matches!(n, CodeNode::BlockOpenOverride(s) if s == " where"));
        assert!(has_override, "should contain BlockOpenOverride(\" where\")");
        let has_block_open = block.nodes.iter().any(|n| matches!(n, CodeNode::BlockOpen));
        assert!(
            !has_block_open,
            "should NOT contain BlockOpen when override is used"
        );
    }

    #[test]
    fn test_begin_control_flow_with_open_empty() {
        let mut b = CodeBlock::builder();
        b.begin_control_flow_with_open("match x with", (), "");
        b.add("| Red -> red", ());
        b.add_line();
        b.end_control_flow();
        let block = b.build().unwrap();
        let has_override = block
            .nodes
            .iter()
            .any(|n| matches!(n, CodeNode::BlockOpenOverride(_)));
        assert!(
            !has_override,
            "empty custom_open should skip BlockOpenOverride"
        );
        let has_block_open = block.nodes.iter().any(|n| matches!(n, CodeNode::BlockOpen));
        assert!(!has_block_open, "should NOT contain BlockOpen either");
    }
}