Add support for Unit enum variants

This commit is contained in:
armaniferrante 2020-09-19 19:47:43 -07:00
parent 383c8b3a08
commit a887b73ead
No known key found for this signature in database
GPG Key ID: 58BEF301E91F7828
1 changed files with 30 additions and 16 deletions

View File

@ -358,20 +358,36 @@ fn enum_to_methods(
#(#method_arg_idents_vec),* #(#method_arg_idents_vec),*
}; };
// The instruction enum, with var names but no types.
let instruction_enum = {
if variant.fields == syn::Fields::Unit {
quote! {
#instruction_enum_ident::#variant_name
}
} else {
quote! {
#instruction_enum_ident::#variant_name {
#method_arg_idents,
}
}
}
};
// Generate the method to create a Solana `Instruction` representing this // Generate the method to create a Solana `Instruction` representing this
// enum variant. // enum variant.
let instruction_method = quote! { let instruction_method = {
pub fn #method_name(program_id: Pubkey, accounts: &[AccountMeta], #method_args) -> Instruction { quote! {
// Create the instruction enum. pub fn #method_name(program_id: Pubkey, accounts: &[AccountMeta], #method_args) -> Instruction {
let instruction = #instruction_enum_ident::#variant_name { // Create the instruction enum.
#method_arg_idents, let instruction = #instruction_enum;
};
// Serialize. // Serialize.
let data = #coder_struct::to_bytes(instruction); let data = #coder_struct::to_bytes(instruction);
Instruction { Instruction {
program_id: program_id, program_id: program_id,
data, data,
accounts: accounts.to_vec(), accounts: accounts.to_vec(),
}
} }
} }
}; };
@ -455,7 +471,7 @@ fn enum_to_methods(
let variant_instr = super::instruction::#method_name( let variant_instr = super::instruction::#method_name(
self.program_id, self.program_id,
&new_accounts, &new_accounts,
#method_arg_idents, #method_arg_idents
); );
// Transaction: create the transaction with the combined instructions. // Transaction: create the transaction with the combined instructions.
@ -599,9 +615,7 @@ fn enum_to_methods(
// Save the single dispatch arm representing this enum variant. // Save the single dispatch arm representing this enum variant.
dispatch_arms.push(quote! { dispatch_arms.push(quote! {
#instruction_enum_ident::#variant_name { #instruction_enum => #method_name(accounts, #method_args)
#method_args
} => #method_name(accounts, #method_args)
}); });
(client_method, instruction_method) (client_method, instruction_method)