@pezkuwi/types-create 16.5.17 → 16.5.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/LICENSE +201 -0
- package/build/README.md +3 -0
- package/build/bundle.js +3 -0
- package/build/cjs/bundle.d.ts +3 -0
- package/build/cjs/bundle.js +9 -0
- package/build/cjs/create/class.js +165 -0
- package/build/cjs/create/index.js +5 -0
- package/build/cjs/create/type.js +66 -0
- package/build/cjs/exports.js +5 -0
- package/build/cjs/index.js +5 -0
- package/build/cjs/packageInfo.js +4 -0
- package/build/cjs/types/augmentRegistry.js +3 -0
- package/build/cjs/types/index.js +6 -0
- package/build/cjs/types/lookup.js +2 -0
- package/build/cjs/types/types.js +29 -0
- package/build/cjs/util/encodeTypes.js +128 -0
- package/build/cjs/util/getTypeDef.js +195 -0
- package/build/cjs/util/index.js +7 -0
- package/build/cjs/util/typeSplit.js +60 -0
- package/build/cjs/util/xcm.js +9 -0
- package/build/create/class.d.ts +5 -0
- package/build/create/class.js +160 -0
- package/build/create/index.d.ts +2 -0
- package/build/create/index.js +2 -0
- package/build/create/type.d.ts +3 -0
- package/build/create/type.js +63 -0
- package/build/exports.d.ts +2 -0
- package/build/exports.js +2 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +2 -0
- package/build/package.json +291 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageDetect.js +4 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/packageInfo.js +1 -0
- package/build/types/augmentRegistry.d.ts +11 -0
- package/build/types/augmentRegistry.js +1 -0
- package/build/types/index.d.ts +4 -0
- package/build/types/index.js +3 -0
- package/build/types/lookup.d.ts +14 -0
- package/build/types/lookup.js +1 -0
- package/build/types/types.d.ts +43 -0
- package/build/types/types.js +26 -0
- package/build/util/encodeTypes.d.ts +11 -0
- package/build/util/encodeTypes.js +123 -0
- package/build/util/getTypeDef.d.ts +8 -0
- package/build/util/getTypeDef.js +192 -0
- package/build/util/index.d.ts +4 -0
- package/build/util/index.js +4 -0
- package/build/util/typeSplit.d.ts +1 -0
- package/build/util/typeSplit.js +57 -0
- package/build/util/xcm.d.ts +2 -0
- package/build/util/xcm.js +5 -0
- package/build-deno/README.md +3 -0
- package/build-deno/bundle.ts +5 -0
- package/build-deno/create/class.ts +252 -0
- package/build-deno/create/index.ts +3 -0
- package/build-deno/create/type.ts +85 -0
- package/build-deno/exports.ts +3 -0
- package/build-deno/index.ts +4 -0
- package/build-deno/mod.ts +2 -0
- package/build-deno/packageDetect.ts +8 -0
- package/build-deno/packageInfo.ts +3 -0
- package/build-deno/types/augmentRegistry.ts +15 -0
- package/build-deno/types/index.ts +7 -0
- package/build-deno/types/lookup.ts +16 -0
- package/build-deno/types/types.ts +45 -0
- package/build-deno/util/encodeTypes.ts +201 -0
- package/build-deno/util/getTypeDef.ts +267 -0
- package/build-deno/util/index.ts +5 -0
- package/build-deno/util/typeSplit.ts +53 -0
- package/build-deno/util/xcm.ts +10 -0
- package/build-tsc-cjs/packageDetect.js +6 -0
- package/{cjs → build-tsc-cjs}/packageInfo.js +1 -1
- package/{packageInfo.js → build-tsc-esm/packageInfo.js} +1 -1
- package/package.json +11 -11
- package/src/bundle.ts +9 -0
- package/src/create/class.ts +257 -0
- package/src/create/index.ts +5 -0
- package/src/create/type.ts +94 -0
- package/src/exports.ts +6 -0
- package/src/index.ts +6 -0
- package/src/mod.ts +4 -0
- package/src/packageDetect.ts +12 -0
- package/src/packageInfo.ts +6 -0
- package/src/types/augmentRegistry.ts +19 -0
- package/src/types/index.ts +11 -0
- package/src/types/lookup.ts +21 -0
- package/src/types/types.ts +47 -0
- package/src/util/encodeTypes.spec.ts +217 -0
- package/src/util/encodeTypes.ts +205 -0
- package/src/util/getTypeDef.spec.ts +704 -0
- package/src/util/getTypeDef.ts +279 -0
- package/src/util/index.ts +7 -0
- package/src/util/typeSplit.spec.ts +50 -0
- package/src/util/typeSplit.ts +56 -0
- package/src/util/xcm.ts +12 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +17 -0
- package/tsconfig.spec.tsbuildinfo +1 -0
- /package/{cjs → build}/bundle.d.ts +0 -0
- /package/{create → build/cjs/create}/class.d.ts +0 -0
- /package/{create → build/cjs/create}/index.d.ts +0 -0
- /package/{create → build/cjs/create}/type.d.ts +0 -0
- /package/{exports.d.ts → build/cjs/exports.d.ts} +0 -0
- /package/{index.d.ts → build/cjs/index.d.ts} +0 -0
- /package/{cjs → build/cjs}/package.json +0 -0
- /package/{packageDetect.d.ts → build/cjs/packageDetect.d.ts} +0 -0
- /package/{cjs → build/cjs}/packageDetect.js +0 -0
- /package/{packageInfo.d.ts → build/cjs/packageInfo.d.ts} +0 -0
- /package/{types → build/cjs/types}/augmentRegistry.d.ts +0 -0
- /package/{types → build/cjs/types}/index.d.ts +0 -0
- /package/{types → build/cjs/types}/lookup.d.ts +0 -0
- /package/{types → build/cjs/types}/types.d.ts +0 -0
- /package/{util → build/cjs/util}/encodeTypes.d.ts +0 -0
- /package/{util → build/cjs/util}/getTypeDef.d.ts +0 -0
- /package/{util → build/cjs/util}/index.d.ts +0 -0
- /package/{util → build/cjs/util}/typeSplit.d.ts +0 -0
- /package/{util → build/cjs/util}/xcm.d.ts +0 -0
- /package/{bundle.d.ts → build-tsc/bundle.d.ts} +0 -0
- /package/{cjs → build-tsc}/create/class.d.ts +0 -0
- /package/{cjs → build-tsc}/create/index.d.ts +0 -0
- /package/{cjs → build-tsc}/create/type.d.ts +0 -0
- /package/{cjs → build-tsc}/exports.d.ts +0 -0
- /package/{cjs → build-tsc}/index.d.ts +0 -0
- /package/{cjs → build-tsc}/packageDetect.d.ts +0 -0
- /package/{cjs → build-tsc}/packageInfo.d.ts +0 -0
- /package/{cjs → build-tsc}/types/augmentRegistry.d.ts +0 -0
- /package/{cjs → build-tsc}/types/index.d.ts +0 -0
- /package/{cjs → build-tsc}/types/lookup.d.ts +0 -0
- /package/{cjs → build-tsc}/types/types.d.ts +0 -0
- /package/{cjs → build-tsc}/util/encodeTypes.d.ts +0 -0
- /package/{cjs → build-tsc}/util/getTypeDef.d.ts +0 -0
- /package/{cjs → build-tsc}/util/index.d.ts +0 -0
- /package/{cjs → build-tsc}/util/typeSplit.d.ts +0 -0
- /package/{cjs → build-tsc}/util/xcm.d.ts +0 -0
- /package/{cjs → build-tsc-cjs}/bundle.js +0 -0
- /package/{cjs → build-tsc-cjs}/create/class.js +0 -0
- /package/{cjs → build-tsc-cjs}/create/index.js +0 -0
- /package/{cjs → build-tsc-cjs}/create/type.js +0 -0
- /package/{cjs → build-tsc-cjs}/exports.js +0 -0
- /package/{cjs → build-tsc-cjs}/index.js +0 -0
- /package/{cjs → build-tsc-cjs}/types/augmentRegistry.js +0 -0
- /package/{cjs → build-tsc-cjs}/types/index.js +0 -0
- /package/{cjs → build-tsc-cjs}/types/lookup.js +0 -0
- /package/{cjs → build-tsc-cjs}/types/types.js +0 -0
- /package/{cjs → build-tsc-cjs}/util/encodeTypes.js +0 -0
- /package/{cjs → build-tsc-cjs}/util/getTypeDef.js +0 -0
- /package/{cjs → build-tsc-cjs}/util/index.js +0 -0
- /package/{cjs → build-tsc-cjs}/util/typeSplit.js +0 -0
- /package/{cjs → build-tsc-cjs}/util/xcm.js +0 -0
- /package/{bundle.js → build-tsc-esm/bundle.js} +0 -0
- /package/{create → build-tsc-esm/create}/class.js +0 -0
- /package/{create → build-tsc-esm/create}/index.js +0 -0
- /package/{create → build-tsc-esm/create}/type.js +0 -0
- /package/{exports.js → build-tsc-esm/exports.js} +0 -0
- /package/{index.js → build-tsc-esm/index.js} +0 -0
- /package/{packageDetect.js → build-tsc-esm/packageDetect.js} +0 -0
- /package/{types → build-tsc-esm/types}/augmentRegistry.js +0 -0
- /package/{types → build-tsc-esm/types}/index.js +0 -0
- /package/{types → build-tsc-esm/types}/lookup.js +0 -0
- /package/{types → build-tsc-esm/types}/types.js +0 -0
- /package/{util → build-tsc-esm/util}/encodeTypes.js +0 -0
- /package/{util → build-tsc-esm/util}/getTypeDef.js +0 -0
- /package/{util → build-tsc-esm/util}/index.js +0 -0
- /package/{util → build-tsc-esm/util}/typeSplit.js +0 -0
- /package/{util → build-tsc-esm/util}/xcm.js +0 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// Copyright 2017-2025 @pezkuwi/types authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { TypeRegistry } from '@pezkuwi/types';
|
|
7
|
+
import { encodeTypeDef, TypeDefInfo } from '@pezkuwi/types-create';
|
|
8
|
+
|
|
9
|
+
describe('encodeTypeDef', (): void => {
|
|
10
|
+
const registry = new TypeRegistry();
|
|
11
|
+
|
|
12
|
+
it('correctly encodes a complex struct', (): void => {
|
|
13
|
+
expect(
|
|
14
|
+
JSON.parse(encodeTypeDef(registry, {
|
|
15
|
+
info: TypeDefInfo.Struct,
|
|
16
|
+
sub: [
|
|
17
|
+
{
|
|
18
|
+
info: TypeDefInfo.Plain,
|
|
19
|
+
name: 'a',
|
|
20
|
+
type: 'u32'
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
info: TypeDefInfo.Struct,
|
|
24
|
+
name: 'b',
|
|
25
|
+
sub: [
|
|
26
|
+
{
|
|
27
|
+
info: TypeDefInfo.Plain,
|
|
28
|
+
name: 'c',
|
|
29
|
+
type: 'u32'
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
info: TypeDefInfo.Vec,
|
|
33
|
+
name: 'd',
|
|
34
|
+
sub: {
|
|
35
|
+
info: TypeDefInfo.Plain,
|
|
36
|
+
type: 'u32'
|
|
37
|
+
},
|
|
38
|
+
type: ''
|
|
39
|
+
}
|
|
40
|
+
],
|
|
41
|
+
type: ''
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
type: ''
|
|
45
|
+
}))
|
|
46
|
+
).toEqual({
|
|
47
|
+
a: 'u32',
|
|
48
|
+
b: '{"c":"u32","d":"Vec<u32>"}'
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('correctly encodes a complex struct (named)', (): void => {
|
|
53
|
+
expect(
|
|
54
|
+
JSON.parse(encodeTypeDef(registry, {
|
|
55
|
+
info: TypeDefInfo.Struct,
|
|
56
|
+
sub: [
|
|
57
|
+
{
|
|
58
|
+
info: TypeDefInfo.Plain,
|
|
59
|
+
name: 'a',
|
|
60
|
+
type: 'u32'
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
info: TypeDefInfo.Struct,
|
|
64
|
+
name: 'b',
|
|
65
|
+
sub: [
|
|
66
|
+
{
|
|
67
|
+
info: TypeDefInfo.Plain,
|
|
68
|
+
name: 'c',
|
|
69
|
+
type: 'u32'
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
displayName: 'Something',
|
|
73
|
+
info: TypeDefInfo.Vec,
|
|
74
|
+
name: 'd',
|
|
75
|
+
sub: {
|
|
76
|
+
info: TypeDefInfo.Plain,
|
|
77
|
+
type: 'u32'
|
|
78
|
+
},
|
|
79
|
+
type: ''
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
type: ''
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
type: ''
|
|
86
|
+
}))
|
|
87
|
+
).toEqual({
|
|
88
|
+
a: 'u32',
|
|
89
|
+
b: '{"c":"u32","d":"Something"}'
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('correctly encodes a complex enum', (): void => {
|
|
94
|
+
expect(
|
|
95
|
+
JSON.parse(encodeTypeDef(registry, {
|
|
96
|
+
info: TypeDefInfo.Enum,
|
|
97
|
+
sub: [
|
|
98
|
+
{
|
|
99
|
+
info: TypeDefInfo.Plain,
|
|
100
|
+
name: 'a',
|
|
101
|
+
type: 'u32'
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
info: TypeDefInfo.Struct,
|
|
105
|
+
name: 'b',
|
|
106
|
+
sub: [
|
|
107
|
+
{
|
|
108
|
+
info: TypeDefInfo.Plain,
|
|
109
|
+
name: 'c',
|
|
110
|
+
type: 'u32'
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
info: TypeDefInfo.Vec,
|
|
114
|
+
name: 'd',
|
|
115
|
+
sub: {
|
|
116
|
+
info: TypeDefInfo.Plain,
|
|
117
|
+
type: 'u32'
|
|
118
|
+
},
|
|
119
|
+
type: ''
|
|
120
|
+
}
|
|
121
|
+
],
|
|
122
|
+
type: ''
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
info: TypeDefInfo.Enum,
|
|
126
|
+
name: 'f',
|
|
127
|
+
sub: [
|
|
128
|
+
{
|
|
129
|
+
info: TypeDefInfo.Plain,
|
|
130
|
+
name: 'g',
|
|
131
|
+
type: 'Null'
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
info: TypeDefInfo.Plain,
|
|
135
|
+
name: 'h',
|
|
136
|
+
type: 'Null'
|
|
137
|
+
}
|
|
138
|
+
],
|
|
139
|
+
type: ''
|
|
140
|
+
}
|
|
141
|
+
],
|
|
142
|
+
type: ''
|
|
143
|
+
}))
|
|
144
|
+
).toEqual({
|
|
145
|
+
_enum: {
|
|
146
|
+
a: 'u32',
|
|
147
|
+
b: '{"c":"u32","d":"Vec<u32>"}',
|
|
148
|
+
f: '{"_enum":["g","h"]}'
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('correctly encodes a complex enum (named)', (): void => {
|
|
154
|
+
expect(
|
|
155
|
+
JSON.parse(encodeTypeDef(registry, {
|
|
156
|
+
info: TypeDefInfo.Enum,
|
|
157
|
+
sub: [
|
|
158
|
+
{
|
|
159
|
+
info: TypeDefInfo.Plain,
|
|
160
|
+
name: 'a',
|
|
161
|
+
type: 'u32'
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
displayName: 'Something',
|
|
165
|
+
info: TypeDefInfo.Struct,
|
|
166
|
+
name: 'b',
|
|
167
|
+
sub: [
|
|
168
|
+
{
|
|
169
|
+
info: TypeDefInfo.Plain,
|
|
170
|
+
name: 'c',
|
|
171
|
+
type: 'u32'
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
info: TypeDefInfo.Vec,
|
|
175
|
+
name: 'd',
|
|
176
|
+
sub: {
|
|
177
|
+
info: TypeDefInfo.Plain,
|
|
178
|
+
type: 'u32'
|
|
179
|
+
},
|
|
180
|
+
type: ''
|
|
181
|
+
}
|
|
182
|
+
],
|
|
183
|
+
type: ''
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
displayName: 'Option',
|
|
187
|
+
info: TypeDefInfo.Option,
|
|
188
|
+
name: 'e',
|
|
189
|
+
sub: {
|
|
190
|
+
displayName: 'Result',
|
|
191
|
+
info: TypeDefInfo.Result,
|
|
192
|
+
sub: [
|
|
193
|
+
{
|
|
194
|
+
info: TypeDefInfo.Null,
|
|
195
|
+
type: ''
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
info: TypeDefInfo.Plain,
|
|
199
|
+
type: 'u32'
|
|
200
|
+
}
|
|
201
|
+
],
|
|
202
|
+
type: ''
|
|
203
|
+
},
|
|
204
|
+
type: ''
|
|
205
|
+
}
|
|
206
|
+
],
|
|
207
|
+
type: ''
|
|
208
|
+
}))
|
|
209
|
+
).toEqual({
|
|
210
|
+
_enum: {
|
|
211
|
+
a: 'u32',
|
|
212
|
+
b: 'Something',
|
|
213
|
+
e: 'Option<Result<Null, u32>>'
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// Copyright 2017-2025 @pezkuwi/types-create authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { Registry } from '@pezkuwi/types-codec/types';
|
|
5
|
+
import type { TypeDef } from '@pezkuwi/types-create/types';
|
|
6
|
+
|
|
7
|
+
import { isNumber, isUndefined, objectSpread, stringify } from '@pezkuwi/util';
|
|
8
|
+
|
|
9
|
+
import { TypeDefInfo } from '../types/index.js';
|
|
10
|
+
|
|
11
|
+
interface ToString { toString: () => string }
|
|
12
|
+
|
|
13
|
+
const stringIdentity = <T extends ToString> (value: T): string => value.toString();
|
|
14
|
+
|
|
15
|
+
const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
|
|
16
|
+
|
|
17
|
+
export function paramsNotation <T extends ToString> (outer: string, inner?: T | T[], transform: (_: T) => string = stringIdentity): string {
|
|
18
|
+
return `${outer}${
|
|
19
|
+
inner
|
|
20
|
+
? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>`
|
|
21
|
+
: ''
|
|
22
|
+
}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function encodeWithParams (registry: Registry, typeDef: TypeDef, outer: string): string {
|
|
26
|
+
const { info, sub } = typeDef;
|
|
27
|
+
|
|
28
|
+
switch (info) {
|
|
29
|
+
case TypeDefInfo.BTreeMap:
|
|
30
|
+
case TypeDefInfo.BTreeSet:
|
|
31
|
+
case TypeDefInfo.Compact:
|
|
32
|
+
case TypeDefInfo.HashMap:
|
|
33
|
+
case TypeDefInfo.Linkage:
|
|
34
|
+
case TypeDefInfo.Option:
|
|
35
|
+
case TypeDefInfo.Range:
|
|
36
|
+
case TypeDefInfo.RangeInclusive:
|
|
37
|
+
case TypeDefInfo.Result:
|
|
38
|
+
case TypeDefInfo.Vec:
|
|
39
|
+
case TypeDefInfo.WrapperKeepOpaque:
|
|
40
|
+
case TypeDefInfo.WrapperOpaque:
|
|
41
|
+
return paramsNotation(outer, sub, (p) => encodeTypeDef(registry, p));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function encodeSubTypes (registry: Registry, sub: TypeDef[], asEnum?: boolean, extra?: Record<string, unknown>): string {
|
|
48
|
+
const names = sub.map(({ name }) => name);
|
|
49
|
+
|
|
50
|
+
if (!names.every((n) => !!n)) {
|
|
51
|
+
throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const inner: Record<string, string> = objectSpread({}, extra);
|
|
55
|
+
|
|
56
|
+
for (let i = 0, count = sub.length; i < count; i++) {
|
|
57
|
+
const def = sub[i];
|
|
58
|
+
|
|
59
|
+
if (!def.name) {
|
|
60
|
+
throw new Error(`No name found in ${stringify(def)}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
inner[def.name] = encodeTypeDef(registry, def);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return stringify(
|
|
67
|
+
asEnum
|
|
68
|
+
? { _enum: inner }
|
|
69
|
+
: inner
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// We setup a record here to ensure we have comprehensive coverage (any item not covered will result
|
|
74
|
+
// in a compile-time error with the missing index)
|
|
75
|
+
const encoders: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef) => string> = {
|
|
76
|
+
[TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef) =>
|
|
77
|
+
encodeWithParams(registry, typeDef, 'BTreeMap'),
|
|
78
|
+
|
|
79
|
+
[TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef) =>
|
|
80
|
+
encodeWithParams(registry, typeDef, 'BTreeSet'),
|
|
81
|
+
|
|
82
|
+
[TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef) =>
|
|
83
|
+
encodeWithParams(registry, typeDef, 'Compact'),
|
|
84
|
+
|
|
85
|
+
[TypeDefInfo.DoNotConstruct]: (registry: Registry, { displayName, lookupIndex, lookupName }: TypeDef) =>
|
|
86
|
+
`DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`,
|
|
87
|
+
|
|
88
|
+
[TypeDefInfo.Enum]: (registry: Registry, { sub }: TypeDef): string => {
|
|
89
|
+
if (!Array.isArray(sub)) {
|
|
90
|
+
throw new Error('Unable to encode Enum type');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// c-like enums have all Null entries
|
|
94
|
+
// TODO We need to take the disciminant into account and auto-add empty entries
|
|
95
|
+
return sub.every(({ type }) => type === 'Null')
|
|
96
|
+
? stringify({ _enum: sub.map(({ name }, index) => `${name || `Empty${index}`}`) })
|
|
97
|
+
: encodeSubTypes(registry, sub, true);
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
[TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef) =>
|
|
101
|
+
encodeWithParams(registry, typeDef, 'HashMap'),
|
|
102
|
+
|
|
103
|
+
[TypeDefInfo.Int]: (_registry: Registry, { length = 32 }: TypeDef) =>
|
|
104
|
+
`Int<${length}>`,
|
|
105
|
+
|
|
106
|
+
[TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef) =>
|
|
107
|
+
encodeWithParams(registry, typeDef, 'Linkage'),
|
|
108
|
+
|
|
109
|
+
[TypeDefInfo.Null]: (_registry: Registry, _typeDef: TypeDef) =>
|
|
110
|
+
'Null',
|
|
111
|
+
|
|
112
|
+
[TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef) =>
|
|
113
|
+
encodeWithParams(registry, typeDef, 'Option'),
|
|
114
|
+
|
|
115
|
+
[TypeDefInfo.Plain]: (_registry: Registry, { displayName, type }: TypeDef) =>
|
|
116
|
+
displayName || type,
|
|
117
|
+
|
|
118
|
+
[TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef) =>
|
|
119
|
+
encodeWithParams(registry, typeDef, 'Range'),
|
|
120
|
+
|
|
121
|
+
[TypeDefInfo.RangeInclusive]: (registry: Registry, typeDef: TypeDef) =>
|
|
122
|
+
encodeWithParams(registry, typeDef, 'RangeInclusive'),
|
|
123
|
+
|
|
124
|
+
[TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef) =>
|
|
125
|
+
encodeWithParams(registry, typeDef, 'Result'),
|
|
126
|
+
|
|
127
|
+
[TypeDefInfo.Set]: (_registry: Registry, { length = 8, sub }: TypeDef): string => {
|
|
128
|
+
if (!Array.isArray(sub)) {
|
|
129
|
+
throw new Error('Unable to encode Set type');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return stringify({
|
|
133
|
+
_set: sub.reduce((all, { index, name }, count) =>
|
|
134
|
+
objectSpread(all, { [`${name || `Unknown${index || count}`}`]: index || count }),
|
|
135
|
+
{ _bitLength: length || 8 })
|
|
136
|
+
});
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
[TypeDefInfo.Si]: (_registry: Registry, { lookupName, type }: TypeDef) =>
|
|
140
|
+
lookupName || type,
|
|
141
|
+
|
|
142
|
+
[TypeDefInfo.Struct]: (registry: Registry, { alias, sub }: TypeDef): string => {
|
|
143
|
+
if (!Array.isArray(sub)) {
|
|
144
|
+
throw new Error('Unable to encode Struct type');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return encodeSubTypes(registry, sub, false, alias
|
|
148
|
+
? {
|
|
149
|
+
_alias: [...alias.entries()].reduce<Record<string, string>>((all, [k, v]) =>
|
|
150
|
+
objectSpread(all, { [k]: v }), {}
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
: {}
|
|
154
|
+
);
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
[TypeDefInfo.Tuple]: (registry: Registry, { sub }: TypeDef): string => {
|
|
158
|
+
if (!Array.isArray(sub)) {
|
|
159
|
+
throw new Error('Unable to encode Tuple type');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return `(${sub.map((type) => encodeTypeDef(registry, type)).join(',')})`;
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
[TypeDefInfo.UInt]: (_registry: Registry, { length = 32 }: TypeDef) =>
|
|
166
|
+
`UInt<${length}>`,
|
|
167
|
+
|
|
168
|
+
[TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef) =>
|
|
169
|
+
encodeWithParams(registry, typeDef, 'Vec'),
|
|
170
|
+
|
|
171
|
+
[TypeDefInfo.VecFixed]: (_registry: Registry, { length, sub }: TypeDef): string => {
|
|
172
|
+
if (!isNumber(length) || !sub || Array.isArray(sub)) {
|
|
173
|
+
throw new Error('Unable to encode VecFixed type');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return `[${sub.type};${length}]`;
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
[TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, typeDef: TypeDef) =>
|
|
180
|
+
encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
|
|
181
|
+
|
|
182
|
+
[TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef) =>
|
|
183
|
+
encodeWithParams(registry, typeDef, 'WrapperOpaque')
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
function encodeType (registry: Registry, typeDef: TypeDef, withLookup = true): string {
|
|
187
|
+
return withLookup && typeDef.lookupName
|
|
188
|
+
? typeDef.lookupName
|
|
189
|
+
: encoders[typeDef.info](registry, typeDef);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function encodeTypeDef (registry: Registry, typeDef: TypeDef): string {
|
|
193
|
+
// In the case of contracts we do have the unfortunate situation where the displayName would
|
|
194
|
+
// refer to "Option" when it is an option. For these, string it out, only using when actually
|
|
195
|
+
// not a top-level element to be used
|
|
196
|
+
return (typeDef.displayName && !INFO_WRAP.some((i) => typeDef.displayName === i))
|
|
197
|
+
? typeDef.displayName
|
|
198
|
+
: encodeType(registry, typeDef);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function withTypeString (registry: Registry, typeDef: Omit<TypeDef, 'type'> & { type?: string }): TypeDef {
|
|
202
|
+
return objectSpread({}, typeDef, {
|
|
203
|
+
type: encodeType(registry, typeDef as TypeDef, false)
|
|
204
|
+
});
|
|
205
|
+
}
|