1use std::vec::Vec as StdVec;
8
9#[cfg(feature = "enum")]
10use crate::builders::EnumBuilder;
11use crate::{
12 builders::{ClassBuilder, FunctionBuilder},
13 constant::IntoConst,
14 flags::{ClassFlags, ConstantFlags, MethodFlags, PropertyFlags},
15 prelude::ModuleBuilder,
16};
17
18use ext_php_rs_introspection::abi::{Option, RString};
19pub use ext_php_rs_introspection::*;
20
21impl From<ModuleBuilder<'_>> for Module {
24 fn from(builder: ModuleBuilder) -> Self {
25 let functions = builder.functions;
26
27 #[allow(unused_mut)]
30 let mut classes = builder
31 .interfaces
32 .into_iter()
33 .chain(builder.classes)
34 .map(|c| c().into())
35 .collect::<StdVec<_>>();
36
37 #[cfg(feature = "closure")]
38 classes.push(Class::closure());
39
40 #[cfg(feature = "enum")]
41 let enums = builder
42 .enums
43 .into_iter()
44 .map(|e| e().into())
45 .collect::<StdVec<_>>();
46 #[cfg(not(feature = "enum"))]
47 let enums = StdVec::new();
48
49 Self {
50 name: builder.name.into(),
51 functions: functions
52 .into_iter()
53 .map(Function::from)
54 .collect::<StdVec<_>>()
55 .into(),
56 classes: classes.into(),
57 enums: enums.into(),
58 constants: builder
59 .constants
60 .into_iter()
61 .map(|(name, value, docs)| constant_from(name, &*value, docs))
62 .collect::<StdVec<_>>()
63 .into(),
64 }
65 }
66}
67
68impl From<FunctionBuilder<'_>> for Function {
69 fn from(val: FunctionBuilder<'_>) -> Self {
70 let ret_allow_null = val.ret_as_null;
71 Function {
72 name: val.name.into(),
73 docs: DocBlock(
74 val.docs
75 .iter()
76 .map(|d| (*d).into())
77 .collect::<StdVec<_>>()
78 .into(),
79 ),
80 ret: val
81 .retval
82 .map(|r| Retval {
83 ty: r,
84 nullable: r != DataType::Mixed && ret_allow_null,
85 })
86 .into(),
87 params: val
88 .args
89 .into_iter()
90 .map(Parameter::from)
91 .collect::<StdVec<_>>()
92 .into(),
93 }
94 }
95}
96
97impl From<ClassBuilder> for Class {
98 fn from(val: ClassBuilder) -> Self {
99 let is_interface =
100 ClassFlags::from_bits_retain(val.get_flags()).contains(ClassFlags::Interface);
101 Self {
102 name: val.name.into(),
103 docs: DocBlock(
104 val.docs
105 .iter()
106 .map(|doc| (*doc).into())
107 .collect::<StdVec<_>>()
108 .into(),
109 ),
110 extends: val.extends.map(|(_, stub)| stub.into()).into(),
111 implements: val
112 .interfaces
113 .into_iter()
114 .map(|(_, stub)| stub.into())
115 .collect::<StdVec<_>>()
116 .into(),
117 properties: val
118 .properties
119 .into_iter()
120 .chain(val.typed_properties)
121 .map(Property::from)
122 .collect::<StdVec<_>>()
123 .into(),
124 methods: val
125 .methods
126 .into_iter()
127 .map(|(builder, flags)| method_from(builder, flags))
128 .collect::<StdVec<_>>()
129 .into(),
130 constants: val
131 .constants
132 .into_iter()
133 .map(|(name, _, docs, stub, flags)| Constant {
134 name: name.into(),
135 value: Option::Some(stub.into()),
136 docs: docs.into(),
137 visibility: flags.into(),
138 })
139 .collect::<StdVec<_>>()
140 .into(),
141 is_interface,
142 }
143 }
144}
145
146#[cfg(feature = "enum")]
147impl From<EnumBuilder> for Enum {
148 fn from(val: EnumBuilder) -> Self {
149 Self {
150 name: val.name.into(),
151 docs: DocBlock(
152 val.docs
153 .iter()
154 .map(|d| (*d).into())
155 .collect::<StdVec<_>>()
156 .into(),
157 ),
158 cases: val
159 .cases
160 .into_iter()
161 .map(EnumCase::from)
162 .collect::<StdVec<_>>()
163 .into(),
164 backing_type: match val.datatype {
165 DataType::Long => Some("int".into()),
166 DataType::String => Some("string".into()),
167 _ => None,
168 }
169 .into(),
170 }
171 }
172}
173
174#[cfg(feature = "enum")]
175impl From<&'static crate::enum_::EnumCase> for EnumCase {
176 fn from(val: &'static crate::enum_::EnumCase) -> Self {
177 Self {
178 name: val.name.into(),
179 docs: DocBlock(
180 val.docs
181 .iter()
182 .map(|d| (*d).into())
183 .collect::<StdVec<_>>()
184 .into(),
185 ),
186 value: val
187 .discriminant
188 .as_ref()
189 .map(|v| match v {
190 crate::enum_::Discriminant::Int(i) => i.to_string().into(),
191 crate::enum_::Discriminant::String(s) => format!("'{s}'").into(),
192 })
193 .into(),
194 }
195 }
196}
197
198impl From<crate::builders::ClassProperty> for Property {
199 fn from(val: crate::builders::ClassProperty) -> Self {
200 let static_ = val.flags.contains(PropertyFlags::Static);
201 let vis = Visibility::from(val.flags);
202 let docs = val.docs.into();
203
204 Self {
205 name: val.name.into(),
206 docs,
207 ty: val.ty.into(),
208 vis,
209 static_,
210 nullable: val.nullable,
211 readonly: val.readonly,
212 default: val.default_stub.map(RString::from).into(),
213 }
214 }
215}
216
217fn method_from(builder: FunctionBuilder<'_>, flags: MethodFlags) -> Method {
218 let ret_allow_null = builder.ret_as_null;
219 Method {
220 name: builder.name.into(),
221 docs: DocBlock(
222 builder
223 .docs
224 .iter()
225 .map(|d| (*d).into())
226 .collect::<StdVec<_>>()
227 .into(),
228 ),
229 retval: builder
230 .retval
231 .map(|r| Retval {
232 ty: r,
233 nullable: r != DataType::Mixed && ret_allow_null,
234 })
235 .into(),
236 params: builder
237 .args
238 .into_iter()
239 .map(Into::into)
240 .collect::<StdVec<_>>()
241 .into(),
242 ty: flags.into(),
243 r#static: flags.contains(MethodFlags::Static),
244 visibility: flags.into(),
245 r#abstract: flags.contains(MethodFlags::Abstract),
246 }
247}
248
249impl From<MethodFlags> for MethodType {
250 fn from(value: MethodFlags) -> Self {
251 if value.contains(MethodFlags::IsConstructor) {
252 return Self::Constructor;
253 }
254 if value.contains(MethodFlags::Static) {
255 return Self::Static;
256 }
257
258 Self::Member
259 }
260}
261
262impl From<PropertyFlags> for Visibility {
263 fn from(value: PropertyFlags) -> Self {
264 if value.contains(PropertyFlags::Protected) {
265 return Self::Protected;
266 }
267 if value.contains(PropertyFlags::Private) {
268 return Self::Private;
269 }
270
271 Self::Public
272 }
273}
274
275impl From<MethodFlags> for Visibility {
276 fn from(value: MethodFlags) -> Self {
277 if value.contains(MethodFlags::Protected) {
278 return Self::Protected;
279 }
280
281 if value.contains(MethodFlags::Private) {
282 return Self::Private;
283 }
284
285 Self::Public
286 }
287}
288
289impl From<ConstantFlags> for Visibility {
290 fn from(value: ConstantFlags) -> Self {
291 if value.contains(ConstantFlags::Protected) {
292 return Self::Protected;
293 }
294 if value.contains(ConstantFlags::Private) {
295 return Self::Private;
296 }
297
298 Self::Public
299 }
300}
301
302fn constant_from(name: String, value: &dyn IntoConst, docs: DocComments) -> Constant {
303 Constant {
304 name: name.into(),
305 value: Option::Some(value.stub_value().into()),
306 docs: docs.into(),
307 visibility: Visibility::Public,
308 }
309}
310
311#[cfg(test)]
312mod tests {
313 #![cfg_attr(windows, feature(abi_vectorcall))]
314 use cfg_if::cfg_if;
315
316 use super::*;
317
318 use crate::{args::Arg, test::test_function};
319
320 #[test]
321 fn test_module_from() {
322 let builder = ModuleBuilder::new("test", "test_version")
323 .function(FunctionBuilder::new("test_function", test_function));
324 let module: Module = builder.into();
325 assert_eq!(module.name, "test".into());
326 assert_eq!(module.functions.len(), 1);
327 cfg_if! {
328 if #[cfg(feature = "closure")] {
329 assert_eq!(module.classes.len(), 1);
330 } else {
331 assert_eq!(module.classes.len(), 0);
332 }
333 }
334 assert_eq!(module.enums.len(), 0);
335 assert_eq!(module.constants.len(), 0);
336 }
337
338 #[test]
339 fn test_function_from() {
340 let builder = FunctionBuilder::new("test_function", test_function)
341 .docs(&["doc1", "doc2"])
342 .arg(Arg::new("foo", DataType::Long))
343 .returns(DataType::Bool, true, true);
344 let function: Function = builder.into();
345 assert_eq!(function.name, "test_function".into());
346 assert_eq!(function.docs.0.len(), 2);
347 assert_eq!(
348 function.params,
349 vec![Parameter {
350 name: "foo".into(),
351 ty: Option::Some(DataType::Long),
352 nullable: false,
353 variadic: false,
354 default: Option::None,
355 }]
356 .into()
357 );
358 assert_eq!(
359 function.ret,
360 Option::Some(Retval {
361 ty: DataType::Bool,
362 nullable: true,
363 })
364 );
365 }
366
367 #[test]
368 fn test_class_from() {
369 let builder = ClassBuilder::new("TestClass")
370 .docs(&["doc1", "doc2"])
371 .extends((|| todo!(), "BaseClass"))
372 .implements((|| todo!(), "Interface1"))
373 .implements((|| todo!(), "Interface2"))
374 .property(crate::builders::ClassProperty {
375 name: "prop1".into(),
376 flags: PropertyFlags::Public,
377 default: None,
378 docs: &["doc1"],
379 ty: None,
380 nullable: false,
381 readonly: false,
382 default_stub: None,
383 })
384 .method(
385 FunctionBuilder::new("test_function", test_function),
386 MethodFlags::Protected,
387 );
388 let class: Class = builder.into();
389
390 assert_eq!(class.name, "TestClass".into());
391 assert!(!class.is_interface);
392 assert_eq!(class.docs.0.len(), 2);
393 assert_eq!(class.extends, Option::Some("BaseClass".into()));
394 assert_eq!(
395 class.implements,
396 vec!["Interface1".into(), "Interface2".into()].into()
397 );
398 assert_eq!(class.properties.len(), 1);
399 assert_eq!(
400 class.properties[0],
401 Property {
402 name: "prop1".into(),
403 docs: DocBlock(vec!["doc1".into()].into()),
404 ty: Option::None,
405 vis: Visibility::Public,
406 static_: false,
407 nullable: false,
408 readonly: false,
409 default: Option::None,
410 }
411 );
412 assert_eq!(class.methods.len(), 1);
413 assert_eq!(
414 class.methods[0],
415 Method {
416 name: "test_function".into(),
417 docs: DocBlock(vec![].into()),
418 ty: MethodType::Member,
419 params: vec![].into(),
420 retval: Option::None,
421 r#static: false,
422 visibility: Visibility::Protected,
423 r#abstract: false
424 }
425 );
426 }
427
428 #[test]
429 fn test_interface_from() {
430 let class: Class = ClassBuilder::new("TestInterface")
431 .flags(ClassFlags::Interface)
432 .into();
433 assert!(class.is_interface);
434 }
435
436 #[test]
437 fn test_property_from() {
438 let property: Property = crate::builders::ClassProperty {
439 name: "test_property".into(),
440 flags: PropertyFlags::Protected,
441 default: None,
442 docs: &["doc1", "doc2"],
443 ty: Some(DataType::String),
444 nullable: true,
445 readonly: false,
446 default_stub: Some("null".into()),
447 }
448 .into();
449 assert_eq!(property.name, "test_property".into());
450 assert_eq!(property.docs.0.len(), 2);
451 assert_eq!(property.vis, Visibility::Protected);
452 assert!(!property.static_);
453 assert!(property.nullable);
454 assert_eq!(property.default, Option::Some("null".into()));
455 assert_eq!(property.ty, Option::Some(DataType::String));
456 }
457
458 #[test]
459 fn test_method_from() {
460 let builder = FunctionBuilder::new("test_method", test_function)
461 .docs(&["doc1", "doc2"])
462 .arg(Arg::new("foo", DataType::Long))
463 .returns(DataType::Bool, true, true);
464 let method = method_from(builder, MethodFlags::Static | MethodFlags::Protected);
465 assert_eq!(method.name, "test_method".into());
466 assert_eq!(method.docs.0.len(), 2);
467 assert_eq!(
468 method.params,
469 vec![Parameter {
470 name: "foo".into(),
471 ty: Option::Some(DataType::Long),
472 nullable: false,
473 variadic: false,
474 default: Option::None,
475 }]
476 .into()
477 );
478 assert_eq!(
479 method.retval,
480 Option::Some(Retval {
481 ty: DataType::Bool,
482 nullable: true,
483 })
484 );
485 assert!(method.r#static);
486 assert_eq!(method.visibility, Visibility::Protected);
487 assert_eq!(method.ty, MethodType::Static);
488 }
489
490 #[test]
491 fn test_ty_from() {
492 let r#static: MethodType = MethodFlags::Static.into();
493 assert_eq!(r#static, MethodType::Static);
494
495 let constructor: MethodType = MethodFlags::IsConstructor.into();
496 assert_eq!(constructor, MethodType::Constructor);
497
498 let member: MethodType = MethodFlags::Public.into();
499 assert_eq!(member, MethodType::Member);
500
501 let mixed: MethodType = (MethodFlags::Protected | MethodFlags::Static).into();
502 assert_eq!(mixed, MethodType::Static);
503
504 let both: MethodType = (MethodFlags::Static | MethodFlags::IsConstructor).into();
505 assert_eq!(both, MethodType::Constructor);
506
507 let empty: MethodType = MethodFlags::empty().into();
508 assert_eq!(empty, MethodType::Member);
509 }
510
511 #[test]
512 fn test_prop_visibility_from() {
513 let private: Visibility = PropertyFlags::Private.into();
514 assert_eq!(private, Visibility::Private);
515
516 let protected: Visibility = PropertyFlags::Protected.into();
517 assert_eq!(protected, Visibility::Protected);
518
519 let public: Visibility = PropertyFlags::Public.into();
520 assert_eq!(public, Visibility::Public);
521
522 let mixed: Visibility = (PropertyFlags::Protected | PropertyFlags::Static).into();
523 assert_eq!(mixed, Visibility::Protected);
524
525 let empty: Visibility = PropertyFlags::empty().into();
526 assert_eq!(empty, Visibility::Public);
527 }
528
529 #[test]
530 fn test_method_visibility_from() {
531 let private: Visibility = MethodFlags::Private.into();
532 assert_eq!(private, Visibility::Private);
533
534 let protected: Visibility = MethodFlags::Protected.into();
535 assert_eq!(protected, Visibility::Protected);
536
537 let public: Visibility = MethodFlags::Public.into();
538 assert_eq!(public, Visibility::Public);
539
540 let mixed: Visibility = (MethodFlags::Protected | MethodFlags::Static).into();
541 assert_eq!(mixed, Visibility::Protected);
542
543 let empty: Visibility = MethodFlags::empty().into();
544 assert_eq!(empty, Visibility::Public);
545 }
546}