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
| Specifier | Name | Argument Type | Purpose |
|---|---|---|---|
%T | Type | TypeName | Emit type reference, track import |
%N | Name | &str or Nameable | Emit identifier name |
%S | String | &str | Emit escaped string literal |
%L | Literal | &str, number, CodeBlock | Emit raw value or nested block |
%W | Wrap | (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
CodeNodetrees 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
CodeBlocktrees 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.