Skip to main content

Module serialisation

Module serialisation 

Source
Expand description

Per-type serialise / deserialise helper generation for workers mode (v0.8 §3.4 / §5.2).

Every Bynk type that crosses a context boundary needs:

  • serialise_<Type>(value): JsonValue — structural lowering.
  • deserialise_<Type>(json): Result<<Type>, BoundaryError> — structural validation + refinement re-validation, then a nominal cast back to the receiving context’s view.

Helpers live in the owning module — commons modules emit helpers for commons types, context modules emit helpers for the types they declare.

Functions§

collect_codec_closure
v0.22b: the codec closure for a set of Json.encode/Json.decode[T] target type-refs — the named types needing per-type helpers (transitively through record fields and sum payloads) plus the generic instantiations needing specialised helpers. The same closure logic as the boundary collectors, rooted at expressions instead of service signatures.
decls_as_stmts
Collects decls into real statements — the shared shape every caller of emit_helpers_for_owner/emit_generic_helpers (and their _qualified twins) collects into its own returned Vec<TsStmt>. Arc F slice 1 (#1451): consolidates what used to be ten separate write!-family call sites into this one; #1478 made it the one real implementation (dropping a hand-duplicated out: &mut String-printing sibling); #1479 removed that sibling’s own last two production callers (project/ tests_emit.rs’s codec-helper glue), so every caller in the crate is on this Vec<TsStmt> form directly now. #1486: no more explicit blank TsStmt after each declaration — none of these are import/comment-kind, so the printer’s automatic top-level spacing policy already supplies exactly one blank between every adjacent pair (and after the last one, before whatever follows) once a real caller prints through it.
decls_as_stmts_block
As decls_as_stmts, plus one extra forced trailing blank TsStmt when anything was collected — the shared if emitted_any { ... } trailer every emit_helpers_for_owner/[emit_helpers_for_owner_ qualified] caller needs. Under the pre-#1486 flat-print regime this trailer’s own blank stacked on top of decls_as_stmts’s own per-decl blank (one TsStmt::blank after every decl, including the last), giving this group’s own tail two blank lines total — a genuine, stronger “owner group” section break (confirmed against 118_workers_target_with_consume’s own real fixture output: the local_boundary helpers group and the following by_commons re-export group are separated by two blank lines, not one), not a redundant duplicate of the ordinary one-blank-between-decls spacing decls_as_stmts now gets for free from the printer’s automatic policy. no_blank_before on the extra blank itself suppresses its own automatic lead-in (which would otherwise stack with the ordinary one the policy already supplies between the last real decl and this blank), leaving exactly one blank line from this statement’s own render plus the policy’s own ordinary blank before whatever follows — two lines total, matching the pre-#1486 text exactly.
deserialise_ref_via
v0.176 (#642): a deserialise reference for ns, shaped to callService’s deserialiseResult parameter. The inline arms become a lambda rather than the unvalidated ((j: any) => ({ tag: "Ok", value: j })) identity the caller path used to fall back to.
emit_generic_helpers_qualified
#661: as emit_generic_helpers, but the value-type positions of each specialised helper are named through the type Qual — so a consumer’s deserialise_Result_AuthId_PaymentError returns Result<commerce_payment.AuthId, commerce_payment.PaymentError> while its codec calls stay local. The codec suffix (Result_AuthId_PaymentError) is namespace-independent by construction, which is exactly what keeps the caller’s and callee’s names in agreement across the wire.
emit_helpers_for_owner_qualified
#661: as emit_helpers_for_owner, but the caller supplies a type Qual. With an empty qualifier this is the owner’s own module (every type named bare, refined validation through .of). With a non-empty one it is a consumer generating its own view of another context’s boundary types: the qualified names reach through the import type * as <ns> alias, and refined validation inlines (transparent) or casts structurally (opaque) because the owner’s .of is not importable.
root_names_any
#3066: whether a codec root names one of names (a suite’s own types) anywhere, through generic arguments. Such a root is served by the suite runner’s own local codecs (super::emit::emit_suite_decls), not the module-level closure, which cannot name a type declared inside a runner.
serialise_expr_via
v0.176 (#642): the one serialise dispatch for the workers cross-context boundary, reaching helpers through ns. Replaces the two parallel dispatches this boundary used to carry — emit.rs’s workers_serialise_expr (which dropped List/Map to a bare as JsonValue cast) and workers_entry.rs’s serialise_call (which did the same to Bytes, the asymmetry that forced bynk.types.bytes_at_workers_boundary).