Skip to content

Commit

Permalink
lang: Fix parsing of some constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen committed Nov 15, 2022
1 parent e8618a5 commit 42847fd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
41 changes: 36 additions & 5 deletions lang/syn/src/idl/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use heck::MixedCase;
use quote::ToTokens;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use syn::{
Expr, ExprLit, ItemConst,
Lit::{Byte, ByteStr},
};

const DERIVE_NAME: &str = "Accounts";
// TODO: share this with `anchor_lang` crate.
Expand Down Expand Up @@ -281,11 +285,7 @@ pub fn parse(

let constants = parse_consts(&ctx)
.iter()
.map(|c: &&syn::ItemConst| IdlConst {
name: c.ident.to_string(),
ty: c.ty.to_token_stream().to_string().parse().unwrap(),
value: c.expr.to_token_stream().to_string().parse().unwrap(),
})
.map(|c: &&syn::ItemConst| to_idl_const(c))
.collect::<Vec<IdlConst>>();

Ok(Some(Idl {
Expand Down Expand Up @@ -615,6 +615,37 @@ fn to_idl_type(ctx: &CrateContext, ty: &syn::Type) -> IdlType {
tts_string.parse().unwrap()
}

// TODO parse other issues
fn to_idl_const(item: &ItemConst) -> IdlConst {
let name = item.ident.to_string();

if let Expr::Lit(ExprLit { lit, .. }) = &*item.expr {
match lit {
ByteStr(lit_byte_str) => {
return IdlConst {
name,
ty: IdlType::Bytes,
value: format!("{:?}", lit_byte_str.value()),
}
}
Byte(lit_byte) => {
return IdlConst {
name,
ty: IdlType::U8,
value: lit_byte.value().to_string(),
}
}
_ => (),
}
}

IdlConst {
name,
ty: item.ty.to_token_stream().to_string().parse().unwrap(),
value: item.expr.to_token_stream().to_string().parse().unwrap(),
}
}

fn idl_accounts(
ctx: &CrateContext,
accounts: &AccountsStruct,
Expand Down
4 changes: 4 additions & 0 deletions tests/misc/programs/misc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ declare_id!("3TEqcc8xhrhdspwbvoamUJe2borm4Nr72JxL66k6rgrh");
pub const BASE: u128 = 1_000_000;
#[constant]
pub const DECIMALS: u8 = 6;
#[constant]
pub const BYTES_STR: &[u8] = b"test";
#[constant]
pub const BYTE_STR: u8 = b't';
pub const NO_IDL: u16 = 55;

#[program]
Expand Down
16 changes: 16 additions & 0 deletions tests/misc/tests/misc/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,22 @@ describe("misc", () => {
);
});

it("Should include BYTES_STR const in IDL", async () => {
assert.isDefined(
miscIdl.constants.find(
(c) => c.name === "BYTES_STR" && c.type === "bytes" && c.value === "[116, 101, 115, 116]"
)
);
});

it("Should include BYTE_STR const in IDL", async () => {
assert.isDefined(
miscIdl.constants.find(
(c) => c.name === "BYTE_STR" && c.type === "u8" && c.value === "116"
)
);
});

it("Should not include NO_IDL const in IDL", async () => {
assert.isUndefined(miscIdl.constants.find((c) => c.name === "NO_IDL"));
});
Expand Down

0 comments on commit 42847fd

Please sign in to comment.