Skip to main content

Module codegen

Module codegen 

Source
Expand description

TypeScript codegen for cargo taut gen (Phase 1 + 2 error narrowing, Phase 4 validator schemas).

Pure logic: takes an taut_rpc::ir::Ir document and renders a single api.gen.ts source string. The CLI’s gen subcommand owns the I/O and validator-flag parsing; this module owns the string-building.

See SPEC.md §3 (type mapping), §3.3/§4.1 (error envelope), §6 (client API), and §7 (validation bridge). The shape of the generated client mirrors the runtime declarations in npm/taut-rpc/src/index.ts — every emitted procedure is a ProcedureDef<I, O, E, Kind>, and the final Procedures type map is what createClient<Procedures> consumes.

Phase 2 additions:

  • For each procedure with a non-empty error set we also emit a Proc_<name>_Error type alias right before its ProcedureDef. Callers import this alias to drive e.code narrowing without touching unknown.
  • We emit a runtime procedureKinds const (as const satisfies …) so createClient can dispatch query vs. mutation vs. subscription without the caller having to thread the map manually. createApi defaults to it but still lets users override via opts.kinds.

Phase 4 additions:

  • For each TypeDef we additionally emit a <Name>Schema const using the selected validator (valibot is the default, zod is opt-in). The pre-existing TS interface is preserved verbatim so callers who only want types can keep ignoring the schema.
  • For each procedure we emit Proc_<name>_inputSchema and Proc_<name>_outputSchema constants — either an alias to an existing <Name>Schema (for Named types) or an inline expression (for primitives / composites that don’t have a TypeDef of their own).
  • We emit a procedureSchemas runtime map mirroring procedureKinds so createClient can locate a procedure’s schemas by name.

Structs§

CodegenOptions
Per-invocation knobs for render_ts.

Enums§

Validator
Validator runtime to target in the generated client.

Functions§

render_ts
Render an Ir into a single api.gen.ts source string.
render_ts_checked
Fallible variant of render_ts.