use crate::account_argument::MultiIndexable;
use crate::{AccountInfo, AccountInfoAccess, CruiserResult};
use solana_program::instruction::AccountMeta as SolanaAccountMeta;
pub trait SingleIndexable<I>: MultiIndexable<I> {
fn index_info(&self, indexer: I) -> CruiserResult<&Self::AccountInfo>;
fn index_to_solana_account_meta(&self, indexer: I) -> CruiserResult<SolanaAccountMeta>
where
Self::AccountInfo: AccountInfo,
{
let info = self.index_info(indexer)?;
Ok(SolanaAccountMeta {
pubkey: *info.key(),
is_signer: info.is_signer(),
is_writable: info.is_writable(),
})
}
}
pub trait Single: SingleIndexable<()> {
fn info(&self) -> &Self::AccountInfo;
}
impl<T> Single for T
where
T: SingleIndexable<()>,
{
fn info(&self) -> &Self::AccountInfo {
self.index_info(()).expect("`()` info is not infallible!")
}
}
pub trait ToSolanaAccountMeta {
fn to_solana_account_meta(&self) -> SolanaAccountMeta;
}
impl<T> ToSolanaAccountMeta for T
where
T: SingleIndexable<()>,
T::AccountInfo: AccountInfo,
{
fn to_solana_account_meta(&self) -> SolanaAccountMeta {
self.index_to_solana_account_meta(())
.expect("`()` info is not infallible!")
}
}
impl ToSolanaAccountMeta for SolanaAccountMeta {
fn to_solana_account_meta(&self) -> SolanaAccountMeta {
self.clone()
}
}