Skip to content

Commit 9e97b64

Browse files
committed
Generate Rust enum shells from spec literalUnions
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.
1 parent b97591f commit 9e97b64

18 files changed

Lines changed: 514 additions & 138 deletions

File tree

codama-attributes/src/codama_directives/account_directive.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use crate::{
44
};
55
use codama_errors::{CodamaError, CodamaResult};
66
use codama_nodes::{
7-
CamelCaseString, Docs, InstructionAccountNode, InstructionInputValueNode, IsAccountSigner,
7+
CamelCaseString, Docs, InstructionAccountNode, InstructionInputValueNode, IsSigner,
88
};
99
use codama_syn_helpers::{extensions::*, Meta};
1010

1111
#[derive(Debug, PartialEq)]
1212
pub struct AccountDirective {
1313
pub name: CamelCaseString,
1414
pub is_writable: bool,
15-
pub is_signer: IsAccountSigner,
15+
pub is_signer: IsSigner,
1616
pub is_optional: bool,
1717
pub docs: Docs,
1818
pub default_value: Option<Resolvable<InstructionInputValueNode>>,
@@ -29,7 +29,7 @@ impl AccountDirective {
2929
name = name.initial_value(ident.to_string().into())
3030
}
3131
let mut is_writable = SetOnce::<bool>::new("writable").initial_value(false);
32-
let mut is_signer = SetOnce::<IsAccountSigner>::new("signer").initial_value(false.into());
32+
let mut is_signer = SetOnce::<IsSigner>::new("signer").initial_value(false.into());
3333
let mut is_optional = SetOnce::<bool>::new("optional").initial_value(false);
3434
let mut default_value =
3535
SetOnce::<Resolvable<InstructionInputValueNode>>::new("default_value");
@@ -41,7 +41,7 @@ impl AccountDirective {
4141
.each(|ref meta| match meta.path_str().as_str() {
4242
"name" => name.set(meta.as_value()?.as_expr()?.as_string()?.into(), meta),
4343
"writable" => is_writable.set(bool::from_meta(meta)?, meta),
44-
"signer" => is_signer.set(IsAccountSigner::from_meta(meta)?, meta),
44+
"signer" => is_signer.set(IsSigner::from_meta(meta)?, meta),
4545
"optional" => is_optional.set(bool::from_meta(meta)?, meta),
4646
"default_value" => default_value.set(
4747
Resolvable::<InstructionInputValueNode>::from_meta(meta.as_value()?)?,
@@ -117,7 +117,7 @@ mod tests {
117117
AccountDirective {
118118
name: "payer".into(),
119119
is_writable: true,
120-
is_signer: IsAccountSigner::True,
120+
is_signer: IsSigner::True,
121121
is_optional: true,
122122
default_value: Some(Resolvable::Resolved(PayerValueNode::new().into())),
123123
docs: Docs::default(),
@@ -142,7 +142,7 @@ mod tests {
142142
AccountDirective {
143143
name: "payer".into(),
144144
is_writable: true,
145-
is_signer: IsAccountSigner::Either,
145+
is_signer: IsSigner::Either,
146146
is_optional: false,
147147
default_value: Some(Resolvable::Resolved(PayerValueNode::new().into())),
148148
docs: Docs::default(),
@@ -161,7 +161,7 @@ mod tests {
161161
AccountDirective {
162162
name: "authority".into(),
163163
is_writable: false,
164-
is_signer: IsAccountSigner::False,
164+
is_signer: IsSigner::False,
165165
is_optional: false,
166166
default_value: None,
167167
docs: Docs::default(),
@@ -189,7 +189,7 @@ mod tests {
189189
AccountDirective {
190190
name: "stake".into(),
191191
is_writable: true,
192-
is_signer: IsAccountSigner::False,
192+
is_signer: IsSigner::False,
193193
is_optional: false,
194194
default_value: None,
195195
docs: vec!["what this account is for".to_string()].into(),
@@ -208,7 +208,7 @@ mod tests {
208208
AccountDirective {
209209
name: "authority".into(),
210210
is_writable: false,
211-
is_signer: IsAccountSigner::True,
211+
is_signer: IsSigner::True,
212212
is_optional: false,
213213
default_value: None,
214214
docs: vec![

codama-attributes/src/utils/from_meta.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use codama_nodes::{BytesEncoding, Docs, IsAccountSigner, OptionalAccountStrategy};
1+
use codama_nodes::{BytesEncoding, Docs, IsSigner, OptionalAccountStrategy};
22
use codama_syn_helpers::{extensions::*, Meta};
33
use syn::Expr;
44

@@ -20,7 +20,7 @@ impl FromMeta for bool {
2020
}
2121
}
2222

23-
impl FromMeta for IsAccountSigner {
23+
impl FromMeta for IsSigner {
2424
fn from_meta(meta: &Meta) -> syn::Result<Self> {
2525
if let Ok(value) = bool::from_meta(meta) {
2626
return Ok(value.into());
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
2+
pub enum IsSigner {
3+
True,
4+
False,
5+
Either,
6+
}

codama-nodes/src/generated/shared/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod bytes_encoding;
22
mod default_value_strategy;
33
mod endianness;
44
mod instruction_lifecycle;
5+
mod is_signer;
56
mod number_format;
67
mod optional_account_strategy;
78
mod post_offset_strategy;
@@ -12,6 +13,7 @@ pub use bytes_encoding::*;
1213
pub use default_value_strategy::*;
1314
pub use endianness::*;
1415
pub use instruction_lifecycle::*;
16+
pub use is_signer::*;
1517
pub use number_format::*;
1618
pub use optional_account_strategy::*;
1719
pub use post_offset_strategy::*;

codama-nodes/src/instruction_account_node.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::{CamelCaseString, Docs, HasName, InstructionInputValueNode, IsAccountSigner};
1+
use crate::{CamelCaseString, Docs, HasName, InstructionInputValueNode, IsSigner};
22
use codama_nodes_derive::node;
33

44
#[node]
55
pub struct InstructionAccountNode {
66
// Data.
77
pub name: CamelCaseString,
88
pub is_writable: bool,
9-
pub is_signer: IsAccountSigner,
9+
pub is_signer: IsSigner,
1010
#[serde(default, skip_serializing_if = "crate::is_default")]
1111
pub is_optional: bool,
1212
#[serde(default, skip_serializing_if = "crate::is_default")]
@@ -21,7 +21,7 @@ impl InstructionAccountNode {
2121
pub fn new<T, U>(name: T, is_writable: bool, is_signer: U) -> Self
2222
where
2323
T: Into<CamelCaseString>,
24-
U: Into<IsAccountSigner>,
24+
U: Into<IsSigner>,
2525
{
2626
Self {
2727
name: name.into(),
@@ -50,7 +50,7 @@ mod tests {
5050
let node = InstructionAccountNode::new("my_account", false, true);
5151
assert_eq!(node.name, CamelCaseString::new("myAccount"));
5252
assert!(!node.is_writable);
53-
assert_eq!(node.is_signer, IsAccountSigner::True);
53+
assert_eq!(node.is_signer, IsSigner::True);
5454
assert!(!node.is_optional);
5555
assert_eq!(node.docs, Docs::default());
5656
assert_eq!(node.default_value, None);
@@ -61,14 +61,14 @@ mod tests {
6161
let node = InstructionAccountNode {
6262
name: "myAccount".into(),
6363
is_writable: false,
64-
is_signer: IsAccountSigner::Either,
64+
is_signer: IsSigner::Either,
6565
is_optional: true,
6666
docs: vec!["Hello".to_string()].into(),
6767
default_value: Some(AccountValueNode::new("myOtherAccount").into()),
6868
};
6969
assert_eq!(node.name, CamelCaseString::new("myAccount"));
7070
assert!(!node.is_writable);
71-
assert_eq!(node.is_signer, IsAccountSigner::Either);
71+
assert_eq!(node.is_signer, IsSigner::Either);
7272
assert!(node.is_optional);
7373
assert_eq!(*node.docs, vec!["Hello".to_string()]);
7474
assert_eq!(

codama-nodes/src/instruction_remaining_accounts_node.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ArgumentValueNode, Docs, IsAccountSigner, ResolverValueNode};
1+
use crate::{ArgumentValueNode, Docs, IsSigner, ResolverValueNode};
22
use codama_nodes_derive::{node, node_union};
33

44
#[node]
@@ -7,7 +7,7 @@ pub struct InstructionRemainingAccountsNode {
77
#[serde(default, skip_serializing_if = "crate::is_default")]
88
pub is_optional: bool,
99
#[serde(default, skip_serializing_if = "crate::is_default")]
10-
pub is_signer: IsAccountSigner,
10+
pub is_signer: IsSigner,
1111
#[serde(default, skip_serializing_if = "crate::is_default")]
1212
pub is_writable: bool,
1313
#[serde(default, skip_serializing_if = "crate::is_default")]
@@ -31,13 +31,13 @@ mod tests {
3131
fn direct_instantiation() {
3232
let node = InstructionRemainingAccountsNode {
3333
is_optional: false,
34-
is_signer: IsAccountSigner::Either,
34+
is_signer: IsSigner::Either,
3535
is_writable: true,
3636
docs: vec!["This is a test".to_string()].into(),
3737
value: ArgumentValueNode::new("myArgument").into(),
3838
};
3939
assert!(!node.is_optional);
40-
assert_eq!(node.is_signer, IsAccountSigner::Either);
40+
assert_eq!(node.is_signer, IsSigner::Either);
4141
assert!(node.is_writable);
4242
assert_eq!(node.docs, vec!["This is a test".to_string()].into());
4343
assert_eq!(
@@ -50,7 +50,7 @@ mod tests {
5050
fn to_json() {
5151
let node = InstructionRemainingAccountsNode {
5252
is_optional: false,
53-
is_signer: IsAccountSigner::Either,
53+
is_signer: IsSigner::Either,
5454
is_writable: true,
5555
docs: vec![].into(),
5656
value: ArgumentValueNode::new("myArgument").into(),
@@ -70,7 +70,7 @@ mod tests {
7070
node,
7171
InstructionRemainingAccountsNode {
7272
is_optional: false,
73-
is_signer: IsAccountSigner::Either,
73+
is_signer: IsSigner::Either,
7474
is_writable: true,
7575
docs: vec![].into(),
7676
value: ArgumentValueNode::new("myArgument").into(),

codama-nodes/src/shared/is_account_signer.rs

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)