Skip to main content

Crate ontogen_ts

Crate ontogen_ts 

Source
Expand description

Rust AST → TypeScript emitter for ontogen’s long-tail type bindings.

This crate is the build-time replacement for the OF-014 spike’s specta side-car. Given a set of root types, a pool of candidate type definitions (syn::Item keyed by canonical TypePath), and an EmitConfig, it produces TypeScript source covering the supported subset documented in OF-015:

  • Named structs over primitive / container / smart-pointer / reference field types
  • C-style enums (and tagged enums where the tag is implicit from variant idents)
  • Vec<T>, Option<T>, HashMap<K, V>, BTreeMap<K, V>
  • Primitives: bool, all integer types, f32/f64, String, &str
  • Smart-pointer wrappers (Box, Rc, Arc, Cow, Pin) peeled silently
  • External types via EmitConfig::external_types
  • #[serde(flatten)] on a struct field (or an enum struct-variant field), emitted as a TS intersection: StepMeta & { program: string }
  • The full rename family, each on the axis serde gives it: rename_all (a container’s members), rename_all_fields (an enum’s struct-variant fields), rename_all on a variant (that variant’s fields), rename

See docs/tasks/OF-015-productionize-typescript-generation.md for the full design pass.

§PR series state (PR 4 of 8)

PR 1-3 landed the crate scaffold, per-type emission, serde rename family, type-pool walker, use-resolution, external-types table, and topological ordering. PR 4 wires them together: the top-level emit function now composes collection → name resolution → collision detection → topological ordering → per-type emission → error aggregation, all in one pass. The #[ts_opaque(target = "...")] and #[ts_name = "..."] proc-macro attrs (shipped in ontogen-macros) are read here to short-circuit emission for opaque types and override TS names. External-types lookup is wired into emit_type’s fall-through so types like chrono::DateTime resolve to string per the shipped defaults.

PR 5 wires ontogen itself to call emit instead of the side-car emitter.

Structs§

EmitConfig
Per-build configuration for an crate::emit call.
ModuleImports
Per-module use tables for a scanned source tree, keyed by the module’s canonical path segments (empty = crate root). The keys mirror the type pool’s key prefixes, so a referencing item’s module — the pool key with its terminal dropped — looks up directly.
TypePath
Fully-qualified canonical path to a type in the user’s crate (or an external crate).

Enums§

BigIntBehavior
How u64 / i64 / usize / isize are rendered in TypeScript.
EmitError
Every way emission can fail.
QuoteStyle
How TypeScript string literals are quoted in emitted source.
RenameAll
Serde’s eight rename_all modes.
Resolution
Outcome of resolving a type reference against the pool.
ScanError
Failure modes for scan_src_dir.
TypePathError
Construction error for TypePath.

Constants§

LOCAL_CRATE_ROOT
First key segment for types scanned from the consuming crate’s own src/.

Functions§

emit
Emit TypeScript source for roots and everything they transitively reach in type_pool, honoring config.
emit_with_imports
Like emit, but resolves bare single-segment references through imports — each referencing module’s use table — so a type pulled in via use (possibly through several re-export hops) links to the right pool key even when several modules define same-terminal types.
render_type
Render one Rust type as TypeScript, with no type pool and no surrounding declaration.
render_type_str
Parse rust_ty as a Rust type expression and render it via render_type.
resolve_reference
Resolve a type reference — segments are the reference’s path idents with generic args already stripped (["BackupManifest"], ["crate","models","Workout"]) — written in module (the referencing item’s canonical module path, i.e. its pool key minus the terminal).
scan_crate_root_with_imports
Scan a src/ directory as the crate named crate_root, so every pool key and every module-imports key begins with that segment.
scan_src_dir
Scan a src/ directory and collect every module-level struct, enum, and type-alias into a pool keyed by canonical TypePath, rooted at LOCAL_CRATE_ROOT.
scan_src_dir_with_imports
Scan a src/ directory, returning both the type pool and the per-module ModuleImports tables built from each module’s use declarations.