metaplex-foundation / shank

Extracts IDL from Solana Rust contracts
https://docs.rs/crate/shank_macro/latest
116 stars 23 forks source link

feat: tuple support #31

Closed thlorenz closed 2 years ago

thlorenz commented 2 years ago

Summary

This adds support to parse out top level tuples of varying sizes and nested tuples as well. The resulting IDL type "tuple" has an array for the inner tuple types.

Changes to Composites

Until now Composite types assumed 1- 2 inner types, i.e. for Vecs and HashMaps. However for Tuples there can be an arbitrary amount of inner types >= 2. Thus inner types in Composites are now stored in a Vec of arbitrary size.

Examples

Top Level Tuple

pub u8_u8: (u8, u8),
{
  "name": "u8U8",
  "type": {
    "tuple": ["u8", "u8"]
  }
}

Nested Tuple

pub hash_map_u8_u16_string_custom: HashMap<(u8, u16), (String, Custom)>,
{
  "name": "hashMapU8U16StringCustom",
  "type": {
    "hashMap": [
      {
        "tuple": ["u8", "u16"]
      },
      {
        "tuple": [
          "string",
          {
            "defined": "Custom"
          }
        ]
      }
    ]
  }
}

Multi Inner Type Tuple

pub string_custom_option_i128_u8_u16_u32_u64:
    (String, Custom, Option<i128>, u8, u16, u32, u64),
{
  "name": "stringCustomOptionI128U8U16U32U64",
  "type": {
    "tuple": [
      "string",
      {
        "defined": "Custom"
      },
      {
        "option": "i128"
      },
      "u8",
      "u16",
      "u32",
      "u64"
    ]
  }
}