From f240de7460fcf85d02dfe5af353a8df1f50ba61b Mon Sep 17 00:00:00 2001 From: James Whaley Date: Tue, 22 Aug 2023 17:44:32 +0100 Subject: [PATCH] Fix comments generated by ShankAccount seeds Comments were being interpreted as string literals in generated code instead of as comments. --- shank-render/src/pda/mod.rs | 2 +- shank-render/src/pda/render_pda.rs | 24 ++++++++++++------------ shank-render/src/pda/render_seeds.rs | 12 +++++------- shank-render/tests/pda/render_impl.rs | 4 +--- shank-render/tests/pda/render_pda_fn.rs | 1 + shank-render/tests/utils/mod.rs | 3 +-- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/shank-render/src/pda/mod.rs b/shank-render/src/pda/mod.rs index 21f0cc6..ee805b2 100644 --- a/shank-render/src/pda/mod.rs +++ b/shank-render/src/pda/mod.rs @@ -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! { diff --git a/shank-render/src/pda/render_pda.rs b/shank-render/src/pda/render_pda.rs index b325ed0..974d718 100644 --- a/shank-render/src/pda/render_pda.rs +++ b/shank-render/src/pda/render_pda.rs @@ -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 { +) -> ParseResult> { 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,16 +37,15 @@ 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. /// @@ -52,14 +53,13 @@ pub fn render_pda_fn( {} /// * **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)] diff --git a/shank-render/src/pda/render_seeds.rs b/shank-render/src/pda/render_seeds.rs index 3dd83e6..0523936 100644 --- a/shank-render/src/pda/render_seeds.rs +++ b/shank-render/src/pda/render_seeds.rs @@ -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()) diff --git a/shank-render/tests/pda/render_impl.rs b/shank-render/tests/pda/render_impl.rs index 872c326..b2b8c04 100644 --- a/shank-render/tests/pda/render_impl.rs +++ b/shank-render/tests/pda/render_impl.rs @@ -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! { diff --git a/shank-render/tests/pda/render_pda_fn.rs b/shank-render/tests/pda/render_pda_fn.rs index ef69123..1c43984 100644 --- a/shank-render/tests/pda/render_pda_fn.rs +++ b/shank-render/tests/pda/render_pda_fn.rs @@ -21,6 +21,7 @@ fn render_pda(code: TokenStream, include_comments: bool) -> TokenStream { include_comments, ) .unwrap() + .unwrap() } #[allow(unused)] diff --git a/shank-render/tests/utils/mod.rs b/shank-render/tests/utils/mod.rs index 142918d..a000a6e 100644 --- a/shank-render/tests/utils/mod.rs +++ b/shank-render/tests/utils/mod.rs @@ -28,6 +28,5 @@ pub fn process_seeds(code: TokenStream) -> ParseResult> { } 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()) }