cruiser/traits/
instruction.rs

1//! An individual instruction for a program.
2
3use borsh::BorshDeserialize;
4use cruiser::account_argument::AccountArgument;
5
6use crate::account_argument::{FromAccounts, ValidateArgument};
7use crate::{CruiserResult, Pubkey};
8
9/// An instruction for a program with it's accounts and data.
10pub trait Instruction<AI>: Sized {
11    /// The account argument for this instruction.
12    type Accounts;
13    /// The instruction data minus the instruction discriminant.
14    type Data;
15}
16
17/// A processor for a given instruction `I`
18pub trait InstructionProcessor<AI, I: Instruction<AI>>
19where
20    I::Data: BorshDeserialize,
21    I::Accounts: AccountArgument<AccountInfo = AI>
22        + FromAccounts<Self::FromAccountsData>
23        + ValidateArgument<Self::ValidateData>,
24{
25    /// The data passed to [`FromAccounts::from_accounts`].
26    type FromAccountsData;
27    /// The data passed to [`ValidateArgument::validate`].
28    type ValidateData;
29    /// The data passed to [`InstructionProcessor::process`].
30    type InstructionData;
31
32    /// Turns the [`Instruction::Data`] into the sub-data types.
33    fn data_to_instruction_arg(
34        data: I::Data,
35    ) -> CruiserResult<(
36        Self::FromAccountsData,
37        Self::ValidateData,
38        Self::InstructionData,
39    )>;
40
41    /// Processes the instruction
42    fn process(
43        program_id: &Pubkey,
44        data: Self::InstructionData,
45        accounts: &mut I::Accounts,
46    ) -> CruiserResult<()>;
47}