trident_idl_spec/
utils.rs

1// Converts an `IdlType` to a corresponding Rust `syn::Type`.
2use quote::format_ident;
3use syn::parse_quote;
4
5use crate::{spec::IdlType, DefinedType, IdlArrayLen};
6
7pub fn idl_type_to_syn_type(
8    idl_type: &IdlType,
9    nestings: u8,
10    convert_pkey: bool,
11) -> (syn::Type, bool) {
12    if nestings >= 5 {
13        panic!("No more than 5 nestings allowed");
14    }
15    match idl_type {
16        IdlType::Bool => (parse_quote!(bool), false),
17        IdlType::U8 => (parse_quote!(u8), false),
18        IdlType::I8 => (parse_quote!(i8), false),
19        IdlType::U16 => (parse_quote!(u16), false),
20        IdlType::I16 => (parse_quote!(i16), false),
21        IdlType::U32 => (parse_quote!(u32), false),
22        IdlType::I32 => (parse_quote!(i32), false),
23        IdlType::F32 => (parse_quote!(f32), false),
24        IdlType::U64 => (parse_quote!(u64), false),
25        IdlType::I64 => (parse_quote!(i64), false),
26        IdlType::F64 => (parse_quote!(f64), false),
27        IdlType::U128 => (parse_quote!(u128), false),
28        IdlType::I128 => (parse_quote!(i128), false),
29        IdlType::U256 => (parse_quote!(u256), false), // Assuming custom type for u256
30        IdlType::I256 => (parse_quote!(i256), false), // Assuming custom type for i256
31        IdlType::Bytes => (parse_quote!(Vec<u8>), false),
32        IdlType::String => (parse_quote!(String), false),
33        IdlType::Pubkey | IdlType::PublicKey => {
34            if convert_pkey {
35                (parse_quote!(AccountId), false)
36            } else {
37                (parse_quote!(Pubkey), false)
38            }
39        }
40        IdlType::Option(inner) => {
41            let (inner_type, is_custom) = idl_type_to_syn_type(inner, 0, convert_pkey);
42            (parse_quote!(Option<#inner_type>), is_custom)
43        }
44        IdlType::Vec(inner) => {
45            let (inner_type, is_custom) = idl_type_to_syn_type(inner, 0, convert_pkey);
46            (parse_quote!(Vec<#inner_type>), is_custom)
47        }
48        IdlType::Array(inner, len) => {
49            let (inner_type, is_custom) = idl_type_to_syn_type(inner, 0, convert_pkey);
50            let len = match len {
51                IdlArrayLen::Generic(_generic) => {
52                    panic!("Generic within Array len not supported")
53                }
54                IdlArrayLen::Value(len) => len,
55            };
56            (parse_quote!([#inner_type;#len]), is_custom)
57        }
58        // Handle defined types
59        IdlType::Defined(inner) => match inner {
60            DefinedType::Simple(name) => {
61                let name_ident: syn::Ident = format_ident!("{}", &name);
62                (parse_quote!(#name_ident), true)
63            }
64            DefinedType::Complex { name, generics: _ } => {
65                let name_ident: syn::Ident = format_ident!("{}", &name);
66                (parse_quote!(#name_ident), true)
67            }
68        },
69        // Handle generic types
70        IdlType::Generic(name) => {
71            let name_ident: syn::Ident = format_ident!("{}", name);
72            (parse_quote!(#name_ident), true)
73        }
74    }
75}