use ratatui::style::Color;
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "serialization",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct FlameNode {
pub(crate) label: String,
pub(crate) value: u64,
pub(crate) color: Color,
pub(crate) children: Vec<FlameNode>,
}
impl FlameNode {
pub fn new(label: impl Into<String>, value: u64) -> Self {
Self {
label: label.into(),
value,
color: Color::Cyan,
children: Vec::new(),
}
}
pub fn with_color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn with_child(mut self, child: FlameNode) -> Self {
self.children.push(child);
self
}
pub fn with_children(mut self, children: Vec<FlameNode>) -> Self {
self.children = children;
self
}
pub fn label(&self) -> &str {
&self.label
}
pub fn value(&self) -> u64 {
self.value
}
pub fn color(&self) -> Color {
self.color
}
pub fn children(&self) -> &[FlameNode] {
&self.children
}
pub fn total_value(&self) -> u64 {
self.value
}
pub fn self_value(&self) -> u64 {
let children_total: u64 = self.children.iter().map(|c| c.total_value()).sum();
self.value.saturating_sub(children_total)
}
pub fn has_children(&self) -> bool {
!self.children.is_empty()
}
}