trident_template/
template_getters.rs

1use quote::ToTokens;
2use syn::parse_quote;
3
4use crate::Template;
5
6impl Template {
7    pub fn get_instructions(&self) -> Vec<(String, String)> {
8        self.instructions
9            .iter()
10            .map(|(name, file)| (name.clone(), file.into_token_stream().to_string()))
11            .collect()
12    }
13    pub fn get_transactions(&self) -> Vec<(String, String)> {
14        self.transactions
15            .iter()
16            .map(|(name, file)| (name.clone(), file.into_token_stream().to_string()))
17            .collect()
18    }
19    pub fn get_instructions_mod(&self) -> String {
20        let (modules, uses): (Vec<syn::ItemMod>, Vec<syn::ItemUse>) = self
21            .instructions_mod
22            .iter()
23            .map(|mod_definition| {
24                (
25                    mod_definition.module.clone(),
26                    mod_definition.use_statement.clone(),
27                )
28            })
29            .unzip();
30
31        let mod_file: syn::File = parse_quote! {
32            #(#modules)*
33            #(#uses)*
34        };
35
36        mod_file.into_token_stream().to_string()
37    }
38    pub fn get_transactions_mod(&self) -> String {
39        let (modules, uses): (Vec<syn::ItemMod>, Vec<syn::ItemUse>) = self
40            .transactions_mod
41            .iter()
42            .map(|mod_definition| {
43                (
44                    mod_definition.module.clone(),
45                    mod_definition.use_statement.clone(),
46                )
47            })
48            .unzip();
49
50        let mod_file: syn::File = parse_quote! {
51            #(#modules)*
52            #(#uses)*
53        };
54
55        mod_file.into_token_stream().to_string()
56    }
57    pub fn get_fuzz_transactions(&self) -> String {
58        let transaction_variants = self.fuzz_transactions.clone();
59        let account_storages: Vec<syn::Field> = self
60            .account_storages
61            .iter()
62            .map(|account_storage| account_storage.1.clone())
63            .collect();
64
65        let module_definition: syn::File = parse_quote! {
66            use trident_fuzz::fuzzing::*;
67            use crate::transactions::*;
68
69            /// FuzzTransactions contains all available transactions
70            ///
71            /// You can create your own transactions by adding new variants to the enum.
72            ///
73            /// Docs: https://ackee.xyz/trident/docs/latest/trident-api-macro/trident-types/fuzz-transactions/
74            #[derive(Arbitrary, TransactionSelector)]
75            pub enum FuzzTransactions {
76                #(#transaction_variants),*
77            }
78
79            /// FuzzAccounts contains all available accounts
80            ///
81            /// You can create your own accounts by adding new fields to the struct.
82            ///
83            /// Docs: https://ackee.xyz/trident/docs/latest/trident-api-macro/trident-types/fuzz-accounts/
84            #[derive(Default)]
85            pub struct FuzzAccounts {
86                #(#account_storages),*
87            }
88
89        };
90        module_definition.into_token_stream().to_string()
91    }
92    pub fn get_custom_types(&self) -> String {
93        let custom_types = self.custom_types.clone();
94        let common_header = quote::quote! {
95            use borsh::{BorshDeserialize, BorshSerialize};
96            use trident_fuzz::fuzzing::*;
97
98            /// File containing all custom types which can be used
99            /// in transactions and instructions or invariant checks.
100            ///
101            /// You can define your own custom types here.
102        };
103
104        let module_definition: syn::File = match custom_types.len() {
105            0 => parse_quote! {
106                #common_header
107
108                #[derive(Arbitrary, Debug, BorshDeserialize, BorshSerialize, Clone)]
109                pub struct ExampleType {
110                    example_data: u8,
111                }
112            },
113            _ => parse_quote! {
114                #common_header
115
116                #(#custom_types)*
117            },
118        };
119        module_definition.into_token_stream().to_string()
120    }
121
122    pub fn get_test_fuzz(&self) -> String {
123        match &self.test_fuzz {
124            Some(file) => file.into_token_stream().to_string(),
125            None => panic!("test_fuzz.rs not prepared, aborting"),
126        }
127    }
128}