mach_core/dag/node.rs
1use serde::{Deserialize, Serialize};
2use crate::dag::Visitor;
3
4
5///
6/// MachNode.
7/// This is where data is referenced/owned as relational to other data.
8/// Nodes are meant to be as small as possible. They are used for relationships, not actual data.
9///
10#[derive(Debug, Serialize, Deserialize)]
11pub struct MachNode {
12 /// Name of this node - does not have to be unique to the graph.
13 pub name: String,
14
15 /// Parent index within graph. Same as 'index' means no parent.
16 pub parent: u32,
17
18 /// Index of this node within graph.
19 pub index: u32,
20
21 /// Children of this node.
22 pub children: Vec<u32>,
23
24 /// Index of components in data store (not owned by graph).
25 pub components: Vec<u32>,
26}
27
28
29///
30/// Default implementation.
31///
32impl Default for MachNode {
33 fn default() -> Self {
34 Self {
35 name: String::from("root"),
36 parent: 0,
37 index: 0,
38 children: Vec::new(),
39 components: Vec::new()
40 }
41 }
42}
43
44
45///
46/// Implementation for MachNode.
47///
48impl MachNode {
49 /// New mach node with name.
50 pub fn new(name: String) -> Self {
51 Self {
52 name: name,
53 ..Default::default()
54 }
55 }
56
57
58 /// Has parent?
59 pub fn has_parent(&self) -> bool {
60 self.index != self.parent
61 }
62
63
64 /// Has children?
65 pub fn has_children(&self) -> bool {
66 self.children.len() > 0
67 }
68
69
70 /// Has components?
71 pub fn has_components(&self) -> bool {
72 self.components.len() > 0
73 }
74
75
76 /**********************************************************
77 * Visitors
78 **********************************************************/
79
80 /// Accept a visitor.
81 pub fn accept(&self, visitor: &impl Visitor) {
82 visitor.visit(self);
83 }
84
85
86 /// Accept a visitor mutable.
87 pub fn accept_mut(&mut self, visitor: &mut impl Visitor) {
88 visitor.visit_mut(self);
89 }
90}
91
92
93///
94/// From a parent.
95///
96impl From<(String, u32)> for MachNode {
97 fn from((name, parent): (String, u32)) -> Self {
98 Self {
99 name: name,
100 parent: parent,
101 ..Default::default()
102 }
103 }
104}