Support for AccountLoaderDynamic

This commit is contained in:
Christian Kamm 2022-07-24 10:11:06 +02:00
parent e5cb5ca747
commit 1153380487
4 changed files with 42 additions and 0 deletions

View File

@ -216,6 +216,7 @@ pub fn generate_constraint_has_one(f: &Field, c: &ConstraintHasOne) -> proc_macr
let field = match &f.ty {
Ty::Loader(_) => quote! {#ident.load()?},
Ty::AccountLoader(_) => quote! {#ident.load()?},
Ty::AccountLoaderDynamic(_) => quote! {#ident.load_fixed()?},
_ => quote! {#ident},
};
let error = generate_custom_error(

View File

@ -358,6 +358,21 @@ impl Field {
}
}
}
Ty::AccountLoaderDynamic(_) => {
if checked {
quote! {
#container_ty::try_from(
&#field,
).map_err(|e| e.with_account_name(#field_str))?
}
} else {
quote! {
#container_ty::try_from_unchecked(
&#field,
).map_err(|e| e.with_account_name(#field_str))?
}
}
}
_ => {
if checked {
quote! {
@ -389,6 +404,9 @@ impl Field {
Ty::AccountLoader(_) => quote! {
anchor_lang::accounts::account_loader::AccountLoader
},
Ty::AccountLoaderDynamic(_) => quote! {
crate::state::AccountLoaderDynamic
},
Ty::Loader(_) => quote! {
anchor_lang::accounts::loader::Loader
},
@ -443,6 +461,12 @@ impl Field {
#ident
}
}
Ty::AccountLoaderDynamic(ty) => {
let ident = &ty.account_type_path;
quote! {
#ident
}
}
Ty::Loader(ty) => {
let ident = &ty.account_type_path;
quote! {
@ -509,6 +533,7 @@ pub enum Ty {
ProgramAccount(ProgramAccountTy),
Loader(LoaderTy),
AccountLoader(AccountLoaderTy),
AccountLoaderDynamic(AccountLoaderDynamicTy),
CpiAccount(CpiAccountTy),
Sysvar(SysvarTy),
Account(AccountTy),
@ -560,6 +585,12 @@ pub struct AccountLoaderTy {
pub account_type_path: TypePath,
}
#[derive(Debug, PartialEq)]
pub struct AccountLoaderDynamicTy {
// The struct type of the account.
pub account_type_path: TypePath,
}
#[derive(Debug, PartialEq)]
pub struct LoaderTy {
// The struct type of the account.

View File

@ -886,7 +886,9 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
&& !matches!(self.f_ty, Some(Ty::Account(_)))
&& !matches!(self.f_ty, Some(Ty::Loader(_)))
&& !matches!(self.f_ty, Some(Ty::AccountLoader(_)))
&& !matches!(self.f_ty, Some(Ty::AccountLoaderDynamic(_)))
{
println!("{:?}", self.f_ty);
return Err(ParseError::new(
c.span(),
"close must be on an Account, ProgramAccount, or Loader",

View File

@ -239,6 +239,7 @@ fn is_field_primitive(f: &syn::Field) -> ParseResult<bool> {
| "CpiState"
| "Loader"
| "AccountLoader"
| "AccountLoaderDynamic"
| "Account"
| "Program"
| "Signer"
@ -263,6 +264,7 @@ fn parse_ty(f: &syn::Field) -> ParseResult<Ty> {
"UncheckedAccount" => Ty::UncheckedAccount,
"Loader" => Ty::Loader(parse_program_account_zero_copy(&path)?),
"AccountLoader" => Ty::AccountLoader(parse_program_account_loader(&path)?),
"AccountLoaderDynamic" => Ty::AccountLoaderDynamic(parse_program_mango_account_loader(&path)?),
"Account" => Ty::Account(parse_account_ty(&path)?),
"Program" => Ty::Program(parse_program_ty(&path)?),
"Signer" => Ty::Signer,
@ -337,6 +339,12 @@ fn parse_program_account_loader(path: &syn::Path) -> ParseResult<AccountLoaderTy
account_type_path: account_ident,
})
}
fn parse_program_mango_account_loader(path: &syn::Path) -> ParseResult<AccountLoaderDynamicTy> {
let account_ident = parse_account(path)?;
Ok(AccountLoaderDynamicTy {
account_type_path: account_ident,
})
}
fn parse_account_ty(path: &syn::Path) -> ParseResult<AccountTy> {
let account_type_path = parse_account(path)?;