Skip to content

Commit 632365f

Browse files
authored
Generate Rust type nodes (#103)
This PR generates 21 of the 28 `type`-category nodes from `@codama/spec`, completing first-class generation support for all 8 spec categories. The 7 nestable wrappers (`fixedSize`, `sizePrefix`, `pre/postOffset`, `sentinel`, `hiddenPrefix/Suffix`) and the four type-category unions stay hand-written, since they carry generic variant types, a cross-category Link member, and bespoke `TryFrom` bridges that don't fit the standard renderers — and they're the natural deletion boundary if a future spec drops nested unions. The generator gains a `NodeMacroFlavor` classifier (`nodeFlavor.ts`) that picks the emission macro per node from the spec: `nestedTypeNode.wrappers` are skipped, `enumVariantTypeNode` members and `structFieldTypeNode` get `#[node]`, and other type nodes get `#[type_node]`. `CategoryRouting` gains an `emitUnions` flag so `type` skips its bespoke unions, and `struct` joins the Rust keyword set so `enumStructVariantTypeNode.struct` renders as `r#struct`. Reconciliation is mostly contained to `codama-nodes/src/type_nodes/`. Spec-driven type changes: `amountTypeNode.decimals → u32`, the enum-variant `discriminator → Option<u32>`, and `optionTypeNode.fixed → Option<bool>`. The box-all-union rule boxes `array/map/set.count → Box<CountNode>` and `structFieldTypeNode.{type, default_value}`, which cascades into a few `codama-attributes`/`codama-koroks`/`codama-korok-visitors` files and their tests. The hand-written nestable nodes are left untouched. 1008 cargo tests pass unchanged; `pnpm test` grew by 8 to 148; fmt, clippy, and the `pnpm generate` round-trip stay clean. The override surface is unchanged.
1 parent a922a04 commit 632365f

71 files changed

Lines changed: 825 additions & 561 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

codama-attributes/src/codama_directives/field_directive.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ impl FieldDirective {
4646
pub fn to_struct_field_type_node(&self) -> CodamaResult<StructFieldTypeNode> {
4747
Ok(StructFieldTypeNode {
4848
name: self.name.clone(),
49-
r#type: self.r#type.try_resolved()?.clone(),
49+
r#type: Box::new(self.r#type.try_resolved()?.clone()),
5050
docs: self.docs.clone(),
51-
default_value: self
52-
.default_value
53-
.as_ref()
54-
.map(|r| r.try_resolved().cloned())
55-
.transpose()?,
51+
default_value: Box::new(
52+
self.default_value
53+
.as_ref()
54+
.map(|r| r.try_resolved().cloned())
55+
.transpose()?,
56+
),
5657
default_value_strategy: self.default_value_strategy,
5758
})
5859
}

codama-attributes/src/codama_directives/type_nodes/struct_field_type_node.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ impl FromMeta for StructFieldTypeNode {
2929

3030
Ok(StructFieldTypeNode {
3131
name: consumer.name.take(meta)?,
32-
r#type,
33-
default_value,
32+
r#type: Box::new(r#type),
33+
default_value: Box::new(default_value),
3434
default_value_strategy,
3535
docs: Docs::default(),
3636
})
@@ -64,7 +64,7 @@ mod tests {
6464
assert_type!(
6565
{ field("age", number(u32), default_value = 42) },
6666
StructFieldTypeNode {
67-
default_value: Some(NumberValueNode::new(42u32).into()),
67+
default_value: Box::new(Some(NumberValueNode::new(42u32).into())),
6868
..StructFieldTypeNode::new("age", NumberTypeNode::le(U32))
6969
}
7070
.into()
@@ -76,7 +76,7 @@ mod tests {
7676
assert_type!(
7777
{ field("age", number(u32), value = 42) },
7878
StructFieldTypeNode {
79-
default_value: Some(NumberValueNode::new(42u32).into()),
79+
default_value: Box::new(Some(NumberValueNode::new(42u32).into())),
8080
default_value_strategy: Some(DefaultValueStrategy::Omitted),
8181
..StructFieldTypeNode::new("age", NumberTypeNode::le(U32))
8282
}

codama-korok-visitors/src/apply_type_modifiers_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn update_type_node(
194194
};
195195

196196
if let Node::Type(RegisteredTypeNode::StructField(mut field)) = node {
197-
field.r#type = update(field.r#type)?;
197+
field.r#type = Box::new(update(*field.r#type)?);
198198
return Ok(Some(field.into()));
199199
};
200200

codama-korok-visitors/src/combine_types_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl KorokVisitor for CombineTypesVisitor {
240240
.ast
241241
.discriminant
242242
.as_ref()
243-
.and_then(|(_, x)| x.as_unsigned_integer::<usize>().ok());
243+
.and_then(|(_, x)| x.as_unsigned_integer::<u32>().ok());
244244

245245
korok.node = match korok.ast.fields {
246246
syn::Fields::Named(_) => {

codama-korok-visitors/src/set_accounts_visitor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ impl KorokVisitor for SetAccountsVisitor {
146146

147147
let discriminator = StructFieldTypeNode {
148148
default_value_strategy: Some(DefaultValueStrategy::Omitted),
149-
default_value: Some(NumberValueNode::new(current_discriminator as u64).into()),
149+
default_value: Box::new(Some(
150+
NumberValueNode::new(current_discriminator as u64).into(),
151+
)),
150152
..StructFieldTypeNode::from(&self.enum_discriminator)
151153
};
152154
let discriminator_name = discriminator.name.clone();

codama-korok-visitors/src/set_default_values_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn get_node_with_default_value(
101101
let value = ValueNode::try_from(resolved_node.clone()).ok();
102102
Ok(value.map(|value| {
103103
StructFieldTypeNode {
104-
default_value: Some(value),
104+
default_value: Box::new(Some(value)),
105105
default_value_strategy: directive.default_value_strategy,
106106
..field.clone()
107107
}

codama-korok-visitors/src/set_events_visitor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ impl KorokVisitor for SetEventsVisitor {
131131

132132
let discriminator = StructFieldTypeNode {
133133
default_value_strategy: Some(DefaultValueStrategy::Omitted),
134-
default_value: Some(NumberValueNode::new(current_discriminator as u64).into()),
134+
default_value: Box::new(Some(
135+
NumberValueNode::new(current_discriminator as u64).into(),
136+
)),
135137
..StructFieldTypeNode::from(&self.enum_discriminator)
136138
};
137139

codama-korok-visitors/src/set_pdas_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn parse_pda_seed_nodes(
8080
}
8181
let (name, type_node) = match &field.node {
8282
Some(Node::Type(RegisteredTypeNode::StructField(struct_field))) => {
83-
(struct_field.name.clone(), struct_field.r#type.clone())
83+
(struct_field.name.clone(), (*struct_field.r#type).clone())
8484
}
8585
_ => match TypeNode::try_from(field.node.clone()) {
8686
Ok(type_node) => (name.clone().into(), type_node),

codama-korok-visitors/tests/combine_types_visitor/enum_variant_korok.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ fn it_prepends_attribute_fields_to_enum_variants() -> CodamaResult<()> {
208208
"person",
209209
StructTypeNode::new(vec![
210210
StructFieldTypeNode {
211-
default_value: Some(NumberValueNode::new(0u8).into()),
211+
default_value: Box::new(Some(NumberValueNode::new(0u8).into())),
212212
default_value_strategy: Some(DefaultValueStrategy::Omitted),
213213
..StructFieldTypeNode::new("discriminator", NumberTypeNode::le(U8))
214214
},

codama-korok-visitors/tests/combine_types_visitor/struct_korok.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn it_prepends_attribute_fields_to_structs() -> CodamaResult<()> {
204204
"person",
205205
StructTypeNode::new(vec![
206206
StructFieldTypeNode {
207-
default_value: Some(NumberValueNode::new(0u8).into()),
207+
default_value: Box::new(Some(NumberValueNode::new(0u8).into())),
208208
default_value_strategy: Some(DefaultValueStrategy::Omitted),
209209
..StructFieldTypeNode::new("discriminator", NumberTypeNode::le(U8))
210210
},

0 commit comments

Comments
 (0)