Skip to content

Commit 0de4fce

Browse files
committed
Sync Cargo.toml when generating code
1 parent a2943d8 commit 0de4fce

File tree

11 files changed

+2586
-1177
lines changed

11 files changed

+2586
-1177
lines changed

.changeset/blue-zebras-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/renderers-rust': patch
3+
---
4+
5+
Add `syncCargoToml` and `dependencyVersions` options in order to create or update the Cargo.toml of the Rust crate when generating its code.

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ An object can be passed as a second argument to further configure the renderer.
3636

3737
The `renderVisitor` accepts the following options.
3838

39-
| Name | Type | Default | Description |
40-
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
41-
| `deleteFolderBeforeRendering` | `boolean` | `true` | Whether the base directory should be cleaned before generating new files. |
42-
| `formatCode` | `boolean` | `false` | Whether we should use `cargo fmt` to format the generated code. When set to `true`, the `crateFolder` option must be provided. |
43-
| `toolchain` | `string` | `"+stable"` | The toolchain to use when formatting the generated code. |
44-
| `crateFolder` | `string` | none | The path to the root folder of the Rust crate. This option is required when `formatCode` is set to `true`. |
45-
| `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. |
46-
| `dependencyMap` | `Record<string, string>` | `{}` | A mapping between import aliases and their actual crate name or path in Rust. |
47-
| `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. |
48-
| `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. |
49-
| `anchorTraits` | `boolean` | `true` | Whether to generate Anchor traits `impl` for account types. |
39+
| Name | Type | Default | Description |
40+
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------- | ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
41+
| `deleteFolderBeforeRendering` | `boolean` | `true` | Whether the base directory should be cleaned before generating new files. |
42+
| `formatCode` | `boolean` | `false` | Whether we should use `cargo fmt` to format the generated code. When set to `true`, the `crateFolder` option must be provided. |
43+
| `toolchain` | `string` | `"+stable"` | The toolchain to use when formatting the generated code. |
44+
| `crateFolder` | `string` | none | The path to the root folder of the Rust crate. This option is required when `formatCode` is set to `true`. |
45+
| `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. |
46+
| `dependencyMap` | `Record<string, string>` | `{}` | A mapping between import aliases and their actual crate name or path in Rust. |
47+
| `dependencyVersions` | `Record<string, CargoDependency>` | `{}` | A mapping between external crates — e.g. `solana-pubkey` — and the version range we should use for them — e.g. `^3.0` or the equivalent dependency object — e.g. `{ version: "^3.0" }`. The renderer offers default values for all external dependencies it relies on but this option may be used to override some of these values or add new ones. |
48+
| `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. |
49+
| `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. |
50+
| `anchorTraits` | `boolean` | `true` | Whether to generate Anchor traits `impl` for account types. |
51+
| `syncCargoToml` | `boolean` | `false` | Whether to update the dependencies of the existing `Cargo.toml` — or create a new `Cargo.toml` when missing — at the provided `crateFolder` option if provided. |
5052

5153
## Trait Options
5254

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@
4545
"@codama/nodes": "^1.4.4",
4646
"@codama/renderers-core": "^1.3.3",
4747
"@codama/visitors-core": "^1.4.4",
48+
"@iarna/toml": "^2.2.5",
4849
"@solana/codecs-strings": "^5.0.0",
49-
"nunjucks": "^3.2.4"
50+
"nunjucks": "^3.2.4",
51+
"semver": "^7.7.3"
5052
},
5153
"devDependencies": {
52-
"@codama/nodes-from-anchor": "^1.3.6",
5354
"@changesets/changelog-github": "^0.5.1",
5455
"@changesets/cli": "^2.29.7",
56+
"@codama/nodes-from-anchor": "^1.3.6",
5557
"@solana/eslint-config-solana": "^5.0.0",
5658
"@solana/prettier-config-solana": "0.0.5",
5759
"@types/node": "^25",
5860
"@types/nunjucks": "^3.2.6",
61+
"@types/semver": "^7.7.1",
5962
"agadoo": "^3.0.0",
6063
"eslint": "^9.35.0",
6164
"happy-dom": "^20.0.0",

0 commit comments

Comments
 (0)