profile-inspect 0.1.3

Analyze V8 CPU and heap profiles from Node.js/Chrome DevTools
Documentation
use super::FrameId;
use serde::Serialize;

/// Unique identifier for a stack
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
pub struct StackId(pub u32);

/// A call stack represented as a sequence of frames
///
/// Frames are ordered from root (caller) to leaf (callee).
/// The last frame is the function that was actually executing.
#[derive(Debug, Clone, Serialize)]
pub struct Stack {
    /// Unique identifier for this stack
    pub id: StackId,

    /// Frame IDs from root to leaf
    pub frames: Vec<FrameId>,
}

impl Stack {
    /// Create a new stack with the given ID and frames
    pub fn new(id: StackId, frames: Vec<FrameId>) -> Self {
        Self { id, frames }
    }

    /// Get the leaf (executing) frame, if any
    pub fn leaf(&self) -> Option<FrameId> {
        self.frames.last().copied()
    }

    /// Get the root frame, if any
    pub fn root(&self) -> Option<FrameId> {
        self.frames.first().copied()
    }

    /// Check if this stack contains the given frame
    pub fn contains(&self, frame: FrameId) -> bool {
        self.frames.contains(&frame)
    }

    /// Get the depth of the stack (number of frames)
    pub fn depth(&self) -> usize {
        self.frames.len()
    }

    /// Check if the stack is empty
    pub fn is_empty(&self) -> bool {
        self.frames.is_empty()
    }
}