Skip to content

Commit 31c9d99

Browse files
committed
feat(stubs): add stubs for RustClosure
Fixes: #373
1 parent 9925a47 commit 31c9d99

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

src/args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ impl From<Arg<'_>> for Parameter {
203203
name: val.name.into(),
204204
ty: Some(val.r#type).into(),
205205
nullable: val.allow_null,
206+
variadic: val.variadic,
206207
default: val.default_value.map(abi::RString::from).into(),
207208
}
208209
}

src/describe/mod.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,25 @@ pub struct Module {
7676
impl From<ModuleBuilder<'_>> for Module {
7777
fn from(builder: ModuleBuilder) -> Self {
7878
let functions = builder.functions;
79+
80+
#[allow(unused_mut)]
81+
let mut classes = builder
82+
.classes
83+
.into_iter()
84+
.map(|c| c().into())
85+
.collect::<StdVec<_>>();
86+
87+
#[cfg(feature = "closure")]
88+
classes.push(Class::closure());
89+
7990
Self {
8091
name: builder.name.into(),
8192
functions: functions
8293
.into_iter()
8394
.map(Function::from)
8495
.collect::<StdVec<_>>()
8596
.into(),
86-
classes: builder
87-
.classes
88-
.into_iter()
89-
.map(|c| c().into())
90-
.collect::<StdVec<_>>()
91-
.into(),
97+
classes: classes.into(),
9298
constants: builder
9399
.constants
94100
.into_iter()
@@ -151,6 +157,8 @@ pub struct Parameter {
151157
pub ty: Option<DataType>,
152158
/// Whether the parameter is nullable.
153159
pub nullable: bool,
160+
/// Whether the parameter is variadic.
161+
pub variadic: bool,
154162
/// Default value of the parameter.
155163
pub default: Option<RString>,
156164
}
@@ -175,6 +183,43 @@ pub struct Class {
175183
pub constants: Vec<Constant>,
176184
}
177185

186+
#[cfg(feature = "closure")]
187+
impl Class {
188+
/// Creates a new class representing a Rust closure used for generating
189+
/// the stubs if the `closure` feature is enabled.
190+
#[must_use]
191+
pub fn closure() -> Self {
192+
Self {
193+
name: "RustClosure".into(),
194+
docs: DocBlock(StdVec::new().into()),
195+
extends: Option::None,
196+
implements: StdVec::new().into(),
197+
properties: StdVec::new().into(),
198+
methods: vec![Method {
199+
name: "__invoke".into(),
200+
docs: DocBlock(StdVec::new().into()),
201+
ty: MethodType::Member,
202+
params: vec![Parameter {
203+
name: "args".into(),
204+
ty: Option::Some(DataType::Mixed),
205+
nullable: false,
206+
variadic: true,
207+
default: Option::None,
208+
}]
209+
.into(),
210+
retval: Option::Some(Retval {
211+
ty: DataType::Mixed,
212+
nullable: false,
213+
}),
214+
r#static: false,
215+
visibility: Visibility::Public,
216+
}]
217+
.into(),
218+
constants: StdVec::new().into(),
219+
}
220+
}
221+
}
222+
178223
impl From<ClassBuilder> for Class {
179224
fn from(val: ClassBuilder) -> Self {
180225
Self {
@@ -426,6 +471,8 @@ impl From<(String, Box<dyn IntoConst + Send>, DocComments)> for Constant {
426471
#[cfg(test)]
427472
mod tests {
428473
#![cfg_attr(windows, feature(abi_vectorcall))]
474+
use cfg_if::cfg_if;
475+
429476
use super::*;
430477

431478
use crate::{args::Arg, test::test_function};
@@ -460,7 +507,13 @@ mod tests {
460507
let module: Module = builder.into();
461508
assert_eq!(module.name, "test".into());
462509
assert_eq!(module.functions.len(), 1);
463-
assert_eq!(module.classes.len(), 0);
510+
cfg_if! {
511+
if #[cfg(feature = "closure")] {
512+
assert_eq!(module.classes.len(), 1);
513+
} else {
514+
assert_eq!(module.classes.len(), 0);
515+
}
516+
}
464517
assert_eq!(module.constants.len(), 0);
465518
}
466519

@@ -479,6 +532,7 @@ mod tests {
479532
name: "foo".into(),
480533
ty: Option::Some(DataType::Long),
481534
nullable: false,
535+
variadic: false,
482536
default: Option::None,
483537
}]
484538
.into()
@@ -568,6 +622,7 @@ mod tests {
568622
name: "foo".into(),
569623
ty: Option::Some(DataType::Long),
570624
nullable: false,
625+
variadic: false,
571626
default: Option::None,
572627
}]
573628
.into()

src/describe/stub.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ impl ToStub for Parameter {
154154
write!(buf, " ")?;
155155
}
156156

157+
if self.variadic {
158+
write!(buf, "...")?;
159+
}
160+
157161
write!(buf, "${}", self.name)
158162
}
159163
}

0 commit comments

Comments
 (0)