leptos_column_browser/topology/
node.rs1use serde::{Deserialize, Serialize};
2
3use crate::topology::node_id::NodeId;
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum NodeKind {
9 Container,
11 Leaf,
13}
14
15impl NodeKind {
16 pub fn is_leaf(&self) -> bool {
18 matches!(self, Self::Leaf)
19 }
20
21 pub fn is_container(&self) -> bool {
23 matches!(self, Self::Container)
24 }
25}
26
27#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct Node {
33 pub id: NodeId,
35 pub label: String,
37 pub node_type: String,
39 pub node_kind: NodeKind,
41 pub children: Vec<Node>,
43}
44
45impl Node {
46 pub fn container(id: NodeId, label: String, node_type: &str) -> Self {
48 Self {
49 id,
50 label,
51 node_type: node_type.to_owned(),
52 node_kind: NodeKind::Container,
53 children: Vec::new(),
54 }
55 }
56
57 pub fn leaf(id: NodeId, label: String, node_type: &str) -> Self {
59 Self {
60 id,
61 label,
62 node_type: node_type.to_owned(),
63 node_kind: NodeKind::Leaf,
64 children: Vec::new(),
65 }
66 }
67
68 pub fn root(id: NodeId, label: String, node_type: &str) -> Self {
70 Self::container(id, label, node_type)
71 }
72
73 #[must_use]
75 pub fn with_child(mut self, child: Node) -> Self {
76 self.children.push(child);
77 self
78 }
79
80 #[must_use]
82 pub fn with_children(mut self, children: Vec<Node>) -> Self {
83 self.children.extend(children);
84 self
85 }
86
87 pub fn count(&self) -> usize {
89 1 + self.children.iter().map(Node::count).sum::<usize>()
90 }
91
92 pub fn leaf_nodes(&self) -> Vec<&Node> {
94 if self.node_kind.is_leaf() {
95 return vec![self];
96 }
97 self.children.iter().flat_map(Node::leaf_nodes).collect()
98 }
99
100 pub fn find(&self, id: &NodeId) -> Option<&Node> {
102 if &self.id == id {
103 return Some(self);
104 }
105 self.children.iter().find_map(|c| c.find(id))
106 }
107
108 pub fn to_view(&self) -> NodeView {
111 NodeView {
112 id: self.id.canonical(),
113 label: self.label.clone(),
114 node_type: self.node_type.clone(),
115 node_kind: match self.node_kind {
116 NodeKind::Container => "container".to_owned(),
117 NodeKind::Leaf => "leaf".to_owned(),
118 },
119 children: Vec::new(),
120 }
121 }
122}
123
124#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
129pub struct NodeView {
130 pub id: String,
132 pub label: String,
134 pub node_type: String,
136 pub node_kind: String,
138 pub children: Vec<NodeView>,
140}
141
142impl NodeView {
143 pub fn is_leaf(&self) -> bool {
145 self.node_kind == "leaf"
146 }
147
148 pub fn is_container(&self) -> bool {
150 self.node_kind == "container"
151 }
152}
153
154impl From<Node> for NodeView {
155 fn from(node: Node) -> Self {
156 node.to_view()
157 }
158}