Generate Rust enum shells from spec literalUnions#101
Merged
Conversation
This was referenced Jun 10, 2026
Merged
Member
Author
This was referenced Jun 10, 2026
Member
Author
Merge activity
|
This PR teaches the generator to emit Rust `enum` shells from spec `literalUnion` TypeExprs, parallel to the enumeration support added in the previous PR. The sole `literalUnion` in v1 is `isSigner: [true, false, "either"]`, referenced by `instructionAccountNode.isSigner` and `instructionRemainingAccountsNode.isSigner` (two value-identical inline copies); both consumers collapse to a single generated type. Because `literalUnion` is anonymous and inline in the spec — there's no name registry, unlike `enumerations` — the type name is derived from the referencing attribute (`pascalCase('isSigner')` → `IsSigner`). Distinct value-sets become distinct types; the same value-set referenced by two differently-named attributes throws as ambiguous.
The renderer uses a deliberately narrower derive set than `enumPage` does: `Debug, PartialEq, Eq, Clone, Copy` only — no `Serialize`/`Deserialize`, no `Default`, no `rename_all`. A `literalUnion`'s wire form is heterogeneous (real JSON booleans for the boolean variants, a string for the `"either"` case) and can't be expressed via serde's declarative attributes; the bespoke `Serialize`/`Deserialize` visitor pair, the `From<bool>` conversion, and the `Default` choice all stay hand-written in a companion file. This is the same generated-shell / hand-written-impls split established by `Number` and re-used for the enumeration `Default` impls in the previous PR.
Reconciling the Rust crate required one breaking rename: `IsAccountSigner → IsSigner`, since the generator derives the type name from the attribute name. ~45 occurrences across `codama-nodes` (2 node files + `shared/`) and `codama-attributes` (the `FromMeta` impl + `account_directive` tests). The hand-written `shared/is_account_signer.rs` (104 lines, enum + impls + tests) becomes `shared/is_signer.rs` (impls + tests only — 122 lines, +2 tests covering `From<bool>` and `default_is_false` that weren't previously covered). One small wiring note: `attributeBodyLine.ts` resolves the literalUnion field type itself (because `getTypeExprFragment` has no access to the attribute name), while `typeExpr.ts`'s `literalUnion` case continues to throw — only nested-position literalUnions would reach it, and those don't occur in v1.
Workspace-wide: 1006 → 1008 cargo tests passing (+2 from the new hand-written tests), 119 → 130 JS tests passing (+11 from the new discovery + renderer + integration tests), fmt and clippy stay clean, `pnpm generate` round-trips deterministically. The override surface (3 maps, `FIELD_TYPE_OVERRIDES` with 1 entry) is unchanged.
9e97b64 to
67a832b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This PR teaches the generator to emit Rust
enumshells from specliteralUnionTypeExprs, parallel to the enumeration support added in the previous PR. The soleliteralUnionin v1 isisSigner: [true, false, "either"], referenced byinstructionAccountNode.isSignerandinstructionRemainingAccountsNode.isSigner(two value-identical inline copies); both consumers collapse to a single generated type. BecauseliteralUnionis anonymous and inline in the spec — there's no name registry, unlikeenumerations— the type name is derived from the referencing attribute (pascalCase('isSigner')→IsSigner). Distinct value-sets become distinct types; the same value-set referenced by two differently-named attributes throws as ambiguous.The renderer uses a deliberately narrower derive set than
enumPagedoes:Debug, PartialEq, Eq, Clone, Copyonly — noSerialize/Deserialize, noDefault, norename_all. AliteralUnion's wire form is heterogeneous (real JSON booleans for the boolean variants, a string for the"either"case) and can't be expressed via serde's declarative attributes; the bespokeSerialize/Deserializevisitor pair, theFrom<bool>conversion, and theDefaultchoice all stay hand-written in a companion file. This is the same generated-shell / hand-written-impls split established byNumberand re-used for the enumerationDefaultimpls in the previous PR.Reconciling the Rust crate required one breaking rename:
IsAccountSigner → IsSigner, since the generator derives the type name from the attribute name. ~45 occurrences acrosscodama-nodes(2 node files +shared/) andcodama-attributes(theFromMetaimpl +account_directivetests). The hand-writtenshared/is_account_signer.rs(104 lines, enum + impls + tests) becomesshared/is_signer.rs(impls + tests only — 122 lines, +2 tests coveringFrom<bool>anddefault_is_falsethat weren't previously covered). One small wiring note:attributeBodyLine.tsresolves the literalUnion field type itself (becausegetTypeExprFragmenthas no access to the attribute name), whiletypeExpr.ts'sliteralUnioncase continues to throw — only nested-position literalUnions would reach it, and those don't occur in v1.Workspace-wide: 1006 → 1008 cargo tests passing (+2 from the new hand-written tests), 119 → 130 JS tests passing (+11 from the new discovery + renderer + integration tests), fmt and clippy stay clean,
pnpm generateround-trips deterministically. The override surface (3 maps,FIELD_TYPE_OVERRIDESwith 1 entry) is unchanged.