Skip to content

Commit

Permalink
Merge pull request #219 from metaplex-foundation/danenbm/serde-pubkey…
Browse files Browse the repository at this point in the history
…-display

Serde Pubkey DisplayFromStr
  • Loading branch information
danenbm authored and lorisleiva committed Apr 19, 2024
1 parent 92a55a7 commit 3f9e025
Show file tree
Hide file tree
Showing 22 changed files with 547 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/real-chairs-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@metaplex-foundation/kinobi": patch
---

Add serde DisplayFromStr for Pubkey and Vec<Pubkey> in Rust rendering.
14 changes: 13 additions & 1 deletion src/renderers/rust/getTypeManifestVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,18 @@ export function getTypeManifestVisitor() {
const childManifest = visit(enumTupleVariantType.tuple, self);
parentName = originalParentName;

let derive = '';
if (childManifest.type === '(Pubkey)') {
derive =
'#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]\n';
} else if (childManifest.type === '(Vec<Pubkey>)') {
derive =
'#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<Vec<serde_with::DisplayFromStr>>"))]\n';
}

return {
...childManifest,
type: `${name}${childManifest.type},`,
type: `${derive}${name}${childManifest.type},`,
};
},

Expand Down Expand Up @@ -333,6 +342,9 @@ export function getTypeManifestVisitor() {
if (fieldManifest.type === 'Pubkey') {
derive =
'#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]\n';
} else if (fieldManifest.type === 'Vec<Pubkey>') {
derive =
'#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<Vec<serde_with::DisplayFromStr>>"))]\n';
} else if (
(isNode(resolvedNestedType, 'arrayTypeNode') &&
isNode(resolvedNestedType.count, 'fixedCountNode') &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
export * from './mplCandyMachineCore';
export * from './mplTokenAuthRules';
export * from './mplTokenMetadata';
export * from './pubkeyTests';
export * from './splMemo';
export * from './splSystem';
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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 { Address } from '@solana/addresses';
import { Program } from '@solana/programs';

export const PUBKEY_TESTS_PROGRAM_ADDRESS =
'PubkeyTests1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d' as Address<'PubkeyTests1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d'>;

export type PubkeyTestsProgram =
Program<'PubkeyTests1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d'>;

export function getPubkeyTestsProgram(): PubkeyTestsProgram {
return {
name: 'pubkeyTests',
address: PUBKEY_TESTS_PROGRAM_ADDRESS,
};
}
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 @@ -38,6 +38,7 @@ export * from './programmableConfig';
export * from './reservation';
export * from './reservationV1';
export * from './revokeArgs';
export * from './ruleSet';
export * from './setCollectionSizeArgs';
export * from './taCreateArgs';
export * from './taKey';
Expand All @@ -46,6 +47,7 @@ export * from './tmKey';
export * from './tokenStandard';
export * from './transferArgs';
export * from './updateArgs';
export * from './updateDelegate';
export * from './useAssetArgs';
export * from './useMethod';
export * from './uses';
Expand Down
117 changes: 117 additions & 0 deletions test/packages/js-experimental/src/generated/types/ruleSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* 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 {
Address,
getAddressDecoder,
getAddressEncoder,
} from '@solana/addresses';
import {
Codec,
Decoder,
Encoder,
GetDataEnumKind,
GetDataEnumKindContent,
combineCodec,
getArrayDecoder,
getArrayEncoder,
getDataEnumDecoder,
getDataEnumEncoder,
getStructDecoder,
getStructEncoder,
getTupleDecoder,
getTupleEncoder,
getUnitDecoder,
getUnitEncoder,
} from '@solana/codecs';

export type RuleSet =
| { __kind: 'None' }
| { __kind: 'Single'; fields: [Address] }
| { __kind: 'ProgramAllowList'; fields: [Array<Address>] }
| { __kind: 'ProgramDenyList'; fields: [Array<Address>] };

export type RuleSetArgs = RuleSet;

export function getRuleSetEncoder(): Encoder<RuleSetArgs> {
return getDataEnumEncoder([
['None', getUnitEncoder()],
[
'Single',
getStructEncoder([['fields', getTupleEncoder([getAddressEncoder()])]]),
],
[
'ProgramAllowList',
getStructEncoder([
['fields', getTupleEncoder([getArrayEncoder(getAddressEncoder())])],
]),
],
[
'ProgramDenyList',
getStructEncoder([
['fields', getTupleEncoder([getArrayEncoder(getAddressEncoder())])],
]),
],
]);
}

export function getRuleSetDecoder(): Decoder<RuleSet> {
return getDataEnumDecoder([
['None', getUnitDecoder()],
[
'Single',
getStructDecoder([['fields', getTupleDecoder([getAddressDecoder()])]]),
],
[
'ProgramAllowList',
getStructDecoder([
['fields', getTupleDecoder([getArrayDecoder(getAddressDecoder())])],
]),
],
[
'ProgramDenyList',
getStructDecoder([
['fields', getTupleDecoder([getArrayDecoder(getAddressDecoder())])],
]),
],
]);
}

export function getRuleSetCodec(): Codec<RuleSetArgs, RuleSet> {
return combineCodec(getRuleSetEncoder(), getRuleSetDecoder());
}

// Data Enum Helpers.
export function ruleSet(kind: 'None'): GetDataEnumKind<RuleSetArgs, 'None'>;
export function ruleSet(
kind: 'Single',
data: GetDataEnumKindContent<RuleSetArgs, 'Single'>['fields']
): GetDataEnumKind<RuleSetArgs, 'Single'>;
export function ruleSet(
kind: 'ProgramAllowList',
data: GetDataEnumKindContent<RuleSetArgs, 'ProgramAllowList'>['fields']
): GetDataEnumKind<RuleSetArgs, 'ProgramAllowList'>;
export function ruleSet(
kind: 'ProgramDenyList',
data: GetDataEnumKindContent<RuleSetArgs, 'ProgramDenyList'>['fields']
): GetDataEnumKind<RuleSetArgs, 'ProgramDenyList'>;
export function ruleSet<K extends RuleSetArgs['__kind'], Data>(
kind: K,
data?: Data
) {
return Array.isArray(data)
? { __kind: kind, fields: data }
: { __kind: kind, ...(data ?? {}) };
}

export function isRuleSet<K extends RuleSet['__kind']>(
kind: K,
value: RuleSet
): value is RuleSet & { __kind: K } {
return value.__kind === kind;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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 {
Address,
getAddressDecoder,
getAddressEncoder,
} from '@solana/addresses';
import {
Codec,
Decoder,
Encoder,
combineCodec,
getArrayDecoder,
getArrayEncoder,
getStructDecoder,
getStructEncoder,
} from '@solana/codecs';

export type UpdateDelegate = { additionalDelegates: Array<Address> };

export type UpdateDelegateArgs = UpdateDelegate;

export function getUpdateDelegateEncoder(): Encoder<UpdateDelegateArgs> {
return getStructEncoder([
['additionalDelegates', getArrayEncoder(getAddressEncoder())],
]);
}

export function getUpdateDelegateDecoder(): Decoder<UpdateDelegate> {
return getStructDecoder([
['additionalDelegates', getArrayDecoder(getAddressDecoder())],
]);
}

export function getUpdateDelegateCodec(): Codec<
UpdateDelegateArgs,
UpdateDelegate
> {
return combineCodec(getUpdateDelegateEncoder(), getUpdateDelegateDecoder());
}
1 change: 1 addition & 0 deletions test/packages/js/src/generated/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
export * from './mplCandyMachineCore';
export * from './mplTokenAuthRules';
export * from './mplTokenMetadata';
export * from './pubkeyTests';
export * from './splMemo';
export * from './splSystem';
42 changes: 42 additions & 0 deletions test/packages/js/src/generated/errors/pubkeyTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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 { Program, ProgramError } from '@metaplex-foundation/umi';

type ProgramErrorConstructor = new (
program: Program,
cause?: Error
) => ProgramError;
const codeToErrorMap: Map<number, ProgramErrorConstructor> = new Map();
const nameToErrorMap: Map<string, ProgramErrorConstructor> = new Map();

/**
* Attempts to resolve a custom program error from the provided error code.
* @category Errors
*/
export function getPubkeyTestsErrorFromCode(
code: number,
program: Program,
cause?: Error
): ProgramError | null {
const constructor = codeToErrorMap.get(code);
return constructor ? new constructor(program, cause) : null;
}

/**
* Attempts to resolve a custom program error from the provided error name, i.e. 'Unauthorized'.
* @category Errors
*/
export function getPubkeyTestsErrorFromName(
name: string,
program: Program,
cause?: Error
): ProgramError | null {
const constructor = nameToErrorMap.get(name);
return constructor ? new constructor(program, cause) : null;
}
1 change: 1 addition & 0 deletions test/packages/js/src/generated/programs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
export * from './mplCandyMachineCore';
export * from './mplTokenAuthRules';
export * from './mplTokenMetadata';
export * from './pubkeyTests';
export * from './splMemo';
export * from './splSystem';
55 changes: 55 additions & 0 deletions test/packages/js/src/generated/programs/pubkeyTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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 {
ClusterFilter,
Context,
Program,
PublicKey,
} from '@metaplex-foundation/umi';
import {
getPubkeyTestsErrorFromCode,
getPubkeyTestsErrorFromName,
} from '../errors';

export const PUBKEY_TESTS_PROGRAM_ID =
'PubkeyTests1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d' as PublicKey<'PubkeyTests1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d'>;

export function createPubkeyTestsProgram(): Program {
return {
name: 'pubkeyTests',
publicKey: PUBKEY_TESTS_PROGRAM_ID,
getErrorFromCode(code: number, cause?: Error) {
return getPubkeyTestsErrorFromCode(code, this, cause);
},
getErrorFromName(name: string, cause?: Error) {
return getPubkeyTestsErrorFromName(name, this, cause);
},
isOnCluster() {
return true;
},
};
}

export function getPubkeyTestsProgram<T extends Program = Program>(
context: Pick<Context, 'programs'>,
clusterFilter?: ClusterFilter
): T {
return context.programs.get<T>('pubkeyTests', clusterFilter);
}

export function getPubkeyTestsProgramId(
context: Pick<Context, 'programs'>,
clusterFilter?: ClusterFilter
): PublicKey {
return context.programs.getPublicKey(
'pubkeyTests',
PUBKEY_TESTS_PROGRAM_ID,
clusterFilter
);
}
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 @@ -38,6 +38,7 @@ export * from './programmableConfig';
export * from './reservation';
export * from './reservationV1';
export * from './revokeArgs';
export * from './ruleSet';
export * from './setCollectionSizeArgs';
export * from './taCreateArgs';
export * from './taKey';
Expand All @@ -46,6 +47,7 @@ export * from './tmKey';
export * from './tokenStandard';
export * from './transferArgs';
export * from './updateArgs';
export * from './updateDelegate';
export * from './useAssetArgs';
export * from './useMethod';
export * from './uses';
Expand Down
Loading

0 comments on commit 3f9e025

Please sign in to comment.