1
1
use proc_macro:: TokenStream ;
2
2
use quote:: quote;
3
- use syn:: spanned:: Spanned ;
4
3
use syn:: { parse_macro_input, parse_quote, Attribute , DeriveInput , Lit , Meta , NestedMeta } ;
5
4
6
5
/// This BoltAccount attribute is used to automatically generate the seed and size functions
@@ -9,7 +8,7 @@ use syn::{parse_macro_input, parse_quote, Attribute, DeriveInput, Lit, Meta, Nes
9
8
/// The macro also adds the InitSpace and Default derives to the struct.
10
9
///
11
10
/// #[account]
12
- /// #[bolt_account(component_id = "bolt-position") ]
11
+ /// #[bolt_account]
13
12
/// pub struct Position {
14
13
/// pub x: i64,
15
14
/// pub y: i64,
@@ -18,45 +17,42 @@ use syn::{parse_macro_input, parse_quote, Attribute, DeriveInput, Lit, Meta, Nes
18
17
/// ```
19
18
#[ proc_macro_attribute]
20
19
pub fn bolt_account ( attr : TokenStream , item : TokenStream ) -> TokenStream {
21
- let attr = parse_macro_input ! ( attr as Meta ) ;
22
20
let mut input = parse_macro_input ! ( item as DeriveInput ) ;
21
+ let mut component_id_value = None ;
23
22
24
- let component_id_value = match attr {
25
- Meta :: NameValue ( meta_name_value) if meta_name_value. path . is_ident ( "component_id" ) => {
26
- if let Lit :: Str ( lit) = meta_name_value. lit {
27
- Some ( lit. value ( ) )
28
- } else {
29
- None
23
+ if !attr. is_empty ( ) {
24
+
25
+ let attr_meta = parse_macro_input ! ( attr as Meta ) ;
26
+
27
+ component_id_value = match attr_meta {
28
+ Meta :: Path ( _) => None ,
29
+ Meta :: NameValue ( meta_name_value) if meta_name_value. path . is_ident ( "component_id" ) => {
30
+ if let Lit :: Str ( lit) = meta_name_value. lit {
31
+ Some ( lit. value ( ) )
32
+ } else {
33
+ None
34
+ }
30
35
}
31
- }
32
- Meta :: List ( meta) => meta. nested . into_iter ( ) . find_map ( |nested_meta| {
33
- if let NestedMeta :: Meta ( Meta :: NameValue ( meta_name_value) ) = nested_meta {
34
- if meta_name_value. path . is_ident ( "component_id" ) {
35
- if let Lit :: Str ( lit) = meta_name_value. lit {
36
- Some ( lit. value ( ) )
36
+ Meta :: List ( meta) => meta. nested . into_iter ( ) . find_map ( |nested_meta| {
37
+ if let NestedMeta :: Meta ( Meta :: NameValue ( meta_name_value) ) = nested_meta {
38
+ if meta_name_value. path . is_ident ( "component_id" ) {
39
+ if let Lit :: Str ( lit) = meta_name_value. lit {
40
+ Some ( lit. value ( ) )
41
+ } else {
42
+ None
43
+ }
37
44
} else {
38
45
None
39
46
}
40
47
} else {
41
48
None
42
49
}
43
- } else {
44
- None
45
- }
46
- } ) ,
47
- _ => {
48
- let error = syn:: Error :: new ( attr. span ( ) , "Missing required attribute `component_id`" ) ;
49
- return error. to_compile_error ( ) . into ( ) ;
50
- }
51
- } ;
50
+ } ) ,
51
+ _ => None
52
+ } ;
53
+ }
52
54
53
- let component_id_value = match component_id_value {
54
- Some ( value) => value,
55
- None => {
56
- let error = syn:: Error :: new ( input. span ( ) , "The `component_id` attribute is required" ) ;
57
- return error. to_compile_error ( ) . into ( ) ;
58
- }
59
- } ;
55
+ let component_id_value = component_id_value. unwrap_or_else ( || "" . to_string ( ) ) ;
60
56
61
57
let additional_derives: Attribute = parse_quote ! { #[ derive( InitSpace , Default ) ] } ;
62
58
input. attrs . push ( additional_derives) ;
0 commit comments