@codama/renderers-rust 1.0.15 → 1.0.17
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/dist/index.node.cjs +225 -167
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +163 -89
- package/dist/index.node.mjs.map +1 -1
- package/dist/templates/accountsPage.njk +6 -6
- package/dist/types/utils/render.d.ts.map +1 -1
- package/package.json +6 -6
package/dist/index.node.mjs
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
import { CodamaError, CODAMA_ERROR__RENDERERS__UNSUPPORTED_NODE, logWarn, logError, CODAMA_ERROR__UNEXPECTED_NODE_KIND } from '@codama/errors';
|
|
2
|
-
import { REGISTERED_TYPE_NODE_KINDS, definedTypeNode, pascalCase, snakeCase, resolveNestedTypeNode, isNode, remainderCountNode, fixedCountNode, prefixedCountNode, arrayTypeNode, numberTypeNode, getAllPrograms, getAllAccounts, getAllInstructionsWithSubs, getAllDefinedTypes, VALUE_NODES, structTypeNodeFromInstructionArgumentNodes, isNodeFilter, camelCase, kebabCase, titleCase, assertIsNode, bytesValueNode, numberValueNode, arrayValueNode, isScalarEnum } from '@codama/nodes';
|
|
3
|
-
import { RenderMap, deleteDirectory, writeRenderMapVisitor } from '@codama/renderers-core';
|
|
4
|
-
import { pipe, mergeVisitor, extendVisitor, visit, LinkableDictionary, NodeStack, staticVisitor, recordNodeStackVisitor, recordLinkablesOnFirstVisitVisitor, rootNodeVisitor } from '@codama/visitors-core';
|
|
5
|
-
import { getBase64Encoder, getBase58Encoder, getBase16Encoder, getUtf8Encoder } from '@solana/codecs-strings';
|
|
6
|
-
import { dirname, join } from 'node:path';
|
|
7
|
-
import { fileURLToPath } from 'node:url';
|
|
8
|
-
import nunjucks from 'nunjucks';
|
|
9
|
-
import { spawnSync } from 'child_process';
|
|
10
|
-
|
|
11
1
|
// src/ImportMap.ts
|
|
12
2
|
var DEFAULT_MODULE_MAP = {
|
|
13
3
|
generated: "crate::generated",
|
|
@@ -78,6 +68,53 @@ var ImportMap = class _ImportMap {
|
|
|
78
68
|
return importStatements.join("\n");
|
|
79
69
|
}
|
|
80
70
|
};
|
|
71
|
+
|
|
72
|
+
// src/getRenderMapVisitor.ts
|
|
73
|
+
import { logWarn } from "@codama/errors";
|
|
74
|
+
import {
|
|
75
|
+
getAllAccounts,
|
|
76
|
+
getAllDefinedTypes,
|
|
77
|
+
getAllInstructionsWithSubs,
|
|
78
|
+
getAllPrograms,
|
|
79
|
+
isNode as isNode4,
|
|
80
|
+
isNodeFilter,
|
|
81
|
+
pascalCase as pascalCase4,
|
|
82
|
+
resolveNestedTypeNode as resolveNestedTypeNode2,
|
|
83
|
+
snakeCase as snakeCase3,
|
|
84
|
+
structTypeNodeFromInstructionArgumentNodes,
|
|
85
|
+
VALUE_NODES
|
|
86
|
+
} from "@codama/nodes";
|
|
87
|
+
import { RenderMap } from "@codama/renderers-core";
|
|
88
|
+
import {
|
|
89
|
+
extendVisitor as extendVisitor2,
|
|
90
|
+
LinkableDictionary,
|
|
91
|
+
NodeStack,
|
|
92
|
+
pipe as pipe2,
|
|
93
|
+
recordLinkablesOnFirstVisitVisitor,
|
|
94
|
+
recordNodeStackVisitor,
|
|
95
|
+
staticVisitor,
|
|
96
|
+
visit as visit3
|
|
97
|
+
} from "@codama/visitors-core";
|
|
98
|
+
|
|
99
|
+
// src/getTypeManifestVisitor.ts
|
|
100
|
+
import { CODAMA_ERROR__RENDERERS__UNSUPPORTED_NODE, CodamaError as CodamaError2 } from "@codama/errors";
|
|
101
|
+
import {
|
|
102
|
+
arrayTypeNode,
|
|
103
|
+
definedTypeNode,
|
|
104
|
+
fixedCountNode,
|
|
105
|
+
isNode as isNode2,
|
|
106
|
+
numberTypeNode,
|
|
107
|
+
pascalCase as pascalCase2,
|
|
108
|
+
prefixedCountNode,
|
|
109
|
+
REGISTERED_TYPE_NODE_KINDS,
|
|
110
|
+
remainderCountNode,
|
|
111
|
+
resolveNestedTypeNode,
|
|
112
|
+
snakeCase as snakeCase2
|
|
113
|
+
} from "@codama/nodes";
|
|
114
|
+
import { extendVisitor, mergeVisitor, pipe, visit } from "@codama/visitors-core";
|
|
115
|
+
|
|
116
|
+
// src/utils/codecs.ts
|
|
117
|
+
import { getBase16Encoder, getBase58Encoder, getBase64Encoder, getUtf8Encoder } from "@solana/codecs-strings";
|
|
81
118
|
function getBytesFromBytesValueNode(node) {
|
|
82
119
|
switch (node.encoding) {
|
|
83
120
|
case "utf8":
|
|
@@ -91,6 +128,9 @@ function getBytesFromBytesValueNode(node) {
|
|
|
91
128
|
return getBase64Encoder().encode(node.data);
|
|
92
129
|
}
|
|
93
130
|
}
|
|
131
|
+
|
|
132
|
+
// src/utils/linkOverrides.ts
|
|
133
|
+
import { CODAMA_ERROR__UNEXPECTED_NODE_KIND, CodamaError } from "@codama/errors";
|
|
94
134
|
function getImportFromFactory(overrides) {
|
|
95
135
|
const linkOverrides = {
|
|
96
136
|
accounts: overrides.accounts ?? {},
|
|
@@ -131,6 +171,12 @@ function getImportFromFactory(overrides) {
|
|
|
131
171
|
}
|
|
132
172
|
};
|
|
133
173
|
}
|
|
174
|
+
|
|
175
|
+
// src/utils/render.ts
|
|
176
|
+
import { dirname as pathDirname, join } from "node:path";
|
|
177
|
+
import { fileURLToPath } from "node:url";
|
|
178
|
+
import { camelCase, kebabCase, pascalCase, snakeCase, titleCase } from "@codama/nodes";
|
|
179
|
+
import nunjucks from "nunjucks";
|
|
134
180
|
function rustDocblock(docs) {
|
|
135
181
|
if (docs.length <= 0) return "";
|
|
136
182
|
const lines = docs.map((doc) => `/// ${doc}`);
|
|
@@ -138,8 +184,8 @@ function rustDocblock(docs) {
|
|
|
138
184
|
`;
|
|
139
185
|
}
|
|
140
186
|
var render = (template, context, options) => {
|
|
141
|
-
const dirname
|
|
142
|
-
const templates = join(dirname
|
|
187
|
+
const dirname = true ? pathDirname(fileURLToPath(import.meta.url)) : __dirname;
|
|
188
|
+
const templates = false ? join(dirname, "..", "..", "public", "templates") : join(dirname, "templates");
|
|
143
189
|
const env = nunjucks.configure(templates, { autoescape: false, trimBlocks: true, ...options });
|
|
144
190
|
env.addFilter("pascalCase", pascalCase);
|
|
145
191
|
env.addFilter("camelCase", camelCase);
|
|
@@ -149,6 +195,14 @@ var render = (template, context, options) => {
|
|
|
149
195
|
env.addFilter("rustDocblock", rustDocblock);
|
|
150
196
|
return env.render(template, context);
|
|
151
197
|
};
|
|
198
|
+
|
|
199
|
+
// src/utils/traitOptions.ts
|
|
200
|
+
import {
|
|
201
|
+
assertIsNode,
|
|
202
|
+
camelCase as camelCase2,
|
|
203
|
+
isNode,
|
|
204
|
+
isScalarEnum
|
|
205
|
+
} from "@codama/nodes";
|
|
152
206
|
var DEFAULT_TRAIT_OPTIONS = {
|
|
153
207
|
baseDefaults: [
|
|
154
208
|
"borsh::BorshSerialize",
|
|
@@ -178,7 +232,7 @@ function getTraitsFromNode(node, userOptions = {}) {
|
|
|
178
232
|
return { imports: new ImportMap(), render: "" };
|
|
179
233
|
}
|
|
180
234
|
const sanitizedOverrides = Object.fromEntries(
|
|
181
|
-
Object.entries(options.overrides).map(([key, value]) => [
|
|
235
|
+
Object.entries(options.overrides).map(([key, value]) => [camelCase2(key), value])
|
|
182
236
|
);
|
|
183
237
|
const nodeOverrides = sanitizedOverrides[node.name];
|
|
184
238
|
const allTraits = nodeOverrides === void 0 ? getDefaultTraits(nodeType, options) : nodeOverrides;
|
|
@@ -267,7 +321,7 @@ function getTypeManifestVisitor(options) {
|
|
|
267
321
|
),
|
|
268
322
|
(v) => extendVisitor(v, {
|
|
269
323
|
visitAccount(account, { self }) {
|
|
270
|
-
parentName =
|
|
324
|
+
parentName = pascalCase2(account.name);
|
|
271
325
|
const manifest = visit(account.data, self);
|
|
272
326
|
const traits = getTraitsFromNode2(account);
|
|
273
327
|
manifest.imports.mergeWith(traits.imports);
|
|
@@ -279,13 +333,13 @@ function getTypeManifestVisitor(options) {
|
|
|
279
333
|
},
|
|
280
334
|
visitArrayType(arrayType, { self }) {
|
|
281
335
|
const childManifest = visit(arrayType.item, self);
|
|
282
|
-
if (
|
|
336
|
+
if (isNode2(arrayType.count, "fixedCountNode")) {
|
|
283
337
|
return {
|
|
284
338
|
...childManifest,
|
|
285
339
|
type: `[${childManifest.type}; ${arrayType.count.value}]`
|
|
286
340
|
};
|
|
287
341
|
}
|
|
288
|
-
if (
|
|
342
|
+
if (isNode2(arrayType.count, "remainderCountNode")) {
|
|
289
343
|
childManifest.imports.add("kaigan::types::RemainderVec");
|
|
290
344
|
return {
|
|
291
345
|
...childManifest,
|
|
@@ -345,16 +399,16 @@ function getTypeManifestVisitor(options) {
|
|
|
345
399
|
return visit(arrayType, self);
|
|
346
400
|
},
|
|
347
401
|
visitDefinedType(definedType, { self }) {
|
|
348
|
-
parentName =
|
|
402
|
+
parentName = pascalCase2(definedType.name);
|
|
349
403
|
const manifest = visit(definedType.type, self);
|
|
350
404
|
const traits = getTraitsFromNode2(definedType);
|
|
351
405
|
manifest.imports.mergeWith(traits.imports);
|
|
352
406
|
parentName = null;
|
|
353
|
-
const renderedType =
|
|
407
|
+
const renderedType = isNode2(definedType.type, ["enumTypeNode", "structTypeNode"]) ? manifest.type : `pub type ${pascalCase2(definedType.name)} = ${manifest.type};`;
|
|
354
408
|
return { ...manifest, type: `${traits.render}${renderedType}` };
|
|
355
409
|
},
|
|
356
410
|
visitDefinedTypeLink(node) {
|
|
357
|
-
const pascalCaseDefinedType =
|
|
411
|
+
const pascalCaseDefinedType = pascalCase2(node.name);
|
|
358
412
|
const importFrom = getImportFrom(node);
|
|
359
413
|
return {
|
|
360
414
|
imports: new ImportMap().add(`${importFrom}::${pascalCaseDefinedType}`),
|
|
@@ -363,7 +417,7 @@ function getTypeManifestVisitor(options) {
|
|
|
363
417
|
};
|
|
364
418
|
},
|
|
365
419
|
visitEnumEmptyVariantType(enumEmptyVariantType) {
|
|
366
|
-
const name =
|
|
420
|
+
const name = pascalCase2(enumEmptyVariantType.name);
|
|
367
421
|
return {
|
|
368
422
|
imports: new ImportMap(),
|
|
369
423
|
nestedStructs: [],
|
|
@@ -371,13 +425,13 @@ function getTypeManifestVisitor(options) {
|
|
|
371
425
|
};
|
|
372
426
|
},
|
|
373
427
|
visitEnumStructVariantType(enumStructVariantType, { self }) {
|
|
374
|
-
const name =
|
|
428
|
+
const name = pascalCase2(enumStructVariantType.name);
|
|
375
429
|
const originalParentName = parentName;
|
|
376
430
|
if (!originalParentName) {
|
|
377
431
|
throw new Error("Enum struct variant type must have a parent name.");
|
|
378
432
|
}
|
|
379
433
|
inlineStruct = true;
|
|
380
|
-
parentName =
|
|
434
|
+
parentName = pascalCase2(originalParentName) + name;
|
|
381
435
|
const typeManifest = visit(enumStructVariantType.struct, self);
|
|
382
436
|
inlineStruct = false;
|
|
383
437
|
parentName = originalParentName;
|
|
@@ -387,12 +441,12 @@ function getTypeManifestVisitor(options) {
|
|
|
387
441
|
};
|
|
388
442
|
},
|
|
389
443
|
visitEnumTupleVariantType(enumTupleVariantType, { self }) {
|
|
390
|
-
const name =
|
|
444
|
+
const name = pascalCase2(enumTupleVariantType.name);
|
|
391
445
|
const originalParentName = parentName;
|
|
392
446
|
if (!originalParentName) {
|
|
393
447
|
throw new Error("Enum struct variant type must have a parent name.");
|
|
394
448
|
}
|
|
395
|
-
parentName =
|
|
449
|
+
parentName = pascalCase2(originalParentName) + name;
|
|
396
450
|
const childManifest = visit(enumTupleVariantType.tuple, self);
|
|
397
451
|
parentName = originalParentName;
|
|
398
452
|
let derive = "";
|
|
@@ -416,7 +470,7 @@ function getTypeManifestVisitor(options) {
|
|
|
416
470
|
const mergedManifest = mergeManifests(variants);
|
|
417
471
|
return {
|
|
418
472
|
...mergedManifest,
|
|
419
|
-
type: `pub enum ${
|
|
473
|
+
type: `pub enum ${pascalCase2(originalParentName)} {
|
|
420
474
|
${variantNames}
|
|
421
475
|
}`
|
|
422
476
|
};
|
|
@@ -473,7 +527,7 @@ ${variantNames}
|
|
|
473
527
|
};
|
|
474
528
|
},
|
|
475
529
|
visitRemainderOptionType(node) {
|
|
476
|
-
throw new
|
|
530
|
+
throw new CodamaError2(CODAMA_ERROR__RENDERERS__UNSUPPORTED_NODE, { kind: node.kind, node });
|
|
477
531
|
},
|
|
478
532
|
visitSetType(setType, { self }) {
|
|
479
533
|
const childManifest = visit(setType.item, self);
|
|
@@ -504,7 +558,7 @@ ${variantNames}
|
|
|
504
558
|
type: `[u8; ${parentSize}]`
|
|
505
559
|
};
|
|
506
560
|
}
|
|
507
|
-
if (
|
|
561
|
+
if (isNode2(parentSize, "numberTypeNode") && parentSize.endian === "le") {
|
|
508
562
|
switch (parentSize.format) {
|
|
509
563
|
case "u32":
|
|
510
564
|
return {
|
|
@@ -535,14 +589,14 @@ ${variantNames}
|
|
|
535
589
|
if (!originalParentName) {
|
|
536
590
|
throw new Error("Struct field type must have a parent name.");
|
|
537
591
|
}
|
|
538
|
-
parentName =
|
|
592
|
+
parentName = pascalCase2(originalParentName) + pascalCase2(structFieldType.name);
|
|
539
593
|
nestedStruct = true;
|
|
540
594
|
inlineStruct = false;
|
|
541
595
|
const fieldManifest = visit(structFieldType.type, self);
|
|
542
596
|
parentName = originalParentName;
|
|
543
597
|
inlineStruct = originalInlineStruct;
|
|
544
598
|
nestedStruct = originalNestedStruct;
|
|
545
|
-
const fieldName =
|
|
599
|
+
const fieldName = snakeCase2(structFieldType.name);
|
|
546
600
|
const docblock = rustDocblock(structFieldType.docs);
|
|
547
601
|
const resolvedNestedType = resolveNestedTypeNode(structFieldType.type);
|
|
548
602
|
let derive = "";
|
|
@@ -550,9 +604,9 @@ ${variantNames}
|
|
|
550
604
|
derive = '#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::DisplayFromStr>"))]\n';
|
|
551
605
|
} else if (fieldManifest.type === "Vec<Pubkey>") {
|
|
552
606
|
derive = '#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<Vec<serde_with::DisplayFromStr>>"))]\n';
|
|
553
|
-
} else if (
|
|
607
|
+
} else if (isNode2(resolvedNestedType, "arrayTypeNode") && isNode2(resolvedNestedType.count, "fixedCountNode") && resolvedNestedType.count.value > 32) {
|
|
554
608
|
derive = '#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]\n';
|
|
555
|
-
} else if (
|
|
609
|
+
} else if (isNode2(resolvedNestedType, ["bytesTypeNode", "stringTypeNode"]) && isNode2(structFieldType.type, "fixedSizeTypeNode") && structFieldType.type.size > 32) {
|
|
556
610
|
derive = '#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]\n';
|
|
557
611
|
}
|
|
558
612
|
return {
|
|
@@ -577,11 +631,11 @@ ${variantNames}
|
|
|
577
631
|
...mergedManifest,
|
|
578
632
|
nestedStructs: [
|
|
579
633
|
...mergedManifest.nestedStructs,
|
|
580
|
-
`${nestedTraits.render}pub struct ${
|
|
634
|
+
`${nestedTraits.render}pub struct ${pascalCase2(originalParentName)} {
|
|
581
635
|
${fieldTypes}
|
|
582
636
|
}`
|
|
583
637
|
],
|
|
584
|
-
type:
|
|
638
|
+
type: pascalCase2(originalParentName)
|
|
585
639
|
};
|
|
586
640
|
}
|
|
587
641
|
if (inlineStruct) {
|
|
@@ -591,7 +645,7 @@ ${fieldTypes}
|
|
|
591
645
|
}
|
|
592
646
|
return {
|
|
593
647
|
...mergedManifest,
|
|
594
|
-
type: `pub struct ${
|
|
648
|
+
type: `pub struct ${pascalCase2(originalParentName)} {
|
|
595
649
|
${fieldTypes}
|
|
596
650
|
}`
|
|
597
651
|
};
|
|
@@ -605,7 +659,7 @@ ${fieldTypes}
|
|
|
605
659
|
};
|
|
606
660
|
},
|
|
607
661
|
visitZeroableOptionType(node) {
|
|
608
|
-
throw new
|
|
662
|
+
throw new CodamaError2(CODAMA_ERROR__RENDERERS__UNSUPPORTED_NODE, { kind: node.kind, node });
|
|
609
663
|
}
|
|
610
664
|
})
|
|
611
665
|
);
|
|
@@ -616,13 +670,23 @@ function mergeManifests(manifests) {
|
|
|
616
670
|
nestedStructs: manifests.flatMap((m) => m.nestedStructs)
|
|
617
671
|
};
|
|
618
672
|
}
|
|
673
|
+
|
|
674
|
+
// src/renderValueNodeVisitor.ts
|
|
675
|
+
import {
|
|
676
|
+
arrayValueNode,
|
|
677
|
+
bytesValueNode,
|
|
678
|
+
isNode as isNode3,
|
|
679
|
+
numberValueNode,
|
|
680
|
+
pascalCase as pascalCase3
|
|
681
|
+
} from "@codama/nodes";
|
|
682
|
+
import { visit as visit2 } from "@codama/visitors-core";
|
|
619
683
|
function renderValueNode(value, getImportFrom, useStr = false) {
|
|
620
|
-
return
|
|
684
|
+
return visit2(value, renderValueNodeVisitor(getImportFrom, useStr));
|
|
621
685
|
}
|
|
622
686
|
function renderValueNodeVisitor(getImportFrom, useStr = false) {
|
|
623
687
|
return {
|
|
624
688
|
visitArrayValue(node) {
|
|
625
|
-
const list = node.items.map((v) =>
|
|
689
|
+
const list = node.items.map((v) => visit2(v, this));
|
|
626
690
|
return {
|
|
627
691
|
imports: new ImportMap().mergeWith(...list.map((c) => c.imports)),
|
|
628
692
|
render: `[${list.map((c) => c.render).join(", ")}]`
|
|
@@ -637,17 +701,17 @@ function renderValueNodeVisitor(getImportFrom, useStr = false) {
|
|
|
637
701
|
visitBytesValue(node) {
|
|
638
702
|
const bytes = getBytesFromBytesValueNode(node);
|
|
639
703
|
const numbers = Array.from(bytes).map(numberValueNode);
|
|
640
|
-
return
|
|
704
|
+
return visit2(arrayValueNode(numbers), this);
|
|
641
705
|
},
|
|
642
706
|
visitConstantValue(node) {
|
|
643
|
-
if (
|
|
644
|
-
return
|
|
707
|
+
if (isNode3(node.value, "bytesValueNode")) {
|
|
708
|
+
return visit2(node.value, this);
|
|
645
709
|
}
|
|
646
|
-
if (
|
|
647
|
-
return
|
|
710
|
+
if (isNode3(node.type, "stringTypeNode") && isNode3(node.value, "stringValueNode")) {
|
|
711
|
+
return visit2(bytesValueNode(node.type.encoding, node.value.string), this);
|
|
648
712
|
}
|
|
649
|
-
if (
|
|
650
|
-
const numberManifest =
|
|
713
|
+
if (isNode3(node.type, "numberTypeNode") && isNode3(node.value, "numberValueNode")) {
|
|
714
|
+
const numberManifest = visit2(node.value, this);
|
|
651
715
|
const { format, endian } = node.type;
|
|
652
716
|
const byteFunction = endian === "le" ? "to_le_bytes" : "to_be_bytes";
|
|
653
717
|
numberManifest.render = `${numberManifest.render}${format}.${byteFunction}()`;
|
|
@@ -657,14 +721,14 @@ function renderValueNodeVisitor(getImportFrom, useStr = false) {
|
|
|
657
721
|
},
|
|
658
722
|
visitEnumValue(node) {
|
|
659
723
|
const imports = new ImportMap();
|
|
660
|
-
const enumName =
|
|
661
|
-
const variantName =
|
|
724
|
+
const enumName = pascalCase3(node.enum.name);
|
|
725
|
+
const variantName = pascalCase3(node.variant);
|
|
662
726
|
const importFrom = getImportFrom(node.enum);
|
|
663
727
|
imports.add(`${importFrom}::${enumName}`);
|
|
664
728
|
if (!node.value) {
|
|
665
729
|
return { imports, render: `${enumName}::${variantName}` };
|
|
666
730
|
}
|
|
667
|
-
const enumValue =
|
|
731
|
+
const enumValue = visit2(node.value, this);
|
|
668
732
|
const fields = enumValue.render;
|
|
669
733
|
return {
|
|
670
734
|
imports: imports.mergeWith(enumValue.imports),
|
|
@@ -672,15 +736,15 @@ function renderValueNodeVisitor(getImportFrom, useStr = false) {
|
|
|
672
736
|
};
|
|
673
737
|
},
|
|
674
738
|
visitMapEntryValue(node) {
|
|
675
|
-
const mapKey =
|
|
676
|
-
const mapValue =
|
|
739
|
+
const mapKey = visit2(node.key, this);
|
|
740
|
+
const mapValue = visit2(node.value, this);
|
|
677
741
|
return {
|
|
678
742
|
imports: mapKey.imports.mergeWith(mapValue.imports),
|
|
679
743
|
render: `[${mapKey.render}, ${mapValue.render}]`
|
|
680
744
|
};
|
|
681
745
|
},
|
|
682
746
|
visitMapValue(node) {
|
|
683
|
-
const map = node.entries.map((entry) =>
|
|
747
|
+
const map = node.entries.map((entry) => visit2(entry, this));
|
|
684
748
|
const imports = new ImportMap().add("std::collection::HashMap");
|
|
685
749
|
return {
|
|
686
750
|
imports: imports.mergeWith(...map.map((c) => c.imports)),
|
|
@@ -706,7 +770,7 @@ function renderValueNodeVisitor(getImportFrom, useStr = false) {
|
|
|
706
770
|
};
|
|
707
771
|
},
|
|
708
772
|
visitSetValue(node) {
|
|
709
|
-
const set = node.items.map((v) =>
|
|
773
|
+
const set = node.items.map((v) => visit2(v, this));
|
|
710
774
|
const imports = new ImportMap().add("std::collection::HashSet");
|
|
711
775
|
return {
|
|
712
776
|
imports: imports.mergeWith(...set.map((c) => c.imports)),
|
|
@@ -714,7 +778,7 @@ function renderValueNodeVisitor(getImportFrom, useStr = false) {
|
|
|
714
778
|
};
|
|
715
779
|
},
|
|
716
780
|
visitSomeValue(node) {
|
|
717
|
-
const child =
|
|
781
|
+
const child = visit2(node.value, this);
|
|
718
782
|
return {
|
|
719
783
|
...child,
|
|
720
784
|
render: `Some(${child.render})`
|
|
@@ -727,21 +791,21 @@ function renderValueNodeVisitor(getImportFrom, useStr = false) {
|
|
|
727
791
|
};
|
|
728
792
|
},
|
|
729
793
|
visitStructFieldValue(node) {
|
|
730
|
-
const structValue =
|
|
794
|
+
const structValue = visit2(node.value, this);
|
|
731
795
|
return {
|
|
732
796
|
imports: structValue.imports,
|
|
733
797
|
render: `${node.name}: ${structValue.render}`
|
|
734
798
|
};
|
|
735
799
|
},
|
|
736
800
|
visitStructValue(node) {
|
|
737
|
-
const struct = node.fields.map((field) =>
|
|
801
|
+
const struct = node.fields.map((field) => visit2(field, this));
|
|
738
802
|
return {
|
|
739
803
|
imports: new ImportMap().mergeWith(...struct.map((c) => c.imports)),
|
|
740
804
|
render: `{ ${struct.map((c) => c.render).join(", ")} }`
|
|
741
805
|
};
|
|
742
806
|
},
|
|
743
807
|
visitTupleValue(node) {
|
|
744
|
-
const tuple = node.items.map((v) =>
|
|
808
|
+
const tuple = node.items.map((v) => visit2(v, this));
|
|
745
809
|
return {
|
|
746
810
|
imports: new ImportMap().mergeWith(...tuple.map((c) => c.imports)),
|
|
747
811
|
render: `(${tuple.map((c) => c.render).join(", ")})`
|
|
@@ -761,46 +825,46 @@ function getRenderMapVisitor(options = {}) {
|
|
|
761
825
|
const getTraitsFromNode2 = getTraitsFromNodeFactory(options.traitOptions);
|
|
762
826
|
const typeManifestVisitor = getTypeManifestVisitor({ getImportFrom, getTraitsFromNode: getTraitsFromNode2 });
|
|
763
827
|
const anchorTraits = options.anchorTraits ?? true;
|
|
764
|
-
return
|
|
828
|
+
return pipe2(
|
|
765
829
|
staticVisitor(() => new RenderMap(), {
|
|
766
830
|
keys: ["rootNode", "programNode", "instructionNode", "accountNode", "definedTypeNode"]
|
|
767
831
|
}),
|
|
768
|
-
(v) =>
|
|
832
|
+
(v) => extendVisitor2(v, {
|
|
769
833
|
visitAccount(node) {
|
|
770
|
-
const typeManifest =
|
|
834
|
+
const typeManifest = visit3(node, typeManifestVisitor);
|
|
771
835
|
const seedsImports = new ImportMap();
|
|
772
836
|
const pda = node.pda ? linkables.get([...stack.getPath(), node.pda]) : void 0;
|
|
773
837
|
const pdaSeeds = pda?.seeds ?? [];
|
|
774
838
|
const seeds = pdaSeeds.map((seed) => {
|
|
775
|
-
if (
|
|
776
|
-
const seedManifest2 =
|
|
839
|
+
if (isNode4(seed, "variablePdaSeedNode")) {
|
|
840
|
+
const seedManifest2 = visit3(seed.type, typeManifestVisitor);
|
|
777
841
|
seedsImports.mergeWith(seedManifest2.imports);
|
|
778
|
-
const resolvedType2 =
|
|
842
|
+
const resolvedType2 = resolveNestedTypeNode2(seed.type);
|
|
779
843
|
return { ...seed, resolvedType: resolvedType2, typeManifest: seedManifest2 };
|
|
780
844
|
}
|
|
781
|
-
if (
|
|
845
|
+
if (isNode4(seed.value, "programIdValueNode")) {
|
|
782
846
|
return seed;
|
|
783
847
|
}
|
|
784
|
-
const seedManifest =
|
|
848
|
+
const seedManifest = visit3(seed.type, typeManifestVisitor);
|
|
785
849
|
const valueManifest = renderValueNode(seed.value, getImportFrom, true);
|
|
786
850
|
seedsImports.mergeWith(valueManifest.imports);
|
|
787
|
-
const resolvedType =
|
|
851
|
+
const resolvedType = resolveNestedTypeNode2(seed.type);
|
|
788
852
|
return { ...seed, resolvedType, typeManifest: seedManifest, valueManifest };
|
|
789
853
|
});
|
|
790
854
|
const hasVariableSeeds = pdaSeeds.filter(isNodeFilter("variablePdaSeedNode")).length > 0;
|
|
791
|
-
const constantSeeds = seeds.filter(isNodeFilter("constantPdaSeedNode")).filter((seed) => !
|
|
855
|
+
const constantSeeds = seeds.filter(isNodeFilter("constantPdaSeedNode")).filter((seed) => !isNode4(seed.value, "programIdValueNode"));
|
|
792
856
|
const { imports } = typeManifest;
|
|
793
857
|
if (hasVariableSeeds) {
|
|
794
858
|
imports.mergeWith(seedsImports);
|
|
795
859
|
}
|
|
796
860
|
return new RenderMap().add(
|
|
797
|
-
`accounts/${
|
|
861
|
+
`accounts/${snakeCase3(node.name)}.rs`,
|
|
798
862
|
render("accountsPage.njk", {
|
|
799
863
|
account: node,
|
|
800
864
|
anchorTraits,
|
|
801
865
|
constantSeeds,
|
|
802
866
|
hasVariableSeeds,
|
|
803
|
-
imports: imports.remove(`generatedAccounts::${
|
|
867
|
+
imports: imports.remove(`generatedAccounts::${pascalCase4(node.name)}`).toString(dependencyMap),
|
|
804
868
|
pda,
|
|
805
869
|
program,
|
|
806
870
|
seeds,
|
|
@@ -809,13 +873,13 @@ function getRenderMapVisitor(options = {}) {
|
|
|
809
873
|
);
|
|
810
874
|
},
|
|
811
875
|
visitDefinedType(node) {
|
|
812
|
-
const typeManifest =
|
|
876
|
+
const typeManifest = visit3(node, typeManifestVisitor);
|
|
813
877
|
const imports = new ImportMap().mergeWithManifest(typeManifest);
|
|
814
878
|
return new RenderMap().add(
|
|
815
|
-
`types/${
|
|
879
|
+
`types/${snakeCase3(node.name)}.rs`,
|
|
816
880
|
render("definedTypesPage.njk", {
|
|
817
881
|
definedType: node,
|
|
818
|
-
imports: imports.remove(`generatedTypes::${
|
|
882
|
+
imports: imports.remove(`generatedTypes::${pascalCase4(node.name)}`).toString(dependencyMap),
|
|
819
883
|
typeManifest
|
|
820
884
|
})
|
|
821
885
|
);
|
|
@@ -836,12 +900,12 @@ function getRenderMapVisitor(options = {}) {
|
|
|
836
900
|
getImportFrom,
|
|
837
901
|
getTraitsFromNode: getTraitsFromNode2,
|
|
838
902
|
nestedStruct: true,
|
|
839
|
-
parentName: `${
|
|
903
|
+
parentName: `${pascalCase4(node.name)}InstructionData`
|
|
840
904
|
});
|
|
841
|
-
const manifest =
|
|
905
|
+
const manifest = visit3(argument.type, argumentVisitor);
|
|
842
906
|
imports.mergeWith(manifest.imports);
|
|
843
|
-
const innerOptionType =
|
|
844
|
-
const hasDefaultValue = !!argument.defaultValue &&
|
|
907
|
+
const innerOptionType = isNode4(argument.type, "optionTypeNode") ? manifest.type.slice("Option<".length, -1) : null;
|
|
908
|
+
const hasDefaultValue = !!argument.defaultValue && isNode4(argument.defaultValue, VALUE_NODES);
|
|
845
909
|
let renderValue = null;
|
|
846
910
|
if (hasDefaultValue) {
|
|
847
911
|
const { imports: argImports, render: value } = renderValueNode(
|
|
@@ -867,18 +931,18 @@ function getRenderMapVisitor(options = {}) {
|
|
|
867
931
|
const structVisitor = getTypeManifestVisitor({
|
|
868
932
|
getImportFrom,
|
|
869
933
|
getTraitsFromNode: getTraitsFromNode2,
|
|
870
|
-
parentName: `${
|
|
934
|
+
parentName: `${pascalCase4(node.name)}InstructionData`
|
|
871
935
|
});
|
|
872
|
-
const typeManifest =
|
|
936
|
+
const typeManifest = visit3(struct, structVisitor);
|
|
873
937
|
const dataTraits = getTraitsFromNode2(node);
|
|
874
938
|
imports.mergeWith(dataTraits.imports);
|
|
875
939
|
return new RenderMap().add(
|
|
876
|
-
`instructions/${
|
|
940
|
+
`instructions/${snakeCase3(node.name)}.rs`,
|
|
877
941
|
render("instructionsPage.njk", {
|
|
878
942
|
dataTraits: dataTraits.render,
|
|
879
943
|
hasArgs,
|
|
880
944
|
hasOptional,
|
|
881
|
-
imports: imports.remove(`generatedInstructions::${
|
|
945
|
+
imports: imports.remove(`generatedInstructions::${pascalCase4(node.name)}`).toString(dependencyMap),
|
|
882
946
|
instruction: node,
|
|
883
947
|
instructionArgs,
|
|
884
948
|
program,
|
|
@@ -888,14 +952,14 @@ function getRenderMapVisitor(options = {}) {
|
|
|
888
952
|
},
|
|
889
953
|
visitProgram(node, { self }) {
|
|
890
954
|
program = node;
|
|
891
|
-
const renderMap = new RenderMap().mergeWith(...node.accounts.map((account) =>
|
|
955
|
+
const renderMap = new RenderMap().mergeWith(...node.accounts.map((account) => visit3(account, self))).mergeWith(...node.definedTypes.map((type) => visit3(type, self))).mergeWith(
|
|
892
956
|
...getAllInstructionsWithSubs(node, {
|
|
893
957
|
leavesOnly: !renderParentInstructions
|
|
894
|
-
}).map((ix) =>
|
|
958
|
+
}).map((ix) => visit3(ix, self))
|
|
895
959
|
);
|
|
896
960
|
if (node.errors.length > 0) {
|
|
897
961
|
renderMap.add(
|
|
898
|
-
`errors/${
|
|
962
|
+
`errors/${snakeCase3(node.name)}.rs`,
|
|
899
963
|
render("errorsPage.njk", {
|
|
900
964
|
errors: node.errors,
|
|
901
965
|
imports: new ImportMap().toString(dependencyMap),
|
|
@@ -941,7 +1005,7 @@ function getRenderMapVisitor(options = {}) {
|
|
|
941
1005
|
if (definedTypesToExport.length > 0) {
|
|
942
1006
|
map.add("types/mod.rs", render("definedTypesMod.njk", ctx));
|
|
943
1007
|
}
|
|
944
|
-
return map.add("mod.rs", render("rootMod.njk", ctx)).mergeWith(...getAllPrograms(node).map((p) =>
|
|
1008
|
+
return map.add("mod.rs", render("rootMod.njk", ctx)).mergeWith(...getAllPrograms(node).map((p) => visit3(p, self)));
|
|
945
1009
|
}
|
|
946
1010
|
}),
|
|
947
1011
|
(v) => recordNodeStackVisitor(v, stack),
|
|
@@ -956,18 +1020,24 @@ function getConflictsForInstructionAccountsAndArgs(instruction) {
|
|
|
956
1020
|
const duplicates = allNames.filter((e, i, a) => a.indexOf(e) !== i);
|
|
957
1021
|
return [...new Set(duplicates)];
|
|
958
1022
|
}
|
|
1023
|
+
|
|
1024
|
+
// src/renderVisitor.ts
|
|
1025
|
+
import { logError, logWarn as logWarn2 } from "@codama/errors";
|
|
1026
|
+
import { deleteDirectory, writeRenderMapVisitor } from "@codama/renderers-core";
|
|
1027
|
+
import { rootNodeVisitor, visit as visit4 } from "@codama/visitors-core";
|
|
1028
|
+
import { spawnSync } from "child_process";
|
|
959
1029
|
function renderVisitor(path, options = {}) {
|
|
960
1030
|
return rootNodeVisitor((root) => {
|
|
961
1031
|
if (options.deleteFolderBeforeRendering ?? true) {
|
|
962
1032
|
deleteDirectory(path);
|
|
963
1033
|
}
|
|
964
|
-
|
|
1034
|
+
visit4(root, writeRenderMapVisitor(getRenderMapVisitor(options), path));
|
|
965
1035
|
if (options.formatCode) {
|
|
966
1036
|
if (options.crateFolder) {
|
|
967
1037
|
const toolchain = options.toolchain ?? "+stable";
|
|
968
1038
|
runFormatter("cargo", [toolchain, "fmt", "--manifest-path", `${options.crateFolder}/Cargo.toml`]);
|
|
969
1039
|
} else {
|
|
970
|
-
|
|
1040
|
+
logWarn2("No crate folder specified, skipping formatting.");
|
|
971
1041
|
}
|
|
972
1042
|
}
|
|
973
1043
|
});
|
|
@@ -975,17 +1045,21 @@ function renderVisitor(path, options = {}) {
|
|
|
975
1045
|
function runFormatter(cmd, args) {
|
|
976
1046
|
const { stdout, stderr, error } = spawnSync(cmd, args);
|
|
977
1047
|
if (error?.message?.includes("ENOENT")) {
|
|
978
|
-
|
|
1048
|
+
logWarn2(`Could not find ${cmd}, skipping formatting.`);
|
|
979
1049
|
return;
|
|
980
1050
|
}
|
|
981
1051
|
if (stdout.length > 0) {
|
|
982
|
-
|
|
1052
|
+
logWarn2(`(cargo-fmt) ${stdout ? stdout?.toString() : error}`);
|
|
983
1053
|
}
|
|
984
1054
|
if (stderr.length > 0) {
|
|
985
1055
|
logError(`(cargo-fmt) ${stderr ? stderr.toString() : error}`);
|
|
986
1056
|
}
|
|
987
1057
|
}
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
1058
|
+
export {
|
|
1059
|
+
ImportMap,
|
|
1060
|
+
renderVisitor as default,
|
|
1061
|
+
getRenderMapVisitor,
|
|
1062
|
+
getTypeManifestVisitor,
|
|
1063
|
+
renderVisitor
|
|
1064
|
+
};
|
|
991
1065
|
//# sourceMappingURL=index.node.mjs.map
|