sigil-stitch 0.6.4

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
//! Shared configuration types used across language implementations.
//!
//! These types live here (rather than inside individual `src/lang/*.rs` files)
//! because they represent cross-cutting concepts — quote style, optional-field
//! semantics, type presentation — that multiple languages express in similar ways.

use crate::spec::fun_spec::{FunctionSignatureStyle, ParamListStyle};
use crate::spec::modifiers::ConstructorDelegationStyle;
use crate::spec::where_spec::WhereClauseStyle;
use crate::type_name::{
    AssociatedTypeStyle, BoundsPresentation, FunctionPresentation, GenericApplicationStyle,
    TypePresentation, WildcardPresentation,
};

/// Quote style for rendering string literals.
///
/// Used by `TypeScript`, `JavaScript`, and `Python` where either single or
/// double quotes are valid and the choice is a project style decision.
/// Languages with a fixed quote style (Rust, Java, Go, etc.) don't consult
/// this enum.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum QuoteStyle {
    /// Single quotes (`'hello'`).
    #[default]
    Single,
    /// Double quotes (`"hello"`).
    Double,
}

impl QuoteStyle {
    /// The quote character for this style.
    pub fn char(self) -> char {
        match self {
            QuoteStyle::Single => '\'',
            QuoteStyle::Double => '"',
        }
    }
}

/// How a language expresses that a field is optional (key may be absent).
///
/// This is distinct from nullability (value may be `null`), which is handled
/// by `TypeName::Optional`. A `FieldSpec` marked `is_optional` is rendered
/// using this style.
///
/// Examples:
///
/// | Language | Style | Rendered output |
/// |----------|-------|-----------------|
/// | TypeScript | `NameSuffix("?")` | `name?: T` |
/// | Rust | `TypeWrap { open: "Option<", close: ">" }` | `name: Option<T>` |
/// | Go | `TypePrefix("*")` | `name *T` |
/// | Python | `UnionWithNone(" \| ")` | `name: T \| None` |
/// | Kotlin, Swift, Dart | `TypeSuffix("?")` | `name: T?` or `T? name` |
/// | Java | `TypeWrap { open: "Optional<", close: ">" }` | `Optional<T> name` |
/// | C++ | `TypeWrap { open: "std::optional<", close: ">" }` | `std::optional<T> name` |
/// | C | `TypePrefix("*")` | `T *name` |
/// | JavaScript, Bash, Zsh | `Ignored` | field rendered without any optionality marker |
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptionalFieldStyle {
    /// Append a suffix to the field name. TypeScript: `name?: T`.
    NameSuffix(&'static str),
    /// Append a suffix to the type. Kotlin/Swift/Dart: `T?`.
    TypeSuffix(&'static str),
    /// Wrap the type in `open...close`. Rust `Option<T>`, Java `Optional<T>`,
    /// C++ `std::optional<T>`.
    TypeWrap {
        /// Opening wrapper, e.g. `"Option<"`.
        open: &'static str,
        /// Closing wrapper, e.g. `">"`.
        close: &'static str,
    },
    /// Prepend a prefix to the type. Go: `name *T`, C: `T *name`.
    TypePrefix(&'static str),
    /// Render as a union with `None`. Python: `T | None` (separator is
    /// language-configurable for future flexibility).
    UnionWithNone(&'static str),
    /// Optional fields are not expressible in this language's type system.
    /// The field is rendered without any marker.
    Ignored,
}

// ── Config structs ─────────────────────────────────────────────────

/// How each compound `TypeName` variant renders.
#[derive(Debug, Clone, Copy)]
pub struct TypePresentationConfig<'a> {
    /// `TypeName::Array(T)` — e.g. `T[]`, `Vec<T>`.
    pub array: TypePresentation<'a>,
    /// `TypeName::ReadonlyArray(T)` — `None` falls back to `readonly` + `array`.
    pub readonly_array: Option<TypePresentation<'a>>,
    /// `TypeName::Optional(T)` — e.g. `T | null`, `T?`.
    pub optional: TypePresentation<'a>,
    /// The absent literal for `Optional` with `Infix` presentation (e.g. `"null"`, `"None"`).
    pub optional_absent_literal: &'a str,
    /// `TypeName::Map { K, V }` — e.g. `Map<K, V>`, `dict[K, V]`.
    pub map: TypePresentation<'a>,
    /// `TypeName::Union(members)` — e.g. `A | B`.
    pub union: TypePresentation<'a>,
    /// `TypeName::Intersection(members)` — e.g. `A & B`.
    pub intersection: TypePresentation<'a>,
    /// `TypeName::Pointer(T)` — e.g. `*T`, `*const T`.
    pub pointer: TypePresentation<'a>,
    /// `TypeName::Slice(T)` — e.g. `[]T`, `[T]`.
    pub slice: TypePresentation<'a>,
    /// `TypeName::Tuple(elements)` — e.g. `(A, B)`.
    pub tuple: TypePresentation<'a>,
    /// `TypeName::Reference { mutable: false }` — e.g. `&T`.
    pub reference: TypePresentation<'a>,
    /// `TypeName::Reference { mutable: true }` — e.g. `&mut T`.
    pub reference_mut: TypePresentation<'a>,
    /// `TypeName::Function { params, return_type }` — e.g. `(A, B) => R`.
    pub function: FunctionPresentation<'a>,
    /// `TypeName::AssociatedType` — e.g. `<T as Trait>::Assoc`.
    pub associated_type: AssociatedTypeStyle<'a>,
    /// `TypeName::ImplTrait` — e.g. `impl Trait`.
    pub impl_trait: BoundsPresentation<'a>,
    /// `TypeName::DynTrait` — e.g. `dyn Trait`.
    pub dyn_trait: BoundsPresentation<'a>,
    /// `TypeName::Wildcard` — e.g. `?`, `? extends T`.
    pub wildcard: WildcardPresentation<'a>,
}

impl Default for TypePresentationConfig<'_> {
    fn default() -> Self {
        Self {
            array: TypePresentation::Postfix { suffix: "[]" },
            readonly_array: None,
            optional: TypePresentation::Infix { sep: " | " },
            optional_absent_literal: "null",
            map: TypePresentation::GenericWrap { name: "Map" },
            union: TypePresentation::Infix { sep: " | " },
            intersection: TypePresentation::Infix { sep: " & " },
            pointer: TypePresentation::Prefix { prefix: "*" },
            slice: TypePresentation::Prefix { prefix: "[]" },
            tuple: TypePresentation::Delimited {
                open: "(",
                sep: ", ",
                close: ")",
            },
            reference: TypePresentation::Prefix { prefix: "" },
            reference_mut: TypePresentation::Prefix { prefix: "" },
            function: FunctionPresentation {
                keyword: "",
                params_open: "(",
                params_sep: ", ",
                params_close: ")",
                arrow: " => ",
                return_first: false,
                curried: false,
                wrapper_open: "",
                wrapper_close: "",
            },
            associated_type: AssociatedTypeStyle::QualifiedPath {
                open: "<",
                as_kw: " as ",
                close_sep: ">::",
                simple_sep: "::",
            },
            impl_trait: BoundsPresentation {
                keyword: "impl ",
                separator: " + ",
            },
            dyn_trait: BoundsPresentation {
                keyword: "dyn ",
                separator: " + ",
            },
            wildcard: WildcardPresentation {
                unbounded: "?",
                upper_keyword: "? extends ",
                lower_keyword: "? super ",
            },
        }
    }
}

/// Generic type parameter delimiters and constraints.
#[derive(Debug, Clone, Copy)]
pub struct GenericSyntaxConfig<'a> {
    /// Opening delimiter (e.g. `"<"`, `"["`).
    pub open: &'a str,
    /// Closing delimiter (e.g. `">"`, `"]"`).
    pub close: &'a str,
    /// How `TypeName::Generic` application renders.
    pub application_style: GenericApplicationStyle,
    /// Keyword introducing a bound (e.g. `": "`, `" extends "`).
    pub constraint_keyword: &'a str,
    /// Separator between multiple bounds (e.g. `" + "`, `" & "`).
    pub constraint_separator: &'a str,
    /// Keyword for context bounds (e.g. Scala `" : "`). Defaults to `constraint_keyword`.
    pub context_bound_keyword: &'a str,
}

impl Default for GenericSyntaxConfig<'_> {
    fn default() -> Self {
        Self {
            open: "<",
            close: ">",
            application_style: GenericApplicationStyle::Delimited,
            constraint_keyword: ": ",
            constraint_separator: " + ",
            context_bound_keyword: ": ",
        }
    }
}

/// Block delimiters, indentation, and statement termination.
#[derive(Debug, Clone, Copy)]
pub struct BlockSyntaxConfig<'a> {
    /// Opening block delimiter after a signature (e.g. `" {"`, `":"`).
    pub block_open: &'a str,
    /// Closing block delimiter (e.g. `"}"`, `""`).
    pub block_close: &'a str,
    /// Indentation unit (e.g. `"  "`, `"\t"`).
    pub indent_unit: &'a str,
    /// Whether statements end with semicolons.
    pub uses_semicolons: bool,
    /// Terminator after a field declaration (e.g. `","`, `";"`).
    pub field_terminator: &'a str,
    /// Terminator after a type's closing brace (e.g. `";"` for C structs).
    pub type_close_terminator: &'a str,
    /// Closing delimiter for base class / implements list (e.g. `")"` for Python).
    pub bases_close: &'a str,
    /// Whether to emit `block_close` during control-flow transitions (e.g. `} else`).
    /// Set to `false` for `end`-delimited languages (Lua, Ruby, Elixir) where
    /// `else`/`elseif` should NOT be preceded by the closing delimiter.
    pub close_on_transition: bool,
}

impl Default for BlockSyntaxConfig<'_> {
    fn default() -> Self {
        Self {
            block_open: " {",
            block_close: "}",
            indent_unit: "  ",
            uses_semicolons: true,
            field_terminator: ",",
            type_close_terminator: "",
            bases_close: "",
            close_on_transition: true,
        }
    }
}

/// Function signature syntax.
#[derive(Debug, Clone, Copy)]
pub struct FunctionSyntaxConfig<'a> {
    /// Separator between parameters and return type (e.g. `" -> "`, `": "`).
    pub return_type_separator: &'a str,
    /// Keyword for async functions as a prefix (e.g. `"async "`).
    pub async_keyword: &'a str,
    /// Keyword for async functions as a suffix after the parameter list
    /// (e.g. Dart: `" async"`). Emitted when `is_async` is set, placed
    /// after the closing `)` and before the block-open brace.
    pub async_suffix: &'a str,
    /// When true, `async_suffix` is emitted between the closing `)` and the
    /// return type arrow (Swift: `func f() async -> T`). When false (default),
    /// it is emitted after the return type (Dart: `Future<T> f() async {`).
    pub async_suffix_before_return: bool,
    /// Keyword for abstract methods (e.g. `"abstract "`, `"virtual "`).
    pub abstract_keyword: &'a str,
    /// Keyword for override methods as an inline modifier (e.g. `"override "`).
    ///
    /// Set to `""` for languages that use an annotation instead (e.g. Java `@Override`).
    pub override_keyword: &'a str,
    /// Annotation emitted on its own line when `is_override` is set and the
    /// language uses an annotation instead of an inline keyword.
    ///
    /// For example, Java sets this to `"@Override"`. When non-empty, it is
    /// emitted as a line before the function signature. Languages that use an
    /// inline keyword (Kotlin, Swift, C#) leave this empty.
    pub override_annotation: &'a str,
    /// How parameter lists are formatted (tupled vs curried).
    pub param_list_style: ParamListStyle,
    /// How signatures are rendered (merged vs split type signature).
    pub function_signature_style: FunctionSignatureStyle,
    /// Function keyword for constructors (e.g. `""`, `"def"`, `"fn"`).
    pub constructor_keyword: &'a str,
    /// Where constructor delegation calls are placed (body vs signature).
    pub constructor_delegation_style: ConstructorDelegationStyle,
    /// How where-clause constraints are rendered (inline vs block).
    pub where_clause_style: WhereClauseStyle,
    /// Content emitted for abstract methods with no body (e.g. `""`, `"..."`).
    pub empty_body: &'a str,
    /// Whether type parameters appear before the return type instead of after the name.
    ///
    /// Java places generic type parameters between modifiers and return type:
    /// `public static <T extends Comparable> List<T> sortList(...)`.
    /// Most other languages place them after the function name:
    /// `fun <T> sortList(...)` or `fn sort_list<T>(...)`.
    pub type_params_before_return_type: bool,
    /// Whether to suppress the `async` keyword for interface/trait member declarations.
    ///
    /// C# sets this to `true` because interface methods declare the contract
    /// (return type `Task<T>`) without the `async` implementation detail.
    /// Swift sets this to `false` because protocols include `async` in signatures.
    pub suppress_async_in_interface: bool,
    /// Keyword emitted for static methods/fields. Default `"static "`.
    /// Python sets to `""` since static is expressed via decorators.
    pub static_keyword: &'a str,
}

impl Default for FunctionSyntaxConfig<'_> {
    fn default() -> Self {
        Self {
            return_type_separator: ": ",
            async_keyword: "async ",
            async_suffix: "",
            async_suffix_before_return: false,
            abstract_keyword: "abstract ",
            override_keyword: "override ",
            override_annotation: "",
            param_list_style: ParamListStyle::Tupled,
            function_signature_style: FunctionSignatureStyle::Merged,
            constructor_keyword: "",
            constructor_delegation_style: ConstructorDelegationStyle::Body,
            where_clause_style: WhereClauseStyle::Inline,
            empty_body: "",
            type_params_before_return_type: false,
            suppress_async_in_interface: false,
            static_keyword: "static ",
        }
    }
}

/// Type declaration syntax (inheritance, annotation, field order).
#[derive(Debug, Clone, Copy)]
pub struct TypeDeclSyntaxConfig<'a> {
    /// Whether type annotations use type-before-name order (e.g. C: `int count`).
    pub type_before_name: bool,
    /// Whether the return type appears before the function name (e.g. C: `int add(...)`).
    pub return_type_is_prefix: bool,
    /// Separator between name and type annotation (e.g. `": "`).
    pub type_annotation_separator: &'a str,
    /// Keyword for super type / base class (e.g. `" extends "`, `": "`).
    pub super_type_keyword: &'a str,
    /// Separator between super types (e.g. `", "`, `", public "`).
    pub super_type_separator: &'a str,
    /// Override separator for 2nd+ super type (e.g. Scala: `Some(" with ")`).
    pub super_type_subsequent_separator: Option<&'a str>,
    /// Keyword for interface implementation (e.g. `" implements "`).
    pub implements_keyword: &'a str,
    /// Whether type alias uses `typedef target name` order (C).
    pub type_alias_target_first: bool,
    /// Whether the language supports primary constructors on type declarations.
    pub supports_primary_constructor: bool,
}

impl Default for TypeDeclSyntaxConfig<'_> {
    fn default() -> Self {
        Self {
            type_before_name: false,
            return_type_is_prefix: false,
            type_annotation_separator: ": ",
            super_type_keyword: "",
            super_type_separator: ", ",
            super_type_subsequent_separator: None,
            implements_keyword: "",
            type_alias_target_first: false,
            supports_primary_constructor: false,
        }
    }
}

/// How an enum variant's value is formatted.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum VariantValueFormat {
    /// `NAME = value` — TypeScript, Rust, C, Swift, etc.
    #[default]
    Assignment,
    /// `NAME(value)` — Java, Kotlin constructor-arg style.
    ConstructorArg,
}

/// Enum variant formatting, annotation syntax, and field mutability keywords.
#[derive(Debug, Clone, Copy)]
pub struct EnumAndAnnotationConfig<'a> {
    /// Prefix before each enum variant (e.g. `""`, `"case "`).
    pub variant_prefix: &'a str,
    /// Prefix before the first variant only. `None` = same as `variant_prefix`.
    pub variant_prefix_first: Option<&'a str>,
    /// Separator after each variant (e.g. `","`).
    pub variant_separator: &'a str,
    /// Whether the separator appears after the last variant too.
    pub variant_trailing_separator: bool,
    /// How the variant value is rendered (assignment `= val` vs constructor `(val)`).
    pub variant_value_format: VariantValueFormat,
    /// Whether enum variants are emitted before fields (Java/Kotlin pattern).
    pub variants_before_fields: bool,
    /// Terminator emitted after the last variant when fields/methods follow (e.g. `";"`).
    pub variant_section_terminator: &'a str,
    /// Prefix wrapping an annotation name (e.g. `"@"`, `"#["`).
    pub annotation_prefix: &'a str,
    /// Suffix closing an annotation (e.g. `""`, `"]"`).
    pub annotation_suffix: &'a str,
    /// Keyword for readonly/immutable fields (e.g. `"const "`, `"readonly "`).
    pub readonly_keyword: &'a str,
    /// Keyword for mutable fields (e.g. `""`, `"var "`).
    pub mutable_field_keyword: &'a str,
}

impl Default for EnumAndAnnotationConfig<'_> {
    fn default() -> Self {
        Self {
            variant_prefix: "",
            variant_prefix_first: None,
            variant_separator: ",",
            variant_trailing_separator: false,
            variant_value_format: VariantValueFormat::Assignment,
            variants_before_fields: false,
            variant_section_terminator: "",
            annotation_prefix: "@",
            annotation_suffix: "",
            readonly_keyword: "const ",
            mutable_field_keyword: "",
        }
    }
}