Skip to content

Commit

Permalink
Improve rust modules (#63)
Browse files Browse the repository at this point in the history
* Add kaigan crate reference

* Add self to module use statements

* Remove blank line

* Reference RemainderVec from kaigan crate

* Add changeset
  • Loading branch information
febo authored Aug 22, 2023
1 parent 5ed583e commit a422c5a
Show file tree
Hide file tree
Showing 30 changed files with 628 additions and 395 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-adults-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Improve rust modules
4 changes: 1 addition & 3 deletions src/renderers/rust/GetRustRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ export class GetRustRenderMapVisitor extends BaseThrowVisitor<RenderMap> {
map.add('instructions/mod.rs', this.render('instructionsMod.njk', ctx));
}
if (definedTypesToExport.length > 0) {
map
.add('types/mod.rs', this.render('definedTypesMod.njk', ctx))
.add('types/helper/mod.rs', this.render('sharedPage.njk', ctx));
map.add('types/mod.rs', this.render('definedTypesMod.njk', ctx));
}

return map
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/rust/GetRustTypeManifestVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class GetRustTypeManifestVisitor implements Visitor<RustTypeManifest> {
}

if (arrayType.size.kind === 'remainder') {
childManifest.imports.add('crate::types::helper::RemainderVec');
childManifest.imports.add('kaigan::types::RemainderVec');
return {
...childManifest,
type: `RemainderVec<${childManifest.type}>`,
Expand Down
7 changes: 4 additions & 3 deletions src/renderers/rust/templates/accountsMod.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
{% block main %}

{% for account in accountsToExport | sort(false, false, 'name') %}
mod {{ account.name | snakeCase }};
pub mod {{ account.name | snakeCase }};
{% endfor %}

{% for account in accountsToExport | sort(false, false, 'name') %}
pub use {{ account.name | snakeCase }}::*;
pub use self::{{ account.name | snakeCase }}::*;
{% endfor %}
{% endblock %}

{% endblock %}
7 changes: 4 additions & 3 deletions src/renderers/rust/templates/definedTypesMod.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
{% block main %}

{% for definedType in definedTypesToExport | sort(false, false, 'name') %}
mod {{ definedType.name | snakeCase }};
pub mod {{ definedType.name | snakeCase }};
{% endfor %}

{% for definedType in definedTypesToExport | sort(false, false, 'name') %}
pub use {{ definedType.name | snakeCase }}::*;
pub use self::{{ definedType.name | snakeCase }}::*;
{% endfor %}
{% endblock %}

{% endblock %}
3 changes: 2 additions & 1 deletion src/renderers/rust/templates/definedTypesPage.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
{{ imports }}

{{ macros.docblock(definedType.docs) }}
{{ typeManifest.type }}
{{- typeManifest.type }}

{% for nestedStruct in typeManifest.nestedStructs %}
{{ nestedStruct }}
{% endfor %}

{% endblock %}
7 changes: 4 additions & 3 deletions src/renderers/rust/templates/errorsMod.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
{% block main %}

{% for program in programsToExport | sort(false, false, 'name') %}
mod {{ program.name | snakeCase }};
pub mod {{ program.name | snakeCase }};
{% endfor %}

{% for program in programsToExport | sort(false, false, 'name') %}
pub use {{ program.name | snakeCase }}::{{ program.name | pascalCase }}Error;
pub use self::{{ program.name | snakeCase }}::{{ program.name | pascalCase }}Error;
{% endfor %}
{% endblock %}

{% endblock %}
3 changes: 2 additions & 1 deletion src/renderers/rust/templates/errorsPage.njk
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ impl solana_program::program_error::PrintProgramError for {{ program.name | pasc
solana_program::msg!(&self.to_string());
}
}
{% endblock %}

{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ struct {{ instruction.name | pascalCase }}CpiBuilderInstruction<'a> {
{{ arg.name | snakeCase }}: {{ arg.type if arg.innerOptionType else 'Option<' + arg.type + '>' }},
{% endif %}
{% endfor %}
}
}
5 changes: 3 additions & 2 deletions src/renderers/rust/templates/instructionsMod.njk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod {{ instruction.name | snakeCase }};
{% endfor %}

{% for instruction in instructionsToExport | sort(false, false, 'name') %}
pub use {{ instruction.name | snakeCase }}::*;
pub use self::{{ instruction.name | snakeCase }}::*;
{% endfor %}
{% endblock %}

{% endblock %}
1 change: 1 addition & 0 deletions src/renderers/rust/templates/programsMod.njk
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ use solana_program::{pubkey, pubkey::Pubkey};
/// `{{ program.name | snakeCase }}` program ID.
pub const {{ program.name | snakeCase | upper }}_ID: Pubkey = pubkey!("{{ program.publicKey }}");
{% endfor %}

{% endblock %}
2 changes: 1 addition & 1 deletion src/renderers/rust/templates/rootMod.njk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
{% endif %}

{% if programsToExport.length > 0 %}
pub use programs::*;
pub(crate) use programs::*;
{% endif %}
{% endblock %}
23 changes: 23 additions & 0 deletions test/mpl_candy_machine_core.json
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,29 @@
}
]
}
},
{
"name": "DummyLines",
"docs": [
"Dummy lines."
],
"type": {
"kind": "struct",
"fields": [
{
"name": "lines",
"docs": [
"The dummy lines."
],
"type": {
"vec": {
"defined": "ConfigLine"
},
"size": "remainder"
}
}
]
}
}
],
"errors": [
Expand Down
42 changes: 42 additions & 0 deletions test/packages/js/src/generated/types/dummyLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import {
Serializer,
array,
struct,
} from '@metaplex-foundation/umi/serializers';
import { ConfigLine, ConfigLineArgs, getConfigLineSerializer } from '.';

/** Dummy lines. */
export type DummyLines = {
/** The dummy lines. */
lines: Array<ConfigLine>;
};

export type DummyLinesArgs = {
/** The dummy lines. */
lines: Array<ConfigLineArgs>;
};

/** @deprecated Use `getDummyLinesSerializer()` without any argument instead. */
export function getDummyLinesSerializer(
_context: object
): Serializer<DummyLinesArgs, DummyLines>;
export function getDummyLinesSerializer(): Serializer<
DummyLinesArgs,
DummyLines
>;
export function getDummyLinesSerializer(
_context: object = {}
): Serializer<DummyLinesArgs, DummyLines> {
return struct<DummyLines>(
[['lines', array(getConfigLineSerializer(), { size: 'remainder' })]],
{ description: 'DummyLines' }
) as Serializer<DummyLinesArgs, DummyLines>;
}
1 change: 1 addition & 0 deletions test/packages/js/src/generated/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './dataV2';
export * from './delegateArgs';
export * from './delegateRole';
export * from './delegateState';
export * from './dummyLines';
export * from './escrowAuthority';
export * from './extendedPayload';
export * from './hiddenSettings';
Expand Down
Loading

0 comments on commit a422c5a

Please sign in to comment.