Skip to content

Commit 64b556d

Browse files
committed
Remove aliasDefaults
1 parent ce131ec commit 64b556d

File tree

3 files changed

+26
-42
lines changed

3 files changed

+26
-42
lines changed

packages/renderers-rust/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@ Using the `traitOptions` attribute, you may configure the default traits that wi
6060
- `dataEnumDefaults`: The default traits to implement for all data enum types, in addition to the `baseDefaults` traits. Data enums are enums with at least one non-unit variant — e.g. `pub enum Command { Write(String), Quit }`.
6161
- `scalarEnumDefaults`: The default traits to implement for all scalar enum types, in addition to the `baseDefaults` traits. Scalar enums are enums with unit variants only — e.g. `pub enum Feedback { Good, Bad }`.
6262
- `structDefaults`: The default traits to implement for all struct types, in addition to the `baseDefaults` traits.
63-
- `aliasDefaults`: The default traits to implement for all type aliases, in addition to the `baseDefaults` traits.
6463

6564
Note that you must provide the fully qualified name of the traits you provide (e.g. `serde::Serialize`). Here are the default values for these attributes:
6665

6766
```ts
6867
const traitOptions = {
69-
aliasDefaults: [],
7068
baseDefaults: ['borsh::BorshSerialize', 'borsh::BorshDeserialize', 'Clone', 'Debug', 'Eq', 'PartialEq'],
7169
dataEnumDefaults: [],
7270
scalarEnumDefaults: ['Copy', 'PartialOrd', 'Hash', 'num_derive::FromPrimitive'],

packages/renderers-rust/src/utils/traitOptions.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { AccountNode, assertIsNode, camelCase, DefinedTypeNode, isNode, isScalar
33
import { ImportMap } from '../ImportMap';
44

55
export type TraitOptions = {
6-
/** The default traits to implement for type aliases only — on top of the base defaults. */
7-
aliasDefaults?: string[];
86
/** The default traits to implement for all types. */
97
baseDefaults?: string[];
108
/**
@@ -32,7 +30,6 @@ export type TraitOptions = {
3230
};
3331

3432
export const DEFAULT_TRAIT_OPTIONS: Required<TraitOptions> = {
35-
aliasDefaults: [],
3633
baseDefaults: ['borsh::BorshSerialize', 'borsh::BorshDeserialize', 'Clone', 'Debug', 'Eq', 'PartialEq'],
3734
dataEnumDefaults: [],
3835
featureFlags: { serde: ['serde::Serialize', 'serde::Deserialize'] },
@@ -55,8 +52,13 @@ export function getTraitsFromNode(
5552
assertIsNode(node, ['accountNode', 'definedTypeNode']);
5653
const options: Required<TraitOptions> = { ...DEFAULT_TRAIT_OPTIONS, ...userOptions };
5754

58-
// Find all the FQN traits for the node.
55+
// Get the node type and return early if it's a type alias.
5956
const nodeType = getNodeType(node);
57+
if (nodeType === 'alias') {
58+
return { imports: new ImportMap(), render: '' };
59+
}
60+
61+
// Find all the FQN traits for the node.
6062
const sanitizedOverrides = Object.fromEntries(
6163
Object.entries(options.overrides).map(([key, value]) => [camelCase(key), value]),
6264
);
@@ -98,21 +100,18 @@ function getNodeType(node: AccountNode | DefinedTypeNode): 'alias' | 'dataEnum'
98100
}
99101

100102
function getDefaultTraits(
101-
nodeType: 'alias' | 'dataEnum' | 'scalarEnum' | 'struct',
103+
nodeType: 'dataEnum' | 'scalarEnum' | 'struct',
102104
options: Pick<
103105
Required<TraitOptions>,
104-
'aliasDefaults' | 'baseDefaults' | 'dataEnumDefaults' | 'scalarEnumDefaults' | 'structDefaults'
106+
'baseDefaults' | 'dataEnumDefaults' | 'scalarEnumDefaults' | 'structDefaults'
105107
>,
106108
): string[] {
107109
switch (nodeType) {
108-
case 'alias':
109-
return [...options.baseDefaults, ...options.aliasDefaults];
110110
case 'dataEnum':
111111
return [...options.baseDefaults, ...options.dataEnumDefaults];
112112
case 'scalarEnum':
113113
return [...options.baseDefaults, ...options.scalarEnumDefaults];
114114
case 'struct':
115-
default:
116115
return [...options.baseDefaults, ...options.structDefaults];
117116
}
118117
}

packages/renderers-rust/test/utils/traitOptions.test.ts

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,20 @@ describe('default values', () => {
8787
]);
8888
});
8989

90-
test('it defaults to a set of traits for aliases', () => {
91-
// Given a defined type node that is not an enum or struct.
92-
const node = definedTypeNode({
93-
name: 'Score',
94-
type: numberTypeNode('u64'),
95-
});
96-
97-
// When we get the traits from the node using the default options.
98-
const { render, imports } = getTraitsFromNode(node);
99-
100-
// Then we expect the following traits to be rendered.
101-
expect(render).toBe(`#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]\n`);
102-
103-
// And the following imports to be used.
104-
expect([...imports.imports]).toStrictEqual(['borsh::BorshSerialize', 'borsh::BorshDeserialize']);
105-
});
106-
10790
test('it does not use default traits if they are overridden', () => {
10891
// Given a defined type node that should use custom traits.
109-
const node = definedTypeNode({
110-
name: 'Score',
111-
type: numberTypeNode('u64'),
92+
const node = accountNode({
93+
data: structTypeNode([
94+
structFieldTypeNode({ name: 'x', type: numberTypeNode('u64') }),
95+
structFieldTypeNode({ name: 'y', type: numberTypeNode('u64') }),
96+
]),
97+
name: 'Coordinates',
11298
});
11399

114100
// When we get the traits from the node using the
115101
// default options with the overrides attribute.
116102
const { render, imports } = getTraitsFromNode(node, {
117-
overrides: { score: ['My', 'special::Traits'] },
103+
overrides: { coordinates: ['My', 'special::Traits'] },
118104
});
119105

120106
// Then we expect the following traits to be rendered.
@@ -126,15 +112,18 @@ describe('default values', () => {
126112

127113
test('it still uses feature flags for overridden traits', () => {
128114
// Given a defined type node that should use custom traits.
129-
const node = definedTypeNode({
130-
name: 'Score',
131-
type: numberTypeNode('u64'),
115+
const node = accountNode({
116+
data: structTypeNode([
117+
structFieldTypeNode({ name: 'x', type: numberTypeNode('u64') }),
118+
structFieldTypeNode({ name: 'y', type: numberTypeNode('u64') }),
119+
]),
120+
name: 'Coordinates',
132121
});
133122

134123
// When we get the traits from the node using custom traits
135124
// such that some are part of the feature flag defaults.
136125
const { render, imports } = getTraitsFromNode(node, {
137-
overrides: { score: ['My', 'special::Traits', 'serde::Serialize'] },
126+
overrides: { coordinates: ['My', 'special::Traits', 'serde::Serialize'] },
138127
});
139128

140129
// Then we expect the following traits to be rendered.
@@ -146,7 +135,6 @@ describe('default values', () => {
146135
});
147136

148137
const RESET_OPTIONS: Required<TraitOptions> = {
149-
aliasDefaults: [],
150138
baseDefaults: [],
151139
dataEnumDefaults: [],
152140
featureFlags: {},
@@ -220,22 +208,21 @@ describe('base traits', () => {
220208
expect(render).toBe(`#[derive(MyBaseTrait, MyStructTrait)]\n`);
221209
});
222210

223-
test('it uses both the base and alias traits', () => {
211+
test('it never uses traits for type aliases', () => {
224212
// Given a defined type node that is not an enum or struct.
225213
const node = definedTypeNode({
226214
name: 'Score',
227215
type: numberTypeNode('u64'),
228216
});
229217

230-
// When we get the traits from the node using custom base and alias defaults.
218+
// When we get the traits from the node such that we have base defaults.
231219
const { render } = getTraitsFromNode(node, {
232220
...RESET_OPTIONS,
233-
aliasDefaults: ['MyAliasTrait'],
234221
baseDefaults: ['MyBaseTrait'],
235222
});
236223

237-
// Then we expect both the base and alias traits to be rendered.
238-
expect(render).toBe(`#[derive(MyBaseTrait, MyAliasTrait)]\n`);
224+
// Then we expect no traits to be rendered.
225+
expect(render).toBe('');
239226
});
240227

241228
test('it identifies feature flags under all default traits', () => {

0 commit comments

Comments
 (0)