Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Apr 9, 2024
1 parent d934034 commit 5d943e6
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 59 deletions.
14 changes: 4 additions & 10 deletions src/nodes/AccountNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../shared';
import { assertIsNode } from './Node';
import { DiscriminatorNode } from './discriminatorNodes';
import { PdaLinkNode, pdaLinkNode } from './linkNodes';
import {
ResolveNestedTypeNode,
StructTypeNode,
structTypeNode,
} from './typeNodes';
import { NestedTypeNode, StructTypeNode, structTypeNode } from './typeNodes';
import { createTypeNodeFromIdl } from './typeNodes/TypeNode';

export interface AccountNode<
TData extends
ResolveNestedTypeNode<StructTypeNode> = ResolveNestedTypeNode<StructTypeNode>,
TData extends NestedTypeNode<StructTypeNode> = NestedTypeNode<StructTypeNode>,
TPda extends PdaLinkNode | undefined = PdaLinkNode | undefined,
TDiscriminators extends DiscriminatorNode[] | undefined =
| DiscriminatorNode[]
Expand All @@ -33,8 +28,7 @@ export interface AccountNode<
}

export type AccountNodeInput<
TData extends
ResolveNestedTypeNode<StructTypeNode> = ResolveNestedTypeNode<StructTypeNode>,
TData extends NestedTypeNode<StructTypeNode> = NestedTypeNode<StructTypeNode>,
TPda extends PdaLinkNode | undefined = PdaLinkNode | undefined,
TDiscriminators extends DiscriminatorNode[] | undefined =
| DiscriminatorNode[]
Expand All @@ -47,7 +41,7 @@ export type AccountNodeInput<
};

export function accountNode<
TData extends ResolveNestedTypeNode<StructTypeNode> = StructTypeNode<[]>,
TData extends NestedTypeNode<StructTypeNode> = StructTypeNode<[]>,
TPda extends PdaLinkNode | undefined = undefined,
const TDiscriminators extends DiscriminatorNode[] | undefined = undefined,
>(
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/countNodes/PrefixedCountNode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NumberTypeNode, ResolveNestedTypeNode } from '../typeNodes';
import { NumberTypeNode, NestedTypeNode } from '../typeNodes';

export interface PrefixedCountNode<
TPrefix extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
> {
readonly kind: 'prefixedCountNode';

Expand All @@ -11,7 +11,7 @@ export interface PrefixedCountNode<
}

export function prefixedCountNode<
TPrefix extends ResolveNestedTypeNode<NumberTypeNode>,
TPrefix extends NestedTypeNode<NumberTypeNode>,
>(prefix: TPrefix): PrefixedCountNode<TPrefix> {
return { kind: 'prefixedCountNode', prefix };
}
12 changes: 7 additions & 5 deletions src/nodes/typeNodes/AmountTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ResolveNestedTypeNode } from './NestedTypeNode';
import { NestedTypeNode } from './NestedTypeNode';
import { NumberTypeNode } from './NumberTypeNode';

export interface AmountTypeNode<
TNumber extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
> {
readonly kind: 'amountTypeNode';

Expand All @@ -15,8 +15,10 @@ export interface AmountTypeNode<
readonly unit?: string;
}

export function amountTypeNode<
TNumber extends ResolveNestedTypeNode<NumberTypeNode>,
>(number: TNumber, decimals: number, unit?: string): AmountTypeNode<TNumber> {
export function amountTypeNode<TNumber extends NestedTypeNode<NumberTypeNode>>(
number: TNumber,
decimals: number,
unit?: string
): AmountTypeNode<TNumber> {
return { kind: 'amountTypeNode', number, decimals, unit };
}
7 changes: 3 additions & 4 deletions src/nodes/typeNodes/BooleanTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ResolveNestedTypeNode } from './NestedTypeNode';
import { NestedTypeNode } from './NestedTypeNode';
import { NumberTypeNode, numberTypeNode } from './NumberTypeNode';

export interface BooleanTypeNode<
TSize extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
TSize extends NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
> {
readonly kind: 'booleanTypeNode';

Expand All @@ -12,7 +11,7 @@ export interface BooleanTypeNode<
}

export function booleanTypeNode<
TSize extends ResolveNestedTypeNode<NumberTypeNode> = NumberTypeNode<'u8'>,
TSize extends NestedTypeNode<NumberTypeNode> = NumberTypeNode<'u8'>,
>(size?: TSize): BooleanTypeNode<TSize> {
return {
kind: 'booleanTypeNode',
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/typeNodes/DateTimeTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NumberTypeNode } from './NumberTypeNode';
import { ResolveNestedTypeNode } from './NestedTypeNode';
import { NestedTypeNode } from './NestedTypeNode';

export interface DateTimeTypeNode<
TNumber extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
> {
readonly kind: 'dateTimeTypeNode';

Expand All @@ -13,7 +13,7 @@ export interface DateTimeTypeNode<

export function dateTimeTypeNode<
TNumber extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
>(number: TNumber): DateTimeTypeNode<TNumber> {
return { kind: 'dateTimeTypeNode', number };
}
6 changes: 3 additions & 3 deletions src/nodes/typeNodes/EnumStructVariantTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { ResolveNestedTypeNode } from './NestedTypeNode';
import type { NestedTypeNode } from './NestedTypeNode';
import type { IdlTypeEnumField, IdlTypeEnumVariant } from '../../idl';
import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../../shared';
import { StructTypeNode, structTypeNodeFromIdl } from './StructTypeNode';

export interface EnumStructVariantTypeNode<
TStruct extends
ResolveNestedTypeNode<StructTypeNode> = ResolveNestedTypeNode<StructTypeNode>,
NestedTypeNode<StructTypeNode> = NestedTypeNode<StructTypeNode>,
> {
readonly kind: 'enumStructVariantTypeNode';

Expand All @@ -17,7 +17,7 @@ export interface EnumStructVariantTypeNode<
}

export function enumStructVariantTypeNode<
TStruct extends ResolveNestedTypeNode<StructTypeNode>,
TStruct extends NestedTypeNode<StructTypeNode>,
>(name: string, struct: TStruct): EnumStructVariantTypeNode<TStruct> {
if (!name) {
throw new InvalidKinobiTreeError(
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/typeNodes/EnumTupleVariantTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { ResolveNestedTypeNode } from './NestedTypeNode';
import type { NestedTypeNode } from './NestedTypeNode';
import type { IdlType, IdlTypeEnumVariant } from '../../idl';
import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../../shared';
import { TupleTypeNode, tupleTypeNodeFromIdl } from './TupleTypeNode';

export interface EnumTupleVariantTypeNode<
TTuple extends
ResolveNestedTypeNode<TupleTypeNode> = ResolveNestedTypeNode<TupleTypeNode>,
TTuple extends NestedTypeNode<TupleTypeNode> = NestedTypeNode<TupleTypeNode>,
> {
readonly kind: 'enumTupleVariantTypeNode';

Expand All @@ -17,7 +16,7 @@ export interface EnumTupleVariantTypeNode<
}

export function enumTupleVariantTypeNode<
TTuple extends ResolveNestedTypeNode<TupleTypeNode>,
TTuple extends NestedTypeNode<TupleTypeNode>,
>(name: string, tuple: TTuple): EnumTupleVariantTypeNode<TTuple> {
if (!name) {
throw new InvalidKinobiTreeError(
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/typeNodes/EnumTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ResolveNestedTypeNode } from './NestedTypeNode';
import type { NestedTypeNode } from './NestedTypeNode';
import type { IdlTypeEnum } from '../../idl';
import { enumEmptyVariantTypeNodeFromIdl } from './EnumEmptyVariantTypeNode';
import { enumStructVariantTypeNodeFromIdl } from './EnumStructVariantTypeNode';
Expand All @@ -8,8 +8,7 @@ import { NumberTypeNode, numberTypeNode } from './NumberTypeNode';

export interface EnumTypeNode<
TVariants extends EnumVariantTypeNode[] = EnumVariantTypeNode[],
TSize extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
TSize extends NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
> {
readonly kind: 'enumTypeNode';

Expand All @@ -20,7 +19,7 @@ export interface EnumTypeNode<

export function enumTypeNode<
const TVariants extends EnumVariantTypeNode[],
TSize extends ResolveNestedTypeNode<NumberTypeNode> = NumberTypeNode<'u8'>,
TSize extends NestedTypeNode<NumberTypeNode> = NumberTypeNode<'u8'>,
>(
variants: TVariants,
options: { size?: TSize } = {}
Expand Down
18 changes: 9 additions & 9 deletions src/nodes/typeNodes/NestedTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { FixedSizeTypeNode } from './FixedSizeTypeNode';
import { SizePrefixTypeNode } from './SizePrefixTypeNode';
import { TypeNode } from './TypeNode';

export type ResolveNestedTypeNode<TType extends TypeNode> =
export type NestedTypeNode<TType extends TypeNode> =
| TType
| ((FixedSizeTypeNode | SizePrefixTypeNode) & {
type: ResolveNestedTypeNode<TType>;
type: NestedTypeNode<TType>;
});

export function resolveNestedTypeNode<TType extends TypeNode>(
typeNode: ResolveNestedTypeNode<TType>
typeNode: NestedTypeNode<TType>
): TType {
switch (typeNode.kind) {
case 'fixedSizeTypeNode':
case 'sizePrefixTypeNode':
return resolveNestedTypeNode<TType>(
typeNode.type as ResolveNestedTypeNode<TType>
typeNode.type as NestedTypeNode<TType>
);
default:
return typeNode;
Expand All @@ -26,27 +26,27 @@ export function transformNestedTypeNode<
TFrom extends TypeNode,
TTo extends TypeNode,
>(
typeNode: ResolveNestedTypeNode<TFrom>,
typeNode: NestedTypeNode<TFrom>,
map: (type: TFrom) => TTo
): ResolveNestedTypeNode<TTo> {
): NestedTypeNode<TTo> {
switch (typeNode.kind) {
case 'fixedSizeTypeNode':
case 'sizePrefixTypeNode':
return {
...typeNode,
type: transformNestedTypeNode(
typeNode.type as ResolveNestedTypeNode<TFrom>,
typeNode.type as NestedTypeNode<TFrom>,
map
),
} as ResolveNestedTypeNode<TTo>;
} as NestedTypeNode<TTo>;
default:
return map(typeNode);
}
}

export function isNestedTypeNode<TType extends TypeNode>(
typeNode: TypeNode
): typeNode is ResolveNestedTypeNode<TType> {
): typeNode is NestedTypeNode<TType> {
// const kinds = Array.isArray(kind) ? kind : [kind];
// return !!node && (kinds as NodeKind[]).includes(node.kind);
return (
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/typeNodes/OptionTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { IdlTypeOption } from '../../idl';
import { ResolveNestedTypeNode } from './NestedTypeNode';
import { NestedTypeNode } from './NestedTypeNode';
import { NumberTypeNode, numberTypeNode } from './NumberTypeNode';
import { TypeNode, createTypeNodeFromIdl } from './TypeNode';

export interface OptionTypeNode<
TItem extends TypeNode = TypeNode,
TPrefix extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
> {
readonly kind: 'optionTypeNode';

Expand All @@ -21,7 +21,7 @@ export interface OptionTypeNode<

export function optionTypeNode<
TItem extends TypeNode,
TPrefix extends ResolveNestedTypeNode<NumberTypeNode> = NumberTypeNode<'u8'>,
TPrefix extends NestedTypeNode<NumberTypeNode> = NumberTypeNode<'u8'>,
>(
item: TItem,
options: {
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/typeNodes/SizePrefixTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ResolveNestedTypeNode } from './NestedTypeNode';
import { NestedTypeNode } from './NestedTypeNode';
import { NumberTypeNode } from './NumberTypeNode';
import { TypeNode } from './TypeNode';

export interface SizePrefixTypeNode<
TType extends TypeNode = TypeNode,
TPrefix extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
> {
readonly kind: 'sizePrefixTypeNode';

Expand All @@ -17,7 +17,7 @@ export interface SizePrefixTypeNode<
export function sizePrefixTypeNode<
TType extends TypeNode = TypeNode,
TPrefix extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
>(type: TType, prefix: TPrefix): SizePrefixTypeNode<TType, TPrefix> {
return { kind: 'sizePrefixTypeNode', type, prefix };
}
6 changes: 3 additions & 3 deletions src/nodes/typeNodes/SolAmountTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ResolveNestedTypeNode } from './NestedTypeNode';
import { NestedTypeNode } from './NestedTypeNode';
import { NumberTypeNode } from './NumberTypeNode';

export interface SolAmountTypeNode<
TNumber extends
ResolveNestedTypeNode<NumberTypeNode> = ResolveNestedTypeNode<NumberTypeNode>,
NestedTypeNode<NumberTypeNode> = NestedTypeNode<NumberTypeNode>,
> {
readonly kind: 'solAmountTypeNode';

Expand All @@ -12,7 +12,7 @@ export interface SolAmountTypeNode<
}

export function solAmountTypeNode<
TNumber extends ResolveNestedTypeNode<NumberTypeNode>,
TNumber extends NestedTypeNode<NumberTypeNode>,
>(number: TNumber): SolAmountTypeNode<TNumber> {
return { kind: 'solAmountTypeNode', number };
}
10 changes: 5 additions & 5 deletions test/nodes/typeNodes/TypeNode.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
numberTypeNode,
NumberTypeNode,
resolveNestedTypeNode,
ResolveNestedTypeNode,
NestedTypeNode,
StringTypeNode,
stringTypeNode,
} from '../../../src';
Expand All @@ -12,12 +12,12 @@ import {
// [ResolveNestedTypeNode]: it constraints the nested type of a node.
const stringNestedNode = fixedSizeTypeNode(stringTypeNode(), 32);
const numberNestedNode = fixedSizeTypeNode(numberTypeNode('u32'), 32);
stringNestedNode satisfies ResolveNestedTypeNode<StringTypeNode>;
numberNestedNode satisfies ResolveNestedTypeNode<NumberTypeNode>;
stringNestedNode satisfies NestedTypeNode<StringTypeNode>;
numberNestedNode satisfies NestedTypeNode<NumberTypeNode>;
// @ts-expect-error The nested type is not a number.
stringNestedNode satisfies ResolveNestedTypeNode<NumberTypeNode>;
stringNestedNode satisfies NestedTypeNode<NumberTypeNode>;
// @ts-expect-error The nested type is not a string.
numberNestedNode satisfies ResolveNestedTypeNode<StringTypeNode>;
numberNestedNode satisfies NestedTypeNode<StringTypeNode>;
}

{
Expand Down

0 comments on commit 5d943e6

Please sign in to comment.