Skip to content

Commit 378f007

Browse files
authored
Add copy derive to scalar enums (#45)
* Add copy derive * Add changeset * Rename helper
1 parent 668b550 commit 378f007

File tree

6 files changed

+94
-4
lines changed

6 files changed

+94
-4
lines changed

.changeset/tall-beds-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@kinobi-so/renderers-rust": patch
3+
---
4+
5+
Add copy derive to scalar enums in Rust renderer

packages/renderers-rust/e2e/system/src/generated/types/nonce_state.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ use borsh::BorshSerialize;
1010
use num_derive::FromPrimitive;
1111

1212
#[derive(
13-
BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq, PartialOrd, Hash, FromPrimitive,
13+
BorshSerialize,
14+
BorshDeserialize,
15+
Clone,
16+
Debug,
17+
Eq,
18+
PartialEq,
19+
Copy,
20+
PartialOrd,
21+
Hash,
22+
FromPrimitive,
1423
)]
1524
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1625
pub enum NonceState {

packages/renderers-rust/e2e/system/src/generated/types/nonce_version.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ use borsh::BorshSerialize;
1010
use num_derive::FromPrimitive;
1111

1212
#[derive(
13-
BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq, PartialOrd, Hash, FromPrimitive,
13+
BorshSerialize,
14+
BorshDeserialize,
15+
Clone,
16+
Debug,
17+
Eq,
18+
PartialEq,
19+
Copy,
20+
PartialOrd,
21+
Hash,
22+
FromPrimitive,
1423
)]
1524
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1625
pub enum NonceVersion {

packages/renderers-rust/src/getTypeManifestVisitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function getTypeManifestVisitor(options: { nestedStruct?: boolean; parent
132132
manifest.imports.add(['borsh::BorshSerialize', 'borsh::BorshDeserialize']);
133133
const traits = ['BorshSerialize', 'BorshDeserialize', 'Clone', 'Debug', 'Eq', 'PartialEq'];
134134
if (isNode(definedType.type, 'enumTypeNode') && isScalarEnum(definedType.type)) {
135-
traits.push('PartialOrd', 'Hash', 'FromPrimitive');
135+
traits.push('Copy', 'PartialOrd', 'Hash', 'FromPrimitive');
136136
manifest.imports.add(['num_derive::FromPrimitive']);
137137
}
138138
return {

packages/renderers-rust/test/_setup.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,14 @@ export function codeContains(actual: string, expected: (RegExp | string)[] | Reg
1010
}
1111
});
1212
}
13+
14+
export function codeDoesNotContains(actual: string, expected: (RegExp | string)[] | RegExp | string) {
15+
const expectedArray = Array.isArray(expected) ? expected : [expected];
16+
expectedArray.forEach(e => {
17+
if (typeof e === 'string') {
18+
expect(actual).not.toContain(e);
19+
} else {
20+
expect(actual).not.toMatch(e);
21+
}
22+
});
23+
}

packages/renderers-rust/test/definedTypesPage.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {
22
definedTypeNode,
3+
enumEmptyVariantTypeNode,
4+
enumStructVariantTypeNode,
5+
enumTypeNode,
36
numberTypeNode,
47
programNode,
58
sizePrefixTypeNode,
@@ -11,7 +14,7 @@ import { visit } from '@kinobi-so/visitors-core';
1114
import { test } from 'vitest';
1215

1316
import { getRenderMapVisitor } from '../src';
14-
import { codeContains } from './_setup';
17+
import { codeContains, codeDoesNotContains } from './_setup';
1518

1619
test('it renders a prefix string on a defined type', () => {
1720
// Given the following program with 1 defined type using a prefixed size string.
@@ -40,3 +43,56 @@ test('it renders a prefix string on a defined type', () => {
4043
`content_type: U8PrefixString,`,
4144
]);
4245
});
46+
47+
test('it renders a scalar enum with Copy derive', () => {
48+
// Given the following program with 1 defined type using a prefixed size string.
49+
const node = programNode({
50+
definedTypes: [
51+
definedTypeNode({
52+
name: 'tag',
53+
type: enumTypeNode([enumEmptyVariantTypeNode('Uninitialized'), enumEmptyVariantTypeNode('Account')]),
54+
}),
55+
],
56+
name: 'splToken',
57+
publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
58+
});
59+
60+
// When we render it.
61+
const renderMap = visit(node, getRenderMapVisitor());
62+
63+
// Then we expect the following use and identifier to be rendered.
64+
codeContains(renderMap.get('types/tag.rs'), [`#[derive(`, `Copy`, `pub enum Tag`]);
65+
});
66+
67+
test('it renders a non-scalar enum without Copy derive', () => {
68+
// Given the following program with 1 defined type using a prefixed size string.
69+
const node = programNode({
70+
definedTypes: [
71+
definedTypeNode({
72+
name: 'tagWithStruct',
73+
type: enumTypeNode([
74+
enumEmptyVariantTypeNode('Uninitialized'),
75+
enumStructVariantTypeNode(
76+
'Account',
77+
structTypeNode([
78+
structFieldTypeNode({
79+
name: 'contentType',
80+
type: sizePrefixTypeNode(stringTypeNode('utf8'), numberTypeNode('u8')),
81+
}),
82+
]),
83+
),
84+
]),
85+
}),
86+
],
87+
name: 'splToken',
88+
publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
89+
});
90+
91+
// When we render it.
92+
const renderMap = visit(node, getRenderMapVisitor());
93+
94+
// Then we expect the following use and identifier to be rendered.
95+
codeContains(renderMap.get('types/tag_with_struct.rs'), [`#[derive(`, `pub enum TagWithStruct`]);
96+
// And we expect the Copy derive to be missing.
97+
codeDoesNotContains(renderMap.get('types/tag_with_struct.rs'), `Copy`);
98+
});

0 commit comments

Comments
 (0)