tramex_tools/interface/association/
relation.rs1#[derive(Debug, Clone, Default, PartialEq)]
5pub enum AssociationStatus {
6 #[default]
8 NotComputed,
9 Found(Vec<usize>),
11 NotFound,
13 NotApplicable,
15}
16
17impl AssociationStatus {
18 pub fn is_computed(&self) -> bool {
20 !matches!(self, AssociationStatus::NotComputed)
21 }
22
23 pub fn get_index(&self) -> Option<usize> {
25 match self {
26 AssociationStatus::Found(indices) => indices.first().copied(),
27 _ => None,
28 }
29 }
30
31 pub fn get_indices(&self) -> Option<&Vec<usize>> {
33 match self {
34 AssociationStatus::Found(indices) => Some(indices),
35 _ => None,
36 }
37 }
38
39 pub fn add_index(&mut self, index: usize) -> bool {
44 match self {
45 AssociationStatus::NotComputed | AssociationStatus::NotFound => {
46 *self = AssociationStatus::Found(vec![index]);
47 true
48 }
49 AssociationStatus::Found(indices) => {
50 if !indices.contains(&index) {
51 indices.push(index);
52 }
53 true
54 }
55 AssociationStatus::NotApplicable => false,
56 }
57 }
58
59 pub fn contains(&self, index: usize) -> bool {
61 match self {
62 AssociationStatus::Found(indices) => indices.contains(&index),
63 _ => false,
64 }
65 }
66}
67
68#[derive(Debug, Clone, Default)]
70pub struct TraceRelation {
71 pub parent: AssociationStatus,
73 pub child: AssociationStatus,
75}
76
77impl TraceRelation {
78 pub fn new() -> Self {
80 Self::default()
81 }
82
83 pub fn has_parent(&self) -> bool {
85 matches!(self.parent, AssociationStatus::Found(_))
86 }
87
88 pub fn has_child(&self) -> bool {
90 matches!(self.child, AssociationStatus::Found(_))
91 }
92
93 pub fn get_parent_index(&self) -> Option<usize> {
95 self.parent.get_index()
96 }
97
98 pub fn get_parent_indices(&self) -> Option<&Vec<usize>> {
100 self.parent.get_indices()
101 }
102
103 pub fn get_child_index(&self) -> Option<usize> {
105 self.child.get_index()
106 }
107
108 pub fn get_child_indices(&self) -> Option<&Vec<usize>> {
110 self.child.get_indices()
111 }
112
113 pub fn set_parent(&mut self, index: usize) {
115 self.parent = AssociationStatus::Found(vec![index]);
116 }
117
118 pub fn add_parent(&mut self, index: usize) -> bool {
120 self.parent.add_index(index)
121 }
122
123 pub fn set_child(&mut self, index: usize) {
125 self.child = AssociationStatus::Found(vec![index]);
126 }
127
128 pub fn add_child(&mut self, index: usize) -> bool {
130 self.child.add_index(index)
131 }
132
133 pub fn set_parent_not_found(&mut self) {
135 self.parent = AssociationStatus::NotFound;
136 }
137
138 pub fn set_child_not_found(&mut self) {
140 self.child = AssociationStatus::NotFound;
141 }
142
143 pub fn set_parent_not_applicable(&mut self) {
145 self.parent = AssociationStatus::NotApplicable;
146 }
147
148 pub fn set_child_not_applicable(&mut self) {
150 self.child = AssociationStatus::NotApplicable;
151 }
152
153 pub fn invalidate(&mut self) {
155 self.parent = AssociationStatus::NotComputed;
156 self.child = AssociationStatus::NotComputed;
157 }
158}