sigil_stitch/lib.rs
1#![warn(missing_docs)]
2
3//! # Sigil-Stitch
4//!
5//! Type-safe, import-aware, width-aware code generation for multiple languages.
6//!
7//! Sigil-Stitch combines JavaPoet's builder + CodeBlock model with Wadler-Lindig
8//! pretty printing and multi-language support. Reference types with `%T` in format
9//! strings, and the library tracks every import for you, resolves naming conflicts,
10//! and emits width-aware formatted output.
11//!
12//! ## Format Specifiers
13//!
14//! | Specifier | Name | Argument Type | Purpose |
15//! |-----------|------|---------------|---------|
16//! | `%T` | Type | `TypeName` | Emit type reference, track import |
17//! | `%N` | Name | `&str` or `Nameable` | Emit identifier name |
18//! | `%S` | String | `&str` | Emit escaped string literal |
19//! | `%L` | Literal | `&str`, number, `CodeBlock` | Emit raw value or nested block |
20//! | `%W` | Wrap | (none) | Soft line break point |
21//! | `%>` | Indent | (none) | Increase indent level |
22//! | `%<` | Dedent | (none) | Decrease indent level |
23//! | `%[` | Statement begin | (none) | Start of statement |
24//! | `%]` | Statement end | (none) | End of statement (appends `;` if needed) |
25//!
26//! ## Quick Example
27//!
28//! ```rust
29//! use sigil_stitch::prelude::*;
30//! use sigil_stitch::lang::typescript::TypeScript;
31//!
32//! let user_type = TypeName::importable_type("./models", "User");
33//!
34//! let mut cb = CodeBlock::builder();
35//! cb.add_statement("const user = await getUser(%S)", ("id",));
36//! cb.add_statement("return user as %T", (user_type.clone(),));
37//! let body = cb.build().unwrap();
38//!
39//! let file = FileSpec::builder("user.ts")
40//! .add_code(body)
41//! .build().unwrap();
42//!
43//! let output = file.render(80).unwrap();
44//! assert!(output.contains("import type { User } from './models'"));
45//! ```
46//!
47//! ## `sigil_quote!` Macro
48//!
49//! For less ceremony, write target-language code inline with the `sigil_quote!` macro:
50//!
51//! ```
52//! use sigil_stitch::prelude::*;
53//! use sigil_stitch::lang::typescript::TypeScript;
54//!
55//! let user_type = TypeName::importable_type("./models", "User");
56//!
57//! let block = sigil_quote!(TypeScript {
58//! const user: $T(user_type) = await getUser($S("id"));
59//! if (!user) {
60//! throw new Error($S("not found"));
61//! }
62//! return user;
63//! }).unwrap();
64//! ```
65//!
66//! Interpolation markers: `$T` (type), `$N` (name), `$S` (string literal),
67//! `$L` (literal), `$C` (nested code block), `$W` (soft line break), `$$` (escape).
68//!
69//! See the full guide at `docs/src/sigil_quote.md` for control flow, limitations,
70//! and advanced usage.
71
72/// Composable code fragments with format specifiers (`%T`, `%N`, `%S`, `%L`, etc.).
73pub mod code_block;
74/// Tree-based intermediate representation for code generation.
75pub mod code_node;
76/// Rendering engine that walks `CodeNode` trees into final output.
77pub mod code_renderer;
78/// Reusable named-parameter templates that produce `CodeBlock`s.
79pub mod code_template;
80/// Error types for sigil-stitch.
81pub mod error;
82/// Import types, grouping, and conflict resolution.
83pub mod import;
84/// Walks `CodeBlock` trees to extract all import references.
85pub mod import_collector;
86/// Language abstraction trait and per-language implementations.
87pub mod lang;
88/// Collision-free name allocation for generated identifiers.
89pub mod name_allocator;
90/// Structural builders (TypeSpec, FunSpec, FileSpec, etc.) that emit `CodeBlock`s.
91pub mod spec;
92/// Type references with recursive import tracking and pretty-printing.
93pub mod type_name;
94
95/// Common re-exports for convenient usage.
96pub mod prelude {
97 pub use crate::code_block::{CodeBlock, CodeBlockBuilder, NameArg, StringLitArg};
98 pub use crate::code_template::{CodeTemplate, ParamKind};
99 pub use crate::error::SigilStitchError;
100 pub use crate::lang::CodeLang;
101 pub use crate::spec::annotation_spec::AnnotationSpec;
102 pub use crate::spec::enum_variant_spec::EnumVariantSpec;
103 pub use crate::spec::field_spec::FieldSpec;
104 pub use crate::spec::file_spec::{FileMember, FileSpec};
105 pub use crate::spec::fun_spec::{
106 FunSpec, TypeParamKind, TypeParamSpec, WhereClauseStyle, WhereConstraint,
107 };
108 pub use crate::spec::import_spec::ImportSpec;
109 pub use crate::spec::modifiers::{DeclarationContext, Modifiers, TypeKind, Visibility};
110 pub use crate::spec::parameter_spec::ParameterSpec;
111 pub use crate::spec::project_spec::{ProjectSpec, RenderedFile};
112 pub use crate::spec::property_spec::PropertySpec;
113 pub use crate::spec::type_spec::TypeSpec;
114 pub use crate::type_name::TypeName;
115 pub use sigil_stitch_macros::sigil_quote;
116}