1use std::ops::Shl;
2
3use crate::{error::PoolResult, node::Node};
4
5use super::{NodeRef};
6
7impl<'a> Shl<Node> for NodeRef<'a> {
10 type Output = PoolResult<NodeRef<'a>>;
11 fn shl(
12 self,
13 node: Node,
14 ) -> Self::Output {
15 self.tree.add_at_index(&self.key.clone(), 0, &node)?;
17 Ok(NodeRef::new(self.tree, self.key.clone()))
18 }
19}
20
21impl<'a> Shl<Vec<Node>> for NodeRef<'a> {
24 type Output = PoolResult<NodeRef<'a>>;
25 fn shl(
26 self,
27 nodes: Vec<Node>,
28 ) -> Self::Output {
29 for (i, node) in nodes.into_iter().enumerate() {
31 self.tree.add_at_index(&self.key.clone(), i, &node)?;
32 }
33 Ok(NodeRef::new(self.tree, self.key.clone()))
34 }
35}
36
37impl<'a> Shl<usize> for NodeRef<'a> {
40 type Output = PoolResult<NodeRef<'a>>;
41 fn shl(
42 self,
43 positions: usize,
44 ) -> Self::Output {
45 if let Some(parent) = self.tree.get_parent_node(&self.key.clone()) {
47 let siblings = self.tree.children(&parent.id).unwrap_or_default();
48
49 if let Some(current_index) =
50 siblings.iter().position(|id| id.clone() == self.key)
51 {
52 let new_index = current_index.saturating_sub(positions);
54
55 if new_index != current_index {
57 let mut node = self
59 .tree
60 .get_node(&self.key.clone())
61 .unwrap()
62 .as_ref()
63 .clone();
64 let mut content = node.content.clone();
65 content.swap(current_index, new_index);
66 node.content = content;
67 self.tree.update_node(node)?;
68 }
69 }
70 }
71
72 Ok(NodeRef::new(self.tree, self.key.clone()))
73 }
74}