From 7088cc4acd449a9c0667ffd737f380a26bb3d503 Mon Sep 17 00:00:00 2001 From: Fernando Otero Date: Fri, 19 Apr 2024 20:53:12 +0100 Subject: [PATCH] Use remainder str type (#223) --- .changeset/wicked-apples-tan.md | 5 ++++ src/renderers/rust/getTypeManifestVisitor.ts | 4 +-- test/renderers/rust/instructionsPage.test.ts | 30 +++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 .changeset/wicked-apples-tan.md diff --git a/.changeset/wicked-apples-tan.md b/.changeset/wicked-apples-tan.md new file mode 100644 index 00000000..59a97094 --- /dev/null +++ b/.changeset/wicked-apples-tan.md @@ -0,0 +1,5 @@ +--- +"@metaplex-foundation/kinobi": patch +--- + +Use remainder str type in Rust client diff --git a/src/renderers/rust/getTypeManifestVisitor.ts b/src/renderers/rust/getTypeManifestVisitor.ts index f652c617..dc5f68a9 100644 --- a/src/renderers/rust/getTypeManifestVisitor.ts +++ b/src/renderers/rust/getTypeManifestVisitor.ts @@ -430,8 +430,8 @@ export function getTypeManifestVisitor() { visitStringType() { if (!parentSize) { return { - type: `&str`, - imports: new RustImportMap(), + type: `RemainderStr`, + imports: new RustImportMap().add(`kaigan::types::RemainderStr`), nestedStructs: [], }; } diff --git a/test/renderers/rust/instructionsPage.test.ts b/test/renderers/rust/instructionsPage.test.ts index bd4dc6a3..308ec2b2 100644 --- a/test/renderers/rust/instructionsPage.test.ts +++ b/test/renderers/rust/instructionsPage.test.ts @@ -1,5 +1,5 @@ import test from 'ava'; -import { instructionNode, programNode, visit } from '../../../src'; +import { instructionArgumentNode, instructionNode, programNode, remainderSizeNode, stringTypeNode, visit } from '../../../src'; import { getRenderMapVisitor } from '../../../src/renderers/rust/getRenderMapVisitor'; import { codeContains } from './_setup'; @@ -20,3 +20,31 @@ test('it renders a public instruction data struct', (t) => { `pub fn new(`, ]); }); + +test('it renders an instruction with a remainder str', (t) => { + // Given the following program with 1 instruction. + const node = programNode({ + name: 'splToken', + publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', + instructions: [ + instructionNode({ + name: 'addMemo', + arguments: [ + instructionArgumentNode({ + name: 'memo', + type: stringTypeNode({ size: remainderSizeNode() }), + }), + ], + }), + ], + }); + + // When we render it. + const renderMap = visit(node, getRenderMapVisitor()); + + // Then we expect the following pub struct. + codeContains(t, renderMap.get('instructions/add_memo.rs'), [ + `use kaigan::types::RemainderStr`, + `pub memo: RemainderStr`, + ]); +});