puffin/
trace_helper.rs

1use crate::agent::AgentName;
2use crate::protocol::ProtocolTypes;
3use crate::trace::Trace;
4
5pub trait TraceHelper<A, PT>
6where
7    PT: ProtocolTypes,
8{
9    fn build_named_trace(self) -> (&'static str, Trace<PT>);
10    fn build_trace(self) -> Trace<PT>;
11    fn fn_name(&self) -> &'static str;
12}
13
14impl<PT, F> TraceHelper<(AgentName, AgentName), PT> for F
15where
16    F: Fn(AgentName, AgentName) -> Trace<PT>,
17    PT: ProtocolTypes,
18{
19    fn build_named_trace(self) -> (&'static str, Trace<PT>) {
20        (self.fn_name(), self.build_trace())
21    }
22
23    fn build_trace(self) -> Trace<PT> {
24        let agent_a = AgentName::first();
25        let agent_b = agent_a.next();
26        (self)(agent_a, agent_b)
27    }
28
29    fn fn_name(&self) -> &'static str {
30        std::any::type_name::<F>()
31    }
32}
33
34impl<PT, F> TraceHelper<AgentName, PT> for F
35where
36    F: Fn(AgentName) -> Trace<PT>,
37    PT: ProtocolTypes,
38{
39    fn build_named_trace(self) -> (&'static str, Trace<PT>) {
40        (self.fn_name(), self.build_trace())
41    }
42
43    fn build_trace(self) -> Trace<PT> {
44        let agent_a = AgentName::first();
45
46        (self)(agent_a)
47    }
48
49    fn fn_name(&self) -> &'static str {
50        std::any::type_name::<F>()
51    }
52}
53
54/// Builds a seed corpus as a `Vec<(Trace, &'static str)>` from a list of
55/// `seed_fn: condition` pairs. Each seed function must implement
56/// [`TraceHelper`] (blanket-implemented for the
57/// usual `Fn(AgentName) -> Trace` / `Fn(AgentName, AgentName) -> Trace` seed
58/// signatures). A seed is included only when its condition evaluates to `true`,
59/// which lets callers gate seeds on PUT capabilities. Protocol-agnostic — usable
60/// from any crate as `puffin::corpus!`.
61#[macro_export]
62macro_rules! corpus {
63    () => {
64        vec![]
65    };
66
67    ( $( $func:ident : $cond:expr ),* $(,)? ) => {
68        {
69            use $crate::trace_helper::TraceHelper;
70            let mut corpus = vec![];
71
72            $(
73                if $cond {
74                    corpus.push(($func.build_trace(), $func.fn_name()));
75                }
76            )*
77
78            corpus
79        }
80    };
81}