1use std::ops::Shl;
2
3use crate::{error::PoolResult, error_helpers, 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 node = {
60 let node = {
61 match self.tree.get_node(&self.key.clone()) {
62 Some(n) => n,
63 None => {
64 return Err(error_helpers::node_not_found(
65 self.key.clone(),
66 ));
67 },
68 }
69 };
70 match node.swap(current_index, new_index) {
71 Some(n) => n,
72 None => return Err(anyhow::anyhow!("下标越界了")),
73 }
74 };
75 self.tree.update_node(node)?;
76 }
77 }
78 }
79
80 Ok(NodeRef::new(self.tree, self.key.clone()))
81 }
82}