Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Improved program_client generated code
Browse files Browse the repository at this point in the history
Removed needles question mark from generated program_client code that caused annoying clippy warnings. Also removed fully qualified Pubkey types in generated instructions to improve IDE suggestions readability.
  • Loading branch information
Ikrk committed Sep 15, 2023
1 parent 1a2f9ee commit e807c6e
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions crates/client/src/program_client_generator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::idl::Idl;
use quote::{format_ident, ToTokens};
use syn::{parse_quote, parse_str};
use syn::{parse, parse_quote, parse_str};

/// Generates `program_client`'s `lib.rs` from [Idl] created from Anchor programs.
/// Disable regenerating the `use` statements with a used imports `use_modules`
Expand Down Expand Up @@ -48,7 +48,22 @@ pub fn generate_source_code(idl: Idl, use_modules: &[syn::ItemUse]) -> String {
.iter()
.map(|(name, ty)| {
let name = format_ident!("a_{name}");
let ty: syn::Type = parse_str(ty).unwrap();
// do not use fully qualified type for Pubkey
let ty = parse_str(ty).unwrap();
let ty: syn::Type = match &ty {
syn::Type::Path(tp) => {
let last_type =
&tp.path.segments.last().unwrap().ident.to_string();
if last_type == "Pubkey" {
let t: syn::Type = parse_str(last_type).unwrap();
t
} else {
// we expect only Pubkey, but if it is something different, than return fully qualified type
ty
}
}
_ => ty,
};
let account: syn::FnArg = parse_quote!(#name: #ty);
account
})
Expand Down Expand Up @@ -83,7 +98,7 @@ pub fn generate_source_code(idl: Idl, use_modules: &[syn::ItemUse]) -> String {
#(#accounts,)*
signers: impl IntoIterator<Item = Keypair> + Send + 'static,
) -> Result<EncodedConfirmedTransactionWithStatusMeta, ClientError> {
Ok(client.send_instruction(
client.send_instruction(
PROGRAM_ID,
#module_name::instruction::#instruction_struct_name {
#(#field_parameters,)*
Expand All @@ -92,7 +107,7 @@ pub fn generate_source_code(idl: Idl, use_modules: &[syn::ItemUse]) -> String {
#(#field_accounts,)*
},
signers,
).await?)
).await
}
};

Expand Down

0 comments on commit e807c6e

Please sign in to comment.