hpl_toolkit_attribute_compressed_account/
lib.rsextern 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,
};
#[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
}