diff --git a/shank-cli/Cargo.toml b/shank-cli/Cargo.toml index e1b288d..9872e49 100644 --- a/shank-cli/Cargo.toml +++ b/shank-cli/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "shank-cli" -description = "Shank CLI that extracts solita compatible IDL from your Rust Solana program code annotated with shank macro attributes" authors = ["Metaplex Maintainers "] -repository = "https://github.com/metaplex-foundation/shank" -license = "Apache-2.0" -version = "0.4.2" +description = "Shank CLI that extracts solita compatible IDL from your Rust Solana program code annotated with shank macro attributes" edition = "2018" +license = "Apache-2.0" +name = "shank-cli" +repository = "https://github.com/metaplex-foundation/shank" +version = "0.5.0" [[bin]] name = "shank" @@ -17,4 +17,4 @@ anyhow = "1.0.48" clap = { version = "3.0.14", features = ["derive"] } fern = { version = "0.6.0", features = ["colored"] } log = "0.4.14" -shank_idl = { version= "0.4.2", path = "../shank-idl" } +shank_idl = { version = "0.5.0", path = "../shank-idl" } diff --git a/shank-idl/Cargo.toml b/shank-idl/Cargo.toml index ffd21d4..c78703d 100644 --- a/shank-idl/Cargo.toml +++ b/shank-idl/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "shank_idl" -version = "0.4.2" -description = "Converts information extracted via shank derive macros to a solita compatible IDL." authors = ["Metaplex Maintainers "] -repository = "https://github.com/metaplex-foundation/shank" -license = "Apache-2.0" +description = "Converts information extracted via shank derive macros to a solita compatible IDL." edition = "2018" +license = "Apache-2.0" +name = "shank_idl" +repository = "https://github.com/metaplex-foundation/shank" +version = "0.5.0" [dependencies] anyhow = "1.0.48" @@ -13,5 +13,5 @@ cargo_toml = "0.17" heck = "0.3.3" serde = { version = "1.0.130", features = ["derive"] } serde_json = "1.0.72" -shank_macro_impl = { version = "0.4.2", path = "../shank-macro-impl" } +shank_macro_impl = { version = "0.5.0", path = "../shank-macro-impl" } shellexpand = "2.1.0" diff --git a/shank-macro-impl/Cargo.toml b/shank-macro-impl/Cargo.toml index 74e79b9..8a89f5c 100644 --- a/shank-macro-impl/Cargo.toml +++ b/shank-macro-impl/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "shank_macro_impl" -version = "0.4.2" -description = "Implements and tests shank derive macros" authors = ["Metaplex Maintainers "] -repository = "https://github.com/metaplex-foundation/shank" -license = "Apache-2.0" +description = "Implements and tests shank derive macros" edition = "2018" +license = "Apache-2.0" +name = "shank_macro_impl" +repository = "https://github.com/metaplex-foundation/shank" +version = "0.5.0" [dependencies] anyhow = "1.0.48" diff --git a/shank-macro/Cargo.toml b/shank-macro/Cargo.toml index 343ce46..6f45b92 100644 --- a/shank-macro/Cargo.toml +++ b/shank-macro/Cargo.toml @@ -1,19 +1,24 @@ [package] -name = "shank_macro" -version = "0.4.2" -description = "Provides macros used to annotate Solana Rust programs in order to extract an IDL with the shank CLI" authors = ["Metaplex Maintainers "] -repository = "https://github.com/metaplex-foundation/shank" -license = "Apache-2.0" +description = "Provides macros used to annotate Solana Rust programs in order to extract an IDL with the shank CLI" edition = "2018" +license = "Apache-2.0" +name = "shank_macro" +repository = "https://github.com/metaplex-foundation/shank" +version = "0.5.0" [lib] -proc-macro = true doctest = false +proc-macro = true [dependencies] proc-macro2 = "1.0.32" quote = "1.0.10" -shank_macro_impl = { version = "0.4.2", path = "../shank-macro-impl" } -shank_render = { version = "0.4.2", path = "../shank-render" } +shank_macro_impl = { version = "0.5.0", path = "../shank-macro-impl" } +shank_render = { version = "0.5.0", path = "../shank-render" } syn = "1.0.82" + +[features] +default = ["solana-program"] +pinocchio = ["shank_render/pinocchio"] +solana-program = ["shank_render/solana-program"] diff --git a/shank-render/Cargo.toml b/shank-render/Cargo.toml index c1ab0b5..d37e533 100644 --- a/shank-render/Cargo.toml +++ b/shank-render/Cargo.toml @@ -1,16 +1,21 @@ [package] -name = "shank_render" -version = "0.4.2" -description = "Renders implementaions derived from shank macros" authors = ["Metaplex Maintainers "] -repository = "https://github.com/metaplex-foundation/shank" -license = "Apache-2.0" +description = "Renders implementaions derived from shank macros" edition = "2018" +license = "Apache-2.0" +name = "shank_render" +repository = "https://github.com/metaplex-foundation/shank" +version = "0.5.0" [dependencies] proc-macro2 = "1.0.46" quote = "1.0.21" -shank_macro_impl = { version = "0.4.2", path = "../shank-macro-impl" } +shank_macro_impl = { version = "0.5.0", path = "../shank-macro-impl" } [dev-dependencies] prettyplease = "0.1.21" + +[features] +default = ["solana-program"] +pinocchio = [] +solana-program = [] diff --git a/shank-render/src/context/mod.rs b/shank-render/src/context/mod.rs index 33b4f73..6c8137e 100644 --- a/shank-render/src/context/mod.rs +++ b/shank-render/src/context/mod.rs @@ -15,13 +15,19 @@ pub fn render_contexts_impl( .map(generate_context) .collect::>(); + let account_info_type = if cfg!(feature = "pinocchio") { + quote! { pinocchio::account_info::AccountInfo } + } else { + quote! { solana_program::account_info::AccountInfo<'a> } + }; + Ok(quote! { pub mod accounts { use super::*; pub struct Context<'a, T> { pub accounts: T, - pub remaining_accounts: &'a [solana_program::account_info::AccountInfo<'a>], + pub remaining_accounts: &'a [#account_info_type], } #(#contexts)* diff --git a/shank-render/src/context/render_context.rs b/shank-render/src/context/render_context.rs index 9303c8a..9f78177 100644 --- a/shank-render/src/context/render_context.rs +++ b/shank-render/src/context/render_context.rs @@ -3,26 +3,47 @@ use quote::quote; use shank_macro_impl::{instruction::InstructionVariant, syn}; pub(crate) fn generate_context(variant: &InstructionVariant) -> TokenStream { + // Define the account type path based on feature flags + let account_info_type = if cfg!(feature = "pinocchio") { + quote! { pinocchio::account_info::AccountInfo } + } else { + quote! { solana_program::account_info::AccountInfo<'a> } + }; + + // Define the ProgramError type path based on feature flags + let program_error_type = if cfg!(feature = "pinocchio") { + quote! { pinocchio::program_error::ProgramError } + } else { + quote! { solana_program::sysvar::slot_history::ProgramError> } + }; + + // Define how to retrieve the key of the account + let key_retrieval = if cfg!(feature = "pinocchio") { + quote! { key() } + } else { + quote! { key } + }; + // accounts fields let struct_fields = variant.accounts.iter().map(|account| { - let account_name = syn::parse_str::(&account.name).unwrap(); - if account.optional { - quote! { - pub #account_name: Option<&'a solana_program::account_info::AccountInfo<'a>> - } - } else { - quote! { - pub #account_name:&'a solana_program::account_info::AccountInfo<'a> - } + let account_name = syn::parse_str::(&account.name).unwrap(); + if account.optional { + quote! { + pub #account_name: Option<&'a #account_info_type> } - }); + } else { + quote! { + pub #account_name: &'a #account_info_type + } + } + }); // accounts initialization let account_fields = variant.accounts.iter().enumerate().map(|(index, account)| { let account_name = syn::parse_str::(&account.name).unwrap(); if account.optional { quote! { - #account_name: if accounts[#index].key == &crate::ID { None } else { Some(&accounts[#index]) } + #account_name: if accounts[#index].#key_retrieval == &crate::ID { None } else { Some(&accounts[#index]) } } } else { quote! { @@ -31,21 +52,22 @@ pub(crate) fn generate_context(variant: &InstructionVariant) -> TokenStream { } }); - let expected = variant.accounts.len(); // number of expected accounts + let expected = variant.accounts.len(); let name = syn::parse_str::(&format!("{}Accounts", variant.ident)) .unwrap(); + // Use the same account type in the impl block quote! { pub struct #name<'a> { #(#struct_fields,)* } impl<'a> #name<'a> { pub fn context( - accounts: &'a [solana_program::account_info::AccountInfo<'a>] - ) -> Result, solana_program::sysvar::slot_history::ProgramError> { + accounts: &'a [#account_info_type] + ) -> Result, #program_error_type> { if accounts.len() < #expected { - return Err(solana_program::sysvar::slot_history::ProgramError::NotEnoughAccountKeys); + return Err(#program_error_type::NotEnoughAccountKeys); } Ok(Context { diff --git a/shank/Cargo.toml b/shank/Cargo.toml index 4bf87e8..ce71778 100644 --- a/shank/Cargo.toml +++ b/shank/Cargo.toml @@ -1,12 +1,16 @@ [package] -name = "shank" -description = "Exposes macros to annotate Rust programs to extract solita compatible IDL in order to generate program SDKs" authors = ["Metaplex Maintainers "] -repository = "https://github.com/metaplex-foundation/shank" -license = "Apache-2.0" -version = "0.4.2" +description = "Exposes macros to annotate Rust programs to extract solita compatible IDL in order to generate program SDKs" edition = "2018" +license = "Apache-2.0" +name = "shank" +repository = "https://github.com/metaplex-foundation/shank" +version = "0.5.0" +[features] +default = ["solana-program"] +pinocchio = ["shank_macro/pinocchio"] +solana-program = ["shank_macro/solana-program"] [dependencies] -shank_macro = { version = "0.4.2", path = "../shank-macro" } +shank_macro = { version = "0.5.0", path = "../shank-macro" }