-
Notifications
You must be signed in to change notification settings - Fork 83
/
visitor.rs
50 lines (44 loc) · 1.38 KB
/
visitor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::alloc::prelude::*;
use crate::alloc::Vec;
use crate::compile::meta;
use crate::compile::{CompileVisitor, ItemBuf, MetaError, MetaRef};
use crate::Hash;
/// Attribute to collect.
#[derive(Debug, Clone, Copy)]
pub(super) enum Attribute {
/// Do not collect any functions.
None,
/// Collect `#[test]` functions.
Test,
/// Collect `#[bench]` functions.
Bench,
}
/// A compile visitor that collects functions with a specific attribute.
pub(super) struct FunctionVisitor {
attribute: Attribute,
functions: Vec<(Hash, ItemBuf)>,
}
impl FunctionVisitor {
pub(super) fn new(kind: Attribute) -> Self {
Self {
attribute: kind,
functions: Vec::default(),
}
}
/// Convert visitor into test functions.
pub(super) fn into_functions(self) -> Vec<(Hash, ItemBuf)> {
self.functions
}
}
impl CompileVisitor for FunctionVisitor {
fn register_meta(&mut self, meta: MetaRef<'_>) -> Result<(), MetaError> {
let type_hash = match (self.attribute, &meta.kind) {
(Attribute::Test, meta::Kind::Function { is_test, .. }) if *is_test => meta.hash,
(Attribute::Bench, meta::Kind::Function { is_bench, .. }) if *is_bench => meta.hash,
_ => return Ok(()),
};
self.functions
.try_push((type_hash, meta.item.try_to_owned()?))?;
Ok(())
}
}