shubhexists
solana_idl_parser
Rust

solana idl compile time parser

Last updated Apr 21, 2026
12
Stars
1
Forks
0
Issues
0
Stars/day
Attention Score
29
Language breakdown
No language data available.
Files click to expand
README

Solana IDL Parser

A Rust procedural macro that generates type-safe code from Solana Anchor IDL (Interface Definition Language) files at compile time.

Overview

This library eliminates boilerplate when working with Solana programs by automatically generating Rust types from Anchor IDL JSON files. Instead of manually writing deserialization code and type definitions, simply point the macro at your IDL file and get fully-typed, ready-to-use structs and enums.

Usage

use solanaidlparser::parse_idl;

// Parse IDL and generate all types parse_idl!("idl/program.json");

// Use generated types to deserialize instruction data let instruction = ProgramInstructions::deserialize(accounts, data)?;

match instruction { ProgramInstructions::Initialize(accounts, args) => { println!("Authority: {}", accounts.authority); println!("Initial amount: {}", args.amount); } ProgramInstructions::Update(accounts, args) => { println!("Updating with new value: {}", args.new_value); } // ... }

What Gets Generated

For each instruction in your IDL, the macro generates:

1. Discriminator Constants

pub const INITIALIZE_DISCRIMINATOR: [u8; 8] = [175, 175, 109, 31, 13, 152, 155, 237];

2. Accounts Structs

#[derive(Copy, Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize)]
pub struct InitializeAccounts {
    pub authority: Pubkey,
    pub system_program: Pubkey,
}

impl InitializeAccounts { pub fn fromaccountmetas(metas: &[AccountMeta]) -> anyhow::Result<Self> { // Validation and conversion logic } }

3. Args Structs

#[derive(Debug, BorshDeserialize, BorshSerialize)]
pub struct InitializeArgs {
    pub amount: u64,
    pub config: ConfigParams,
}

4. Instructions Enum

#[derive(Debug, BorshDeserialize, BorshSerialize)]
pub enum ProgramInstructions {
    Initialize(InitializeAccounts, InitializeArgs),
    Update(UpdateAccounts, UpdateArgs),
    Close(CloseAccounts),
}

5. Custom Types

All type definitions from the IDL are generated with proper Borsh derives:
#[derive(Debug, Clone, BorshDeserialize, BorshSerialize)]
pub struct ConfigParams {
    pub fee_rate: u16,
    pub max_supply: u64,
}

#[derive(Debug, Clone, BorshDeserialize, BorshSerialize)] pub enum Status { Active, Paused, Closed, }

Type Mapping

The parser intelligently maps IDL types to their Rust equivalents:

| IDL Type | Rust Type | Notes | |----------|-----------|-------| | bool, u8, u16, u32, u64, u128 | Native types | Direct mapping | | i8, i16, i32, i64, i128 | Native types | Signed integers | | f32, f64 | Native types | Floating point | | string | String | Heap-allocated string | | pubkey | solana_sdk::pubkey::Pubkey | Solana public key | | bytes | Vec<u8> | Dynamic byte array | | { "option": T } | Option<T> | Optional values | | { "vec": T } | Vec<T> | Dynamic arrays | | { "array": [T, N] } | [T; N] | Fixed-size arrays | | { "defined": "CustomType" } | CustomType | User-defined types |

License

MIT

© 2026 GitRepoTrend · shubhexists/solana_idl_parser · Updated daily from GitHub