hpl_toolkit_attribute_compressed_account/
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
extern crate proc_macro;
mod renderers;
use syn::parse_macro_input;

#[proc_macro_attribute]
pub fn compressed_account(
    args: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let renderer = args.to_string();

    let input_str = input.to_string();
    let input_split = input_str.split("{").collect::<Vec<&str>>();
    let renderee = input_split.first().unwrap();

    let chunk_key = if renderer.starts_with("chunk = ") {
        Some(renderer.replace("chunk = ", ""))
    } else {
        None
    };

    let placeholders = if renderer.starts_with("placeholders = ") {
        renderer
            .replace("placeholders = ", "")
            .parse::<u8>()
            .unwrap()
    } else {
        0
    };

    if renderee.contains("struct") {
        let account = parse_macro_input!(input as syn::ItemStruct);

        if account.generics.params.len() > 0 {
            panic!("`compressed_account` attribute does not allow generics");
        }

        renderers::CompressedAccountStruct::new(account, chunk_key, placeholders).render()
    } else if renderee.contains("enum") {
        let account = parse_macro_input!(input as syn::ItemEnum);

        if account.generics.params.len() > 0 {
            panic!("`compressed_account` attribute does not allow generics");
        }

        renderers::CompressedAccountEnum::new(account, chunk_key).render()
    } else {
        panic!("Unknown structured provided for compressed data, only enum and struct are allowed")
    }
}

#[cfg(feature = "idl-build")]
mod idl_build;
#[cfg(feature = "idl-build")]
use {
    idl_build::{gen_idl_build_impl_for_enum, gen_idl_build_impl_for_struct, get_no_docs},
    quote::quote,
};
// #[cfg(feature = "idl-build")]
// use {
//     anchor_syn::idl::build::{
//         gen_idl_build_impl_for_enum, gen_idl_build_impl_for_struct, get_no_docs,
//     },
//     quote::quote,
// };

#[proc_macro_attribute]
pub fn struct_idl(
    _args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    #[cfg(feature = "idl-build")]
    {
        let account_strct = parse_macro_input!(item as syn::ItemStruct);
        let no_docs = get_no_docs();
        let idl_build_impl = gen_idl_build_impl_for_struct(&account_strct, no_docs);
        return proc_macro::TokenStream::from(quote! {
            #account_strct
            #idl_build_impl
        });
    }

    #[cfg(not(feature = "idl-build"))]
    item
}
#[proc_macro_attribute]
pub fn enum_idl(
    _args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    #[cfg(feature = "idl-build")]
    {
        let account_enum = parse_macro_input!(item as syn::ItemEnum);
        let no_docs = get_no_docs();
        let idl_build_impl = gen_idl_build_impl_for_enum(account_enum.clone(), no_docs);
        return proc_macro::TokenStream::from(quote! {
            #account_enum
            #idl_build_impl
        });
    }

    #[cfg(not(feature = "idl-build"))]
    item
}