Skip to content

Feat/interface impl #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
27d96cd
feat: Add interface bindings
Norbytus Jul 10, 2025
9ed4ad8
feat: Add interface builders
Norbytus Jul 13, 2025
eabefe9
feat(macro): Add macro to declare interface from trait
Norbytus Jul 13, 2025
263a086
feat: Add tests
Norbytus Jul 13, 2025
aa2290a
feat: Add missing things in interface builder
Norbytus Jul 20, 2025
cec3b92
feat: Add methods, const registration for interface
Norbytus Jul 20, 2025
d5443b5
feat: Add tests for interface registration
Norbytus Jul 20, 2025
458d019
chore: Add internal function for interface attribute macros
Norbytus Jul 21, 2025
2f85f73
feat: Add doc for interface macros and add test for expand
Norbytus Jul 21, 2025
aba640f
chore: CI things
Norbytus Jul 21, 2025
f99b28a
feat: Change const registration for interface
Norbytus Jul 21, 2025
75ce70e
feat: Rewrite attribute parse
Norbytus Jul 22, 2025
04fdc10
refactor: Change parser function
Norbytus Aug 4, 2025
9b13b24
fix: Add path to hashmap
Norbytus Aug 4, 2025
19a902d
fix: Fix constant registration for interface
Norbytus Aug 4, 2025
9c45dd8
chore: Add test with default value
Norbytus Aug 4, 2025
d354e48
chore: Delete unnecessary impl generation
Norbytus Aug 4, 2025
e67f974
feat: Define constructor
Norbytus Aug 4, 2025
b7d2273
feat: Describe interface classes
Norbytus Aug 4, 2025
473f628
feat: Separate interfaces from classes in module
Norbytus Aug 4, 2025
855b49a
feat: Add doc about interface in guide
Norbytus Aug 10, 2025
500a7f7
chore: Delete unused interface.rs
Norbytus Aug 10, 2025
34580c9
chore: Clean from duplicated code
Norbytus Aug 10, 2025
fa449e5
chore: Fix clippy and etc
Norbytus Aug 10, 2025
c0dcacd
chore: Remove expand test
Norbytus Aug 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions allowed_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ bind! {
zend_register_double_constant,
zend_register_ini_entries,
zend_register_internal_enum,
zend_register_internal_interface,
zend_ini_entry_def,
zend_register_internal_class_ex,
zend_register_long_constant,
Expand Down
19 changes: 13 additions & 6 deletions crates/macros/src/class.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use darling::util::Flag;
use darling::{FromAttributes, FromMeta, ToTokens};
use proc_macro2::TokenStream;
use quote::quote;
use quote::{quote, TokenStreamExt};
use syn::{Attribute, Expr, Fields, ItemStruct};

use crate::helpers::get_docs;
Expand All @@ -28,8 +28,17 @@ pub struct StructAttributes {

#[derive(FromMeta, Debug)]
pub struct ClassEntryAttribute {
ce: syn::Expr,
stub: String,
pub ce: syn::Expr,
Copy link
Contributor Author

@Norbytus Norbytus Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make pub cause not see necessary to duplicate

pub stub: String,
}

impl ToTokens for ClassEntryAttribute {
fn to_tokens(&self, tokens: &mut TokenStream) {
let ce = &self.ce;
let stub = &self.stub;
let token = quote! { (#ce, #stub) };
tokens.append_all(token);
}
}

pub fn parser(mut input: ItemStruct) -> Result<TokenStream> {
Expand Down Expand Up @@ -151,10 +160,8 @@ fn generate_registered_class_impl(
};

let extends = if let Some(extends) = extends {
let ce = &extends.ce;
let stub = &extends.stub;
quote! {
Some((#ce, #stub))
Some(#extends)
}
} else {
quote! { None }
Expand Down
34 changes: 34 additions & 0 deletions crates/macros/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,40 @@ impl<'a> Function<'a> {
format_ident!("_internal_{}", &self.ident)
}

pub fn abstract_function_builder(&self) -> TokenStream {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make new builder, cause for interface we don't need a CallType

let name = &self.name;
let (required, not_required) = self.args.split_args(self.optional.as_ref());

// `entry` impl
let required_args = required
.iter()
.map(TypedArg::arg_builder)
.collect::<Vec<_>>();
let not_required_args = not_required
.iter()
.map(TypedArg::arg_builder)
.collect::<Vec<_>>();

let returns = self.build_returns();
let docs = if self.docs.is_empty() {
quote! {}
} else {
let docs = &self.docs;
quote! {
.docs(&[#(#docs),*])
}
};

quote! {
::ext_php_rs::builders::FunctionBuilder::new_abstract(#name)
#(.arg(#required_args))*
.not_required()
#(.arg(#not_required_args))*
#returns
#docs
}
}

/// Generates the function builder for the function.
pub fn function_builder(&self, call_type: CallType) -> TokenStream {
let name = &self.name;
Expand Down
10 changes: 10 additions & 0 deletions crates/macros/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ pub fn get_docs(attrs: &[Attribute]) -> Result<Vec<String>> {
})
.collect::<Result<Vec<_>>>()
}

pub trait CleanPhpAttr {
fn clean_php(&mut self);
}

impl CleanPhpAttr for Vec<Attribute> {
fn clean_php(&mut self) {
self.retain(|attr| !attr.path().is_ident("php"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tired write retain for php everywhere))

}
}
12 changes: 6 additions & 6 deletions crates/macros/src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct ParsedImpl<'a> {
}

#[derive(Debug, Eq, Hash, PartialEq)]
enum MethodModifier {
pub enum MethodModifier {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make public to not duplicate

Abstract,
Static,
}
Expand All @@ -141,7 +141,7 @@ impl quote::ToTokens for MethodModifier {
}

#[derive(Debug)]
struct FnBuilder {
pub struct FnBuilder {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make public to not duplicate

/// Tokens which represent the `FunctionBuilder` for this function.
pub builder: TokenStream,
/// The visibility of this method.
Expand All @@ -151,13 +151,13 @@ struct FnBuilder {
}

#[derive(Debug)]
struct Constant<'a> {
pub struct Constant<'a> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make public to not duplicate

/// Name of the constant in PHP land.
name: String,
pub name: String,
/// Identifier of the constant in Rust land.
ident: &'a syn::Ident,
pub ident: &'a syn::Ident,
/// Documentation for the constant.
docs: Vec<String>,
pub docs: Vec<String>,
}

impl<'a> ParsedImpl<'a> {
Expand Down
Loading
Loading