Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sway type matchers #3137

Merged
merged 17 commits into from
Sep 12, 2024
Merged
35 changes: 35 additions & 0 deletions packages/abi/src/coder/encoding/v1/coder-matcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { swayTypeMatchers } from '../../../matchers/sway-type-matchers';

import { EmptyCoder } from './coders/empty-coder';
import { EnumCoder } from './coders/enum-coder';
import { OptionCoder } from './coders/option-coder';
import type { Coder } from './coders/types';
import { U8Coder } from './coders/u8-coder';

// Coder | undefined for proof-of-concept, it'll become only Coder later
export const coderMatcher: Record<keyof typeof swayTypeMatchers, Coder | undefined> = {
empty: EmptyCoder,
u8: U8Coder,
enum: EnumCoder,
result: EnumCoder,
option: OptionCoder,
string: undefined,
bool: undefined,
u16: undefined,
u32: undefined,
u64: undefined,
u256: undefined,
b256: undefined,
generic: undefined,
stdString: undefined,
struct: undefined,
b512: undefined,
bytes: undefined,
vector: undefined,
tuple: undefined,
array: undefined,
assetId: undefined,
evmAddress: undefined,
rawUntypedPtr: undefined,
rawUntypedSlice: undefined,
};
3 changes: 3 additions & 0 deletions packages/abi/src/coder/encoding/v1/coders/empty-coder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Coder } from './types';

export class EmptyCoder implements Coder {}
3 changes: 3 additions & 0 deletions packages/abi/src/coder/encoding/v1/coders/enum-coder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Coder } from './types';

export class EnumCoder implements Coder {}
3 changes: 3 additions & 0 deletions packages/abi/src/coder/encoding/v1/coders/option-coder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Coder } from './types';

export class OptionCoder implements Coder {}
1 change: 1 addition & 0 deletions packages/abi/src/coder/encoding/v1/coders/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface Coder {}
3 changes: 3 additions & 0 deletions packages/abi/src/coder/encoding/v1/coders/u8-coder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Coder } from './types';

export class U8Coder implements Coder {}
35 changes: 35 additions & 0 deletions packages/abi/src/gen/typer-matcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { swayTypeMatchers } from '../matchers/sway-type-matchers';

import { EmptyTyper } from './typers/empty-typer';
import { EnumTyper } from './typers/enum-typer';
import { OptionTyper } from './typers/option-typer';
import { ResultTyper } from './typers/result-typer';
import type { Typer } from './typers/types';
import { U8Typer } from './typers/u8-typer';

export const coderMatcher: Record<keyof typeof swayTypeMatchers, Typer | undefined> = {
empty: EmptyTyper,
u8: U8Typer,
enum: EnumTyper,
result: ResultTyper,
option: OptionTyper,
string: undefined,
bool: undefined,
u16: undefined,
u32: undefined,
u64: undefined,
u256: undefined,
b256: undefined,
generic: undefined,
stdString: undefined,
struct: undefined,
b512: undefined,
bytes: undefined,
vector: undefined,
tuple: undefined,
array: undefined,
assetId: undefined,
evmAddress: undefined,
rawUntypedPtr: undefined,
rawUntypedSlice: undefined,
};
3 changes: 3 additions & 0 deletions packages/abi/src/gen/typers/empty-typer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Typer } from './types';

export class EmptyTyper implements Typer {}
3 changes: 3 additions & 0 deletions packages/abi/src/gen/typers/enum-typer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Typer } from './types';

export class EnumTyper implements Typer {}
3 changes: 3 additions & 0 deletions packages/abi/src/gen/typers/option-typer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Typer } from './types';

export class OptionTyper implements Typer {}
3 changes: 3 additions & 0 deletions packages/abi/src/gen/typers/result-typer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Typer } from './types';

export class ResultTyper implements Typer {}
1 change: 1 addition & 0 deletions packages/abi/src/gen/typers/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface Typer {}
3 changes: 3 additions & 0 deletions packages/abi/src/gen/typers/u8-typer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Typer } from './types';

export class U8Typer implements Typer {}
48 changes: 48 additions & 0 deletions packages/abi/src/matchers/sway-type-matchers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { swayTypeMatchers } from './sway-type-matchers';

const testCases: Record<keyof typeof swayTypeMatchers, string> = {
empty: '()',
generic: 'generic T',
bool: 'bool',
u8: 'u8',
u16: 'u16',
u32: 'u32',
u64: 'u64',
u256: 'u256',
b256: 'b256',

string: 'str[5]',
array: '[_; 2]',
tuple: '(_, _, _)',

struct: 'struct MyStruct',
assetId: 'struct std::asset_id::AssetId',
b512: 'struct std::b512::B512',
bytes: 'struct std::bytes::Bytes',
evmAddress: 'struct std::vm::evm::evm_address::EvmAddress',
stdString: 'struct std::string::String',
vector: 'struct std::vec::Vec',

enum: 'enum MyEnum',
option: 'enum std::option::Option',
result: 'enum std::result::Result',

rawUntypedPtr: 'raw untyped ptr',
rawUntypedSlice: 'raw untyped slice',
};

describe('sway type matchers', () => {
test.each(Object.entries(testCases))('%s - %s', (type, value) => {
expect(swayTypeMatchers[type as keyof typeof swayTypeMatchers](value)).toEqual(true);

// verify that it doesn't match any other type
const allOtherMatchers = Object.entries(swayTypeMatchers).filter(([key]) => key !== type);
allOtherMatchers.forEach(([, matcher]) => {
expect(matcher(value)).toEqual(false);
});
});

test('raw vector is not interpreted as vector', () => {
expect(swayTypeMatchers.vector('struct std::vec::RawVec')).toEqual(false);
});
});
89 changes: 89 additions & 0 deletions packages/abi/src/matchers/sway-type-matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
type SwayType =
| 'empty'
| 'bool'
| 'u8'
| 'u16'
| 'u32'
| 'u64'
| 'u256'
| 'b256'
| 'generic'
| 'string'
| 'stdString'
| 'option'
| 'result'
| 'enum'
| 'struct'
| 'b512'
| 'bytes'
| 'vector'
| 'tuple'
| 'array'
| 'assetId'
| 'evmAddress'
| 'rawUntypedPtr' // might not need it
| 'rawUntypedSlice'; // might not need it

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

const empty: Matcher = (type) => type === '()';
const bool: Matcher = (type) => type === 'bool';
const u8: Matcher = (type) => type === 'u8';
const u16: Matcher = (type) => type === 'u16';
const u32: Matcher = (type) => type === 'u32';
const u64: Matcher = (type) => type === 'u64';
const u256: Matcher = (type) => type === 'u256';
const b256: Matcher = (type) => type === 'b256';
const generic: Matcher = (type) => /^generic ([^\s]+)$/m.test(type);
const string: Matcher = (type) => /^str\[(.+)\]$/m.test(type);
const tuple: Matcher = (type) => /^\([_,\s]+\)$/m.test(type);
const array: Matcher = (type) => /^\[_; ([0-9]+)\]$/m.test(type);

const struct: Matcher = (type) =>
/^struct (.+::)?(.+)$/m.test(type) &&
!/^struct (std::.*)?(AssetId|B512|Vec|RawVec|EvmAddress|Bytes|String|RawBytes)$/m.test(type);
const assetId: Matcher = (type) => type === 'struct std::asset_id::AssetId';
const b512: Matcher = (type) => type === 'struct std::b512::B512';
const bytes: Matcher = (type) => type === 'struct std::bytes::Bytes';
const evmAddress: Matcher = (type) => type === 'struct std::vm::evm::evm_address::EvmAddress';
const stdString: Matcher = (type) => type === 'struct std::string::String';
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);

const rawUntypedPtr: Matcher = (type) => type === 'raw untyped ptr';
const rawUntypedSlice: Matcher = (type) => type === 'raw untyped slice';

export const swayTypeMatchers: Record<SwayType, Matcher> = {
empty,
generic,
bool,
u8,
u16,
u32,
u64,
u256,
b256,

string,
tuple,
array,

struct,
assetId,
b512,
bytes,
evmAddress,
stdString,
vector,

enum: enumMatcher,
option,
result,

rawUntypedPtr,
rawUntypedSlice,
};
Loading