Skip to content

Commit 2683528

Browse files
committed
ctest: Add extraction of relevant types.
1 parent 5fb5060 commit 2683528

File tree

5 files changed

+92
-80
lines changed

5 files changed

+92
-80
lines changed

ctest-next/src/ast/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ impl From<&str> for Abi {
4242
/// This is not an exhaustive list and only contains variants directly useful
4343
/// for our purposes.
4444
#[derive(Debug, Clone)]
45-
pub enum Item {
45+
#[expect(unused)]
46+
pub(crate) enum Item {
4647
/// Represents a constant defined in Rust.
4748
Const(Const),
4849
/// Represents a function defined in Rust.

ctest-next/src/ffi_items.rs

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{Abi, Const, Field, Fn, Parameter, Static, Struct, Type, Union};
88
///
99
/// Includes foreign functions/statics, type aliases, structs, unions, and constants.
1010
#[derive(Default, Clone, Debug)]
11-
pub struct FfiItems {
11+
pub(crate) struct FfiItems {
1212
aliases: Vec<Type>,
1313
structs: Vec<Struct>,
1414
unions: Vec<Union>,
@@ -19,49 +19,55 @@ pub struct FfiItems {
1919

2020
impl FfiItems {
2121
/// Creates a new blank FfiItems.
22-
pub fn new() -> Self {
22+
pub(crate) fn new() -> Self {
2323
Self::default()
2424
}
2525

2626
/// 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 {
2829
self.structs()
2930
.iter()
3031
.any(|structure| structure.ident() == ident)
3132
}
3233

3334
/// 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 {
3537
self.unions().iter().any(|union| union.ident() == ident)
3638
}
3739

3840
/// 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> {
4043
&self.aliases
4144
}
4245

4346
/// Return a list of all structs found.
44-
pub fn structs(&self) -> &Vec<Struct> {
47+
pub(crate) fn structs(&self) -> &Vec<Struct> {
4548
&self.structs
4649
}
4750

4851
/// Return a list of all unions found.
49-
pub fn unions(&self) -> &Vec<Union> {
52+
pub(crate) fn unions(&self) -> &Vec<Union> {
5053
&self.unions
5154
}
5255

5356
/// 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> {
5559
&self.constants
5660
}
5761

5862
/// 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> {
6065
&self.foreign_functions
6166
}
6267

6368
/// 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> {
6571
&self.foreign_statics
6672
}
6773
}
@@ -211,3 +217,75 @@ impl<'ast> Visit<'ast> for FfiItems {
211217
}
212218
}
213219
}
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+
}

ctest-next/src/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::Path;
22

33
use syn::visit::Visit;
44

5-
use crate::{expand, FfiItems, Result};
5+
use crate::{expand, ffi_items::FfiItems, Result};
66

77
/// A builder used to generate a test suite.
88
#[non_exhaustive]

ctest-next/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ mod ffi_items;
1313
mod generator;
1414
mod macro_expansion;
1515

16-
pub use ast::{Abi, Const, Field, Fn, Item, Parameter, Static, Struct, Type, Union};
17-
pub use ffi_items::FfiItems;
16+
pub use ast::{Abi, Const, Field, Fn, Parameter, Static, Struct, Type, Union};
1817
pub use generator::TestGenerator;
1918
pub use macro_expansion::expand;
2019

ctest-next/tests/extraction.rs

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)