Skip to content

Commit

Permalink
Rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Aug 8, 2024
1 parent de7e64e commit 11003b4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/web3/src/codec/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ export class ObjectCodec<T> extends Codec<T> {
}
}

type ExtractType<T> = T extends { kind: infer U extends string } ? U : never
type ExtractKind<T> = T extends { kind: infer U extends string } ? U : never
type ExtractValue<T, K> = T extends { kind: K; value: infer V } ? V : never

export class EnumCodec<T extends { kind: ExtractType<T>; value: ExtractValue<T, ExtractType<T>> }> extends Codec<T> {
private kinds: ExtractType<T>[]
export class EnumCodec<T extends { kind: ExtractKind<T>; value: ExtractValue<T, ExtractKind<T>> }> extends Codec<T> {
private kinds: ExtractKind<T>[]

constructor(private name: string, private codecs: { [K in ExtractType<T>]: Codec<ExtractValue<T, K>> }) {
constructor(private name: string, private codecs: { [K in ExtractKind<T>]: Codec<ExtractValue<T, K>> }) {
super()
this.kinds = Object.keys(codecs) as ExtractType<T>[]
this.kinds = Object.keys(codecs) as ExtractKind<T>[]
}

encode(value: T): Uint8Array {
Expand All @@ -114,7 +114,7 @@ export class EnumCodec<T extends { kind: ExtractType<T>; value: ExtractValue<T,
const index = input.consumeByte()
if (index >= 0 && index < this.kinds.length) {
const kind = this.kinds[`${index}`]
const codec = this.codecs[kind as ExtractType<T>]
const codec = this.codecs[kind as ExtractKind<T>]
return { kind, value: codec._decode(input) } as T
}
throw new Error(`Invalid encoded ${this.name} kind: ${index}`)
Expand Down

0 comments on commit 11003b4

Please sign in to comment.