@@ -5,6 +5,7 @@ use quote::quote;
55use std:: collections:: HashMap ;
66use syn:: { Attribute , AttributeArgs , ItemImpl , Lit , Meta , NestedMeta } ;
77
8+ use crate :: class:: Class ;
89use crate :: helpers:: get_docs;
910use crate :: {
1011 class:: { Property , PropertyAttr } ,
@@ -94,7 +95,11 @@ pub enum PropAttrTy {
9495 Setter ,
9596}
9697
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 > {
98103 let args = AttrArgs :: from_list ( & args)
99104 . map_err ( |e| anyhow ! ( "Unable to parse attribute arguments: {:?}" , e) ) ?;
100105
@@ -105,29 +110,23 @@ pub fn parser(args: AttributeArgs, input: ItemImpl) -> Result<TokenStream> {
105110 bail ! ( "This macro cannot be used on trait implementations." ) ;
106111 }
107112
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+ } ) ?;
119118
120119 let tokens = items
121120 . into_iter ( )
122121 . map ( |item| {
123122 Ok ( match item {
124123 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+ } ) ;
131130
132131 quote ! {
133132 #[ allow( dead_code) ]
@@ -141,24 +140,24 @@ pub fn parser(args: AttributeArgs, input: ItemImpl) -> Result<TokenStream> {
141140 // TODO(david): How do we handle comments for getter/setter? Take the comments
142141 // from the methods??
143142 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 ) ) ;
148147 let ident = parsed_method. method . orig_ident . clone ( ) ;
149148
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+ }
154153 }
155154 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 ) ;
160159 } else {
161- // class.methods.push(parsed_method.method);
160+ class. methods . push ( parsed_method. method ) ;
162161 }
163162 parsed_method. tokens
164163 }
0 commit comments