Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
567466c
[wip]: Add renderer skeleton
febo Nov 1, 2024
4eb480e
[wip]: Add tests
febo Nov 1, 2024
02b86d5
[wip]: Add account meta
febo Sep 5, 2025
3ee7f4a
Add fragment utility
lorisleiva Sep 6, 2025
43b3d9e
Create fragments for mod pages
lorisleiva Sep 6, 2025
b168763
Setup page fragment wrapper and render scope
lorisleiva Sep 6, 2025
6a8c068
Use Fragments in TypeManifestVisitor
lorisleiva Sep 6, 2025
05ad4b4
Use Fragments in ValueVisitor
lorisleiva Sep 6, 2025
ba1a4b1
Implement ix argument parsing
lorisleiva Sep 6, 2025
e4b2c42
Implement instruction page fragment
lorisleiva Sep 6, 2025
e309c61
Reorganise folder structure
lorisleiva Sep 6, 2025
0c9ad16
Display nested structs
lorisleiva Sep 6, 2025
ba99180
Renamed
febo Sep 6, 2025
8a059e2
Update code generation
febo Sep 9, 2025
5f19b00
Merge branch 'main' into febo/pinocchio
lorisleiva Sep 9, 2025
ce117fc
Update code to match main branch
lorisleiva Sep 9, 2025
5275bb7
Regenerate code from previous changes
lorisleiva Sep 9, 2025
27a40f0
Resolved default value should be used in fragments
lorisleiva Sep 9, 2025
a215ba0
Remove nunjucks dependency
lorisleiva Sep 9, 2025
68d3727
Move e2e folder under test
lorisleiva Sep 9, 2025
08fd301
Add browser support
lorisleiva Sep 9, 2025
28e55e2
Add getInstructionDataFromSingleArgumentFragment helper
lorisleiva Sep 9, 2025
7277c20
Update LICENSE
lorisleiva Sep 9, 2025
f5c6a83
Merge branch 'main' into febo/pinocchio
lorisleiva Sep 11, 2025
591be15
Use inlined scripts
lorisleiva Sep 11, 2025
739ff24
Lint fix
lorisleiva Sep 11, 2025
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
1 change: 1 addition & 0 deletions packages/renderers-rust-cpi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
5 changes: 5 additions & 0 deletions packages/renderers-rust-cpi/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist/
test/e2e/
test-ledger/
target/
CHANGELOG.md
22 changes: 22 additions & 0 deletions packages/renderers-rust-cpi/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2025 Codama

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
134 changes: 134 additions & 0 deletions packages/renderers-rust-cpi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Codama ➤ Renderers ➤ Pinocchio

[![npm][npm-image]][npm-url]
[![npm-downloads][npm-downloads-image]][npm-url]

[npm-downloads-image]: https://img.shields.io/npm/dm/@codama/renderers-rust.svg?style=flat
[npm-image]: https://img.shields.io/npm/v/@codama/renderers-rust.svg?style=flat&label=%40codama%2Frenderers-pinocchio
[npm-url]: https://www.npmjs.com/package/@codama/renderers-pinocchio

This package generates Pinocchio-based Rust clients from your Codama IDLs.

## Installation

```sh
pnpm install @codama/renderers-pinocchio
```

> [!NOTE]
> This package is **not** included in the main [`codama`](../library) package.
>
> However, note that the [`renderers`](../renderers) package re-exports the `renderVisitor` function of this package as `renderRustVisitor`.

## Usage

Once you have a Codama IDL, you can use the `renderVisitor` of this package to generate Rust clients. You will need to provide the base directory where the generated files will be saved and an optional set of options to customize the output.

```ts
// node ./codama.mjs
import { renderVisitor } from '@codama/renderers-pinocchio';

const pathToGeneratedFolder = path.join(__dirname, 'clients', 'pinocchio', 'src', 'generated');
const options = {}; // See below.
codama.accept(renderVisitor(pathToGeneratedFolder, options));
```

## Options

The `renderVisitor` accepts the following options.

| Name | Type | Default | Description |
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `deleteFolderBeforeRendering` | `boolean` | `true` | Whether the base directory should be cleaned before generating new files. |
| `formatCode` | `boolean` | `false` | Whether we should use `cargo fmt` to format the generated code. When set to `true`, the `crateFolder` option must be provided. |
| `toolchain` | `string` | `"+stable"` | The toolchain to use when formatting the generated code. |
| `crateFolder` | `string` | none | The path to the root folder of the Rust crate. This option is required when `formatCode` is set to `true`. |
| `linkOverrides` | `Record<'accounts' \| 'definedTypes' \| 'instructions' \| 'pdas' \| 'programs' \| 'resolvers', Record<string, string>>` | `{}` | A object that overrides the import path of link nodes. For instance, `{ definedTypes: { counter: 'hooked' } }` uses the `hooked` folder to import any link node referring to the `counter` type. |
| `dependencyMap` | `Record<string, string>` | `{}` | A mapping between import aliases and their actual crate name or path in Rust. |
| `renderParentInstructions` | `boolean` | `false` | When using nested instructions, whether the parent instructions should also be rendered. When set to `false` (default), only the instruction leaves are being rendered. |
| `traitOptions` | [`TraitOptions`](#trait-options) | `DEFAULT_TRAIT_OPTIONS` | A set of options that can be used to configure how traits are rendered for every Rust types. See [documentation below](#trait-options) for more information. |
| `anchorTraits` | `boolean` | `true` | Whether to generate Anchor traits `impl` for account types. |

## Trait Options

The Rust renderer provides sensible default traits when generating the various Rust types you client will use. However, you may wish to configure these traits to better suit your needs. The `traitOptions` attribute is here to help you with that. Let's see the various settings it provides.

### Default traits

Using the `traitOptions` attribute, you may configure the default traits that will be applied to every Rust type. These default traits can be configured using 4 different attributes:

- `baseDefaults`: The default traits to implement for all types.
- `dataEnumDefaults`: The default traits to implement for all data enum types, in addition to the `baseDefaults` traits. Data enums are enums with at least one non-unit variant — e.g. `pub enum Command { Write(String), Quit }`.
- `scalarEnumDefaults`: The default traits to implement for all scalar enum types, in addition to the `baseDefaults` traits. Scalar enums are enums with unit variants only — e.g. `pub enum Feedback { Good, Bad }`.
- `structDefaults`: The default traits to implement for all struct types, in addition to the `baseDefaults` traits.

Note that you must provide the fully qualified name of the traits you provide (e.g. `serde::Serialize`). Here are the default values for these attributes:

```ts
const traitOptions = {
baseDefaults: ['Clone', 'Debug', 'Eq', 'PartialEq'],
dataEnumDefaults: [],
scalarEnumDefaults: ['Copy', 'PartialOrd', 'Hash', 'num_derive::FromPrimitive'],
structDefaults: [],
};
```

### Overridden traits

In addition to configure the default traits, you may also override the traits for specific types. This will completely replace the default traits for the given type. To do so, you may use the `overrides` attribute of the `traitOptions` object.

This attribute is a map where the keys are the names of the types you want to override, and the values are the traits you want to apply to these types. Here is an example:

```ts
const traitOptions = {
overrides: {
myCustomType: ['Clone', 'my::custom::Trait', 'my::custom::OtherTrait'],
myTypeWithNoTraits: [],
},
};
```

### Feature Flags

You may also configure which traits should be rendered under a feature flag by using the `featureFlags` attribute. This attribute is a map where the keys are feature flag names and the values are the traits that should be rendered under that feature flag. Here is an example:

```ts
const traitOptions = {
featureFlags: { fruits: ['fruits::Apple', 'fruits::Banana'] },
};
```

Now, if at any point, we encounter a `fruits::Apple` or `fruits::Banana` trait to be rendered (either as default traits or as overridden traits), they will be rendered under the `fruits` feature flag. For instance:

```rust
#[cfg_attr(feature = "fruits", derive(fruits::Apple, fruits::Banana))]
```

Note that for feature flags to be effective, they must be added to the `Cargo.toml` file of the generated Rust client.

### Using the Fully Qualified Name

By default, all traits are imported using the provided Fully Qualified Name which means their short name will be used within the `derive` attributes.

However, you may want to avoid importing these traits and use the Fully Qualified Name directly in the generated code. To do so, you may use the `useFullyQualifiedName` attribute of the `traitOptions` object by setting it to `true`:

```ts
const traitOptions = {
useFullyQualifiedName: true,
};
```

Here is an example of rendered traits with this option set to `true` and `false` (which is the default):

```rust
// With `useFullyQualifiedName` set to `false` (default).
use serde::Serialize;
use serde::Deserialize;
// ...
#[derive(Serialize, Deserialize)]

// With `useFullyQualifiedName` set to `true`.
#[derive(serde::Serialize, serde::Deserialize)]
```

Note that any trait rendered under a feature flag will always use the Fully Qualified Name in order to ensure we only reference the trait when the feature is enabled.
70 changes: 70 additions & 0 deletions packages/renderers-rust-cpi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "@codama/renderers-rust-cpi",
"version": "0.1.0",
"description": "Renders CPI clients for your programs",
"exports": {
"types": "./dist/types/index.d.ts",
"react-native": "./dist/index.react-native.mjs",
"browser": {
"import": "./dist/index.browser.mjs",
"require": "./dist/index.browser.cjs"
},
"node": {
"import": "./dist/index.node.mjs",
"require": "./dist/index.node.cjs"
}
},
"browser": {
"./dist/index.node.cjs": "./dist/index.browser.cjs",
"./dist/index.node.mjs": "./dist/index.browser.mjs"
},
"main": "./dist/index.node.cjs",
"module": "./dist/index.node.mjs",
"react-native": "./dist/index.react-native.mjs",
"types": "./dist/types/index.d.ts",
"type": "commonjs",
"files": [
"./dist/types",
"./dist/index.*"
],
"sideEffects": false,
"keywords": [
"solana",
"framework",
"standard",
"renderers",
"rust",
"client"
],
"scripts": {
"build": "rimraf dist && tsup && tsc -p ./tsconfig.declarations.json",
"dev": "vitest --project node",
"lint": "eslint . && prettier --check .",
"lint:fix": "eslint --fix . && prettier --write .",
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:unit && pnpm test:e2e && pnpm test:exports",
"test:e2e": "./test/e2e/test.sh",
"test:exports": "node ./test/exports/module.mjs && node ./test/exports/commonjs.cjs",
"test:treeshakability": "for file in dist/index.*.mjs; do agadoo $file; done",
"test:types": "tsc --noEmit",
"test:unit": "vitest run"
},
"dependencies": {
"@codama/errors": "workspace:*",
"@codama/nodes": "workspace:*",
"@codama/renderers-core": "workspace:*",
"@codama/visitors-core": "workspace:*",
"@solana/codecs-strings": "^3.0.2"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/codama-idl/codama"
},
"bugs": {
"url": "http://github.com/codama-idl/codama/issues"
},
"browserslist": [
"supports bigint and not dead",
"maintained node versions"
]
}
4 changes: 4 additions & 0 deletions packages/renderers-rust-cpi/src/fragments/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './instructionPage';
export * from './modPage';
export * from './programModPage';
export * from './rootModPage';
38 changes: 38 additions & 0 deletions packages/renderers-rust-cpi/src/fragments/instructionModPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { InstructionNode } from '@codama/nodes';

import { addFragmentImports, Fragment, fragment, getPageFragment, mergeFragments, RenderScope } from '../utils';
import { getModImportsFragment } from './modPage';

/**
* Get the mod page fragment for instructions.
*/
export function getInstructionModPageFragment(
scope: Pick<RenderScope, 'dependencyMap'> & { instructions: InstructionNode[] },
): Fragment | undefined {
const imports = getModImportsFragment(scope.instructions);
if (!imports) return;

return getPageFragment(
mergeFragments([imports, getInstructionHelpersFragment()], cs => cs.join('\n\n')),
scope,
);
}

/**
* Helpers for handling `MaybeUninit<u8>` buffers.
*/
function getInstructionHelpersFragment(): Fragment {
return addFragmentImports(
fragment`
const UNINIT_BYTE: MaybeUninit<u8> = MaybeUninit::<u8>::uninit();

/// Write bytes from a source slice to a destination slice of \`MaybeUninit<u8>\`.
#[inline(always)]
fn write_bytes(destination: &mut [MaybeUninit<u8>], source: &[u8]) {
for (d, s) in destination.iter_mut().zip(source.iter()) {
d.write(*s);
}
}`,
['core::mem::MaybeUninit'],
);
}
Loading