Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comments generated by ShankAccount seeds #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix comments generated by ShankAccount seeds
Comments were being interpreted as string literals in generated code
instead of as comments.
Juici committed Aug 22, 2023
commit f240de7460fcf85d02dfe5af353a8df1f50ba61b
2 changes: 1 addition & 1 deletion shank-render/src/pda/mod.rs
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ pub fn render_pda_and_seeds_impl(
&pda_fn_ident,
&pda_fn_with_bump_ident,
include_comments,
);
)?;

if let (Some(pub_seeds_fn), Some(pub_pda_fn)) = (pub_seeds_fn, pub_pda_fn) {
Ok(quote! {
24 changes: 12 additions & 12 deletions shank-render/src/pda/render_pda.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::str::FromStr;

use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use quote::quote;
use shank_macro_impl::{
parsed_struct::{ProcessedSeed, Seed},
syn::Ident,
syn::{Ident, Result as ParseResult},
};

use crate::consts::solana_program_pubkey;
@@ -16,14 +18,14 @@ pub fn render_pda_fn(
pda_fn_name: &Ident,
pda_fn_with_bump_name: &Ident,
include_comments: bool,
) -> Option<TokenStream> {
) -> ParseResult<Option<TokenStream>> {
let RenderedPdaParts {
seed_param_assigns,
seed_fn_args,
pda_fn_args,
} = render_pda_parts(processed_seeds);
if pda_fn_args.is_empty() {
return None;
return Ok(None);
}

let pubkey = solana_program_pubkey();
@@ -35,31 +37,29 @@ pub fn render_pda_fn(
let (pda_comments, pda_with_bump_comments) = if include_comments {
let args_comments = render_args_comments(processed_seeds, true);
(
format!(
TokenStream::from_str(&format!(
r#"
/// Derives the PDA for this account.
///
/// * **program_id**: The id of the program
{}"#,
args_comments.join("\n")
)
.to_token_stream(),
format!(
))?,
TokenStream::from_str(&format!(
r#"
/// Derives the PDA for this account allowing to provide a bump seed.
///
/// * **program_id**: The id of the program
{}
/// * **bump**: the bump seed to pass when deriving the PDA"#,
args_comments.join("\n")
)
.to_token_stream(),
))?,
)
} else {
(TokenStream::new(), TokenStream::new())
};

Some(quote! {
Ok(Some(quote! {
#pda_comments
#[allow(unused)]
pub fn #pda_fn_name(#(#pda_fn_args),*) -> (#pubkey, u8) {
@@ -75,7 +75,7 @@ pub fn render_pda_fn(
let seeds = Self::#seeds_fn_with_bump_name(#(#seed_fn_args),* #seed_bump_arg);
#pubkey::find_program_address(&seeds, program_id)
}
})
}))
}

#[derive(Debug)]
12 changes: 5 additions & 7 deletions shank-render/src/pda/render_seeds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use quote::{quote, ToTokens};
use quote::quote;
use std::str::FromStr;

use proc_macro2::{Ident, Span, TokenStream};
@@ -40,23 +40,21 @@ pub fn try_render_seeds_fn(
let (seeds_comments, seeds_with_bump_comments) = if include_comments {
let args_comments = render_args_comments(processed_seeds, false);
(
format!(
TokenStream::from_str(&format!(
r#"
/// Derives the seeds for this account.
///
{}"#,
args_comments.join("\n")
)
.to_token_stream(),
format!(
))?,
TokenStream::from_str(&format!(
r#"
/// Derives the seeds for this account allowing to provide a bump seed.
///
{}
/// * **bump**: the bump seed to pass when deriving the PDA"#,
args_comments.join("\n")
)
.to_token_stream(),
))?,
)
} else {
(TokenStream::new(), TokenStream::new())
4 changes: 1 addition & 3 deletions shank-render/tests/pda/render_impl.rs
Original file line number Diff line number Diff line change
@@ -177,9 +177,7 @@ fn candy_guard_mint_limit_impl() {
// Including Comments
// -----------------

// NOTE: once comments are involved it is very brittle to compare rendered code
// thus this test only exists to allow uncommenting dumping the rendered code
// #[test]
#[test]
#[allow(unused)]
fn literal_pubkeys_and_u8_byte_impl_commented() {
let code = quote! {
1 change: 1 addition & 0 deletions shank-render/tests/pda/render_pda_fn.rs
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ fn render_pda(code: TokenStream, include_comments: bool) -> TokenStream {
include_comments,
)
.unwrap()
.unwrap()
}

#[allow(unused)]
3 changes: 1 addition & 2 deletions shank-render/tests/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -28,6 +28,5 @@ pub fn process_seeds(code: TokenStream) -> ParseResult<Vec<ProcessedSeed>> {
}

pub fn pretty_print(code: TokenStream) -> String {
let syn_tree = syn::parse_file(code.to_string().as_str()).unwrap();
prettyplease::unparse(&syn_tree)
prettyplease::unparse(&syn::parse2(code).unwrap())
}