Skip to content

Commit 935a2d2

Browse files
NorbytusXenira
andauthored
feat(interface): add support for interfaces
* feat: Add interface bindings * feat: Add interface builders * feat(macro): Add macro to declare interface from trait * feat: Add tests * feat: Add missing things in interface builder * feat: Add methods, const registration for interface * feat: Add tests for interface registration * chore: Add internal function for interface attribute macros * feat: Add doc for interface macros and add test for expand * chore: CI things * feat: Change const registration for interface * feat: Rewrite attribute parse * refactor: Change parser function * fix: Add path to hashmap * fix: Fix constant registration for interface * chore: Add test with default value * chore: Delete unnecessary impl generation * feat: Define constructor * feat: Describe interface classes * feat: Separate interfaces from classes in module * feat: Add doc about interface in guide * chore: Delete unused interface.rs * chore: Clean from duplicated code * chore: Fix clippy and etc * chore: Remove expand test * test(interface): add macro expansion tests Refs: #533 * feat(interface): add doc comment support Refs: #533 * docs(interface): update lib docs Refs: #533 * test(interface): fix expansion test Refs: #533 * feat(interface): add support for renaming all consts/methods Refs: #533 * docs(macro): update lib docs Refs: #533 --------- Co-authored-by: Xenira <[email protected]>
1 parent 6f1e99e commit 935a2d2

File tree

26 files changed

+1380
-185
lines changed

26 files changed

+1380
-185
lines changed

allowed_bindings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ bind! {
118118
zend_register_double_constant,
119119
zend_register_ini_entries,
120120
zend_register_internal_enum,
121+
zend_register_internal_interface,
121122
zend_ini_entry_def,
122123
zend_register_internal_class_ex,
123124
zend_register_long_constant,

crates/macros/src/class.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use darling::util::Flag;
22
use darling::{FromAttributes, FromMeta, ToTokens};
33
use proc_macro2::TokenStream;
4-
use quote::quote;
4+
use quote::{TokenStreamExt, quote};
55
use syn::{Attribute, Expr, Fields, ItemStruct};
66

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

2929
#[derive(FromMeta, Debug)]
3030
pub struct ClassEntryAttribute {
31-
ce: syn::Expr,
32-
stub: String,
31+
pub ce: syn::Expr,
32+
pub stub: String,
33+
}
34+
35+
impl ToTokens for ClassEntryAttribute {
36+
fn to_tokens(&self, tokens: &mut TokenStream) {
37+
let ce = &self.ce;
38+
let stub = &self.stub;
39+
let token = quote! { (#ce, #stub) };
40+
tokens.append_all(token);
41+
}
3342
}
3443

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

153162
let extends = if let Some(extends) = extends {
154-
let ce = &extends.ce;
155-
let stub = &extends.stub;
156163
quote! {
157-
Some((#ce, #stub))
164+
Some(#extends)
158165
}
159166
} else {
160167
quote! { None }

crates/macros/src/function.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,40 @@ impl<'a> Function<'a> {
131131
format_ident!("_internal_{}", &self.ident)
132132
}
133133

134+
pub fn abstract_function_builder(&self) -> TokenStream {
135+
let name = &self.name;
136+
let (required, not_required) = self.args.split_args(self.optional.as_ref());
137+
138+
// `entry` impl
139+
let required_args = required
140+
.iter()
141+
.map(TypedArg::arg_builder)
142+
.collect::<Vec<_>>();
143+
let not_required_args = not_required
144+
.iter()
145+
.map(TypedArg::arg_builder)
146+
.collect::<Vec<_>>();
147+
148+
let returns = self.build_returns();
149+
let docs = if self.docs.is_empty() {
150+
quote! {}
151+
} else {
152+
let docs = &self.docs;
153+
quote! {
154+
.docs(&[#(#docs),*])
155+
}
156+
};
157+
158+
quote! {
159+
::ext_php_rs::builders::FunctionBuilder::new_abstract(#name)
160+
#(.arg(#required_args))*
161+
.not_required()
162+
#(.arg(#not_required_args))*
163+
#returns
164+
#docs
165+
}
166+
}
167+
134168
/// Generates the function builder for the function.
135169
pub fn function_builder(&self, call_type: CallType) -> TokenStream {
136170
let name = &self.name;

crates/macros/src/helpers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ pub fn get_docs(attrs: &[Attribute]) -> Result<Vec<String>> {
2424
})
2525
.collect::<Result<Vec<_>>>()
2626
}
27+
28+
pub trait CleanPhpAttr {
29+
fn clean_php(&mut self);
30+
}
31+
32+
impl CleanPhpAttr for Vec<Attribute> {
33+
fn clean_php(&mut self) {
34+
self.retain(|attr| !attr.path().is_ident("php"));
35+
}
36+
}

crates/macros/src/impl_.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ struct ParsedImpl<'a> {
125125
}
126126

127127
#[derive(Debug, Eq, Hash, PartialEq)]
128-
enum MethodModifier {
128+
pub enum MethodModifier {
129129
Abstract,
130130
Static,
131131
}
@@ -141,7 +141,7 @@ impl quote::ToTokens for MethodModifier {
141141
}
142142

143143
#[derive(Debug)]
144-
struct FnBuilder {
144+
pub struct FnBuilder {
145145
/// Tokens which represent the `FunctionBuilder` for this function.
146146
pub builder: TokenStream,
147147
/// The visibility of this method.
@@ -151,13 +151,13 @@ struct FnBuilder {
151151
}
152152

153153
#[derive(Debug)]
154-
struct Constant<'a> {
154+
pub struct Constant<'a> {
155155
/// Name of the constant in PHP land.
156-
name: String,
156+
pub name: String,
157157
/// Identifier of the constant in Rust land.
158-
ident: &'a syn::Ident,
158+
pub ident: &'a syn::Ident,
159159
/// Documentation for the constant.
160-
docs: Vec<String>,
160+
pub docs: Vec<String>,
161161
}
162162

163163
impl<'a> ParsedImpl<'a> {

0 commit comments

Comments
 (0)