Skip to main content

troupekit_trace/
category.rs

1use crate::error::TraceError;
2use std::fmt;
3use std::str::FromStr;
4
5/// One trace category; every trace point belongs to exactly one.
6/// `as_str` gives the snake-case name used in JSON lines and TOML, and
7/// `FromStr` parses it back. Later releases may add categories.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
9#[non_exhaustive]
10pub enum Trace {
11    Agents,
12    Models,
13    Tools,
14    Memory,
15    Knowledge,
16    Graphs,
17    Laws,
18    Evals,
19    Budget,
20    Store,
21    Routing,
22}
23
24const COUNT: usize = 11;
25const ALL_BITS: u32 = (1u32 << COUNT) - 1;
26
27const _: () = {
28    assert!(COUNT <= 32);
29    assert!(Trace::ALL.len() == COUNT);
30    let mut i = 0;
31    while i < COUNT {
32        assert!(Trace::ALL[i] as usize == i);
33        i += 1;
34    }
35};
36
37impl Trace {
38    /// Every category, in bit order.
39    /// Input: none. Output: an array of all categories.
40    /// `TraceSet::all()` holds exactly these.
41    pub const ALL: [Trace; COUNT] = [
42        Trace::Agents,
43        Trace::Models,
44        Trace::Tools,
45        Trace::Memory,
46        Trace::Knowledge,
47        Trace::Graphs,
48        Trace::Laws,
49        Trace::Evals,
50        Trace::Budget,
51        Trace::Store,
52        Trace::Routing,
53    ];
54
55    /// Returns the snake-case name of the category.
56    /// Input: the category. Output: a static name such as `"tools"`.
57    /// The same name appears in the `cat` key of every record.
58    pub const fn as_str(self) -> &'static str {
59        match self {
60            Trace::Agents => "agents",
61            Trace::Models => "models",
62            Trace::Tools => "tools",
63            Trace::Memory => "memory",
64            Trace::Knowledge => "knowledge",
65            Trace::Graphs => "graphs",
66            Trace::Laws => "laws",
67            Trace::Evals => "evals",
68            Trace::Budget => "budget",
69            Trace::Store => "store",
70            Trace::Routing => "routing",
71        }
72    }
73
74    pub(crate) const fn index(self) -> usize {
75        self as usize
76    }
77
78    pub(crate) const fn bit(self) -> u32 {
79        1u32 << self.index()
80    }
81}
82
83impl fmt::Display for Trace {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        f.write_str(self.as_str())
86    }
87}
88
89impl FromStr for Trace {
90    type Err = TraceError;
91
92    fn from_str(text: &str) -> Result<Trace, TraceError> {
93        Trace::ALL
94            .into_iter()
95            .find(|t| t.as_str() == text)
96            .ok_or_else(|| TraceError::unknown(text))
97    }
98}
99
100/// A set of trace categories stored as one bit per category.
101/// Input: categories through `with` or `FromIterator`. Output: `contains`.
102/// `bits` and `from_bits` round-trip it for storage without serde.
103#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
104pub struct TraceSet(u32);
105
106impl TraceSet {
107    /// Returns the set of every category.
108    /// Input: none. Output: the full set.
109    pub const fn all() -> TraceSet {
110        TraceSet(ALL_BITS)
111    }
112
113    /// Returns the empty set.
114    /// Input: none. Output: a set that contains nothing.
115    pub const fn none() -> TraceSet {
116        TraceSet(0)
117    }
118
119    /// Returns this set with `t` added.
120    /// Input: one category. Output: the larger set.
121    pub const fn with(self, t: Trace) -> TraceSet {
122        TraceSet(self.0 | t.bit())
123    }
124
125    /// Returns whether the set holds `t`.
126    /// Input: one category. Output: true when present.
127    pub const fn contains(self, t: Trace) -> bool {
128        self.0 & t.bit() != 0
129    }
130
131    /// Returns every category in either set.
132    /// Input: another set. Output: the union.
133    pub const fn union(self, other: TraceSet) -> TraceSet {
134        TraceSet(self.0 | other.0)
135    }
136
137    /// Returns whether the set holds no category.
138    /// Input: none. Output: true when empty.
139    pub const fn is_empty(self) -> bool {
140        self.0 == 0
141    }
142
143    /// Returns the raw bits, one per category in `Trace::ALL` order.
144    /// Input: none. Output: the bits; `from_bits` reads them back.
145    pub const fn bits(self) -> u32 {
146        self.0
147    }
148
149    /// Builds a set from raw bits, ignoring bits with no category.
150    /// Input: bits from `bits`. Output: the set.
151    pub const fn from_bits(bits: u32) -> TraceSet {
152        TraceSet(bits & ALL_BITS)
153    }
154
155    /// Iterates the categories in the set, in `Trace::ALL` order.
156    /// Input: none. Output: an iterator of categories.
157    pub fn iter(self) -> impl Iterator<Item = Trace> {
158        Trace::ALL.into_iter().filter(move |t| self.contains(*t))
159    }
160}
161
162impl FromIterator<Trace> for TraceSet {
163    fn from_iter<I: IntoIterator<Item = Trace>>(items: I) -> TraceSet {
164        items.into_iter().fold(TraceSet::none(), TraceSet::with)
165    }
166}