Skip to main content

Crate sigil_stitch

Crate sigil_stitch 

Source
Expand description

§Sigil-Stitch

Type-safe, import-aware, width-aware code generation for multiple languages.

Sigil-Stitch combines JavaPoet’s builder + CodeBlock model with Wadler-Lindig pretty printing and multi-language support. Reference types with %T in format strings, and the library tracks every import for you, resolves naming conflicts, and emits width-aware formatted output.

§Format Specifiers

SpecifierNameArgument TypePurpose
%TTypeTypeNameEmit type reference, track import
%NName&str or NameableEmit identifier name
%SString&strEmit escaped string literal
%LLiteral&str, number, CodeBlockEmit raw value or nested block
%WWrap(none)Soft line break point
%>Indent(none)Increase indent level
%<Dedent(none)Decrease indent level
%[Statement begin(none)Start of statement
%]Statement end(none)End of statement (appends ; if needed)

§Quick Example

use sigil_stitch::prelude::*;
use sigil_stitch::lang::typescript::TypeScript;

let user_type = TypeName::importable_type("./models", "User");

let mut cb = CodeBlock::builder();
cb.add_statement("const user = await getUser(%S)", ("id",));
cb.add_statement("return user as %T", (user_type.clone(),));
let body = cb.build().unwrap();

let file = FileSpec::builder("user.ts")
    .add_code(body)
    .build().unwrap();

let output = file.render(80).unwrap();
assert!(output.contains("import type { User } from './models'"));

§sigil_quote! Macro

For less ceremony, write target-language code inline with the sigil_quote! macro:

use sigil_stitch::prelude::*;
use sigil_stitch::lang::typescript::TypeScript;

let user_type = TypeName::importable_type("./models", "User");

let block = sigil_quote!(TypeScript {
    const user: $T(user_type) = await getUser($S("id"));
    if (!user) {
        throw new Error($S("not found"));
    }
    return user;
}).unwrap();

Interpolation markers: $T (type), $N (name), $S (string literal), $L (literal), $C (nested code block), $W (soft line break), $$ (escape).

See the full guide at docs/src/sigil_quote.md for control flow, limitations, and advanced usage.

Modules§

code_block
Composable code fragments with format specifiers (%T, %N, %S, %L, etc.).
code_node
Tree-based intermediate representation for code generation. Tree-based intermediate representation for code generation.
code_renderer
Rendering engine that walks CodeNode trees into final output.
code_template
Reusable named-parameter templates that produce CodeBlocks. Reusable parameterized code templates.
error
Error types for sigil-stitch. Error types for sigil-stitch.
import
Import types, grouping, and conflict resolution. Import resolution data structures.
import_collector
Walks CodeBlock trees to extract all import references.
lang
Language abstraction trait and per-language implementations.
name_allocator
Collision-free name allocation for generated identifiers.
prelude
Common re-exports for convenient usage.
spec
Structural builders (TypeSpec, FunSpec, FileSpec, etc.) that emit CodeBlocks.
type_name
Type references with recursive import tracking and pretty-printing.