diff --git a/.changeset/silent-frogs-sing.md b/.changeset/silent-frogs-sing.md new file mode 100644 index 00000000..7b739db0 --- /dev/null +++ b/.changeset/silent-frogs-sing.md @@ -0,0 +1,5 @@ +--- +'@codama/renderers-rust': patch +--- + +Use trait options for instruction data diff --git a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction1.rs b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction1.rs index ad0d5673..567eec8e 100644 --- a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction1.rs +++ b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction1.rs @@ -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 { diff --git a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction2.rs b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction2.rs index d3e8dc3e..2c6519a5 100644 --- a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction2.rs +++ b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction2.rs @@ -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 { diff --git a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction3.rs b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction3.rs index 8bb1fb94..678b90e9 100644 --- a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction3.rs +++ b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction3.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction4.rs b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction4.rs index c18ce073..cc14b546 100644 --- a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction4.rs +++ b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction4.rs @@ -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 { diff --git a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction5.rs b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction5.rs index c5421a4c..d7d4e136 100644 --- a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction5.rs +++ b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction5.rs @@ -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 { diff --git a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction6.rs b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction6.rs index 2058936d..00291a80 100644 --- a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction6.rs +++ b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction6.rs @@ -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 { diff --git a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction7.rs b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction7.rs index 87cc19f0..fe173e7d 100644 --- a/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction7.rs +++ b/packages/renderers-rust/e2e/dummy/src/generated/instructions/instruction7.rs @@ -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 { diff --git a/packages/renderers-rust/e2e/memo/src/generated/instructions/add_memo.rs b/packages/renderers-rust/e2e/memo/src/generated/instructions/add_memo.rs index 5f3c1a41..0b8835e8 100644 --- a/packages/renderers-rust/e2e/memo/src/generated/instructions/add_memo.rs +++ b/packages/renderers-rust/e2e/memo/src/generated/instructions/add_memo.rs @@ -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 { diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/advance_nonce_account.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/advance_nonce_account.rs index 51cfc8e2..7ca6c1e4 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/advance_nonce_account.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/advance_nonce_account.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/allocate.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/allocate.rs index a7cdfc64..3632659a 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/allocate.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/allocate.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/allocate_with_seed.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/allocate_with_seed.rs index 5ce997a3..ff4f287e 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/allocate_with_seed.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/allocate_with_seed.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/assign.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/assign.rs index 88551bd1..fb080279 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/assign.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/assign.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/assign_with_seed.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/assign_with_seed.rs index 9a237f3d..6123bcad 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/assign_with_seed.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/assign_with_seed.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/authorize_nonce_account.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/authorize_nonce_account.rs index 8d3c715f..43775390 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/authorize_nonce_account.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/authorize_nonce_account.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/create_account.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/create_account.rs index 6f7cd2e3..f5260092 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/create_account.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/create_account.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/create_account_with_seed.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/create_account_with_seed.rs index 8e4b906d..4e1940ad 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/create_account_with_seed.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/create_account_with_seed.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/initialize_nonce_account.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/initialize_nonce_account.rs index f4afcc73..ae9293d1 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/initialize_nonce_account.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/initialize_nonce_account.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/transfer_sol.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/transfer_sol.rs index 05ac91f0..bf35d6b1 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/transfer_sol.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/transfer_sol.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/transfer_sol_with_seed.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/transfer_sol_with_seed.rs index 373c3ed6..93249e67 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/transfer_sol_with_seed.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/transfer_sol_with_seed.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/upgrade_nonce_account.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/upgrade_nonce_account.rs index dd987d4f..08ba1473 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/upgrade_nonce_account.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/upgrade_nonce_account.rs @@ -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, } diff --git a/packages/renderers-rust/e2e/system/src/generated/instructions/withdraw_nonce_account.rs b/packages/renderers-rust/e2e/system/src/generated/instructions/withdraw_nonce_account.rs index 122d79cb..e933cb48 100644 --- a/packages/renderers-rust/e2e/system/src/generated/instructions/withdraw_nonce_account.rs +++ b/packages/renderers-rust/e2e/system/src/generated/instructions/withdraw_nonce_account.rs @@ -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, } diff --git a/packages/renderers-rust/public/templates/instructionsPage.njk b/packages/renderers-rust/public/templates/instructionsPage.njk index 355c65cc..d5e814db 100644 --- a/packages/renderers-rust/public/templates/instructionsPage.njk +++ b/packages/renderers-rust/public/templates/instructionsPage.njk @@ -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 }}, @@ -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 }}, @@ -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 %} diff --git a/packages/renderers-rust/src/getRenderMapVisitor.ts b/packages/renderers-rust/src/getRenderMapVisitor.ts index 63b0cd2b..031abbf3 100644 --- a/packages/renderers-rust/src/getRenderMapVisitor.ts +++ b/packages/renderers-rust/src/getRenderMapVisitor.ts @@ -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); @@ -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 diff --git a/packages/renderers-rust/src/utils/traitOptions.ts b/packages/renderers-rust/src/utils/traitOptions.ts index 04615a89..170ddac6 100644 --- a/packages/renderers-rust/src/utils/traitOptions.ts +++ b/packages/renderers-rust/src/utils/traitOptions.ts @@ -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'; @@ -48,17 +56,20 @@ export const DEFAULT_TRAIT_OPTIONS: Required = { 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 = { ...DEFAULT_TRAIT_OPTIONS, ...userOptions }; // Get the node type and return early if it's a type alias. @@ -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';