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
//! Bash shell language implementation.

use crate::import::ImportGroup;
use crate::lang::CodeLang;
use crate::lang::config::{
    BlockSyntaxConfig, EnumAndAnnotationConfig, FunctionSyntaxConfig, GenericSyntaxConfig,
    TypeDeclSyntaxConfig, TypePresentationConfig,
};
use crate::spec::modifiers::{DeclarationContext, TypeKind, Visibility};

/// Bash shell language implementation.
///
/// Bash-specific behaviors:
/// - 4-space indentation (configurable)
/// - No semicolons (newline-separated statements)
/// - `source "path"` imports
/// - `#` comments
/// - Double-quoted string literals with `$`, `` ` ``, `\`, `"`, `!` escaping
/// - `function` keyword for function declarations
/// - `{ }` brace blocks for functions
///
/// # Control Flow
///
/// Bash uses keyword-based block closers that vary per construct (`fi`, `done`,
/// `esac`). The `begin_control_flow`/`end_control_flow` helpers auto-append
/// `block_open()`/`block_close()` (i.e., `{`/`}`), which is wrong for shell
/// control flow. Instead, use manual `add()` with explicit indent/dedent:
///
/// ```text
/// // if/then/fi
/// b.add("if [ -f \"$file\" ]; then\n", ());
/// b.add("%>", ());
/// b.add_statement("echo \"found\"", ());
/// b.add("%<", ());
/// b.add("fi\n", ());
///
/// // for/do/done
/// b.add("for f in *.txt; do\n", ());
/// b.add("%>", ());
/// b.add_statement("process \"$f\"", ());
/// b.add("%<", ());
/// b.add("done\n", ());
///
/// // while/do/done
/// b.add("while read -r line; do\n", ());
/// b.add("%>", ());
/// b.add_statement("echo \"$line\"", ());
/// b.add("%<", ());
/// b.add("done\n", ());
///
/// // case/esac
/// b.add("case \"$1\" in\n", ());
/// b.add("%>", ());
/// b.add("start)\n", ());
/// b.add("%>", ());
/// b.add_statement("start_service", ());
/// b.add("%<", ());
/// b.add(";;\n", ());
/// b.add("*)\n", ());
/// b.add("%>", ());
/// b.add_statement("echo \"unknown\"", ());
/// b.add("%<", ());
/// b.add(";;\n", ());
/// b.add("%<", ());
/// b.add("esac\n", ());
/// ```
///
/// The `begin_control_flow`/`end_control_flow` helpers **do** work for function
/// bodies since functions use `{ }` braces:
///
/// ```text
/// let mut fb = FunSpec::builder("greet");
/// fb.body(body);
/// let fun = fb.build().unwrap();
/// // function greet {
/// //     echo "hello"
/// // }
/// ```
///
/// # Shebang
///
/// Use `FileSpec::header()` for the shebang line:
///
/// ```text
/// let mut header_b = CodeBlock::builder();
/// header_b.add("#!/usr/bin/env bash\n", ());
/// header_b.add("set -euo pipefail", ());
/// fb.header(header_b.build().unwrap());
/// ```
#[derive(Debug, Clone)]
pub struct Bash {
    /// Indent with this string (default: "    " -- 4 spaces).
    pub indent: String,
    /// File extension (default: "bash"). Set to "sh" for POSIX-ish scripts.
    pub extension: String,
}

impl Default for Bash {
    fn default() -> Self {
        Self {
            indent: "    ".to_string(),
            extension: "bash".to_string(),
        }
    }
}

impl Bash {
    /// Create a new Bash language instance.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the indent string (e.g., `"    "` for 4-space default, `"  "` for 2 spaces, `"\t"` for tabs).
    pub fn with_indent(mut self, s: &str) -> Self {
        self.indent = s.to_string();
        self
    }

    /// Set the file extension (e.g., `"bash"` or `"sh"`).
    pub fn with_extension(mut self, s: &str) -> Self {
        self.extension = s.to_string();
        self
    }
}

const BASH_RESERVED: &[&str] = &[
    "break", "case", "continue", "coproc", "declare", "do", "done", "elif", "else", "esac", "eval",
    "exec", "exit", "export", "fi", "for", "function", "if", "in", "local", "readonly", "return",
    "select", "shift", "source", "then", "time", "trap", "typeset", "unset", "until", "while",
];

impl CodeLang for Bash {
    fn file_extension(&self) -> &str {
        &self.extension
    }

    fn reserved_words(&self) -> &[&str] {
        BASH_RESERVED
    }

    fn render_imports(&self, imports: &ImportGroup) -> String {
        if imports.entries.is_empty() {
            return String::new();
        }

        // Deduplicate to unique source paths.
        let mut paths: Vec<&str> = Vec::new();
        let mut seen = std::collections::HashSet::new();
        for entry in &imports.entries {
            if seen.insert(entry.module.as_str()) {
                paths.push(&entry.module);
            }
        }
        paths.sort();

        paths
            .iter()
            .map(|p| format!("source \"{p}\""))
            .collect::<Vec<_>>()
            .join("\n")
    }

    fn render_string_literal(&self, s: &str) -> String {
        // Double-quoted string with Bash-specific escaping.
        // Must escape: \, ", $, `, !
        let escaped = s
            .replace('\\', "\\\\")
            .replace('"', "\\\"")
            .replace('$', "\\$")
            .replace('`', "\\`")
            .replace('!', "\\!");
        format!("\"{escaped}\"")
    }

    fn render_doc_comment(&self, lines: &[&str]) -> String {
        // Bash has no doc comment convention; use # comment blocks.
        lines
            .iter()
            .map(|line| {
                if line.is_empty() {
                    "#".to_string()
                } else {
                    format!("# {line}")
                }
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    fn line_comment_prefix(&self) -> &str {
        "#"
    }

    // --- Spec support ---
    // Shell has no visibility, generics, inheritance, or interfaces.
    // Return empty/no-op for all structural methods.

    fn render_visibility(&self, _vis: Visibility, _ctx: DeclarationContext) -> &str {
        ""
    }

    fn function_keyword(&self, _ctx: DeclarationContext) -> &str {
        "function"
    }

    fn type_keyword(&self, _kind: TypeKind) -> &str {
        ""
    }

    fn methods_inside_type_body(&self, _kind: TypeKind) -> bool {
        true
    }

    // --- Config struct accessors ---

    fn type_presentation(&self) -> TypePresentationConfig<'_> {
        TypePresentationConfig::default()
    }

    fn generic_syntax(&self) -> GenericSyntaxConfig<'_> {
        GenericSyntaxConfig {
            constraint_keyword: "",
            constraint_separator: "",
            ..Default::default()
        }
    }

    fn block_syntax(&self) -> BlockSyntaxConfig<'_> {
        BlockSyntaxConfig {
            indent_unit: &self.indent,
            uses_semicolons: false,
            field_terminator: "",
            ..Default::default()
        }
    }

    fn function_syntax(&self) -> FunctionSyntaxConfig<'_> {
        FunctionSyntaxConfig {
            return_type_separator: "",
            ..Default::default()
        }
    }

    fn type_decl_syntax(&self) -> TypeDeclSyntaxConfig<'_> {
        TypeDeclSyntaxConfig::default()
    }

    fn enum_and_annotation(&self) -> EnumAndAnnotationConfig<'_> {
        EnumAndAnnotationConfig::default()
    }
}

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

    #[test]
    fn test_file_extension() {
        let bash = Bash::new();
        assert_eq!(bash.file_extension(), "bash");
    }

    #[test]
    fn test_reserved_words() {
        let bash = Bash::new();
        let reserved = bash.reserved_words();
        assert!(reserved.contains(&"if"));
        assert!(reserved.contains(&"fi"));
        assert!(reserved.contains(&"function"));
        assert!(reserved.contains(&"esac"));
        assert!(!reserved.contains(&"echo"));
    }

    #[test]
    fn test_escape_reserved() {
        let bash = Bash::new();
        assert_eq!(bash.escape_reserved("if"), "if_");
        assert_eq!(bash.escape_reserved("name"), "name");
        assert_eq!(bash.escape_reserved("function"), "function_");
    }

    #[test]
    fn test_string_literal_basic() {
        let bash = Bash::new();
        assert_eq!(bash.render_string_literal("hello"), "\"hello\"");
    }

    #[test]
    fn test_string_literal_escaping() {
        let bash = Bash::new();
        // Dollar sign
        assert_eq!(bash.render_string_literal("$HOME"), "\"\\$HOME\"");
        // Double quote
        assert_eq!(
            bash.render_string_literal("say \"hi\""),
            "\"say \\\"hi\\\"\""
        );
        // Backtick
        assert_eq!(bash.render_string_literal("`cmd`"), "\"\\`cmd\\`\"");
        // Backslash
        assert_eq!(bash.render_string_literal("a\\b"), "\"a\\\\b\"");
        // Exclamation
        assert_eq!(bash.render_string_literal("wow!"), "\"wow\\!\"");
    }

    #[test]
    fn test_string_literal_combined() {
        let bash = Bash::new();
        assert_eq!(
            bash.render_string_literal("$USER says \"hi!\""),
            "\"\\$USER says \\\"hi\\!\\\"\"",
        );
    }

    #[test]
    fn test_render_imports_empty() {
        let bash = Bash::new();
        let imports = ImportGroup { entries: vec![] };
        assert_eq!(bash.render_imports(&imports), "");
    }

    #[test]
    fn test_render_imports_single() {
        let bash = Bash::new();
        let imports = ImportGroup {
            entries: vec![crate::import::ImportEntry {
                module: "./lib/utils.sh".into(),
                name: "log_info".into(),
                alias: None,
                is_type_only: false,
                is_side_effect: false,
                is_wildcard: false,
            }],
        };
        assert_eq!(bash.render_imports(&imports), "source \"./lib/utils.sh\"");
    }

    #[test]
    fn test_render_imports_dedup() {
        let bash = Bash::new();
        let imports = ImportGroup {
            entries: vec![
                crate::import::ImportEntry {
                    module: "./lib/utils.sh".into(),
                    name: "log_info".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
                crate::import::ImportEntry {
                    module: "./lib/utils.sh".into(),
                    name: "log_error".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
                crate::import::ImportEntry {
                    module: "./lib/config.sh".into(),
                    name: "load_config".into(),
                    alias: None,
                    is_type_only: false,
                    is_side_effect: false,
                    is_wildcard: false,
                },
            ],
        };
        let output = bash.render_imports(&imports);
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0], "source \"./lib/config.sh\"");
        assert_eq!(lines[1], "source \"./lib/utils.sh\"");
    }

    #[test]
    fn test_doc_comment_single() {
        let bash = Bash::new();
        assert_eq!(bash.render_doc_comment(&["A function."]), "# A function.");
    }

    #[test]
    fn test_doc_comment_multi() {
        let bash = Bash::new();
        let doc = bash.render_doc_comment(&["First line.", "", "Second paragraph."]);
        let lines: Vec<&str> = doc.lines().collect();
        assert_eq!(lines[0], "# First line.");
        assert_eq!(lines[1], "#");
        assert_eq!(lines[2], "# Second paragraph.");
    }

    #[test]
    fn test_no_semicolons() {
        let bash = Bash::new();
        assert!(!bash.block_syntax().uses_semicolons);
    }

    #[test]
    fn test_comment_prefix() {
        let bash = Bash::new();
        assert_eq!(bash.line_comment_prefix(), "#");
    }

    #[test]
    fn test_function_keyword() {
        let bash = Bash::new();
        assert_eq!(
            bash.function_keyword(DeclarationContext::TopLevel),
            "function"
        );
    }

    #[test]
    fn test_block_delimiters() {
        let bash = Bash::new();
        assert_eq!(bash.block_syntax().block_open, " {");
        assert_eq!(bash.block_syntax().block_close, "}");
    }

    #[test]
    fn test_bash_builder_fluent() {
        let bash = Bash::new().with_indent("  ").with_extension("sh");
        assert_eq!(bash.file_extension(), "sh");
        assert_eq!(bash.block_syntax().indent_unit, "  ");
    }

    #[test]
    fn test_module_separator() {
        let bash = Bash::new();
        assert_eq!(bash.module_separator(), None);
    }
}