File tree 4 files changed +73
-9
lines changed
4 files changed +73
-9
lines changed Original file line number Diff line number Diff line change 1
- # Generated by Cargo
2
- # will have compiled files and executables
3
1
debug /
4
2
target /
5
-
6
- # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7
- # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8
3
Cargo.lock
9
-
10
- # These are backup files generated by rustfmt
11
4
** /* .rs.bk
12
-
13
- # MSVC Windows builds of rustc generate these, which store debugging information
14
5
* .pdb
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " assert_proc"
3
+ version = " 0.1.0"
4
+ edition = " 2021"
5
+
6
+ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
+
8
+ [lib ]
9
+ proc-macro = true
10
+
11
+ [dependencies ]
12
+ proc-macro2 = " 1.0.60"
13
+ quote = " 1.0.28"
14
+ syn = { version = " 2.0.18" , features = [" full" ] }
15
+
Original file line number Diff line number Diff line change
1
+ use proc_macro:: TokenStream ;
2
+ use syn:: { parse_macro_input, ItemStruct } ;
3
+ use quote:: quote;
4
+
5
+ pub fn generate_test_functions_for_struct ( validated : TokenStream , tokens : TokenStream ) -> TokenStream {
6
+ let item = parse_macro_input ! ( validated as ItemStruct ) ;
7
+ let struct_item = item. ident . clone ( ) ;
8
+ let value = if tokens. is_empty ( ) { quote ! ( <#struct_item>:: default ( ) ) } else { tokens. into ( ) } ;
9
+ TokenStream :: from ( quote ! {
10
+ #item
11
+ mod tests {
12
+
13
+ use super :: * ;
14
+
15
+ // #[test]
16
+ pub fn foo( ) {
17
+ let value = #value;
18
+ }
19
+ }
20
+
21
+ } )
22
+
23
+
24
+ }
Original file line number Diff line number Diff line change
1
+
2
+ use proc_macro:: { TokenStream } ;
3
+ use syn:: { parse_macro_input, Item , ItemStruct , Ident } ;
4
+
5
+ mod generator;
6
+
7
+
8
+ #[ proc_macro_attribute]
9
+ pub fn assert_proc ( tokens : TokenStream , inputs : TokenStream ) -> TokenStream {
10
+ let inp = inputs. clone ( ) ;
11
+ let input_tokens = parse_macro_input ! ( inputs as Item ) ;
12
+ match input_tokens{
13
+ Item :: Struct ( s) => {
14
+ let validated = validate_fields ( s) . unwrap_or ( inp) ;
15
+ generator:: generate_test_functions_for_struct ( validated, tokens)
16
+ } ,
17
+ _ => todo ! ( )
18
+ }
19
+
20
+ }
21
+
22
+ fn validate_fields ( shtruct : ItemStruct ) -> Option < TokenStream > {
23
+ let mut attributed_fields = shtruct. fields . iter ( ) . flat_map ( |field| field. attrs . iter ( ) . map ( |attr| {
24
+ if attr. path ( ) . segments . first ( ) . unwrap ( ) . ident == Ident :: new ( "assert" , proc_macro:: Span :: call_site ( ) . into ( ) ) {
25
+ None
26
+ } else {
27
+ todo ! ( )
28
+ }
29
+ } ) ) ;
30
+
31
+ attributed_fields. next ( ) . and_then ( |fields| fields)
32
+ }
33
+
34
+
You can’t perform that action at this time.
0 commit comments