Skip to content

Commit

Permalink
Add byte slice(&[u8]) support for idl-build
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto committed Sep 7, 2023
1 parent a1e4453 commit 4cf48de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
9 changes: 8 additions & 1 deletion lang/syn/src/idl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ pub fn idl_type_ts_from_syn_type(
}
}
}
syn::Type::Reference(reference) => match reference.elem.as_ref() {
syn::Type::Slice(slice) if matches!(&*slice.elem, syn::Type::Path(path) if the_only_segment_is(path, "u8")) =>
{
return Ok((quote! {#idl::IdlType::Bytes}, vec![]));
}
_ => panic!("Reference types other than byte slice(`&[u8]`) are not allowed"),
},
_ => Err(()),
}
}
Expand Down Expand Up @@ -846,7 +853,7 @@ pub fn gen_idl_print_function_for_constant(item: &syn::ItemConst) -> TokenStream

let impl_ts = match idl_type_ts_from_syn_type(&item.ty, &vec![]) {
Ok((ty, _)) => quote! {
let value = format!("{}", #expr);
let value = format!("{:?}", #expr);

let idl = #idl::IdlConst {
name: #name.into(),
Expand Down
8 changes: 4 additions & 4 deletions tests/idl/programs/idl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use std::str::FromStr;
declare_id!("id11111111111111111111111111111111111111111");

#[constant]
pub const FOO_CONST: u128 = 1_000_000;
pub const U8: u8 = 6;

#[constant]
pub const BAR_CONST: u8 = 6;
pub const I128: i128 = 1_000_000;

#[constant]
pub const BYTES_STR: &[u8] = b"test";
pub const BYTE_STR: u8 = b't';

#[constant]
pub const BYTE_STR: u8 = b't';
pub const BYTES_STR: &[u8] = b"test";

pub const NO_IDL: u16 = 55;

Expand Down
33 changes: 19 additions & 14 deletions tests/idl/tests/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,29 @@ describe("IDL", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.idl as Program<Idl>;

it("Should include `FOO_CONST`", () => {
assert.isDefined(
program.idl.constants.find(
(c) =>
c.name === "FOO_CONST" && c.type === "u128" && c.value === "1000000"
)
);
});
it("Includes constants that use `#[constant]` macro", () => {
const checkDefined = (
cb: (constant: typeof program["idl"]["constants"][number]) => boolean
) => {
program.idl.constants.find((c) => cb(c));
};

it("Should include `BAR_CONST`", () => {
assert.isDefined(
program.idl.constants.find(
(c) => c.name === "BAR_CONST" && c.type === "u8" && c.value === "6"
)
checkDefined((c) => c.name === "U8" && c.type === "u8" && c.value === "6");
checkDefined(
(c) => c.name === "I128" && c.type === "i128" && c.value === "1000000"
);
checkDefined(
(c) => c.name === "BYTE_STR" && c.type === "u8" && c.value === "116"
);
checkDefined(
(c) =>
c.name === "BYTES_STR" &&
c.type === "bytes" &&
c.value === "[116, 101, 115, 116]"
);
});

it("Should not include `NO_IDL` const", () => {
it("Does not include constants that does not use `#[constant]` macro ", () => {
// @ts-expect-error
assert.isUndefined(program.idl.constants.find((c) => c.name === "NO_IDL"));
});
Expand Down

0 comments on commit 4cf48de

Please sign in to comment.