drift_idl_gen/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use std::{
    fs::{self},
    io::Write,
    path::Path,
    process::{Command, Stdio},
};

use proc_macro2::TokenStream;
use quote::quote;
use serde::{Deserialize, Serialize};
use sha2::Digest;
use syn::{Ident, Type};

#[derive(Debug, Serialize, Deserialize)]
struct Idl {
    version: String,
    name: String,
    instructions: Vec<Instruction>,
    types: Vec<TypeDef>,
    accounts: Vec<AccountDef>,
    events: Vec<EventDef>,
    errors: Vec<ErrorDef>,
}

#[derive(Debug, Serialize, Deserialize)]
struct Instruction {
    name: String,
    accounts: Vec<Account>,
    args: Vec<Arg>,
}

#[derive(Debug, Serialize, Deserialize)]
struct Account {
    name: String,
    #[serde(rename = "isMut")]
    is_mut: bool,
    #[serde(rename = "isSigner")]
    is_signer: bool,
}

#[derive(Debug, Serialize, Deserialize)]
struct Arg {
    name: String,
    #[serde(rename = "type")]
    arg_type: ArgType,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum ArgType {
    Simple(String),
    Defined { defined: String },
    Array { array: (Box<ArgType>, usize) },
    Option { option: Box<ArgType> },
    Vec { vec: Box<ArgType> },
}

impl ArgType {
    fn to_rust_type(&self) -> String {
        match self {
            ArgType::Simple(t) => {
                // special cases likely from manual edits to IDL
                if t == "publicKey" {
                    "Pubkey".to_string()
                } else if t == "bytes" {
                    "Vec<u8>".to_string()
                } else {
                    t.clone()
                }
            }
            ArgType::Defined { defined } => defined.clone(),
            ArgType::Array { array: (t, len) } => {
                let rust_type = t.to_rust_type();
                // this is a common signature representation
                if *len == 64_usize && rust_type == "u8" {
                    // [u8; 64] does not have a Default impl
                    "Signature".into()
                } else {
                    format!("[{}; {}]", t.to_rust_type(), len)
                }
            }
            ArgType::Option { option } => format!("Option<{}>", option.to_rust_type()),
            ArgType::Vec { vec } => format!("Vec<{}>", vec.to_rust_type()),
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct TypeDef {
    name: String,
    #[serde(rename = "type")]
    type_def: TypeData,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "kind")]
enum TypeData {
    #[serde(rename = "struct")]
    Struct { fields: Vec<StructField> },
    #[serde(rename = "enum")]
    Enum { variants: Vec<EnumVariant> },
}

#[derive(Debug, Serialize, Deserialize)]
struct StructField {
    name: String,
    #[serde(rename = "type")]
    field_type: ArgType,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum EnumVariant {
    // NB: this must come before `Simple` (harder match -> easiest match)
    Complex {
        name: String,
        fields: Vec<StructField>,
    },
    Simple {
        name: String,
    },
}

#[derive(Debug, Serialize, Deserialize)]
struct AccountDef {
    name: String,
    #[serde(rename = "type")]
    account_type: AccountType,
}

#[derive(Debug, Serialize, Deserialize)]
struct AccountType {
    kind: String, // Typically "struct"
    fields: Vec<StructField>,
}

#[derive(Debug, Serialize, Deserialize)]
struct ErrorDef {
    code: u32,
    name: String,
    msg: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct EventDef {
    name: String,
    fields: Vec<EventField>,
}

#[derive(Debug, Serialize, Deserialize)]
struct EventField {
    name: String,
    #[serde(rename = "type")]
    field_type: ArgType,
    index: bool,
}

fn generate_idl_types(idl: &Idl) -> String {
    let mut instructions_tokens = quote! {};
    let mut types_tokens = quote! {};
    let mut accounts_tokens = quote! {};
    let mut errors_tokens = quote! {};
    let mut events_tokens = quote! {};

    // Generate enums and structs from the types section
    for type_def in &idl.types {
        let type_name = Ident::new(
            &capitalize_first_letter(&type_def.name),
            proc_macro2::Span::call_site(),
        );
        let type_tokens = match &type_def.type_def {
            TypeData::Enum { variants } => {
                let has_complex_variant = variants.iter().any(|v| match v {
                    EnumVariant::Complex { .. } => true,
                    _ => false,
                });

                let variant_tokens =
                    variants
                        .iter()
                        .enumerate()
                        .map(|(i, variant)| match variant {
                            EnumVariant::Simple { name } => {
                                let variant_name = Ident::new(name, proc_macro2::Span::call_site());
                                if i == 0 {
                                    quote! {
                                        #[default]
                                        #variant_name,
                                    }
                                } else {
                                    quote! {
                                        #variant_name,
                                    }
                                }
                            }
                            EnumVariant::Complex { name, fields } => {
                                let variant_name = Ident::new(name, proc_macro2::Span::call_site());
                                let field_tokens = fields.iter().map(|field| {
                                    let field_name = Ident::new(
                                        &to_snake_case(&field.name),
                                        proc_macro2::Span::call_site(),
                                    );
                                    let field_type: Type =
                                        syn::parse_str(&field.field_type.to_rust_type()).unwrap();
                                    quote! {
                                        #field_name: #field_type,
                                    }
                                });
                                quote! {
                                    #variant_name {
                                        #(#field_tokens)*
                                    },
                                }
                            }
                        });

                if has_complex_variant {
                    quote! {
                        #[derive(AnchorSerialize, AnchorDeserialize, InitSpace, Copy, Clone, Debug, PartialEq)]
                        pub enum #type_name {
                            #(#variant_tokens)*
                        }
                    }
                } else {
                    // TODO: need more work to derive 'Default' on complex enums, not currently required
                    quote! {
                        #[derive(AnchorSerialize, AnchorDeserialize, InitSpace, Copy, Clone, Default, Debug, PartialEq)]
                        pub enum #type_name {
                            #(#variant_tokens)*
                        }
                    }
                }
            }
            TypeData::Struct { fields } => {
                let struct_name =
                    Ident::new(type_def.name.as_str(), proc_macro2::Span::call_site());
                let struct_fields = fields.iter().map(|field| {
                    let field_name =
                        Ident::new(&to_snake_case(&field.name), proc_macro2::Span::call_site());
                    let field_type: syn::Type =
                        syn::parse_str(&field.field_type.to_rust_type()).unwrap();
                    quote! {
                        pub #field_name: #field_type,
                    }
                });

                quote! {
                    #[repr(C)]
                    #[derive(AnchorSerialize, AnchorDeserialize, InitSpace, Copy, Clone, Default, Debug, PartialEq)]
                    pub struct #struct_name {
                        #(#struct_fields)*
                    }
                }
            }
        };

        types_tokens = quote! {
            #types_tokens
            #type_tokens
        };
    }

    // Generate structs for accounts section
    for account in &idl.accounts {
        let struct_name = Ident::new(&account.name, proc_macro2::Span::call_site());

        let struct_fields = account.account_type.fields.iter().map(|field| {
            let field_name =
                Ident::new(&to_snake_case(&field.name), proc_macro2::Span::call_site());

            let mut field_type: Type = syn::parse_str(&field.field_type.to_rust_type()).unwrap();
            // workaround for padding types preventing outertype from deriving 'Default'
            if field_name == "padding" {
                if let ArgType::Array { array: (_t, len) } = &field.field_type {
                    field_type = syn::parse_str(&format!("Padding<{len}>")).unwrap();
                }
            }

            quote! {
                pub #field_name: #field_type,
            }
        });

        let discriminator: TokenStream = format!("{:?}", sighash("account", &account.name))
            .parse()
            .unwrap();
        let struct_def = quote! {
            #[repr(C)]
            #[derive(AnchorSerialize, AnchorDeserialize, InitSpace, Copy, Clone, Default, Debug, PartialEq)]
            pub struct #struct_name {
                #(#struct_fields)*
            }
            #[automatically_derived]
            impl anchor_lang::Discriminator for #struct_name {
                const DISCRIMINATOR: [u8; 8] = #discriminator;
            }
            #[automatically_derived]
            unsafe impl anchor_lang::__private::bytemuck::Pod for #struct_name {}
            #[automatically_derived]
            unsafe impl anchor_lang::__private::bytemuck::Zeroable for #struct_name {}
            #[automatically_derived]
            impl anchor_lang::ZeroCopy for #struct_name {}
            #[automatically_derived]
            impl anchor_lang::AccountSerialize for #struct_name {
                fn try_serialize<W: std::io::Write>(&self, writer: &mut W) -> anchor_lang::Result<()> {
                    if writer.write_all(&Self::DISCRIMINATOR).is_err() {
                        return Err(anchor_lang::error::ErrorCode::AccountDidNotSerialize.into());
                    }

                    if AnchorSerialize::serialize(self, writer).is_err() {
                        return Err(anchor_lang::error::ErrorCode::AccountDidNotSerialize.into());
                    }

                    Ok(())
                }
            }
            #[automatically_derived]
            impl anchor_lang::AccountDeserialize for #struct_name {
                fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
                    let given_disc = &buf[..8];
                    if Self::DISCRIMINATOR != given_disc {
                        return Err(anchor_lang::error!(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch));
                    }
                    Self::try_deserialize_unchecked(buf)
                }

                fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
                    let mut data: &[u8] = &buf[8..];
                    AnchorDeserialize::deserialize(&mut data)
                        .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize.into())
                }
            }
        };

        accounts_tokens = quote! {
            #accounts_tokens
            #struct_def
        };
    }

    // Generate structs for instructions
    for instr in &idl.instructions {
        let name = capitalize_first_letter(&instr.name);
        let fn_name = to_snake_case(&instr.name);
        let struct_name = Ident::new(&name, proc_macro2::Span::call_site());
        let fields = instr.args.iter().map(|arg| {
            let field_name = Ident::new(&to_snake_case(&arg.name), proc_macro2::Span::call_site());
            let field_type: Type = syn::parse_str(&arg.arg_type.to_rust_type()).unwrap();
            quote! {
                pub #field_name: #field_type,
            }
        });
        // https://github.com/coral-xyz/anchor/blob/e48e7e60a64de77d878cdb063965cf125bec741a/lang/syn/src/codegen/program/instruction.rs#L32
        let discriminator: TokenStream = format!("{:?}", sighash("global", &fn_name))
            .parse()
            .unwrap();
        let struct_def = quote! {
            #[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
            pub struct #struct_name {
                #(#fields)*
            }
            #[automatically_derived]
            impl anchor_lang::Discriminator for #struct_name {
                const DISCRIMINATOR: [u8; 8] = #discriminator;
            }
            #[automatically_derived]
            impl anchor_lang::InstructionData for #struct_name {}
        };

        instructions_tokens = quote! {
            #instructions_tokens
            #struct_def
        };

        let accounts = instr.accounts.iter().map(|acc| {
            let account_name =
                Ident::new(&to_snake_case(&acc.name), proc_macro2::Span::call_site());
            quote! {
                pub #account_name: Pubkey,
            }
        });

        let to_account_metas = instr.accounts.iter().map(|acc| {
            let account_name_str = to_snake_case(&acc.name);
            let account_name =
                Ident::new(&account_name_str, proc_macro2::Span::call_site());
            let is_mut: TokenStream = acc.is_mut.to_string().parse().unwrap();
            let is_signer: TokenStream = acc.is_signer.to_string().parse().unwrap();
            quote! {
                AccountMeta { pubkey: self.#account_name, is_signer: #is_signer, is_writable: #is_mut },
            }
        });

        let discriminator: TokenStream =
            format!("{:?}", sighash("account", &name)).parse().unwrap();
        let account_struct_def = quote! {
            #[repr(C)]
            #[derive(Copy, Clone, Default, AnchorSerialize, AnchorDeserialize)]
            pub struct #struct_name {
                #(#accounts)*
            }
            #[automatically_derived]
            impl anchor_lang::Discriminator for #struct_name {
                const DISCRIMINATOR: [u8; 8] = #discriminator;
            }
            #[automatically_derived]
            unsafe impl anchor_lang::__private::bytemuck::Pod for #struct_name {}
            #[automatically_derived]
            unsafe impl anchor_lang::__private::bytemuck::Zeroable for #struct_name {}
            #[automatically_derived]
            impl anchor_lang::ZeroCopy for #struct_name {}
            #[automatically_derived]
            impl anchor_lang::InstructionData for #struct_name {}
            #[automatically_derived]
            impl ToAccountMetas for #struct_name {
                fn to_account_metas(
                    &self,
                ) -> Vec<AccountMeta> {
                   vec![
                        #(#to_account_metas)*
                    ]
                }
            }
            #[automatically_derived]
            impl anchor_lang::AccountSerialize for #struct_name {
                fn try_serialize<W: std::io::Write>(&self, writer: &mut W) -> anchor_lang::Result<()> {
                    if writer.write_all(&Self::DISCRIMINATOR).is_err() {
                        return Err(anchor_lang::error::ErrorCode::AccountDidNotSerialize.into());
                    }

                    if AnchorSerialize::serialize(self, writer).is_err() {
                        return Err(anchor_lang::error::ErrorCode::AccountDidNotSerialize.into());
                    }

                    Ok(())
                }
            }
            #[automatically_derived]
            impl anchor_lang::AccountDeserialize for #struct_name {
                fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
                    let given_disc = &buf[..8];
                    if Self::DISCRIMINATOR != given_disc {
                        return Err(anchor_lang::error!(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch));
                    }
                    Self::try_deserialize_unchecked(buf)
                }

                fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
                    let mut data: &[u8] = &buf[8..];
                    AnchorDeserialize::deserialize(&mut data)
                        .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize.into())
                }
            }
        };

        accounts_tokens = quote! {
            #accounts_tokens
            #account_struct_def
        };
    }

    // Generate enum for errors
    let error_variants = idl.errors.iter().map(|error| {
        let variant_name = Ident::new(&error.name, proc_macro2::Span::call_site());
        let error_msg = &error.msg;
        quote! {
            #[msg(#error_msg)]
            #variant_name,
        }
    });

    let error_enum = quote! {
        #[derive(PartialEq)]
        #[error_code]
        pub enum ErrorCode {
            #(#error_variants)*
        }
    };

    errors_tokens = quote! {
        #errors_tokens
        #error_enum
    };

    // Generate event structs from the events section
    for event in &idl.events {
        let struct_name = Ident::new(&event.name, proc_macro2::Span::call_site());
        let fields = event.fields.iter().map(|field| {
            let field_name =
                Ident::new(&to_snake_case(&field.name), proc_macro2::Span::call_site());
            let field_type: Type = syn::parse_str(&field.field_type.to_rust_type()).unwrap();
            quote! {
                pub #field_name: #field_type,
            }
        });

        let struct_def = quote! {
            //#[derive(InitSpace)]
            #[event]
            pub struct #struct_name {
                #(#fields)*
            }
        };

        events_tokens = quote! {
            #events_tokens
            #struct_def
        };
    }

    // Wrap generated code in modules with necessary imports
    let output = quote! {
        #![allow(unused_imports)]
        //!
        //! Auto-generated IDL types, manual edits do not persist (see `crates/drift-idl-gen`)
        //!
        use anchor_lang::{prelude::{account, AnchorSerialize, AnchorDeserialize, InitSpace, event, error_code, msg, borsh::{self}}, Discriminator};
        // use solana-sdk PUbkey, the vendored anchor-lang Pubkey maybe behind
        use solana_sdk::{instruction::AccountMeta, pubkey::Pubkey};
        use self::traits::ToAccountMetas;

        pub mod traits {
            use solana_sdk::instruction::AccountMeta;

            /// This is distinct from the anchor version of the trait
            /// reimplemented to ensure the types used are from solana crates _not_ the anchor vendored versions which may be lagging behind
            pub trait ToAccountMetas {
                fn to_account_metas(&self) -> Vec<AccountMeta>;
            }
        }

        pub mod instructions {
            use super::{*, types::*};

            #instructions_tokens
        }

        pub mod types {
            use std::ops::Mul;

            use super::*;
            /// backwards compatible u128 deserializing data from rust <=1.76.0 when u/i128 was 8-byte aligned
            /// https://solana.stackexchange.com/questions/7720/using-u128-without-sacrificing-alignment-8
            #[derive(
                Default,
                PartialEq,
                AnchorSerialize,
                AnchorDeserialize,
                Copy,
                Clone,
                bytemuck::Zeroable,
                bytemuck::Pod,
                Debug,
            )]
            #[repr(C)]
            pub struct u128(pub [u8; 16]);

            impl u128 {
                /// convert self into the std `u128` type
                pub fn as_u128(&self) -> std::primitive::u128 {
                    std::primitive::u128::from_le_bytes(self.0)
                }
            }

            impl From<std::primitive::u128> for self::u128 {
                fn from(value: std::primitive::u128) -> Self {
                    Self(value.to_le_bytes())
                }
            }

            /// backwards compatible i128 deserializing data from rust <=1.76.0 when u/i128 was 8-byte aligned
            /// https://solana.stackexchange.com/questions/7720/using-u128-without-sacrificing-alignment-8
            #[derive(
                Default,
                PartialEq,
                AnchorSerialize,
                AnchorDeserialize,
                Copy,
                Clone,
                bytemuck::Zeroable,
                bytemuck::Pod,
                Debug,
            )]
            #[repr(C)]
            pub struct i128(pub [u8; 16]);

            impl i128 {
                /// convert self into the std `i128` type
                pub fn as_i128(&self) -> core::primitive::i128 {
                    core::primitive::i128::from_le_bytes(self.0)
                }
            }

            impl From<core::primitive::i128> for i128 {
                fn from(value: core::primitive::i128) -> Self {
                    Self(value.to_le_bytes())
                }
            }

            #[repr(transparent)]
            #[derive(AnchorDeserialize, AnchorSerialize, Copy, Clone, PartialEq, Debug)]
            pub struct Signature(pub [u8; 64]);

            impl Default for Signature {
                fn default() -> Self {
                    Self([0_u8; 64])
                }
            }

            impl anchor_lang::Space for Signature {
                const INIT_SPACE: usize = 8 * 64;
            }

            /// wrapper around fixed array types used for padding with `Default` implementation
            #[repr(transparent)]
            #[derive(AnchorDeserialize, AnchorSerialize, Copy, Clone, PartialEq)]
            pub struct Padding<const N: usize>([u8; N]);
            impl<const N: usize> Default for Padding<N> {
                fn default() -> Self {
                    Self([0u8; N])
                }
            }

            impl<const N: usize> std::fmt::Debug for Padding<N> {
                fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    // don't print anything for padding...
                    Ok(())
                }
            }

            impl<const N: usize> anchor_lang::Space for Padding<N> {
                const INIT_SPACE: usize = 8 * N;
            }

            #types_tokens
        }

        pub mod accounts {
            use super::{*, types::*};

            #accounts_tokens
        }

        pub mod errors {
            use super::{*, types::*};

            #errors_tokens
        }

        pub mod events {
            use super::{*, types::*};
            #events_tokens
        }
    };

    output.to_string()
}

fn sighash(namespace: &str, name: &str) -> [u8; 8] {
    let preimage = format!("{namespace}:{name}");
    let mut hasher = sha2::Sha256::default();
    let mut sighash = <[u8; 8]>::default();
    hasher.update(preimage.as_bytes());
    let digest = hasher.finalize();
    sighash.copy_from_slice(&digest.as_slice()[..8]);

    sighash
}

fn to_snake_case(s: &str) -> String {
    let mut snake_case = String::new();
    for (i, c) in s.chars().enumerate() {
        if c.is_uppercase() {
            if i != 0 {
                snake_case.push('_');
            }
            snake_case.push(c.to_ascii_lowercase());
        } else {
            snake_case.push(c);
        }
    }
    snake_case
}

fn capitalize_first_letter(s: &str) -> String {
    let mut c = s.chars();
    match c.next() {
        None => String::new(),
        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
    }
}

fn format_rust_code(code: &str) -> String {
    let mut rustfmt = Command::new("rustfmt")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("Failed to start rustfmt");

    {
        let stdin = rustfmt.stdin.as_mut().expect("Failed to open stdin");
        stdin
            .write_all(code.as_bytes())
            .expect("Failed to write to stdin");
    }

    let output = rustfmt
        .wait_with_output()
        .expect("Failed to read rustfmt output");

    String::from_utf8(output.stdout).expect("rustfmt output is not valid UTF-8")
}

/// Generate rust types from IDL json
/// Output is emitted to `<NAME>_idl.rs`
pub fn generate_rust_types(idl_path: &Path) -> Result<String, Box<dyn std::error::Error>> {
    // Load the JSON file
    let data = fs::read_to_string(idl_path)?;
    let idl: Idl = serde_json::from_str(&data)?;

    // Generate Rust structs organized into modules
    let rust_idl_types = format_rust_code(&generate_idl_types(&idl));
    Ok(rust_idl_types)
}