Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]: support macro generated types #39

Open
febo opened this issue Oct 4, 2022 · 2 comments
Open

[feat]: support macro generated types #39

febo opened this issue Oct 4, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request priority: critical

Comments

@febo
Copy link
Contributor

febo commented Oct 4, 2022

Solita currently does not support Rust macro generated types, resulting in an error:

AssertionError [ERR_ASSERTION]: Unknown type GuardType is neither found in types nor an Account
    at TypeMapper.definedTypesImport (/Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/type-mapper.js:275:190)
    at TypeMapper.mapDefinedType (/Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/type-mapper.js:107:34)
    at TypeMapper.map (/Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/type-mapper.js:133:24)
    at TypeRenderer.renderTypeField (/Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:24:52)
    at /Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:44:34
    at Array.map (<anonymous>)
    at TypeRenderer.renderTypeScriptType (/Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:44:14)
    at TypeRenderer.renderDataStructs (/Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:91:37)
    at TypeRenderer.determineIsFixable (/Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:101:14)
    at determineTypeIsFixable (/Users/febo/Developer/metaplex-foundation/candy-guard/js/node_modules/@metaplex-foundation/solita/dist/src/render-type.js:127:21) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: undefined,
  expected: undefined,
  operator: 'fail'
}

A potential solution would be to use cargo expand to run the macros and then look up any type not found in the source code.

@febo febo added enhancement New feature or request priority: critical labels Oct 4, 2022
@thlorenz
Copy link
Contributor

thlorenz commented Oct 4, 2022

Yeah this would be tricky to implement since at this point we go through the written code and find structs that are annotated with either BorshSerialize or BorshDeserialize to include them as custom types.

cargo expand might work, but could also result in other issues due to annotations of expanded macros no longer being present in the result, etc. It's not gonna be a simple solution I'm afraid.
At any rate the changes for this need to happen in shank, so I moved the issue here (from solita).

@thlorenz thlorenz transferred this issue from metaplex-foundation/solita Oct 4, 2022
@febo
Copy link
Contributor Author

febo commented Oct 5, 2022

Summarizing our chat here:

  1. Generated code does not have any macro annotations, since at that point all macros have been expanded;
  2. First-pass goes through the written code;
  3. Add a second-pass to the process, where any missing type from the first-pass is looked up on the expanded code (output of cargo expand). At this point, the types to be exported do not need to have any annotations. Any type referenced by the types exported in the first-pass would be included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority: critical
Projects
None yet
Development

No branches or pull requests

2 participants