moduforge_model/ops/
shl.rs1use std::ops::Shl;
2
3use crate::{
4 error::PoolResult,
5 node::Node,
6};
7
8use super::{NodeRef};
9
10impl<'a> Shl<Node> for NodeRef<'a> {
13 type Output = PoolResult<NodeRef<'a>>;
14 fn shl(
15 self,
16 node: Node,
17 ) -> Self::Output {
18 self.tree.add_at_index(&self.key.clone().into(), 0, &node)?;
20 Ok(NodeRef::new(self.tree, self.key.clone()))
21 }
22}
23
24impl<'a> Shl<Vec<Node>> for NodeRef<'a> {
27 type Output = PoolResult<NodeRef<'a>>;
28 fn shl(
29 self,
30 nodes: Vec<Node>,
31 ) -> Self::Output {
32 for (i, node) in nodes.into_iter().enumerate() {
34 self.tree.add_at_index(&self.key.clone().into(), i, &node)?;
35 }
36 Ok(NodeRef::new(self.tree, self.key.clone()))
37 }
38}
39
40impl<'a> Shl<usize> for NodeRef<'a> {
43 type Output = PoolResult<NodeRef<'a>>;
44 fn shl(
45 self,
46 positions: usize,
47 ) -> Self::Output {
48 if let Some(parent) = self.tree.get_parent_node(&self.key.clone().into()) {
50 let siblings = self.tree.children(&parent.id).unwrap_or_default();
51
52 if let Some(current_index) = siblings.iter().position(|id| id.clone() == self.key) {
53 let new_index = current_index.saturating_sub(positions);
55
56 if new_index != current_index {
58 let mut node = self.tree.get_node(&self.key.clone().into()).unwrap().as_ref().clone();
60 let mut content = node.content.clone();
61 content.swap(current_index, new_index);
62 node.content = content;
63 self.tree.update_node(node)?;
64 }
65 }
66 }
67
68 Ok(NodeRef::new(self.tree, self.key.clone()))
69 }
70}