Skip to content

Commit

Permalink
refactor: use parseFunctionArgs method which performs generic type ch…
Browse files Browse the repository at this point in the history
…ecking with better error messages
  • Loading branch information
boblat committed Nov 6, 2024
1 parent 898cd45 commit f369afc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/awst_build/eb/bytes-expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { wtypes } from '../../awst/wtypes'

import { CodeError, wrapInCodeError } from '../../errors'
import { logger } from '../../logger'
import { base32ToUint8Array, base64ToUint8Array, hexToUint8Array, uint8ArrayToUtf8, utf8ToUint8Array } from '../../util'
import { base32ToUint8Array, base64ToUint8Array, enumKeyFromValue, hexToUint8Array, uint8ArrayToUtf8, utf8ToUint8Array } from '../../util'
import type { InstanceType, PType } from '../ptypes'
import {
ArrayPType,
Expand Down Expand Up @@ -194,11 +194,11 @@ export class BytesExpressionBuilder extends InstanceExpressionBuilder<InstanceTy
case 'bitwiseInvert':
return new BytesInvertBuilder(this._expr)
case 'bitwiseAnd':
return new BitwiseOpExpressionBuilder(this._expr, BytesBinaryOperator.bitAnd)
return new BitwiseOpFunctionBuilder(this._expr, BytesBinaryOperator.bitAnd)
case 'bitwiseOr':
return new BitwiseOpExpressionBuilder(this._expr, BytesBinaryOperator.bitOr)
return new BitwiseOpFunctionBuilder(this._expr, BytesBinaryOperator.bitOr)
case 'bitwiseXor':
return new BitwiseOpExpressionBuilder(this._expr, BytesBinaryOperator.bitXor)
return new BitwiseOpFunctionBuilder(this._expr, BytesBinaryOperator.bitXor)
case 'toString':
return new ToStringBuilder(this._expr)
case 'concat':
Expand Down Expand Up @@ -279,7 +279,7 @@ export class BytesInvertBuilder extends ParameterlessFunctionBuilder {
}
}

export class BitwiseOpExpressionBuilder extends FunctionBuilder {
export class BitwiseOpFunctionBuilder extends FunctionBuilder {
constructor(
private expr: awst.Expression,
private op: BytesBinaryOperator,
Expand All @@ -288,12 +288,21 @@ export class BitwiseOpExpressionBuilder extends FunctionBuilder {
}

call(args: ReadonlyArray<NodeBuilder>, typeArgs: ReadonlyArray<PType>, sourceLocation: SourceLocation): NodeBuilder {
const [other] = requireExpressionsOfType(args, [bytesPType], sourceLocation)
const {
args: [other],
} = parseFunctionArgs({
args,
typeArgs,
genericTypeArgs: 0,
callLocation: sourceLocation,
funcName: enumKeyFromValue(this.op, BytesBinaryOperator),
argSpec: (a) => [a.required(bytesPType)],
})
return new BytesExpressionBuilder(
nodeFactory.bytesBinaryOperation({
wtype: wtypes.bytesWType,
left: this.expr,
right: other,
right: other.resolve(),
op: this.op,
sourceLocation,
}),
Expand Down
12 changes: 12 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ export const enumFromValue = <TValue, TEnum extends TValue>(
throw new Error(`${message} ${value}`)
}

export const enumKeyFromValue = <TValue, TEnum extends TValue>(
value: TValue,
enumType: Record<string, TEnum>,
message: string = 'Invalid enum value: ',
) => {
const key = Object.entries(enumType).find(([_, v]) => v === value)?.[0]
if (key) {
return key
}
throw new Error(`${message} ${value}`)
}

export const convertEnum = <TEnumIn, TEnumOut, TKeys extends string>(
value: TEnumIn,
fromEnum: Record<TKeys, TEnumIn>,
Expand Down

0 comments on commit f369afc

Please sign in to comment.