moduforge_model/ops/
bitor.rs1use std::ops::BitOr;
2
3use crate::{
4 error::PoolResult, id_generator::IdGenerator, mark::Mark, node::Node, types::NodeId
5};
6
7use super::{MarkRef, NodeRef};
8
9impl<'a> BitOr<NodeId> for NodeRef<'a> {
12 type Output = PoolResult<NodeRef<'a>>;
13 fn bitor(
14 self,
15 other_node_id: NodeId,
16 ) -> Self::Output {
17 let other_children = self.tree.children(&other_node_id).unwrap_or_default();
19 let mut nodes_to_add = Vec::new();
20
21 for child_id in other_children {
22 if let Some(child_node) = self.tree.get_node(&child_id){
23 let mut node = child_node.as_ref().clone();
24 node.id = IdGenerator::get_id();
25 nodes_to_add.push(node);
26 }
27 }
28
29 if !nodes_to_add.is_empty() {
30 self.tree.add_node(&self.key.clone().into(), &nodes_to_add)?;
31 }
32 Ok(NodeRef::new(self.tree, self.key.clone()))
33 }
34}
35
36impl<'a> BitOr<Vec<NodeId>> for NodeRef<'a> {
39 type Output = PoolResult<NodeRef<'a>>;
40 fn bitor(
41 self,
42 other_node_ids: Vec<NodeId>,
43 ) -> Self::Output {
44 let mut all_nodes_to_add = Vec::new();
45
46 for node_id in other_node_ids {
47 let children = self.tree.children(&node_id).unwrap_or_default();
48 for child_id in children {
49 if let Some(child_node) = self.tree.get_node(&child_id) {
50 let mut node = child_node.as_ref().clone();
51 node.id = IdGenerator::get_id();
52 all_nodes_to_add.push(node);
53 }
54 }
55 }
56
57 if !all_nodes_to_add.is_empty() {
58 self.tree.add_node(&self.key.clone().into(), &all_nodes_to_add)?;
59 }
60
61 Ok(NodeRef::new(self.tree, self.key.clone()))
62 }
63}
64
65impl<'a> BitOr<Vec<Node>> for NodeRef<'a> {
68 type Output = PoolResult<NodeRef<'a>>;
69 fn bitor(
70 self,
71 nodes: Vec<Node>,
72 ) -> Self::Output {
73 if !nodes.is_empty() {
74 self.tree.add_node(&self.key.clone().into(), &nodes)?;
75 }
76 Ok(NodeRef::new(self.tree, self.key.clone()))
77 }
78}
79
80impl<'a> BitOr<Mark> for MarkRef<'a> {
83 type Output = PoolResult<MarkRef<'a>>;
84 fn bitor(
85 self,
86 mark: Mark,
87 ) -> Self::Output {
88 let existing_marks = self.tree.get_marks(&self.key.clone().into()).unwrap_or_default();
90 let mark_exists = existing_marks.iter().any(|existing_mark| {
91 existing_mark.r#type == mark.r#type && existing_mark.attrs == mark.attrs
92 });
93
94 if !mark_exists {
95 self.tree.add_mark(&self.key.clone().into(), &vec![mark])?;
96 }
97
98 Ok(MarkRef::new(self.tree, self.key.clone()))
99 }
100}
101
102impl<'a> BitOr<Vec<Mark>> for MarkRef<'a> {
105 type Output = PoolResult<MarkRef<'a>>;
106 fn bitor(
107 self,
108 marks: Vec<Mark>,
109 ) -> Self::Output {
110 let existing_marks = self.tree.get_marks(&self.key.clone().into()).unwrap_or_default();
111 let mut unique_marks = Vec::new();
112
113 for mark in marks {
114 let mark_exists = existing_marks.iter().any(|existing_mark| {
115 existing_mark.r#type == mark.r#type && existing_mark.attrs == mark.attrs
116 });
117
118 if !mark_exists {
119 unique_marks.push(mark);
120 }
121 }
122
123 if !unique_marks.is_empty() {
124 self.tree.add_mark(&self.key.clone().into(), &unique_marks)?;
125 }
126
127 Ok(MarkRef::new(self.tree, self.key.clone()))
128 }
129}