use std::marker::PhantomData;
use solana_sdk::pubkey::Pubkey;
use crate::{decoder::AccountAccessDecoder, AccountAccess, Decode, DecodeError, Decoder, Visitor};
use super::OwnedData;
#[derive(Debug, Clone, Copy)]
pub struct Account<T> {
pubkey: Pubkey,
lamports: u64,
slot: u64,
data: OwnedData<T>,
}
impl<T> Account<T> {
pub fn owner(&self) -> &Pubkey {
self.data.owner()
}
pub fn pubkey(&self) -> &Pubkey {
&self.pubkey
}
pub fn lamports(&self) -> u64 {
self.lamports
}
pub fn data(&self) -> &T {
self.data.data()
}
pub fn into_data(self) -> T {
self.data.into_data()
}
pub fn slot(&self) -> u64 {
self.slot
}
}
impl<T> Decode for Account<T>
where
T: Decode,
{
fn decode<D: Decoder>(decoder: D) -> Result<Self, DecodeError> {
struct AccountVisitor<T>(PhantomData<T>);
impl<T: Decode> Visitor for AccountVisitor<T> {
type Value = Account<T>;
fn visit_account(
self,
account: impl AccountAccess,
) -> Result<Self::Value, DecodeError> {
Ok(Account {
pubkey: account.pubkey()?,
lamports: account.lamports()?,
slot: account.slot()?,
data: OwnedData::<T>::decode(AccountAccessDecoder::new(account))?,
})
}
}
decoder.decode_account(AccountVisitor(PhantomData))
}
}