rust-codegen-group/sigil-stitch
À propos
Type-safe, import-aware, width-aware code generation across multiple languages
Outils de développementProgression mesurée des étoiles
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- Historique en cours
Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.
README.md
Originalsigil-stitch
Type-safe, import-aware, width-aware code generation for multiple languages.
sigil-stitch is a structured, language-aware code generation toolkit for Rust. Build source
with composable CodeBlocks, declaration specs, or the sigil_quote! macro. Semantic types,
functions, properties, enums, closed sums, annotations, and imports stay structured until a
selected language adapter validates the intent and lowers it to target syntax. File and project
rendering then rewrites and validates the prepared source tree, resolves import aliases and
naming conflicts, and applies Wadler-Lindig layout for width-aware output across the supported
languages.
Quick Start
cargo add sigil-stitch
Requires Rust edition 2024, MSRV 1.88.0. Runtime dependencies: pretty (Wadler-Lindig formatting), serde with derive (first-class serializable specs and source values support same-version round-trips with a caller-provided Serde format such as JSON or YAML), and snafu (structured errors). FileSpec::add_spec() extension members are not serializable; a deserialized FileSpec, or each file inside a deserialized ProjectSpec, needs a language configured again before rendering. These Serde representations are not a versioned wire protocol.
Builder vs Macro
sigil-stitch offers two ways to build code. Both produce the same CodeBlock with
the same import tracking and rendering.
Builder API -- programmatic, good for dynamic code generation:
use sigil_stitch::prelude::*;
use sigil_stitch::code_block::StringLitArg;
let user_type = TypeName::importable_type("./models", "User");
let mut cb = CodeBlock::builder();
cb.add_statement(
"const user: %T = await getUser(%S)",
(user_type.clone(), StringLitArg("id".into())),
);
cb.add_statement("return user", ());
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'"));
assert!(output.contains("const user: User = await getUser('id');"));
sigil_quote! macro -- inline target-language code, less ceremony:
use sigil_stitch::prelude::*;
use sigil_stitch::lang::typescript::TypeScript;
let user_type = TypeName::importable_type("./models", "User");
let body = sigil_quote!(TypeScript {
const user: $T(user_type) = await getUser($S("id"));
if (!user) {
throw new Error($S("not found"));
}
return user;
}).unwrap();
The macro uses $T/$S/$N/$L/$C/$V/$W interpolation markers that expand to
the equivalent %T/%S/%N/%L/%V format specifiers at compile time. It also
supports $V("@{expr}") for compile-time interpolation in verbatim strings,
$attr("text") for structural annotations with language-specific rendering,
$T_join(sep, iter) for type-name joins with per-item import tracking,
$C_each for splicing iterables of code blocks, $if/$else_if/$else
for meta-conditionals, $for for compile-time iteration, $let for Rust-level
variable bindings, $join for separator-joined lists, and $+ for line
continuation in multi-line expressions. Go const (, var (, import (, and
type ( paren-delimited blocks are recognized as structural blocks so $for,
$if, and $C_each expand inside them.
Format Specifiers
| Specifier | Name | Argument Type | Purpose |
|---|---|---|---|
%T |
Type | TypeName |
Emit type reference, track import |
%N |
Name | NameArg |
Emit identifier name, escape reserved words |
%S |
String | StringLitArg |
Emit escaped string literal |
%V |
Verbatim | VerbatimStrArg |
Emit string with interpolation preserved |
%L |
Literal | &str, String, CodeBlock, CodeFragment, FloatValue |
Compose raw source, nested code, or an exact floating value |
%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) |
Bare &str maps to %L. Use NameArg for %N, StringLitArg for %S, and VerbatimStrArg for %V.
See the Format Specifiers chapter for the full deep dive.
For bit-exact Rust floating values, FloatValue::from_f32(value) and
FloatValue::from_f64(value) capture the original bits. Pass a FloatValue
directly through %L or macro $L; the file or renderer's selected language
constructs the expression during source preparation. Rust emits from_bits,
while other built-in targets explicitly reject the request at render time.
This is value construction, not a singleton type or quantization API. See
Floating Values for raw bit constructors and scope.
Declaration Specs
Build structured declarations with the spec builders:
| Spec | Purpose |
|---|---|
| ParameterSpec | Function parameter (name + type + default + variadic + property + storage metadata) |
| FieldSpec | Struct field / class property (visibility, static, readonly) |
| FunSpec | Function or method (params, ordered return slots, body, async, abstract) |
| TypeSpec | Class, struct, interface, trait, enum, type alias, newtype, embedded types |
| ClosedSumSpec | A complete named set of unit, positional, or record cases |
| PropertySpec | Computed property with getter/setter |
| AnnotationSpec | @Override, #[derive(...)], [[nodiscard]] |
| EnumVariantSpec | Enum variant with a discriminant, constructor arguments, or positional/record payload |
| ImportSpec | Explicit imports (aliased, side-effect, wildcard) |
| FileSpec | Top-level file with automatic import resolution |
| ProjectSpec | Multi-file project generation with pre-write validation |
| CodeTemplate | Reusable parameterized templates with named parameters |
Specs record declaration intent. At render time the selected language validates
that intent and lowers it to structured CodeBlocks, so type references remain
available to import tracking and alias resolution.
FileSpec rewrites and validates each source tree, lowers every complete
TypeName, then collects and resolves the resulting imports before emitting
any source. Callers that need project-specific conflict naming can supply a
borrowed ImportAliasConflictResolver for one file or project render.
See Building Functions & Fields, Building Types & Enums, and Files & Projects for examples and the full API.
Supported Languages
| Language | Extension | Semicolons | Import Style |
|---|---|---|---|
| TypeScript | .ts |
yes | ES modules |
| JavaScript | .js |
yes | ES modules |
| Rust | .rs |
yes | use paths |
| Go | .go |
no | package imports |
| Python | .py |
no | import/from |
| Java | .java |
yes | package imports |
| Kotlin | .kt |
no | package imports |
| Swift | .swift |
no | import module |
| Dart | .dart |
yes | package imports |
| Scala | .scala |
no | import paths |
| Haskell | .hs |
no | import module |
| OCaml | .ml |
no | open module |
| C | .c |
yes | #include |
| C++ | .cpp |
yes | #include/using |
| C# | .cs |
yes | using namespace |
| Lua | .lua |
no | require() |
| Bash | .bash |
no | source |
| Zsh | .zsh |
no | source |
Documentation
The sigil-stitch book covers everything in depth:
User Guide:
- Introduction -- what it is and how the pieces fit together
- Getting Started -- first CodeBlock, first FileSpec, first output
- Format Specifiers -- deep dive on
%T,%N,%S,%V,%L,%W, and friends - TypeName -- semantic type references, import tracking, and language-owned lowering
- Building Functions & Fields -- ParameterSpec, FieldSpec, FunSpec
- Building Types & Enums -- TypeSpec, ClosedSumSpec, ClosedSumCaseSpec, PropertySpec, AnnotationSpec, EnumVariantSpec
- Files & Projects -- ImportSpec, FileSpec, ProjectSpec
- sigil_quote! Macro -- inline code with
$T/$S/$N/$L/$V/$C_each/$if/$for/$let/$join/$+/$attr/$T_joininterpolation, plus Go paren-block support - Code Templates -- reusable
#{name:K}templates - Language Cookbook -- idiomatic recipes per language
Development Guide:
- Architecture -- ownership, source preparation, rendering, and import resolution
- Design -- accepted seams and design invariants
- Declaration Specs and Language Lowering -- declaration intent, capabilities, and language-owned grammar
- TypeName Validation and Lowering -- fallible language-owned type-expression lowering
- Adding a Language -- implementing the CodeLang trait step by step
MSRV
The minimum supported Rust version is 1.88.0 (edition 2024, let-chains).
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Explorer des projets similaires
Chargement…