@@ -8,7 +8,7 @@ use crate::{Abi, Const, Field, Fn, Parameter, Static, Struct, Type, Union};
8
8
///
9
9
/// Includes foreign functions/statics, type aliases, structs, unions, and constants.
10
10
#[ derive( Default , Clone , Debug ) ]
11
- pub struct FfiItems {
11
+ pub ( crate ) struct FfiItems {
12
12
aliases : Vec < Type > ,
13
13
structs : Vec < Struct > ,
14
14
unions : Vec < Union > ,
@@ -19,49 +19,55 @@ pub struct FfiItems {
19
19
20
20
impl FfiItems {
21
21
/// Creates a new blank FfiItems.
22
- pub fn new ( ) -> Self {
22
+ pub ( crate ) fn new ( ) -> Self {
23
23
Self :: default ( )
24
24
}
25
25
26
26
/// Return whether the type has parsed a struct with the given identifier.
27
- pub fn contains_struct ( & self , ident : & str ) -> bool {
27
+ #[ expect( unused) ]
28
+ pub ( crate ) fn contains_struct ( & self , ident : & str ) -> bool {
28
29
self . structs ( )
29
30
. iter ( )
30
31
. any ( |structure| structure. ident ( ) == ident)
31
32
}
32
33
33
34
/// Return whether the type has parsed a union with the given identifier.
34
- pub fn contains_union ( & self , ident : & str ) -> bool {
35
+ #[ expect( unused) ]
36
+ pub ( crate ) fn contains_union ( & self , ident : & str ) -> bool {
35
37
self . unions ( ) . iter ( ) . any ( |union| union. ident ( ) == ident)
36
38
}
37
39
38
40
/// Return a list of all type aliases found.
39
- pub fn aliases ( & self ) -> & Vec < Type > {
41
+ #[ expect( unused) ]
42
+ pub ( crate ) fn aliases ( & self ) -> & Vec < Type > {
40
43
& self . aliases
41
44
}
42
45
43
46
/// Return a list of all structs found.
44
- pub fn structs ( & self ) -> & Vec < Struct > {
47
+ pub ( crate ) fn structs ( & self ) -> & Vec < Struct > {
45
48
& self . structs
46
49
}
47
50
48
51
/// Return a list of all unions found.
49
- pub fn unions ( & self ) -> & Vec < Union > {
52
+ pub ( crate ) fn unions ( & self ) -> & Vec < Union > {
50
53
& self . unions
51
54
}
52
55
53
56
/// Return a list of all constants found.
54
- pub fn constants ( & self ) -> & Vec < Const > {
57
+ #[ expect( unused) ]
58
+ pub ( crate ) fn constants ( & self ) -> & Vec < Const > {
55
59
& self . constants
56
60
}
57
61
58
62
/// Return a list of all foreign functions found mapped by their ABI.
59
- pub fn foreign_functions ( & self ) -> & Vec < Fn > {
63
+ #[ expect( unused) ]
64
+ pub ( crate ) fn foreign_functions ( & self ) -> & Vec < Fn > {
60
65
& self . foreign_functions
61
66
}
62
67
63
68
/// Return a list of all foreign statics found mapped by their ABI.
64
- pub fn foreign_statics ( & self ) -> & Vec < Static > {
69
+ #[ expect( unused) ]
70
+ pub ( crate ) fn foreign_statics ( & self ) -> & Vec < Static > {
65
71
& self . foreign_statics
66
72
}
67
73
}
@@ -211,3 +217,75 @@ impl<'ast> Visit<'ast> for FfiItems {
211
217
}
212
218
}
213
219
}
220
+
221
+ #[ cfg( test) ]
222
+ mod tests {
223
+ use crate :: expand;
224
+
225
+ use super :: * ;
226
+
227
+ use syn:: visit:: Visit ;
228
+
229
+ #[ test]
230
+ fn test_extraction_ffi_items ( ) {
231
+ let expanded = expand ( "./tests/input/all_items.rs" ) . unwrap ( ) ;
232
+ let ast = syn:: parse_file ( & expanded) . unwrap ( ) ;
233
+
234
+ let mut ffi_items = FfiItems :: new ( ) ;
235
+ ffi_items. visit_file ( & ast) ;
236
+
237
+ assert_eq ! (
238
+ ffi_items
239
+ . aliases( )
240
+ . iter( )
241
+ . map( |a| a. ident( ) )
242
+ . collect:: <Vec <_>>( ) ,
243
+ [ "Foo" ]
244
+ ) ;
245
+
246
+ assert_eq ! (
247
+ ffi_items
248
+ . constants( )
249
+ . iter( )
250
+ . map( |a| a. ident( ) )
251
+ . collect:: <Vec <_>>( ) ,
252
+ [ "bar" ]
253
+ ) ;
254
+
255
+ assert_eq ! (
256
+ ffi_items
257
+ . foreign_functions( )
258
+ . iter( )
259
+ . map( |a| a. ident( ) )
260
+ . collect:: <Vec <_>>( ) ,
261
+ [ "malloc" ]
262
+ ) ;
263
+
264
+ assert_eq ! (
265
+ ffi_items
266
+ . foreign_statics( )
267
+ . iter( )
268
+ . map( |a| a. ident( ) )
269
+ . collect:: <Vec <_>>( ) ,
270
+ [ "baz" ]
271
+ ) ;
272
+
273
+ assert_eq ! (
274
+ ffi_items
275
+ . structs( )
276
+ . iter( )
277
+ . map( |a| a. ident( ) )
278
+ . collect:: <Vec <_>>( ) ,
279
+ [ "Array" ]
280
+ ) ;
281
+
282
+ assert_eq ! (
283
+ ffi_items
284
+ . unions( )
285
+ . iter( )
286
+ . map( |a| a. ident( ) )
287
+ . collect:: <Vec <_>>( ) ,
288
+ [ "Word" ]
289
+ ) ;
290
+ }
291
+ }
0 commit comments