Skip to content

Commit

Permalink
refactor(experimental): use DrainOuterGeneric helper on codec type ma…
Browse files Browse the repository at this point in the history
…ppings
  • Loading branch information
lorisleiva committed Mar 15, 2024
1 parent cbebf37 commit 2206d24
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/codecs-data-structures/src/data-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
SolanaError,
} from '@solana/errors';

import { getMaxSize, maxCodecSizes, sumCodecSizes } from './utils';
import { DrainOuterGeneric, getMaxSize, maxCodecSizes, sumCodecSizes } from './utils';

/**
* Defines a data enum using discriminated union types.
Expand Down Expand Up @@ -74,21 +74,21 @@ export type DataEnumCodecConfig<TDiscriminator = NumberCodec | NumberDecoder | N
type Variants<T> = readonly (readonly [string, T])[];
type ArrayIndices<T extends readonly unknown[]> = Exclude<Partial<T>['length'], T['length']> & number;

type GetEncoderTypeFromVariants<TVariants extends Variants<Encoder<any>>> = {
type GetEncoderTypeFromVariants<TVariants extends Variants<Encoder<any>>> = DrainOuterGeneric<{
[I in ArrayIndices<TVariants>]: (TVariants[I][1] extends Encoder<infer TFrom>
? TFrom extends object
? TFrom
: object
: never) & { __kind: TVariants[I][0] };
}[ArrayIndices<TVariants>];
}>[ArrayIndices<TVariants>];

type GetDecoderTypeFromVariants<TVariants extends Variants<Decoder<any>>> = {
type GetDecoderTypeFromVariants<TVariants extends Variants<Decoder<any>>> = DrainOuterGeneric<{
[I in ArrayIndices<TVariants>]: (TVariants[I][1] extends Decoder<infer TTo>
? TTo extends object
? TTo
: object
: never) & { __kind: TVariants[I][0] };
}[ArrayIndices<TVariants>];
}>[ArrayIndices<TVariants>];

/**
* Creates a data enum encoder.
Expand Down
10 changes: 5 additions & 5 deletions packages/codecs-data-structures/src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import {
VariableSizeEncoder,
} from '@solana/codecs-core';

import { getFixedSize, getMaxSize, sumCodecSizes } from './utils';
import { DrainOuterGeneric, getFixedSize, getMaxSize, sumCodecSizes } from './utils';

type Fields<T> = readonly (readonly [string, T])[];
type ArrayIndices<T extends readonly unknown[]> = Exclude<Partial<T>['length'], T['length']> & number;

type GetEncoderTypeFromFields<TFields extends Fields<Encoder<any>>> = {
type GetEncoderTypeFromFields<TFields extends Fields<Encoder<any>>> = DrainOuterGeneric<{
[I in ArrayIndices<TFields> as TFields[I][0]]: TFields[I][1] extends Encoder<infer TFrom> ? TFrom : never;
};
}>;

type GetDecoderTypeFromFields<TFields extends Fields<Decoder<any>>> = {
type GetDecoderTypeFromFields<TFields extends Fields<Decoder<any>>> = DrainOuterGeneric<{
[I in ArrayIndices<TFields> as TFields[I][0]]: TFields[I][1] extends Decoder<infer TTo> ? TTo : never;
};
}>;

/**
* Creates a encoder for a custom object.
Expand Down
2 changes: 2 additions & 0 deletions packages/codecs-data-structures/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isFixedSize } from '@solana/codecs-core';

export type DrainOuterGeneric<T> = [T] extends [unknown] ? T : never;

export function maxCodecSizes(sizes: (number | null)[]): number | null {
return sizes.reduce(
(all, size) => (all === null || size === null ? null : Math.max(all, size)),
Expand Down

0 comments on commit 2206d24

Please sign in to comment.