Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/humble-singers-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codama/renderers-rust': patch
---

Support `@codama/nodes@1.10`, whose node array attributes are now optional (`Array<T> | undefined`). Array reads are guarded with `?? []` throughout the renderer, and the new `injectedValueNode` value kind now throws an explicit unsupported-node error rather than being silently mishandled.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
"test:unit": "vitest run"
},
"dependencies": {
"@codama/errors": "^1.8.0",
"@codama/nodes": "^1.8.0",
"@codama/renderers-core": "^1.3.9",
"@codama/visitors-core": "^1.8.0",
"@codama/errors": "^1.10.0",
"@codama/nodes": "^1.10.0",
"@codama/renderers-core": "^1.3.11",
"@codama/visitors-core": "^1.10.0",
"@iarna/toml": "^2.2.5",
"@solana/codecs-strings": "^6.0.0",
"nunjucks": "^3.2.4",
Expand All @@ -53,7 +53,7 @@
"devDependencies": {
"@changesets/changelog-github": "^0.7.0",
"@changesets/cli": "^2.29.7",
"@codama/nodes-from-anchor": "^1.5.1",
"@codama/nodes-from-anchor": "^1.5.3",
"@solana/eslint-config-solana": "^6.0.1",
"@solana/prettier-config-solana": "0.0.5",
"@types/node": "^26",
Expand Down
104 changes: 52 additions & 52 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions src/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
const typeManifest = visit(node, typeManifestVisitor);

// Discriminator constants.
const fields = resolveNestedTypeNode(node.data).fields;
const fields = resolveNestedTypeNode(node.data).fields ?? [];
const discriminatorConstants = getDiscriminatorConstants({
discriminatorNodes: node.discriminators ?? [],
fields,
Expand Down Expand Up @@ -166,7 +166,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
// Discriminator constants.
const discriminatorConstants = getDiscriminatorConstants({
discriminatorNodes: node.discriminators ?? [],
fields: node.arguments,
fields: node.arguments ?? [],
getImportFrom,
prefix: node.name,
typeManifestVisitor,
Expand All @@ -184,7 +184,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
let hasArgs = false;
let hasOptional = false;

node.arguments.forEach(argument => {
(node.arguments ?? []).forEach(argument => {
const argumentVisitor = getTypeManifestVisitor({
getImportFrom,
getTraitsFromNode,
Expand Down Expand Up @@ -225,7 +225,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
});
});

const struct = structTypeNodeFromInstructionArgumentNodes(node.arguments);
const struct = structTypeNodeFromInstructionArgumentNodes(node.arguments ?? []);
const structVisitor = getTypeManifestVisitor({
getImportFrom,
getTraitsFromNode,
Expand Down Expand Up @@ -258,18 +258,18 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
visitProgram(node, { self }) {
program = node;
let renders = mergeRenderMaps([
...node.accounts.map(account => visit(account, self)),
...node.definedTypes.map(type => visit(type, self)),
...(node.accounts ?? []).map(account => visit(account, self)),
...(node.definedTypes ?? []).map(type => visit(type, self)),
...getAllInstructionsWithSubs(node, {
leavesOnly: !renderParentInstructions,
}).map(ix => visit(ix, self)),
]);

// Errors.
if (node.errors.length > 0) {
if ((node.errors ?? []).length > 0) {
renders = addToRenderMap(renders, `errors/${snakeCase(node.name)}.rs`, {
content: render('errorsPage.njk', {
errors: node.errors,
errors: node.errors ?? [],
imports: new ImportMap().toString(dependencyMap),
program: node,
}),
Expand Down Expand Up @@ -342,8 +342,8 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {

function getConflictsForInstructionAccountsAndArgs(instruction: InstructionNode): string[] {
const allNames = [
...instruction.accounts.map(account => account.name),
...instruction.arguments.map(argument => argument.name),
...(instruction.accounts ?? []).map(account => account.name),
...(instruction.arguments ?? []).map(argument => argument.name),
];
const duplicates = allNames.filter((e, i, a) => a.indexOf(e) !== i);
return [...new Set(duplicates)];
Expand Down
6 changes: 3 additions & 3 deletions src/getTypeManifestVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export function getTypeManifestVisitor(options: {
throw new Error('Enum type must have a parent name.');
}

const variants = enumType.variants.map(variant => visit(variant, self));
const variants = (enumType.variants ?? []).map(variant => visit(variant, self));
const variantNames = variants.map(variant => variant.type).join('\n');
const mergedManifest = mergeManifests(variants);

Expand Down Expand Up @@ -454,7 +454,7 @@ export function getTypeManifestVisitor(options: {
throw new Error('Struct type must have a parent name.');
}

const fields = structType.fields.map(field => visit(field, self));
const fields = (structType.fields ?? []).map(field => visit(field, self));
const fieldTypes = fields.map(field => field.type).join('\n');
const mergedManifest = mergeManifests(fields);

Expand Down Expand Up @@ -484,7 +484,7 @@ export function getTypeManifestVisitor(options: {
},

visitTupleType(tupleType, { self }) {
const items = tupleType.items.map(item => visit(item, self));
const items = (tupleType.items ?? []).map(item => visit(item, self));
const mergedManifest = mergeManifests(items);

return {
Expand Down
15 changes: 10 additions & 5 deletions src/renderValueNodeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function renderValueNodeVisitor(
> {
return {
visitArrayValue(node) {
const list = node.items.map(v => visit(v, this));
const list = (node.items ?? []).map(v => visit(v, this));
return {
imports: new ImportMap().mergeWith(...list.map(c => c.imports)),
render: `[${list.map(c => c.render).join(', ')}]`,
Expand Down Expand Up @@ -84,6 +84,11 @@ export function renderValueNodeVisitor(
render: `${enumName}::${variantName} ${fields}`,
};
},
visitInjectedValue() {
// An injected value is resolved by key from a surrounding provider at a later stage,
// so it has no concrete Rust rendering here.
throw new Error('Unsupported injected value node.');
},
visitMapEntryValue(node) {
const mapKey = visit(node.key, this);
const mapValue = visit(node.value, this);
Expand All @@ -93,7 +98,7 @@ export function renderValueNodeVisitor(
};
},
visitMapValue(node) {
const map = node.entries.map(entry => visit(entry, this));
const map = (node.entries ?? []).map(entry => visit(entry, this));
const imports = new ImportMap().add('std::collection::HashMap');
return {
imports: imports.mergeWith(...map.map(c => c.imports)),
Expand All @@ -119,7 +124,7 @@ export function renderValueNodeVisitor(
};
},
visitSetValue(node) {
const set = node.items.map(v => visit(v, this));
const set = (node.items ?? []).map(v => visit(v, this));
const imports = new ImportMap().add('std::collection::HashSet');
return {
imports: imports.mergeWith(...set.map(c => c.imports)),
Expand Down Expand Up @@ -147,14 +152,14 @@ export function renderValueNodeVisitor(
};
},
visitStructValue(node) {
const struct = node.fields.map(field => visit(field, this));
const struct = (node.fields ?? []).map(field => visit(field, this));
return {
imports: new ImportMap().mergeWith(...struct.map(c => c.imports)),
render: `{ ${struct.map(c => c.render).join(', ')} }`,
};
},
visitTupleValue(node) {
const tuple = node.items.map(v => visit(v, this));
const tuple = (node.items ?? []).map(v => visit(v, this));
return {
imports: new ImportMap().mergeWith(...tuple.map(c => c.imports)),
render: `(${tuple.map(c => c.render).join(', ')})`,
Expand Down
Loading