Skip to content

Commit

Permalink
Add support to prefix string type (#148)
Browse files Browse the repository at this point in the history
* Add support for prefix string

* Update tests

* Add changeset
  • Loading branch information
febo authored Jan 10, 2024
1 parent 5a88fc0 commit f987339
Show file tree
Hide file tree
Showing 13 changed files with 422 additions and 135 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-singers-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Add support to prefix string type in Rust client
30 changes: 24 additions & 6 deletions src/renderers/rust/GetRustTypeManifestVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,32 @@ export class GetRustTypeManifestVisitor implements Visitor<RustTypeManifest> {
visitStringType(stringType: nodes.StringTypeNode): RustTypeManifest {
if (
stringType.size.kind === 'prefixed' &&
stringType.size.prefix.format === 'u32' &&
stringType.size.prefix.endian === 'le'
) {
return {
type: 'String',
imports: new RustImportMap(),
nestedStructs: [],
};
switch (stringType.size.prefix.format) {
case 'u32':
return {
type: 'String',
imports: new RustImportMap(),
nestedStructs: [],
};
case 'u8':
case 'u16':
case 'u64': {
const prefix = stringType.size.prefix.format.toUpperCase();
return {
type: `${prefix}PrefixString`,
imports: new RustImportMap().add(
`kaigan::types::${prefix}PrefixString`
),
nestedStructs: [],
};
}
default:
throw new Error(
`'String size not supported: ${stringType.size.prefix.format}`
);
}
}

if (stringType.size.kind === 'fixed') {
Expand Down
2 changes: 2 additions & 0 deletions test/packages/js-experimental/src/generated/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export * from './dummyLines';
export * from './escrowAuthority';
export * from './extendedPayload';
export * from './hiddenSettings';
export * from './link';
export * from './links';
export * from './migrateArgs';
export * from './mintArgs';
export * from './mintNewEditionFromMasterEditionViaTokenArgs';
Expand Down
37 changes: 37 additions & 0 deletions test/packages/js-experimental/src/generated/types/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import { Codec, Decoder, Encoder, combineCodec } from '@solana/codecs-core';
import {
getStructDecoder,
getStructEncoder,
} from '@solana/codecs-data-structures';
import { getU8Decoder, getU8Encoder } from '@solana/codecs-numbers';
import { getStringDecoder, getStringEncoder } from '@solana/codecs-strings';

export type Link = { name: string; uri: string };

export type LinkArgs = Link;

export function getLinkEncoder() {
return getStructEncoder<LinkArgs>([
['name', getStringEncoder({ size: getU8Encoder() })],
['uri', getStringEncoder({ size: getU8Encoder() })],
]) satisfies Encoder<LinkArgs>;
}

export function getLinkDecoder() {
return getStructDecoder<Link>([
['name', getStringDecoder({ size: getU8Decoder() })],
['uri', getStringDecoder({ size: getU8Decoder() })],
]) satisfies Decoder<Link>;
}

export function getLinkCodec(): Codec<LinkArgs, Link> {
return combineCodec(getLinkEncoder(), getLinkDecoder());
}
36 changes: 36 additions & 0 deletions test/packages/js-experimental/src/generated/types/links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import { Codec, Decoder, Encoder, combineCodec } from '@solana/codecs-core';
import {
getArrayDecoder,
getArrayEncoder,
getStructDecoder,
getStructEncoder,
} from '@solana/codecs-data-structures';
import { Link, LinkArgs, getLinkDecoder, getLinkEncoder } from '.';

export type Links = { values: Array<Link> };

export type LinksArgs = { values: Array<LinkArgs> };

export function getLinksEncoder() {
return getStructEncoder<LinksArgs>([
['values', getArrayEncoder(getLinkEncoder(), { size: 'remainder' })],
]) satisfies Encoder<LinksArgs>;
}

export function getLinksDecoder() {
return getStructDecoder<Links>([
['values', getArrayDecoder(getLinkDecoder(), { size: 'remainder' })],
]) satisfies Decoder<Links>;
}

export function getLinksCodec(): Codec<LinksArgs, Links> {
return combineCodec(getLinksEncoder(), getLinksDecoder());
}
2 changes: 2 additions & 0 deletions test/packages/js/src/generated/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export * from './dummyLines';
export * from './escrowAuthority';
export * from './extendedPayload';
export * from './hiddenSettings';
export * from './link';
export * from './links';
export * from './migrateArgs';
export * from './mintArgs';
export * from './mintNewEditionFromMasterEditionViaTokenArgs';
Expand Down
28 changes: 28 additions & 0 deletions test/packages/js/src/generated/types/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import {
Serializer,
string,
struct,
u8,
} from '@metaplex-foundation/umi/serializers';

export type Link = { name: string; uri: string };

export type LinkArgs = Link;

export function getLinkSerializer(): Serializer<LinkArgs, Link> {
return struct<Link>(
[
['name', string({ size: u8() })],
['uri', string({ size: u8() })],
],
{ description: 'Link' }
) as Serializer<LinkArgs, Link>;
}
25 changes: 25 additions & 0 deletions test/packages/js/src/generated/types/links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import {
Serializer,
array,
struct,
} from '@metaplex-foundation/umi/serializers';
import { Link, LinkArgs, getLinkSerializer } from '.';

export type Links = { values: Array<Link> };

export type LinksArgs = { values: Array<LinkArgs> };

export function getLinksSerializer(): Serializer<LinksArgs, Links> {
return struct<Links>(
[['values', array(getLinkSerializer(), { size: 'remainder' })]],
{ description: 'Links' }
) as Serializer<LinksArgs, Links>;
}
Loading

0 comments on commit f987339

Please sign in to comment.