From fb834d2d8e8a7022b93e5a91fd42094ed8735f9f Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Sat, 6 Apr 2024 16:33:26 +0100 Subject: [PATCH] Rename sizeNodes to countNodes --- .changeset/eight-numbers-admire.md | 5 +++ src/nodes/Node.ts | 4 +-- src/nodes/countNodes/CountNode.ts | 23 +++++++++++++ src/nodes/countNodes/FixedCountNode.ts | 10 ++++++ src/nodes/countNodes/PrefixedCountNode.ts | 12 +++++++ src/nodes/countNodes/RemainderCountNode.ts | 7 ++++ src/nodes/countNodes/index.ts | 4 +++ src/nodes/index.ts | 2 +- src/nodes/sizeNodes/FixedSizeNode.ts | 10 ------ src/nodes/sizeNodes/PrefixedSizeNode.ts | 12 ------- src/nodes/sizeNodes/RemainderSizeNode.ts | 7 ---- src/nodes/sizeNodes/SizeNode.ts | 23 ------------- src/nodes/sizeNodes/index.ts | 4 --- src/nodes/typeNodes/ArrayTypeNode.ts | 26 ++++++++------ src/nodes/typeNodes/MapTypeNode.ts | 24 ++++++------- src/nodes/typeNodes/SetTypeNode.ts | 24 ++++++------- .../fragments/discriminatorCondition.ts | 2 +- .../js-experimental/getTypeManifestVisitor.ts | 22 ++++++------ src/renderers/js/getTypeManifestVisitor.ts | 14 ++++---- src/renderers/rust/getTypeManifestVisitor.ts | 34 +++++++++---------- src/visitors/getByteSizeVisitor.ts | 4 +-- src/visitors/getDebugStringVisitor.ts | 4 +-- src/visitors/identityVisitor.ts | 22 ++++++------ src/visitors/mergeVisitor.ts | 10 +++--- .../setAnchorDiscriminatorsVisitor.ts | 6 ++-- .../transformU8ArraysToBytesVisitor.ts | 14 ++++---- test/testFile.cjs | 2 +- .../nodes/sizeNodes/FixedSizeNode.test.ts | 8 ++--- .../nodes/sizeNodes/PrefixedSizeNode.test.ts | 8 ++--- .../nodes/sizeNodes/RemainderSizeNode.test.ts | 8 ++--- .../nodes/typeNodes/ArrayTypeNode.test.ts | 8 ++--- .../nodes/typeNodes/MapTypeNode.test.ts | 8 ++--- .../nodes/typeNodes/SetTypeNode.test.ts | 8 ++--- 33 files changed, 194 insertions(+), 185 deletions(-) create mode 100644 .changeset/eight-numbers-admire.md create mode 100644 src/nodes/countNodes/CountNode.ts create mode 100644 src/nodes/countNodes/FixedCountNode.ts create mode 100644 src/nodes/countNodes/PrefixedCountNode.ts create mode 100644 src/nodes/countNodes/RemainderCountNode.ts create mode 100644 src/nodes/countNodes/index.ts delete mode 100644 src/nodes/sizeNodes/FixedSizeNode.ts delete mode 100644 src/nodes/sizeNodes/PrefixedSizeNode.ts delete mode 100644 src/nodes/sizeNodes/RemainderSizeNode.ts delete mode 100644 src/nodes/sizeNodes/SizeNode.ts delete mode 100644 src/nodes/sizeNodes/index.ts diff --git a/.changeset/eight-numbers-admire.md b/.changeset/eight-numbers-admire.md new file mode 100644 index 000000000..52ed16137 --- /dev/null +++ b/.changeset/eight-numbers-admire.md @@ -0,0 +1,5 @@ +--- +"@metaplex-foundation/kinobi": minor +--- + +Rename sizeNodes to countNodes diff --git a/src/nodes/Node.ts b/src/nodes/Node.ts index 88daa1a89..296e94d76 100644 --- a/src/nodes/Node.ts +++ b/src/nodes/Node.ts @@ -14,7 +14,7 @@ import { REGISTERED_CONTEXTUAL_VALUE_NODES } from './contextualValueNodes/Contex import { REGISTERED_DISCRIMINATOR_NODES } from './discriminatorNodes/DiscriminatorNode'; import { REGISTERED_LINK_NODES } from './linkNodes/LinkNode'; import { REGISTERED_PDA_SEED_NODES } from './pdaSeedNodes/PdaSeedNode'; -import { REGISTERED_SIZE_NODES } from './sizeNodes/SizeNode'; +import { REGISTERED_COUNT_NODES } from './countNodes/CountNode'; import { REGISTERED_TYPE_NODES } from './typeNodes/TypeNode'; import { REGISTERED_VALUE_NODES } from './valueNodes/ValueNode'; @@ -38,7 +38,7 @@ const REGISTERED_NODES = { ...REGISTERED_DISCRIMINATOR_NODES, ...REGISTERED_LINK_NODES, ...REGISTERED_PDA_SEED_NODES, - ...REGISTERED_SIZE_NODES, + ...REGISTERED_COUNT_NODES, ...REGISTERED_TYPE_NODES, ...REGISTERED_VALUE_NODES, }; diff --git a/src/nodes/countNodes/CountNode.ts b/src/nodes/countNodes/CountNode.ts new file mode 100644 index 000000000..6401b3215 --- /dev/null +++ b/src/nodes/countNodes/CountNode.ts @@ -0,0 +1,23 @@ +import { getNodeKinds } from '../../shared/utils'; +import type { FixedCountNode } from './FixedCountNode'; +import type { PrefixedCountNode } from './PrefixedCountNode'; +import type { RemainderCountNode } from './RemainderCountNode'; + +// Count Node Registration. + +export const REGISTERED_COUNT_NODES = { + fixedCountNode: {} as FixedCountNode, + remainderCountNode: {} as RemainderCountNode, + prefixedCountNode: {} as PrefixedCountNode, +}; + +export const REGISTERED_COUNT_NODE_KINDS = getNodeKinds(REGISTERED_COUNT_NODES); +export type RegisteredCountNodeKind = + (typeof REGISTERED_COUNT_NODE_KINDS)[number]; +export type RegisteredCountNode = + (typeof REGISTERED_COUNT_NODES)[RegisteredCountNodeKind]; + +// Count Node Helpers. + +export type CountNode = RegisteredCountNode; +export const COUNT_NODES = REGISTERED_COUNT_NODE_KINDS; diff --git a/src/nodes/countNodes/FixedCountNode.ts b/src/nodes/countNodes/FixedCountNode.ts new file mode 100644 index 000000000..1a22c7287 --- /dev/null +++ b/src/nodes/countNodes/FixedCountNode.ts @@ -0,0 +1,10 @@ +export type FixedCountNode = { + readonly kind: 'fixedCountNode'; + + // Data. + readonly value: number; +}; + +export function fixedCountNode(value: number): FixedCountNode { + return { kind: 'fixedCountNode', value }; +} diff --git a/src/nodes/countNodes/PrefixedCountNode.ts b/src/nodes/countNodes/PrefixedCountNode.ts new file mode 100644 index 000000000..d0c24a549 --- /dev/null +++ b/src/nodes/countNodes/PrefixedCountNode.ts @@ -0,0 +1,12 @@ +import { NumberTypeNode } from '../typeNodes'; + +export type PrefixedCountNode = { + readonly kind: 'prefixedCountNode'; + + // Children. + readonly prefix: NumberTypeNode; +}; + +export function prefixedCountNode(prefix: NumberTypeNode): PrefixedCountNode { + return { kind: 'prefixedCountNode', prefix }; +} diff --git a/src/nodes/countNodes/RemainderCountNode.ts b/src/nodes/countNodes/RemainderCountNode.ts new file mode 100644 index 000000000..ca31a3116 --- /dev/null +++ b/src/nodes/countNodes/RemainderCountNode.ts @@ -0,0 +1,7 @@ +export type RemainderCountNode = { + readonly kind: 'remainderCountNode'; +}; + +export function remainderCountNode(): RemainderCountNode { + return { kind: 'remainderCountNode' }; +} diff --git a/src/nodes/countNodes/index.ts b/src/nodes/countNodes/index.ts new file mode 100644 index 000000000..acd18ccd7 --- /dev/null +++ b/src/nodes/countNodes/index.ts @@ -0,0 +1,4 @@ +export * from './CountNode'; +export * from './FixedCountNode'; +export * from './PrefixedCountNode'; +export * from './RemainderCountNode'; diff --git a/src/nodes/index.ts b/src/nodes/index.ts index 391c3e5ad..fe82bd20a 100644 --- a/src/nodes/index.ts +++ b/src/nodes/index.ts @@ -12,9 +12,9 @@ export * from './ProgramNode'; export * from './RootNode'; export * from './contextualValueNodes'; +export * from './countNodes'; export * from './discriminatorNodes'; export * from './linkNodes'; export * from './pdaSeedNodes'; -export * from './sizeNodes'; export * from './typeNodes'; export * from './valueNodes'; diff --git a/src/nodes/sizeNodes/FixedSizeNode.ts b/src/nodes/sizeNodes/FixedSizeNode.ts deleted file mode 100644 index e70eeef47..000000000 --- a/src/nodes/sizeNodes/FixedSizeNode.ts +++ /dev/null @@ -1,10 +0,0 @@ -export type FixedSizeNode = { - readonly kind: 'fixedSizeNode'; - - // Data. - readonly size: number; -}; - -export function fixedSizeNode(size: number): FixedSizeNode { - return { kind: 'fixedSizeNode', size }; -} diff --git a/src/nodes/sizeNodes/PrefixedSizeNode.ts b/src/nodes/sizeNodes/PrefixedSizeNode.ts deleted file mode 100644 index bc8b7cbf7..000000000 --- a/src/nodes/sizeNodes/PrefixedSizeNode.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { NumberTypeNode } from '../typeNodes'; - -export type PrefixedSizeNode = { - readonly kind: 'prefixedSizeNode'; - - // Children. - readonly prefix: NumberTypeNode; -}; - -export function prefixedSizeNode(prefix: NumberTypeNode): PrefixedSizeNode { - return { kind: 'prefixedSizeNode', prefix }; -} diff --git a/src/nodes/sizeNodes/RemainderSizeNode.ts b/src/nodes/sizeNodes/RemainderSizeNode.ts deleted file mode 100644 index 9cf6eadd1..000000000 --- a/src/nodes/sizeNodes/RemainderSizeNode.ts +++ /dev/null @@ -1,7 +0,0 @@ -export type RemainderSizeNode = { - readonly kind: 'remainderSizeNode'; -}; - -export function remainderSizeNode(): RemainderSizeNode { - return { kind: 'remainderSizeNode' }; -} diff --git a/src/nodes/sizeNodes/SizeNode.ts b/src/nodes/sizeNodes/SizeNode.ts deleted file mode 100644 index a39aa68e0..000000000 --- a/src/nodes/sizeNodes/SizeNode.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { getNodeKinds } from '../../shared/utils'; -import type { FixedSizeNode } from './FixedSizeNode'; -import type { PrefixedSizeNode } from './PrefixedSizeNode'; -import type { RemainderSizeNode } from './RemainderSizeNode'; - -// Size Node Registration. - -export const REGISTERED_SIZE_NODES = { - fixedSizeNode: {} as FixedSizeNode, - remainderSizeNode: {} as RemainderSizeNode, - prefixedSizeNode: {} as PrefixedSizeNode, -}; - -export const REGISTERED_SIZE_NODE_KINDS = getNodeKinds(REGISTERED_SIZE_NODES); -export type RegisteredSizeNodeKind = - (typeof REGISTERED_SIZE_NODE_KINDS)[number]; -export type RegisteredSizeNode = - (typeof REGISTERED_SIZE_NODES)[RegisteredSizeNodeKind]; - -// Size Node Helpers. - -export type SizeNode = RegisteredSizeNode; -export const SIZE_NODES = REGISTERED_SIZE_NODE_KINDS; diff --git a/src/nodes/sizeNodes/index.ts b/src/nodes/sizeNodes/index.ts deleted file mode 100644 index f5f688624..000000000 --- a/src/nodes/sizeNodes/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './FixedSizeNode'; -export * from './PrefixedSizeNode'; -export * from './RemainderSizeNode'; -export * from './SizeNode'; diff --git a/src/nodes/typeNodes/ArrayTypeNode.ts b/src/nodes/typeNodes/ArrayTypeNode.ts index 6e2f10f81..0a1f67f29 100644 --- a/src/nodes/typeNodes/ArrayTypeNode.ts +++ b/src/nodes/typeNodes/ArrayTypeNode.ts @@ -1,10 +1,10 @@ import type { IdlTypeArray, IdlTypeVec } from '../../idl'; import { - SizeNode, - fixedSizeNode, - prefixedSizeNode, - remainderSizeNode, -} from '../sizeNodes'; + CountNode, + fixedCountNode, + prefixedCountNode, + remainderCountNode, +} from '../countNodes'; import { numberTypeNode } from './NumberTypeNode'; import { TypeNode, createTypeNodeFromIdl } from './TypeNode'; @@ -13,14 +13,17 @@ export type ArrayTypeNode = { // Children. readonly item: TypeNode; - readonly size: SizeNode; + readonly count: CountNode; }; -export function arrayTypeNode(item: TypeNode, size?: SizeNode): ArrayTypeNode { +export function arrayTypeNode( + item: TypeNode, + count?: CountNode +): ArrayTypeNode { return { kind: 'arrayTypeNode', item, - size: size ?? prefixedSizeNode(numberTypeNode('u32')), + count: count ?? prefixedCountNode(numberTypeNode('u32')), }; } @@ -29,10 +32,11 @@ export function arrayTypeNodeFromIdl( ): ArrayTypeNode { if ('array' in idl) { const item = createTypeNodeFromIdl(idl.array[0]); - return arrayTypeNode(item, fixedSizeNode(idl.array[1])); + return arrayTypeNode(item, fixedCountNode(idl.array[1])); } const item = createTypeNodeFromIdl(idl.vec); if (!idl.size) return arrayTypeNode(item); - if (idl.size === 'remainder') return arrayTypeNode(item, remainderSizeNode()); - return arrayTypeNode(item, prefixedSizeNode(numberTypeNode(idl.size))); + if (idl.size === 'remainder') + return arrayTypeNode(item, remainderCountNode()); + return arrayTypeNode(item, prefixedCountNode(numberTypeNode(idl.size))); } diff --git a/src/nodes/typeNodes/MapTypeNode.ts b/src/nodes/typeNodes/MapTypeNode.ts index 7ad011b49..d4b0aa885 100644 --- a/src/nodes/typeNodes/MapTypeNode.ts +++ b/src/nodes/typeNodes/MapTypeNode.ts @@ -1,10 +1,10 @@ import type { IdlTypeMap } from '../../idl'; import { - SizeNode, - fixedSizeNode, - prefixedSizeNode, - remainderSizeNode, -} from '../sizeNodes'; + CountNode, + fixedCountNode, + prefixedCountNode, + remainderCountNode, +} from '../countNodes'; import { numberTypeNode } from './NumberTypeNode'; import { TypeNode, createTypeNodeFromIdl } from './TypeNode'; @@ -14,7 +14,7 @@ export type MapTypeNode = { // Children. readonly key: TypeNode; readonly value: TypeNode; - readonly size: SizeNode; + readonly count: CountNode; // Data. readonly idlMap: 'hashMap' | 'bTreeMap'; @@ -23,27 +23,27 @@ export type MapTypeNode = { export function mapTypeNode( key: TypeNode, value: TypeNode, - size?: SizeNode, + count?: CountNode, idlMap?: MapTypeNode['idlMap'] ): MapTypeNode { return { kind: 'mapTypeNode', key, value, - size: size ?? prefixedSizeNode(numberTypeNode('u32')), + count: count ?? prefixedCountNode(numberTypeNode('u32')), idlMap: idlMap ?? 'hashMap', }; } export function mapTypeNodeFromIdl(idl: IdlTypeMap): MapTypeNode { const [key, value] = 'hashMap' in idl ? idl.hashMap : idl.bTreeMap; - let size: SizeNode | undefined; + let size: CountNode | undefined; if (idl.size === 'remainder') { - size = remainderSizeNode(); + size = remainderCountNode(); } else if (typeof idl.size === 'number') { - size = fixedSizeNode(idl.size); + size = fixedCountNode(idl.size); } else if (idl.size) { - size = prefixedSizeNode(numberTypeNode(idl.size)); + size = prefixedCountNode(numberTypeNode(idl.size)); } return mapTypeNode( createTypeNodeFromIdl(key), diff --git a/src/nodes/typeNodes/SetTypeNode.ts b/src/nodes/typeNodes/SetTypeNode.ts index 47b48e92a..257cc5814 100644 --- a/src/nodes/typeNodes/SetTypeNode.ts +++ b/src/nodes/typeNodes/SetTypeNode.ts @@ -1,10 +1,10 @@ import type { IdlTypeSet } from '../../idl'; import { - SizeNode, - fixedSizeNode, - prefixedSizeNode, - remainderSizeNode, -} from '../sizeNodes'; + CountNode, + fixedCountNode, + prefixedCountNode, + remainderCountNode, +} from '../countNodes'; import { numberTypeNode } from './NumberTypeNode'; import { TypeNode, createTypeNodeFromIdl } from './TypeNode'; @@ -13,7 +13,7 @@ export type SetTypeNode = { // Children. readonly item: TypeNode; - readonly size: SizeNode; + readonly count: CountNode; // Data. readonly idlSet: 'hashSet' | 'bTreeSet'; @@ -21,26 +21,26 @@ export type SetTypeNode = { export function setTypeNode( item: TypeNode, - size?: SizeNode, + count?: CountNode, idlSet?: SetTypeNode['idlSet'] ): SetTypeNode { return { kind: 'setTypeNode', item, - size: size ?? prefixedSizeNode(numberTypeNode('u32')), + count: count ?? prefixedCountNode(numberTypeNode('u32')), idlSet: idlSet ?? 'hashSet', }; } export function setTypeNodeFromIdl(idl: IdlTypeSet): SetTypeNode { const child = 'hashSet' in idl ? idl.hashSet : idl.bTreeSet; - let size: SetTypeNode['size'] | undefined; + let size: SetTypeNode['count'] | undefined; if (idl.size === 'remainder') { - size = remainderSizeNode(); + size = remainderCountNode(); } else if (typeof idl.size === 'number') { - size = fixedSizeNode(idl.size); + size = fixedCountNode(idl.size); } else if (idl.size) { - size = prefixedSizeNode(numberTypeNode(idl.size)); + size = prefixedCountNode(numberTypeNode(idl.size)); } return setTypeNode( createTypeNodeFromIdl(child), diff --git a/src/renderers/js-experimental/fragments/discriminatorCondition.ts b/src/renderers/js-experimental/fragments/discriminatorCondition.ts index 5924dfdd7..be4aa1d36 100644 --- a/src/renderers/js-experimental/fragments/discriminatorCondition.ts +++ b/src/renderers/js-experimental/fragments/discriminatorCondition.ts @@ -95,7 +95,7 @@ function getFieldConditionFragment( isNode(field.type, 'arrayTypeNode') && isNode(field.type.item, 'numberTypeNode') && field.type.item.format === 'u8' && - isNode(field.type.size, 'fixedSizeNode') && + isNode(field.type.count, 'fixedCountNode') && isNode(field.defaultValue, 'arrayValueNode') && field.defaultValue.items.every(isNodeFilter('numberValueNode')) ) { diff --git a/src/renderers/js-experimental/getTypeManifestVisitor.ts b/src/renderers/js-experimental/getTypeManifestVisitor.ts index 4696b56e7..59854ac8c 100644 --- a/src/renderers/js-experimental/getTypeManifestVisitor.ts +++ b/src/renderers/js-experimental/getTypeManifestVisitor.ts @@ -1,7 +1,7 @@ import { NumberTypeNode, REGISTERED_TYPE_NODE_KINDS, - SizeNode, + CountNode, isNode, isScalarEnum, structFieldTypeNode, @@ -98,7 +98,7 @@ export function getTypeManifestVisitor(input: { const childManifest = visit(arrayType.item, self); childManifest.looseType.mapRender((r) => `Array<${r}>`); childManifest.strictType.mapRender((r) => `Array<${r}>`); - const sizeManifest = getArrayLikeSizeOption(arrayType.size, self); + const sizeManifest = getArrayLikeSizeOption(arrayType.count, self); const encoderOptions = sizeManifest.encoder.render ? `, { ${sizeManifest.encoder.render} }` : ''; @@ -319,7 +319,7 @@ export function getTypeManifestVisitor(input: { ([k, v]) => `Map<${k}, ${v}>`, ([k, v]) => `${k}, ${v}` ); - const sizeManifest = getArrayLikeSizeOption(mapType.size, self); + const sizeManifest = getArrayLikeSizeOption(mapType.count, self); const encoderOptions = sizeManifest.encoder.render ? `, { ${sizeManifest.encoder.render} }` : ''; @@ -386,7 +386,7 @@ export function getTypeManifestVisitor(input: { childManifest.strictType.mapRender((r) => `Set<${r}>`); childManifest.looseType.mapRender((r) => `Set<${r}>`); - const sizeManifest = getArrayLikeSizeOption(setType.size, self); + const sizeManifest = getArrayLikeSizeOption(setType.count, self); const encoderOptions = sizeManifest.encoder.render ? `, { ${sizeManifest.encoder.render} }` : ''; @@ -733,28 +733,28 @@ export function getTypeManifestVisitor(input: { } function getArrayLikeSizeOption( - size: SizeNode, + count: CountNode, visitor: Visitor ): { encoder: Fragment; decoder: Fragment; } { - if (isNode(size, 'fixedSizeNode')) { + if (isNode(count, 'fixedCountNode')) { return { - encoder: fragment(`size: ${size.size}`), - decoder: fragment(`size: ${size.size}`), + encoder: fragment(`size: ${count.value}`), + decoder: fragment(`size: ${count.value}`), }; } - if (isNode(size, 'remainderSizeNode')) { + if (isNode(count, 'remainderCountNode')) { return { encoder: fragment(`size: 'remainder'`), decoder: fragment(`size: 'remainder'`), }; } - if (size.prefix.format === 'u32' && size.prefix.endian === 'le') { + if (count.prefix.format === 'u32' && count.prefix.endian === 'le') { return { encoder: fragment(''), decoder: fragment('') }; } - const prefixManifest = visit(size.prefix, visitor); + const prefixManifest = visit(count.prefix, visitor); prefixManifest.encoder.mapRender((r) => `size: ${r}`); prefixManifest.decoder.mapRender((r) => `size: ${r}`); return prefixManifest; diff --git a/src/renderers/js/getTypeManifestVisitor.ts b/src/renderers/js/getTypeManifestVisitor.ts index 111898cd2..0432d316b 100644 --- a/src/renderers/js/getTypeManifestVisitor.ts +++ b/src/renderers/js/getTypeManifestVisitor.ts @@ -97,7 +97,7 @@ export function getTypeManifestVisitor(input: { const childManifest = visit(arrayType.item, self); childManifest.serializerImports.add('umiSerializers', 'array'); const sizeOption = getArrayLikeSizeOption( - arrayType.size, + arrayType.count, childManifest, self ); @@ -282,7 +282,7 @@ export function getTypeManifestVisitor(input: { const mergedManifest = mergeManifests([key, value]); mergedManifest.serializerImports.add('umiSerializers', 'map'); const sizeOption = getArrayLikeSizeOption( - mapType.size, + mapType.count, mergedManifest, self ); @@ -336,7 +336,7 @@ export function getTypeManifestVisitor(input: { const childManifest = visit(setType.item, self); childManifest.serializerImports.add('umiSerializers', 'set'); const sizeOption = getArrayLikeSizeOption( - setType.size, + setType.count, childManifest, self ); @@ -710,17 +710,17 @@ function mergeManifests( } function getArrayLikeSizeOption( - size: ArrayTypeNode['size'], + count: ArrayTypeNode['count'], manifest: Pick< JavaScriptTypeManifest, 'strictImports' | 'looseImports' | 'serializerImports' >, self: Visitor ): string | null { - if (isNode(size, 'fixedSizeNode')) return `size: ${size.size}`; - if (isNode(size, 'remainderSizeNode')) return `size: 'remainder'`; + if (isNode(count, 'fixedCountNode')) return `size: ${count.value}`; + if (isNode(count, 'remainderCountNode')) return `size: 'remainder'`; - const prefixManifest = visit(size.prefix, self); + const prefixManifest = visit(count.prefix, self); if (prefixManifest.serializer === 'u32()') return null; manifest.strictImports.mergeWith(prefixManifest.strictImports); diff --git a/src/renderers/rust/getTypeManifestVisitor.ts b/src/renderers/rust/getTypeManifestVisitor.ts index df2147fe5..a8fb879f3 100644 --- a/src/renderers/rust/getTypeManifestVisitor.ts +++ b/src/renderers/rust/getTypeManifestVisitor.ts @@ -1,14 +1,14 @@ import { NumberTypeNode, REGISTERED_TYPE_NODE_KINDS, - SizeNode, + CountNode, arrayTypeNode, - fixedSizeNode, + fixedCountNode, isNode, isScalarEnum, numberTypeNode, - prefixedSizeNode, - remainderSizeNode, + prefixedCountNode, + remainderCountNode, resolveNestedTypeNode, } from '../../nodes'; import { pascalCase, pipe, rustDocblock, snakeCase } from '../../shared'; @@ -100,18 +100,18 @@ export function getTypeManifestVisitor() { visitArrayType(arrayType, { self }) { const childManifest = visit(arrayType.item, self); - if (isNode(arrayType.size, 'fixedSizeNode')) { + if (isNode(arrayType.count, 'fixedCountNode')) { return { ...childManifest, - type: `[${childManifest.type}; ${arrayType.size.size}]`, + type: `[${childManifest.type}; ${arrayType.count.value}]`, }; } if ( - isNode(arrayType.size, 'prefixedSizeNode') && - arrayType.size.prefix.endian === 'le' + isNode(arrayType.count, 'prefixedCountNode') && + arrayType.count.prefix.endian === 'le' ) { - switch (arrayType.size.prefix.format) { + switch (arrayType.count.prefix.format) { case 'u32': return { ...childManifest, @@ -120,7 +120,7 @@ export function getTypeManifestVisitor() { case 'u8': case 'u16': case 'u64': { - const prefix = arrayType.size.prefix.format.toUpperCase(); + const prefix = arrayType.count.prefix.format.toUpperCase(); childManifest.imports.add(`kaigan::types::${prefix}PrefixVec`); return { ...childManifest, @@ -129,12 +129,12 @@ export function getTypeManifestVisitor() { } default: throw new Error( - `Array prefix not supported: ${arrayType.size.prefix.format}` + `Array prefix not supported: ${arrayType.count.prefix.format}` ); } } - if (isNode(arrayType.size, 'remainderSizeNode')) { + if (isNode(arrayType.count, 'remainderCountNode')) { childManifest.imports.add('kaigan::types::RemainderVec'); return { ...childManifest, @@ -338,8 +338,8 @@ export function getTypeManifestVisitor() { '#[cfg_attr(feature = "serde", serde(with = "serde_with::As::"))]\n'; } else if ( (isNode(resolvedNestedType, 'arrayTypeNode') && - isNode(resolvedNestedType.size, 'fixedSizeNode') && - resolvedNestedType.size.size > 32) || + isNode(resolvedNestedType.count, 'fixedCountNode') && + resolvedNestedType.count.value > 32) || (isNode(resolvedNestedType, ['bytesTypeNode', 'stringTypeNode']) && isNode(structFieldType.type, 'fixedSizeTypeNode') && structFieldType.type.size > 32) @@ -383,11 +383,11 @@ export function getTypeManifestVisitor() { }, visitBytesType(_bytesType, { self }) { - let arraySize: SizeNode = remainderSizeNode(); + let arraySize: CountNode = remainderCountNode(); if (typeof parentSize === 'number') { - arraySize = fixedSizeNode(parentSize); + arraySize = fixedCountNode(parentSize); } else if (parentSize && typeof parentSize === 'object') { - arraySize = prefixedSizeNode(parentSize); + arraySize = prefixedCountNode(parentSize); } const arrayType = arrayTypeNode(numberTypeNode('u8'), arraySize); return visit(arrayType, self); diff --git a/src/visitors/getByteSizeVisitor.ts b/src/visitors/getByteSizeVisitor.ts index 402631f5d..db222e29a 100644 --- a/src/visitors/getByteSizeVisitor.ts +++ b/src/visitors/getByteSizeVisitor.ts @@ -68,8 +68,8 @@ export function getByteSizeVisitor( }, visitArrayType(node) { - if (!isNode(node.size, 'fixedSizeNode')) return null; - const fixedSize = node.size.size; + if (!isNode(node.count, 'fixedCountNode')) return null; + const fixedSize = node.count.value; const itemSize = visit(node.item, this); const arraySize = itemSize !== null ? itemSize * fixedSize : null; return fixedSize === 0 ? 0 : arraySize; diff --git a/src/visitors/getDebugStringVisitor.ts b/src/visitors/getDebugStringVisitor.ts index cf52fcc1f..675e7e094 100644 --- a/src/visitors/getDebugStringVisitor.ts +++ b/src/visitors/getDebugStringVisitor.ts @@ -89,8 +89,8 @@ function getNodeDetails(node: Node): string[] { return [node.encoding]; case 'optionTypeNode': return node.fixed ? ['fixed'] : []; - case 'fixedSizeNode': - return [node.size.toString()]; + case 'fixedCountNode': + return [node.value.toString()]; case 'numberValueNode': return [node.number.toString()]; case 'stringValueNode': diff --git a/src/visitors/identityVisitor.ts b/src/visitors/identityVisitor.ts index b7cdf6d64..583ecfd5f 100644 --- a/src/visitors/identityVisitor.ts +++ b/src/visitors/identityVisitor.ts @@ -7,7 +7,7 @@ import { NodeKind, PDA_SEED_NODES, REGISTERED_NODE_KINDS, - SIZE_NODES, + COUNT_NODES, TYPE_NODES, VALUE_NODES, accountNode, @@ -38,7 +38,7 @@ import { pdaNode, pdaSeedValueNode, pdaValueNode, - prefixedSizeNode, + prefixedCountNode, programNode, removeNullAndAssertIsNodeFilter, resolverValueNode, @@ -236,9 +236,9 @@ export function identityVisitor( if (castedNodeKeys.includes('arrayTypeNode')) { visitor.visitArrayType = function visitArrayType(node) { - const size = visit(this)(node.size); + const size = visit(this)(node.count); if (size === null) return null; - assertIsNode(size, SIZE_NODES); + assertIsNode(size, COUNT_NODES); const item = visit(this)(node.item); if (item === null) return null; assertIsNode(item, TYPE_NODES); @@ -291,9 +291,9 @@ export function identityVisitor( if (castedNodeKeys.includes('mapTypeNode')) { visitor.visitMapType = function visitMapType(node) { - const size = visit(this)(node.size); + const size = visit(this)(node.count); if (size === null) return null; - assertIsNode(size, SIZE_NODES); + assertIsNode(size, COUNT_NODES); const key = visit(this)(node.key); if (key === null) return null; assertIsNode(key, TYPE_NODES); @@ -327,9 +327,9 @@ export function identityVisitor( if (castedNodeKeys.includes('setTypeNode')) { visitor.visitSetType = function visitSetType(node) { - const size = visit(this)(node.size); + const size = visit(this)(node.count); if (size === null) return null; - assertIsNode(size, SIZE_NODES); + assertIsNode(size, COUNT_NODES); const item = visit(this)(node.item); if (item === null) return null; assertIsNode(item, TYPE_NODES); @@ -395,12 +395,12 @@ export function identityVisitor( }; } - if (castedNodeKeys.includes('prefixedSizeNode')) { - visitor.visitPrefixedSize = function visitPrefixedSize(node) { + if (castedNodeKeys.includes('prefixedCountNode')) { + visitor.visitPrefixedCount = function visitPrefixedCount(node) { const prefix = visit(this)(node.prefix); if (prefix === null) return null; assertIsNode(prefix, 'numberTypeNode'); - return prefixedSizeNode(prefix); + return prefixedCountNode(prefix); }; } diff --git a/src/visitors/mergeVisitor.ts b/src/visitors/mergeVisitor.ts index e93326562..38f752761 100644 --- a/src/visitors/mergeVisitor.ts +++ b/src/visitors/mergeVisitor.ts @@ -103,7 +103,7 @@ export function mergeVisitor( if (castedNodeKeys.includes('arrayTypeNode')) { visitor.visitArrayType = function visitArrayType(node) { return merge(node, [ - ...visit(this)(node.size), + ...visit(this)(node.count), ...visit(this)(node.item), ]); }; @@ -137,7 +137,7 @@ export function mergeVisitor( if (castedNodeKeys.includes('mapTypeNode')) { visitor.visitMapType = function visitMapType(node) { return merge(node, [ - ...visit(this)(node.size), + ...visit(this)(node.count), ...visit(this)(node.key), ...visit(this)(node.value), ]); @@ -162,7 +162,7 @@ export function mergeVisitor( if (castedNodeKeys.includes('setTypeNode')) { visitor.visitSetType = function visitSetType(node) { return merge(node, [ - ...visit(this)(node.size), + ...visit(this)(node.count), ...visit(this)(node.item), ]); }; @@ -207,8 +207,8 @@ export function mergeVisitor( }; } - if (castedNodeKeys.includes('prefixedSizeNode')) { - visitor.visitPrefixedSize = function visitPrefixedSize(node) { + if (castedNodeKeys.includes('prefixedCountNode')) { + visitor.visitPrefixedCount = function visitPrefixedCount(node) { return merge(node, visit(this)(node.prefix)); }; } diff --git a/src/visitors/setAnchorDiscriminatorsVisitor.ts b/src/visitors/setAnchorDiscriminatorsVisitor.ts index acd7ccc5e..824347ee4 100644 --- a/src/visitors/setAnchorDiscriminatorsVisitor.ts +++ b/src/visitors/setAnchorDiscriminatorsVisitor.ts @@ -3,7 +3,7 @@ import { accountNode, arrayTypeNode, fieldDiscriminatorNode, - fixedSizeNode, + fixedCountNode, instructionArgumentNode, instructionNode, numberTypeNode, @@ -42,7 +42,7 @@ export function setAnchorDiscriminatorsVisitor() { const discriminatorArgument = structFieldTypeNode({ name: 'discriminator', - type: arrayTypeNode(numberTypeNode('u8'), fixedSizeNode(8)), + type: arrayTypeNode(numberTypeNode('u8'), fixedCountNode(8)), defaultValue: getAnchorAccountDiscriminator(node.idlName), defaultValueStrategy: 'omitted', }); @@ -63,7 +63,7 @@ export function setAnchorDiscriminatorsVisitor() { const discriminatorArgument = instructionArgumentNode({ name: 'discriminator', - type: arrayTypeNode(numberTypeNode('u8'), fixedSizeNode(8)), + type: arrayTypeNode(numberTypeNode('u8'), fixedCountNode(8)), defaultValue: getAnchorInstructionDiscriminator(node.idlName), defaultValueStrategy: 'omitted', }); diff --git a/src/visitors/transformU8ArraysToBytesVisitor.ts b/src/visitors/transformU8ArraysToBytesVisitor.ts index 1aaa0374d..fea9211fb 100644 --- a/src/visitors/transformU8ArraysToBytesVisitor.ts +++ b/src/visitors/transformU8ArraysToBytesVisitor.ts @@ -15,9 +15,9 @@ import { visit } from './visitor'; export function transformU8ArraysToBytesVisitor( sizes: number[] | '*' = [32, 64] ) { - const hasRequiredSize = (size: ArrayTypeNode['size']): boolean => { - if (!isNode(size, 'fixedSizeNode')) return false; - return sizes === '*' || sizes.includes(size.size); + const hasRequiredSize = (count: ArrayTypeNode['count']): boolean => { + if (!isNode(count, 'fixedCountNode')) return false; + return sizes === '*' || sizes.includes(count.value); }; return pipe(nonNullableIdentityVisitor(), (v) => @@ -29,13 +29,13 @@ export function transformU8ArraysToBytesVisitor( if ( isNode(child, 'numberTypeNode') && child.format === 'u8' && - isNode(node.size, 'fixedSizeNode') && - hasRequiredSize(node.size) + isNode(node.count, 'fixedCountNode') && + hasRequiredSize(node.count) ) { - return fixedSizeTypeNode(bytesTypeNode(), node.size.size); + return fixedSizeTypeNode(bytesTypeNode(), node.count.value); } - return arrayTypeNode(child, node.size); + return arrayTypeNode(child, node.count); }, }) ); diff --git a/test/testFile.cjs b/test/testFile.cjs index c7f859597..f2a1fdd29 100644 --- a/test/testFile.cjs +++ b/test/testFile.cjs @@ -151,7 +151,7 @@ kinobi.update( defaultValue: k.identityValueNode(), }, proof: { - type: k.arrayTypeNode(k.publicKeyTypeNode(), k.remainderSizeNode()), + type: k.arrayTypeNode(k.publicKeyTypeNode(), k.remainderCountNode()), defaultValue: k.arrayValueNode([]), }, }, diff --git a/test/visitors/nodes/sizeNodes/FixedSizeNode.test.ts b/test/visitors/nodes/sizeNodes/FixedSizeNode.test.ts index 7b58c3cde..edcd93bd2 100644 --- a/test/visitors/nodes/sizeNodes/FixedSizeNode.test.ts +++ b/test/visitors/nodes/sizeNodes/FixedSizeNode.test.ts @@ -1,5 +1,5 @@ import test from 'ava'; -import { fixedSizeNode } from '../../../../src'; +import { fixedCountNode } from '../../../../src'; import { deleteNodesVisitorMacro, getDebugStringVisitorMacro, @@ -7,9 +7,9 @@ import { mergeVisitorMacro, } from '../_setup'; -const node = fixedSizeNode(42); +const node = fixedCountNode(42); test(mergeVisitorMacro, node, 1); test(identityVisitorMacro, node); -test(deleteNodesVisitorMacro, node, '[fixedSizeNode]', null); -test(getDebugStringVisitorMacro, node, `fixedSizeNode [42]`); +test(deleteNodesVisitorMacro, node, '[fixedCountNode]', null); +test(getDebugStringVisitorMacro, node, `fixedCountNode [42]`); diff --git a/test/visitors/nodes/sizeNodes/PrefixedSizeNode.test.ts b/test/visitors/nodes/sizeNodes/PrefixedSizeNode.test.ts index 367602a36..848799d16 100644 --- a/test/visitors/nodes/sizeNodes/PrefixedSizeNode.test.ts +++ b/test/visitors/nodes/sizeNodes/PrefixedSizeNode.test.ts @@ -1,5 +1,5 @@ import test from 'ava'; -import { numberTypeNode, prefixedSizeNode } from '../../../../src'; +import { numberTypeNode, prefixedCountNode } from '../../../../src'; import { deleteNodesVisitorMacro, getDebugStringVisitorMacro, @@ -7,16 +7,16 @@ import { mergeVisitorMacro, } from '../_setup'; -const node = prefixedSizeNode(numberTypeNode('u64')); +const node = prefixedCountNode(numberTypeNode('u64')); test(mergeVisitorMacro, node, 2); test(identityVisitorMacro, node); -test(deleteNodesVisitorMacro, node, '[prefixedSizeNode]', null); +test(deleteNodesVisitorMacro, node, '[prefixedCountNode]', null); test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); test( getDebugStringVisitorMacro, node, ` -prefixedSizeNode +prefixedCountNode | numberTypeNode [u64]` ); diff --git a/test/visitors/nodes/sizeNodes/RemainderSizeNode.test.ts b/test/visitors/nodes/sizeNodes/RemainderSizeNode.test.ts index 9007d3a3b..73dfc59b5 100644 --- a/test/visitors/nodes/sizeNodes/RemainderSizeNode.test.ts +++ b/test/visitors/nodes/sizeNodes/RemainderSizeNode.test.ts @@ -1,5 +1,5 @@ import test from 'ava'; -import { remainderSizeNode } from '../../../../src'; +import { remainderCountNode } from '../../../../src'; import { deleteNodesVisitorMacro, getDebugStringVisitorMacro, @@ -7,9 +7,9 @@ import { mergeVisitorMacro, } from '../_setup'; -const node = remainderSizeNode(); +const node = remainderCountNode(); test(mergeVisitorMacro, node, 1); test(identityVisitorMacro, node); -test(deleteNodesVisitorMacro, node, '[remainderSizeNode]', null); -test(getDebugStringVisitorMacro, node, `remainderSizeNode`); +test(deleteNodesVisitorMacro, node, '[remainderCountNode]', null); +test(getDebugStringVisitorMacro, node, `remainderCountNode`); diff --git a/test/visitors/nodes/typeNodes/ArrayTypeNode.test.ts b/test/visitors/nodes/typeNodes/ArrayTypeNode.test.ts index 4d1f84ecf..d889111b3 100644 --- a/test/visitors/nodes/typeNodes/ArrayTypeNode.test.ts +++ b/test/visitors/nodes/typeNodes/ArrayTypeNode.test.ts @@ -2,7 +2,7 @@ import test from 'ava'; import { arrayTypeNode, numberTypeNode, - prefixedSizeNode, + prefixedCountNode, publicKeyTypeNode, } from '../../../../src'; import { @@ -14,21 +14,21 @@ import { const node = arrayTypeNode( publicKeyTypeNode(), - prefixedSizeNode(numberTypeNode('u64')) + prefixedCountNode(numberTypeNode('u64')) ); test(mergeVisitorMacro, node, 4); test(identityVisitorMacro, node); test(deleteNodesVisitorMacro, node, '[arrayTypeNode]', null); test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); -test(deleteNodesVisitorMacro, node, '[prefixedSizeNode]', null); +test(deleteNodesVisitorMacro, node, '[prefixedCountNode]', null); test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); test( getDebugStringVisitorMacro, node, ` arrayTypeNode -| prefixedSizeNode +| prefixedCountNode | | numberTypeNode [u64] | publicKeyTypeNode` ); diff --git a/test/visitors/nodes/typeNodes/MapTypeNode.test.ts b/test/visitors/nodes/typeNodes/MapTypeNode.test.ts index 9e6d31bfb..b6826842e 100644 --- a/test/visitors/nodes/typeNodes/MapTypeNode.test.ts +++ b/test/visitors/nodes/typeNodes/MapTypeNode.test.ts @@ -3,7 +3,7 @@ import { fixedSizeTypeNode, mapTypeNode, numberTypeNode, - prefixedSizeNode, + prefixedCountNode, publicKeyTypeNode, stringTypeNode, } from '../../../../src'; @@ -17,7 +17,7 @@ import { const node = mapTypeNode( fixedSizeTypeNode(stringTypeNode(), 32), publicKeyTypeNode(), - prefixedSizeNode(numberTypeNode('u8')) + prefixedCountNode(numberTypeNode('u8')) ); test(mergeVisitorMacro, node, 6); @@ -25,13 +25,13 @@ test(identityVisitorMacro, node); test(deleteNodesVisitorMacro, node, '[mapTypeNode]', null); test(deleteNodesVisitorMacro, node, '[stringTypeNode]', null); test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); -test(deleteNodesVisitorMacro, node, '[prefixedSizeNode]', null); +test(deleteNodesVisitorMacro, node, '[prefixedCountNode]', null); test( getDebugStringVisitorMacro, node, ` mapTypeNode -| prefixedSizeNode +| prefixedCountNode | | numberTypeNode [u8] | fixedSizeTypeNode [32] | | stringTypeNode [utf8] diff --git a/test/visitors/nodes/typeNodes/SetTypeNode.test.ts b/test/visitors/nodes/typeNodes/SetTypeNode.test.ts index c811ce6e2..0eb129629 100644 --- a/test/visitors/nodes/typeNodes/SetTypeNode.test.ts +++ b/test/visitors/nodes/typeNodes/SetTypeNode.test.ts @@ -1,7 +1,7 @@ import test from 'ava'; import { publicKeyTypeNode, - remainderSizeNode, + remainderCountNode, setTypeNode, } from '../../../../src'; import { @@ -11,18 +11,18 @@ import { mergeVisitorMacro, } from '../_setup'; -const node = setTypeNode(publicKeyTypeNode(), remainderSizeNode()); +const node = setTypeNode(publicKeyTypeNode(), remainderCountNode()); test(mergeVisitorMacro, node, 3); test(identityVisitorMacro, node); test(deleteNodesVisitorMacro, node, '[setTypeNode]', null); test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); -test(deleteNodesVisitorMacro, node, '[remainderSizeNode]', null); +test(deleteNodesVisitorMacro, node, '[remainderCountNode]', null); test( getDebugStringVisitorMacro, node, ` setTypeNode -| remainderSizeNode +| remainderCountNode | publicKeyTypeNode` );