Skip to content

Commit 41eced9

Browse files
committed
Factor out prepare from impl.
Note that prepare isn't pure here as it mutates a class. We may want to rearrange things later.
1 parent fbddbe1 commit 41eced9

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

crates/macros/src/impl_.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use quote::quote;
55
use std::collections::HashMap;
66
use syn::{Attribute, AttributeArgs, ItemImpl, Lit, Meta, NestedMeta};
77

8+
use crate::class::Class;
89
use crate::helpers::get_docs;
910
use crate::{
1011
class::{Property, PropertyAttr},
@@ -94,26 +95,20 @@ pub enum PropAttrTy {
9495
Setter,
9596
}
9697

97-
pub fn parser(args: AttributeArgs, input: ItemImpl) -> Result<TokenStream> {
98-
let args = AttrArgs::from_list(&args)
99-
.map_err(|e| anyhow!("Unable to parse attribute arguments: {:?}", e))?;
100-
98+
// note: this takes a mutable argument as it mutates a class
99+
fn prepare(
100+
args: AttrArgs,
101+
input: ItemImpl,
102+
classes: &mut HashMap<String, Class>,
103+
) -> Result<TokenStream> {
101104
let ItemImpl { self_ty, items, .. } = input;
102105
let class_name = self_ty.to_token_stream().to_string();
103106

104107
if input.trait_.is_some() {
105108
bail!("This macro cannot be used on trait implementations.");
106109
}
107110

108-
let mut state = crate::STATE.lock();
109-
110-
if state.startup_function.is_some() {
111-
bail!(
112-
"Impls must be declared before you declare your startup function and module function."
113-
);
114-
}
115-
116-
let class = state.classes.get_mut(&class_name).ok_or_else(|| {
111+
let class = classes.get_mut(&class_name).ok_or_else(|| {
117112
anyhow!(
118113
"You must use `#[php_class]` on the struct before using this attribute on the impl."
119114
)
@@ -178,6 +173,21 @@ pub fn parser(args: AttributeArgs, input: ItemImpl) -> Result<TokenStream> {
178173
Ok(output)
179174
}
180175

176+
pub fn parser(args: AttributeArgs, input: ItemImpl) -> Result<TokenStream> {
177+
let args = AttrArgs::from_list(&args)
178+
.map_err(|e| anyhow!("Unable to parse attribute arguments: {:?}", e))?;
179+
180+
let mut state = crate::STATE.lock();
181+
182+
if state.startup_function.is_some() {
183+
bail!(
184+
"Impls must be declared before you declare your startup function and module function."
185+
);
186+
}
187+
188+
prepare(args, input, &mut state.classes)
189+
}
190+
181191
pub fn parse_attribute(attr: &Attribute) -> Result<Option<ParsedAttribute>> {
182192
let name = attr.path.to_token_stream().to_string();
183193
let meta = attr

0 commit comments

Comments
 (0)