1use std::ops::Add;
2
3use serde_json::Value;
4
5use crate::{
6    attrs::Attrs,
7    error::{PoolResult},
8    mark::Mark,
9    node::Node,
10    node_definition::NodeTree,
11};
12
13use super::{AttrsRef, MarkRef, NodeRef};
14
15impl<'a> Add<Node> for NodeRef<'a> {
18    type Output = PoolResult<NodeRef<'a>>;
19    fn add(
20        self,
21        node: Node,
22    ) -> Self::Output {
23        self.tree.add_node(&self.key.clone(), &vec![node])?;
24        Ok(NodeRef::new(self.tree, self.key.clone()))
25    }
26}
27impl<'a> Add<(usize, Node)> for NodeRef<'a> {
30    type Output = PoolResult<NodeRef<'a>>;
31    fn add(
32        self,
33        (index, node): (usize, Node),
34    ) -> Self::Output {
35        self.tree.add_at_index(&self.key.clone(), index, &node)?;
36        Ok(NodeRef::new(self.tree, self.key.clone()))
37    }
38}
39
40impl<'a> Add<Vec<Node>> for NodeRef<'a> {
43    type Output = PoolResult<NodeRef<'a>>;
44    fn add(
45        self,
46        nodes: Vec<Node>,
47    ) -> Self::Output {
48        self.tree.add_node(&self.key.clone(), &nodes)?;
49        Ok(NodeRef::new(self.tree, self.key.clone()))
50    }
51}
52impl<'a> Add<NodeTree> for NodeRef<'a> {
53    type Output = PoolResult<NodeRef<'a>>;
54    fn add(
55        self,
56        nodes: NodeTree,
57    ) -> Self::Output {
58        self.tree.add(&self.key.clone(), vec![nodes])?;
59        Ok(NodeRef::new(self.tree, self.key.clone()))
60    }
61}
62
63impl<'a> Add<Mark> for MarkRef<'a> {
66    type Output = PoolResult<MarkRef<'a>>;
67    fn add(
68        self,
69        mark: Mark,
70    ) -> Self::Output {
71        self.tree.add_mark(&self.key.clone(), &[mark])?;
72        Ok(MarkRef::new(self.tree, self.key.clone()))
73    }
74}
75
76impl<'a> Add<Vec<Mark>> for MarkRef<'a> {
79    type Output = PoolResult<MarkRef<'a>>;
80    fn add(
81        self,
82        marks: Vec<Mark>,
83    ) -> Self::Output {
84        self.tree.add_mark(&self.key.clone(), &marks)?;
85        Ok(MarkRef::new(self.tree, self.key.clone()))
86    }
87}
88
89impl<'a> Add<Attrs> for AttrsRef<'a> {
92    type Output = PoolResult<AttrsRef<'a>>;
93    fn add(
94        self,
95        attrs: Attrs,
96    ) -> Self::Output {
97        self.tree.update_attr(&self.key.clone(), attrs.attrs)?;
98        Ok(AttrsRef::new(self.tree, self.key.clone()))
99    }
100}
101impl<'a> Add<(String, Value)> for AttrsRef<'a> {
102    type Output = PoolResult<AttrsRef<'a>>;
103    fn add(
104        self,
105        (key, value): (String, Value),
106    ) -> Self::Output {
107        self.tree
108            .update_attr(&self.key.clone(), imbl::hashmap! {key=>value})?;
109        Ok(AttrsRef::new(self.tree, self.key.clone()))
110    }
111}
112
113impl<'a> Add<imbl::HashMap<String, Value>> for AttrsRef<'a> {
116    type Output = PoolResult<AttrsRef<'a>>;
117    fn add(
118        self,
119        attrs: imbl::HashMap<String, Value>,
120    ) -> Self::Output {
121        self.tree.update_attr(&self.key.clone(), attrs)?;
122        Ok(AttrsRef::new(self.tree, self.key.clone()))
123    }
124}