Skip to content

Commit 9d348f2

Browse files
committed
✨ Modify macro to allow [bolt_account] macro without component_id
1 parent 4be0728 commit 9d348f2

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

crates/bolt-lang/attribute/account/src/lib.rs

+27-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use proc_macro::TokenStream;
22
use quote::quote;
3-
use syn::spanned::Spanned;
43
use syn::{parse_macro_input, parse_quote, Attribute, DeriveInput, Lit, Meta, NestedMeta};
54

65
/// 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
98
/// The macro also adds the InitSpace and Default derives to the struct.
109
///
1110
/// #[account]
12-
/// #[bolt_account(component_id = "bolt-position")]
11+
/// #[bolt_account]
1312
/// pub struct Position {
1413
/// pub x: i64,
1514
/// pub y: i64,
@@ -18,45 +17,42 @@ use syn::{parse_macro_input, parse_quote, Attribute, DeriveInput, Lit, Meta, Nes
1817
/// ```
1918
#[proc_macro_attribute]
2019
pub fn bolt_account(attr: TokenStream, item: TokenStream) -> TokenStream {
21-
let attr = parse_macro_input!(attr as Meta);
2220
let mut input = parse_macro_input!(item as DeriveInput);
21+
let mut component_id_value = None;
2322

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+
}
3035
}
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+
}
3744
} else {
3845
None
3946
}
4047
} else {
4148
None
4249
}
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+
}
5254

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());
6056

6157
let additional_derives: Attribute = parse_quote! { #[derive(InitSpace, Default)] };
6258
input.attrs.push(additional_derives);

crates/bolt-lang/attribute/component/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syn::{
1919
/// }
2020
///
2121
/// #[account]
22-
/// #[bolt_account(component_id = "bolt-position")]
22+
/// #[bolt_account]
2323
/// pub struct Position {
2424
/// pub x: i64,
2525
/// pub y: i64,

examples/component-position/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub mod component_position {
99
}
1010

1111
#[account]
12-
#[bolt_account(component_id = "component-position")]
12+
#[bolt_account]
1313
#[derive(Copy)]
1414
pub struct Position {
1515
pub x: i64,

tests/bolt.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ describe("bolt", () => {
189189
componentPositionEntity1 = FindComponentPda(
190190
boltComponentPositionProgram.programId,
191191
entity1,
192-
"component-position"
192+
""
193193
);
194194

195195
console.log("Component Position E1: ", componentPositionEntity1.toBase58());
@@ -227,7 +227,7 @@ describe("bolt", () => {
227227
componentPositionEntity2 = FindComponentPda(
228228
boltComponentPositionProgram.programId,
229229
entity2,
230-
"component-position"
230+
""
231231
);
232232

233233
await worldProgram.methods

0 commit comments

Comments
 (0)