Forráskód Böngészése

Add byte slice(`&[u8]`) support for `idl-build` (#2622)

acheron 2 éve
szülő
commit
4955a92593

+ 8 - 1
lang/syn/src/idl/build.rs

@@ -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(()),
     }
 }
@@ -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(),

+ 10 - 5
tests/idl/idls/build.json

@@ -6,9 +6,9 @@
   ],
   "constants": [
     {
-      "name": "BAR_CONST",
-      "type": "u8",
-      "value": "6"
+      "name": "BYTES_STR",
+      "type": "bytes",
+      "value": "[116, 101, 115, 116]"
     },
     {
       "name": "BYTE_STR",
@@ -16,9 +16,14 @@
       "value": "116"
     },
     {
-      "name": "FOO_CONST",
-      "type": "u128",
+      "name": "I128",
+      "type": "i128",
       "value": "1000000"
+    },
+    {
+      "name": "U8",
+      "type": "u8",
+      "value": "6"
     }
   ],
   "instructions": [

+ 9 - 9
tests/idl/idls/parse.json

@@ -6,24 +6,24 @@
   ],
   "constants": [
     {
-      "name": "FOO_CONST",
-      "type": "u128",
-      "value": "1_000_000"
-    },
-    {
-      "name": "BAR_CONST",
+      "name": "U8",
       "type": "u8",
       "value": "6"
     },
     {
-      "name": "BYTES_STR",
-      "type": "bytes",
-      "value": "[116, 101, 115, 116]"
+      "name": "I128",
+      "type": "i128",
+      "value": "1_000_000"
     },
     {
       "name": "BYTE_STR",
       "type": "u8",
       "value": "116"
+    },
+    {
+      "name": "BYTES_STR",
+      "type": "bytes",
+      "value": "[116, 101, 115, 116]"
     }
   ],
   "instructions": [

+ 4 - 4
tests/idl/programs/idl/src/lib.rs

@@ -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;
 

+ 19 - 14
tests/idl/tests/idl.ts

@@ -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"));
   });