Skip to main content

traverse_runtime/trace/
store.rs

1//! In-memory store for public and private trace entries.
2
3use super::{private::PrivateTraceEntry, public::PublicTraceEntry};
4use std::collections::HashMap;
5
6/// In-memory trace store keyed by trace UUID string.
7///
8/// Each entry holds a [`PublicTraceEntry`] and an optional [`PrivateTraceEntry`].
9#[derive(Debug, Default)]
10pub struct TraceStore {
11    entries: HashMap<String, (PublicTraceEntry, Option<PrivateTraceEntry>)>,
12}
13
14impl TraceStore {
15    /// Creates an empty [`TraceStore`].
16    #[must_use]
17    pub fn new() -> Self {
18        Self {
19            entries: HashMap::new(),
20        }
21    }
22
23    /// Inserts a public entry and an optional private entry into the store.
24    pub fn insert(&mut self, public: PublicTraceEntry, private: Option<PrivateTraceEntry>) {
25        self.entries.insert(public.id.clone(), (public, private));
26    }
27
28    /// Returns all public entries, optionally filtered to a specific `capability_id`.
29    #[must_use]
30    pub fn list_public(&self, capability_id: Option<&str>) -> Vec<&PublicTraceEntry> {
31        self.entries
32            .values()
33            .filter(|(pub_entry, _)| capability_id.is_none_or(|id| pub_entry.capability_id == id))
34            .map(|(pub_entry, _)| pub_entry)
35            .collect()
36    }
37
38    /// Looks up a trace by its UUID string.
39    ///
40    /// Returns `None` when the `trace_id` is not present in the store.
41    #[must_use]
42    pub fn get(&self, trace_id: &str) -> Option<(&PublicTraceEntry, Option<&PrivateTraceEntry>)> {
43        self.entries
44            .get(trace_id)
45            .map(|(pub_entry, priv_entry)| (pub_entry, priv_entry.as_ref()))
46    }
47}