use crate::CruiserAccountInfo;
use solana_program::entrypoint::SUCCESS;
use solana_program::msg;
use solana_program::pubkey::Pubkey;
use crate::traits::error::CruiserResult;
pub use solana_program::custom_heap_default;
pub use solana_program::custom_panic_default;
use std::vec::IntoIter;
#[macro_export]
macro_rules! entrypoint {
($process_instruction:path) => {
$crate::entrypoint!($process_instruction, no_heap, no_panic);
$crate::entrypoint::custom_heap_default!();
$crate::entrypoint::custom_panic_default!();
};
($process_instruction:path, no_heap) => {
$crate::entrypoint!($process_instruction, no_heap, no_panic);
$crate::entrypoint::custom_panic_default!();
};
($process_instruction:path, no_panic) => {
$crate::entrypoint!($process_instruction, no_heap, no_panic);
$crate::entrypoint::custom_heap_default!();
};
($process_instruction:path, no_heap, no_panic) => {
#[no_mangle]
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
$crate::entrypoint::entry(input, $process_instruction)
}
};
}
#[macro_export]
macro_rules! entrypoint_list {
($instruction_list:ty, $processor:ty) => {
$crate::entrypoint_list!($instruction_list, $processor, no_heap, no_panic);
$crate::entrypoint::custom_heap_default!();
$crate::entrypoint::custom_panic_default!();
};
($instruction_list:ty, $processor:ty, no_heap) => {
$crate::entrypoint_list!($instruction_list, $processor, no_heap, no_panic);
$crate::entrypoint::custom_panic_default!();
};
($instruction_list:ty, $processor:ty, no_panic) => {
$crate::entrypoint_list!($instruction_list, $processor, no_heap, no_panic);
$crate::entrypoint::custom_heap_default!();
};
($instruction_list:ty, $processor:ty, no_heap, no_panic) => {
#[no_mangle]
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
$crate::entrypoint::entry(
input,
<$processor as $crate::instruction_list::InstructionListProcessor<
$crate::CruiserAccountInfo,
$instruction_list,
>>::process_instruction,
)
}
};
}
pub unsafe fn entry(
input: *mut u8,
function: impl FnOnce(
&'static Pubkey,
&mut IntoIter<CruiserAccountInfo>,
&[u8],
) -> CruiserResult<()>,
) -> u64 {
let (program_id, accounts, instruction_data) = CruiserAccountInfo::deserialize(input);
match function(program_id, &mut accounts.into_iter(), instruction_data) {
Ok(()) => SUCCESS,
Err(error) => {
msg!("Error: {}", error.message());
error.to_program_error().into()
}
}
}