Skip to content

Commit

Permalink
add missing matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Oct 4, 2024
1 parent 3659e50 commit 9ba270b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/abi/src/matchers/sway-type-matchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const testMappings: Record<keyof typeof swayTypeMatchers, `${string}-matched`> =
evmAddress: 'evmAddress-matched',
rawUntypedPtr: 'rawUntypedPtr-matched',
rawUntypedSlice: 'rawUntypedSlice-matched',
str: 'str-matched',
};

const matcher = createMatcher(testMappings);
Expand Down
16 changes: 11 additions & 5 deletions packages/abi/src/matchers/sway-type-matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type SwayType =
| 'b256'
| 'generic'
| 'string'
| 'str'
| 'stdString'
| 'option'
| 'result'
Expand All @@ -22,7 +23,7 @@ export type SwayType =
| 'assetId'
| 'evmAddress'
| 'rawUntypedPtr' // might not need it
| 'rawUntypedSlice'; // might not need it
| 'rawUntypedSlice';

type Matcher = (type: string) => boolean;

Expand All @@ -41,13 +42,15 @@ const generic: Matcher = (type) => GENERIC_REGEX.test(type);
export const STRING_REGEX = /^str\[(?<length>[0-9]+)\]/;
const string: Matcher = (type) => STRING_REGEX.test(type);

const str: Matcher = (type) => type === 'str';

export const TUPLE_REGEX = /^\((?<items>.+)\)$/m;
const tuple: Matcher = (type) => TUPLE_REGEX.test(type);

export const ARRAY_REGEX = /^\[(?<item>[\w\s\\[\]]+);\s*(?<length>[0-9]+)\]/;
export const ARRAY_REGEX = /\[(?<item>[\w\s\\[\]]+);\s*(?<length>[0-9]+)\]/;
const array: Matcher = (type) => ARRAY_REGEX.test(type);

const STRUCT_REGEX = /^struct .+$/m;
export const STRUCT_REGEX = /^struct (.+::)?(?<name>.+)$/m;
const STRUCT_STD_REGEX =
/^struct std::.*(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m;
const struct: Matcher = (type) => STRUCT_REGEX.test(type) && !STRUCT_STD_REGEX.test(type);
Expand All @@ -60,7 +63,9 @@ const vector: Matcher = (type) => type === 'struct std::vec::Vec';

const option: Matcher = (type) => type === 'enum std::option::Option';
const result: Matcher = (type) => type === 'enum std::result::Result';
const enumMatcher: Matcher = (type) => !option(type) && !result(type) && /^enum .*$/m.test(type);

export const ENUM_REGEX = /^enum (.+::)?(?<name>.+)$/m;
const enumMatcher: Matcher = (type) => !option(type) && !result(type) && ENUM_REGEX.test(type);

const rawUntypedPtr: Matcher = (type) => type === 'raw untyped ptr';
const rawUntypedSlice: Matcher = (type) => type === 'raw untyped slice';
Expand All @@ -75,6 +80,7 @@ export const swayTypeMatchers: Record<SwayType, Matcher> = {
u64,
u256,
b256,
str,

string,
tuple,
Expand Down Expand Up @@ -105,7 +111,7 @@ export function createMatcher<T>(mappings: Record<SwayType, T>) {
for (const [key, matcher] of swayTypeMatcherEntries) {
if (matcher(swayType)) {
const mapping = mappings[key as SwayType];
if (mapping) {
if (mapping !== undefined) {
return mapping;
}
break;
Expand Down

0 comments on commit 9ba270b

Please sign in to comment.