Skip to content

Commit

Permalink
Add ability to pass modifier function for classes (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcole1340 authored Mar 8, 2022
1 parent 75ea323 commit 4468656
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions crates/macros/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct Class {
pub constructor: Option<crate::method::Method>,
pub constants: Vec<crate::constant::Constant>,
pub properties: HashMap<String, Property>,
/// A function name called when creating the class entry. Given an instance
/// of `ClassBuilder` and must return it.
pub modifier: Option<String>,
}

#[derive(Debug)]
Expand All @@ -33,6 +36,7 @@ pub enum ParsedAttribute {
#[darling(default)]
pub struct AttrArgs {
name: Option<String>,
modifier: Option<String>,
}

pub fn parser(args: AttributeArgs, mut input: ItemStruct) -> Result<TokenStream> {
Expand Down Expand Up @@ -120,6 +124,7 @@ pub fn parser(args: AttributeArgs, mut input: ItemStruct) -> Result<TokenStream>
interfaces,
docs: comments,
properties,
modifier: args.modifier,
..Default::default()
};

Expand Down
13 changes: 10 additions & 3 deletions crates/macros/src/startup_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,23 @@ fn build_classes(classes: &HashMap<String, Class>) -> Result<Vec<TokenStream>> {
// Ok(quote! { .property(#name, #default_expr, #flags_expr) })
// })
// .collect::<Result<Vec<_>>>()?;
let class_modifier = class.modifier.as_ref().map(|modifier| {
let modifier = Ident::new(modifier, Span::call_site());
quote! {
let builder = #modifier(builder).expect(concat!("Failed to build class ", #class_name));
}
});

Ok(quote! {{
let class = ::ext_php_rs::builders::ClassBuilder::new(#class_name)
let builder = ::ext_php_rs::builders::ClassBuilder::new(#class_name)
#(#methods)*
#(#constants)*
#(#interfaces)*
// #(#properties)*
#parent
.object_override::<#ident>()
.build()
.object_override::<#ident>();
#class_modifier
let class = builder.build()
.expect(concat!("Unable to build class `", #class_name, "`"));

#meta.set_ce(class);
Expand Down

0 comments on commit 4468656

Please sign in to comment.