troupekit_trace/
category.rs1use crate::error::TraceError;
2use std::fmt;
3use std::str::FromStr;
4
5#[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 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 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#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
104pub struct TraceSet(u32);
105
106impl TraceSet {
107 pub const fn all() -> TraceSet {
110 TraceSet(ALL_BITS)
111 }
112
113 pub const fn none() -> TraceSet {
116 TraceSet(0)
117 }
118
119 pub const fn with(self, t: Trace) -> TraceSet {
122 TraceSet(self.0 | t.bit())
123 }
124
125 pub const fn contains(self, t: Trace) -> bool {
128 self.0 & t.bit() != 0
129 }
130
131 pub const fn union(self, other: TraceSet) -> TraceSet {
134 TraceSet(self.0 | other.0)
135 }
136
137 pub const fn is_empty(self) -> bool {
140 self.0 == 0
141 }
142
143 pub const fn bits(self) -> u32 {
146 self.0
147 }
148
149 pub const fn from_bits(bits: u32) -> TraceSet {
152 TraceSet(bits & ALL_BITS)
153 }
154
155 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}