Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use trait options for instruction data in rust renderer #366

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-frogs-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codama/renderers-rust': patch
---

Use trait options for instruction data
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl Instruction1 {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instruction1InstructionData {}

impl Instruction1InstructionData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl Instruction2 {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instruction2InstructionData {}

impl Instruction2InstructionData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ impl Instruction3 {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instruction3InstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl Instruction4 {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instruction4InstructionData {}

impl Instruction4InstructionData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl Instruction5 {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instruction5InstructionData {}

impl Instruction5InstructionData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl Instruction6 {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instruction6InstructionData {}

impl Instruction6InstructionData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ impl Instruction7 {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Instruction7InstructionData {}

impl Instruction7InstructionData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl AddMemo {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AddMemoInstructionData {}

impl AddMemoInstructionData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl AdvanceNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AdvanceNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ impl Allocate {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AllocateInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ impl AllocateWithSeed {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AllocateWithSeedInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl Assign {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AssignInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ impl AssignWithSeed {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AssignWithSeedInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ impl AuthorizeNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AuthorizeNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl CreateAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateAccountInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ impl CreateAccountWithSeed {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateAccountWithSeedInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl InitializeNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct InitializeNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl TransferSol {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TransferSolInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl TransferSolWithSeed {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TransferSolWithSeedInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ impl UpgradeNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UpgradeNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ impl WithdrawNonceAccount {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WithdrawNonceAccountInstructionData {
discriminator: u32,
}
Expand Down
9 changes: 2 additions & 7 deletions packages/renderers-rust/public/templates/instructionsPage.njk
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ impl {{ instruction.name | pascalCase }} {
}
}

#[derive(BorshDeserialize, BorshSerialize)]
pub struct {{ instruction.name | pascalCase }}InstructionData {
{{ dataTraits }} pub struct {{ instruction.name | pascalCase }}InstructionData {
{% for arg in instructionArgs %}
{% if arg.default %}
{{ arg.name | snakeCase }}: {{ arg.type }},
Expand All @@ -135,9 +134,7 @@ impl Default for {{ instruction.name | pascalCase }}InstructionData {
}

{% if hasArgs %}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct {{ instruction.name | pascalCase }}InstructionArgs {
{{ dataTraits }} pub struct {{ instruction.name | pascalCase }}InstructionArgs {
{% for arg in instructionArgs %}
{% if not arg.default %}
pub {{ arg.name | snakeCase }}: {{ arg.type }},
Expand All @@ -147,8 +144,6 @@ pub struct {{ instruction.name | pascalCase }}InstructionArgs {
{% endif %}

{% for nestedStruct in typeManifest.nestedStructs %}
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
{{ nestedStruct }}
{% endfor %}

Expand Down
5 changes: 4 additions & 1 deletion packages/renderers-rust/src/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
visitInstruction(node) {
// Imports.
const imports = new ImportMap();
imports.add(['borsh::BorshDeserialize', 'borsh::BorshSerialize']);

// canMergeAccountsAndArgs
const accountsAndArgsConflicts = getConflictsForInstructionAccountsAndArgs(node);
Expand Down Expand Up @@ -201,9 +200,13 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
});
const typeManifest = visit(struct, structVisitor);

const dataTraits = getTraitsFromNode(node);
imports.mergeWith(dataTraits.imports);

return new RenderMap().add(
`instructions/${snakeCase(node.name)}.rs`,
render('instructionsPage.njk', {
dataTraits: dataTraits.render,
hasArgs,
hasOptional,
imports: imports
Expand Down
25 changes: 19 additions & 6 deletions packages/renderers-rust/src/utils/traitOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { AccountNode, assertIsNode, camelCase, DefinedTypeNode, isNode, isScalarEnum } from '@codama/nodes';
import {
AccountNode,
assertIsNode,
camelCase,
DefinedTypeNode,
InstructionNode,
isNode,
isScalarEnum,
} from '@codama/nodes';

import { ImportMap } from '../ImportMap';

Expand Down Expand Up @@ -48,17 +56,20 @@ export const DEFAULT_TRAIT_OPTIONS: Required<TraitOptions> = {
useFullyQualifiedName: false,
};

export type GetTraitsFromNodeFunction = (node: AccountNode | DefinedTypeNode) => { imports: ImportMap; render: string };
export type GetTraitsFromNodeFunction = (node: AccountNode | DefinedTypeNode | InstructionNode) => {
imports: ImportMap;
render: string;
};

export function getTraitsFromNodeFactory(options: TraitOptions = {}): GetTraitsFromNodeFunction {
return node => getTraitsFromNode(node, options);
}

export function getTraitsFromNode(
node: AccountNode | DefinedTypeNode,
node: AccountNode | DefinedTypeNode | InstructionNode,
userOptions: TraitOptions = {},
): { imports: ImportMap; render: string } {
assertIsNode(node, ['accountNode', 'definedTypeNode']);
assertIsNode(node, ['accountNode', 'definedTypeNode', 'instructionNode']);
const options: Required<TraitOptions> = { ...DEFAULT_TRAIT_OPTIONS, ...userOptions };

// Get the node type and return early if it's a type alias.
Expand Down Expand Up @@ -96,8 +107,10 @@ export function getTraitsFromNode(
return { imports, render: traitLines.join('') };
}

function getNodeType(node: AccountNode | DefinedTypeNode): 'alias' | 'dataEnum' | 'scalarEnum' | 'struct' {
if (isNode(node, 'accountNode')) return 'struct';
function getNodeType(
node: AccountNode | DefinedTypeNode | InstructionNode,
): 'alias' | 'dataEnum' | 'scalarEnum' | 'struct' {
if (isNode(node, ['accountNode', 'instructionNode'])) return 'struct';
if (isNode(node.type, 'structTypeNode')) return 'struct';
if (isNode(node.type, 'enumTypeNode')) {
return isScalarEnum(node.type) ? 'scalarEnum' : 'dataEnum';
Expand Down
Loading