Skip to content

Commit

Permalink
feat: add support for vec and array
Browse files Browse the repository at this point in the history
  • Loading branch information
sohrab- committed Jun 1, 2022
1 parent f4384f6 commit 13ed8e4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Tested with Anchor 0.24.x.
- [x] Types: Struct
- [x] Types: Enum
- [x] Types: Option & COption
- [ ] Types: Vectors
- [x] Types: Vector & Array
- [x] Errors
- [ ] Constants
- [ ] State
- [ ] Events
- [ ] Metadata
- [ ] Docs
- [ ] Docs (waiting for <https://github.com/project-serum/anchor/pull/1561> to make it to a canonical release)

Contributing
---
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function generate(
// setup cli
const argv = await yargs(hideBin(process.argv))
.usage(
'$0 <Program ID>',
'$0 <Program ID> [options]',
'Reads an Anchor IDL from stdin and prints the equivalent Rust interface to stdout'
)
.demandCommand(1)
Expand All @@ -70,7 +70,7 @@ export async function generate(

// generate code
const output = await generate(await readStream(process.stdin), {
programId: argv._[0] as string,
programId: argv['ProgramID'] as string,
templatePath: argv.template,
});

Expand Down
4 changes: 2 additions & 2 deletions src/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ const mapType = (type: IdlType): string => {
if (isDefined(type)) return type.defined;
if (isOption(type)) return `Option<${mapType(type.option)}>`;
if (isCOption(type)) return `COption<${mapType(type.coption)}>`;
// TODO Vec
// TODO [;]
if (isVec(type)) return `Vec<${mapType(type.vec)}>`;
if (isArray(type)) return `[${type.array[0]}; ${type.array[1]}]`;

if (type === 'bytes') return 'Vec<u8>';
if (type === 'string') return '&str'; // TODO how about "String"
Expand Down
12 changes: 12 additions & 0 deletions test/files/idl.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@
{
"name": "publicKey",
"type": "publicKey"
},
{
"name": "vec",
"type": {
"vec": "u8"
}
},
{
"name": "array",
"type": {
"array": ["bytes", 20]
}
}
]
}
Expand Down
4 changes: 3 additions & 1 deletion test/files/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Generated by @lab11/anchorgen@1.0.0 at 2022-05-26T11:41:29.255Z
/// Generated by @lab11/anchorgen@0.0.0-development at 2022-06-01T08:37:16.401Z

use anchor_lang::prelude::*;

Expand All @@ -18,6 +18,8 @@ pub mod my_program {
bytes: Vec<u8>,
string: &str,
public_key: Pubkey,
vec: Vec<u8>,
array: [bytes; 20],
) -> Result<()> {
Ok(())
}
Expand Down

0 comments on commit 13ed8e4

Please sign in to comment.