Skip to content

Commit 6869625

Browse files
committed
feat(stubs)!: add stubs for RustClosure
Fixes: #373 BREAKING CHANGE: New field `variadic` added to `Parameter` struct.
1 parent 25f508f commit 6869625

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
@@ -81,19 +81,25 @@ pub struct Module {
8181
impl From<ModuleBuilder<'_>> for Module {
8282
fn from(builder: ModuleBuilder) -> Self {
8383
let functions = builder.functions;
84+
85+
#[allow(unused_mut)]
86+
let mut classes = builder
87+
.classes
88+
.into_iter()
89+
.map(|c| c().into())
90+
.collect::<StdVec<_>>();
91+
92+
#[cfg(feature = "closure")]
93+
classes.push(Class::closure());
94+
8495
Self {
8596
name: builder.name.into(),
8697
functions: functions
8798
.into_iter()
8899
.map(Function::from)
89100
.collect::<StdVec<_>>()
90101
.into(),
91-
classes: builder
92-
.classes
93-
.into_iter()
94-
.map(|c| c().into())
95-
.collect::<StdVec<_>>()
96-
.into(),
102+
classes: classes.into(),
97103
constants: builder
98104
.constants
99105
.into_iter()
@@ -163,6 +169,8 @@ pub struct Parameter {
163169
pub ty: Option<DataType>,
164170
/// Whether the parameter is nullable.
165171
pub nullable: bool,
172+
/// Whether the parameter is variadic.
173+
pub variadic: bool,
166174
/// Default value of the parameter.
167175
pub default: Option<RString>,
168176
}
@@ -187,6 +195,43 @@ pub struct Class {
187195
pub constants: Vec<Constant>,
188196
}
189197

198+
#[cfg(feature = "closure")]
199+
impl Class {
200+
/// Creates a new class representing a Rust closure used for generating
201+
/// the stubs if the `closure` feature is enabled.
202+
#[must_use]
203+
pub fn closure() -> Self {
204+
Self {
205+
name: "RustClosure".into(),
206+
docs: DocBlock(StdVec::new().into()),
207+
extends: Option::None,
208+
implements: StdVec::new().into(),
209+
properties: StdVec::new().into(),
210+
methods: vec![Method {
211+
name: "__invoke".into(),
212+
docs: DocBlock(StdVec::new().into()),
213+
ty: MethodType::Member,
214+
params: vec![Parameter {
215+
name: "args".into(),
216+
ty: Option::Some(DataType::Mixed),
217+
nullable: false,
218+
variadic: true,
219+
default: Option::None,
220+
}]
221+
.into(),
222+
retval: Option::Some(Retval {
223+
ty: DataType::Mixed,
224+
nullable: false,
225+
}),
226+
r#static: false,
227+
visibility: Visibility::Public,
228+
}]
229+
.into(),
230+
constants: StdVec::new().into(),
231+
}
232+
}
233+
}
234+
190235
impl From<ClassBuilder> for Class {
191236
fn from(val: ClassBuilder) -> Self {
192237
Self {
@@ -518,6 +563,8 @@ impl From<(String, Box<dyn IntoConst + Send>, DocComments)> for Constant {
518563
#[cfg(test)]
519564
mod tests {
520565
#![cfg_attr(windows, feature(abi_vectorcall))]
566+
use cfg_if::cfg_if;
567+
521568
use super::*;
522569

523570
use crate::{args::Arg, test::test_function};
@@ -554,7 +601,13 @@ mod tests {
554601
let module: Module = builder.into();
555602
assert_eq!(module.name, "test".into());
556603
assert_eq!(module.functions.len(), 1);
557-
assert_eq!(module.classes.len(), 0);
604+
cfg_if! {
605+
if #[cfg(feature = "closure")] {
606+
assert_eq!(module.classes.len(), 1);
607+
} else {
608+
assert_eq!(module.classes.len(), 0);
609+
}
610+
}
558611
assert_eq!(module.constants.len(), 0);
559612
}
560613

@@ -573,6 +626,7 @@ mod tests {
573626
name: "foo".into(),
574627
ty: Option::Some(DataType::Long),
575628
nullable: false,
629+
variadic: false,
576630
default: Option::None,
577631
}]
578632
.into()
@@ -662,6 +716,7 @@ mod tests {
662716
name: "foo".into(),
663717
ty: Option::Some(DataType::Long),
664718
nullable: false,
719+
variadic: false,
665720
default: Option::None,
666721
}]
667722
.into()

src/describe/stub.rs

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

170+
if self.variadic {
171+
write!(buf, "...")?;
172+
}
173+
170174
write!(buf, "${}", self.name)
171175
}
172176
}

0 commit comments

Comments
 (0)