Skip to main content

hindsight/analyzer/
tree.rs

1//! Execution tree builder
2//!
3//! Transforms flat JSONL nodes into hierarchical tree structure for visualization.
4
5use crate::parser::ExecutionNode;
6use serde::{Deserialize, Serialize};
7use std::rc::Rc;
8
9/// A node in the execution tree with children
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct TreeNode {
12    /// The execution node data (wrapped in Rc to avoid expensive cloning)
13    #[serde(serialize_with = "serialize_rc", deserialize_with = "deserialize_rc")]
14    pub node: Rc<ExecutionNode>,
15
16    /// Child nodes
17    pub children: Vec<TreeNode>,
18
19    /// Depth in the tree (0 = root)
20    pub depth: usize,
21}
22
23// Custom serialization for Rc<ExecutionNode>
24fn serialize_rc<S>(node: &Rc<ExecutionNode>, serializer: S) -> Result<S::Ok, S::Error>
25where
26    S: serde::Serializer,
27{
28    node.as_ref().serialize(serializer)
29}
30
31// Custom deserialization for Rc<ExecutionNode>
32fn deserialize_rc<'de, D>(deserializer: D) -> Result<Rc<ExecutionNode>, D::Error>
33where
34    D: serde::Deserializer<'de>,
35{
36    ExecutionNode::deserialize(deserializer).map(Rc::new)
37}
38
39impl TreeNode {
40    // TreeNode methods can be added here as needed
41}