@@ -5,6 +5,7 @@ use quote::quote;
5
5
use std:: collections:: HashMap ;
6
6
use syn:: { Attribute , AttributeArgs , ItemImpl , Lit , Meta , NestedMeta } ;
7
7
8
+ use crate :: class:: Class ;
8
9
use crate :: helpers:: get_docs;
9
10
use crate :: {
10
11
class:: { Property , PropertyAttr } ,
@@ -94,7 +95,11 @@ pub enum PropAttrTy {
94
95
Setter ,
95
96
}
96
97
97
- pub fn parser ( args : AttributeArgs , input : ItemImpl ) -> Result < TokenStream > {
98
+ pub fn parser (
99
+ args : AttributeArgs ,
100
+ input : ItemImpl ,
101
+ classes : & mut HashMap < String , Class > ,
102
+ ) -> Result < TokenStream > {
98
103
let args = AttrArgs :: from_list ( & args)
99
104
. map_err ( |e| anyhow ! ( "Unable to parse attribute arguments: {:?}" , e) ) ?;
100
105
@@ -105,29 +110,23 @@ pub fn parser(args: AttributeArgs, input: ItemImpl) -> Result<TokenStream> {
105
110
bail ! ( "This macro cannot be used on trait implementations." ) ;
106
111
}
107
112
108
- // if state.startup_function.is_some() {
109
- // bail!(
110
- // "Impls must be declared before you declare your startup function and module function."
111
- // );
112
- // }
113
- //
114
- // let class = state.classes.get_mut(&class_name).ok_or_else(|| {
115
- // anyhow!(
116
- // "You must use `#[php_class]` on the struct before using this attribute on the impl."
117
- // )
118
- // })?;
113
+ let class = classes. get_mut ( & class_name) . ok_or_else ( || {
114
+ anyhow ! (
115
+ "You must use `#[php_class]` on the struct before using this attribute on the impl."
116
+ )
117
+ } ) ?;
119
118
120
119
let tokens = items
121
120
. into_iter ( )
122
121
. map ( |item| {
123
122
Ok ( match item {
124
123
syn:: ImplItem :: Const ( constant) => {
125
- // class.constants.push(Constant {
126
- // name: constant.ident.to_string(),
127
- // // visibility: Visibility::Public,
128
- // docs: get_docs(&constant.attrs),
129
- // value: constant.expr.to_token_stream().to_string(),
130
- // });
124
+ class. constants . push ( Constant {
125
+ name : constant. ident . to_string ( ) ,
126
+ // visibility: Visibility::Public,
127
+ docs : get_docs ( & constant. attrs ) ,
128
+ value : constant. expr . to_token_stream ( ) . to_string ( ) ,
129
+ } ) ;
131
130
132
131
quote ! {
133
132
#[ allow( dead_code) ]
@@ -141,24 +140,24 @@ pub fn parser(args: AttributeArgs, input: ItemImpl) -> Result<TokenStream> {
141
140
// TODO(david): How do we handle comments for getter/setter? Take the comments
142
141
// from the methods??
143
142
if let Some ( ( prop, ty) ) = parsed_method. property {
144
- // let prop = class
145
- // .properties
146
- // .entry(prop)
147
- // .or_insert_with(|| Property::method(vec![], None));
143
+ let prop = class
144
+ . properties
145
+ . entry ( prop)
146
+ . or_insert_with ( || Property :: method ( vec ! [ ] , None ) ) ;
148
147
let ident = parsed_method. method . orig_ident . clone ( ) ;
149
148
150
- // match ty {
151
- // PropAttrTy::Getter => prop.add_getter(ident)?,
152
- // PropAttrTy::Setter => prop.add_setter(ident)?,
153
- // }
149
+ match ty {
150
+ PropAttrTy :: Getter => prop. add_getter ( ident) ?,
151
+ PropAttrTy :: Setter => prop. add_setter ( ident) ?,
152
+ }
154
153
}
155
154
if parsed_method. constructor {
156
- // if class.constructor.is_some() {
157
- // bail!("You cannot have two constructors on the same class.");
158
- // }
159
- // class.constructor = Some(parsed_method.method);
155
+ if class. constructor . is_some ( ) {
156
+ bail ! ( "You cannot have two constructors on the same class." ) ;
157
+ }
158
+ class. constructor = Some ( parsed_method. method ) ;
160
159
} else {
161
- // class.methods.push(parsed_method.method);
160
+ class. methods . push ( parsed_method. method ) ;
162
161
}
163
162
parsed_method. tokens
164
163
}
0 commit comments