moduforge_model/ops/
add.rs

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_type::NodeEnum,
11};
12
13use super::{AttrsRef, MarkRef, NodeRef};
14
15/// 为 NodeRef 实现自定义的 + 运算符,用于添加单个节点
16/// 当使用 + 运算符时,会将新节点添加到当前节点的子节点列表中
17impl<'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().into(), &vec![node])?;
24        Ok(NodeRef::new(self.tree, self.key.clone()))
25    }
26}
27/// 为 NodeRef 实现自定义的 + 运算符,用于在指定位置添加单个节点
28/// 当使用 + 运算符时,会将新节点添加到当前节点的子节点列表中的指定位置
29impl<'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().into(), index, &node)?;
36        Ok(NodeRef::new(self.tree, self.key.clone()))
37    }
38}
39
40/// 为 NodeRef 实现自定义的 + 运算符,用于添加多个节点
41/// 当使用 + 运算符时,会将多个新节点添加到当前节点的子节点列表中
42impl<'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().into(), &nodes)?;
49        Ok(NodeRef::new(self.tree, self.key.clone()))
50    }
51}
52impl<'a> Add<NodeEnum> for NodeRef<'a> {
53    type Output = PoolResult<NodeRef<'a>>;
54    fn add(
55        self,
56        nodes: NodeEnum,
57    ) -> Self::Output {
58        self.tree.add(&self.key.clone().into(), vec![nodes])?;
59        Ok(NodeRef::new(self.tree, self.key.clone()))
60    }
61}
62
63/// 为 MarkRef 实现自定义的 + 运算符,用于添加单个标记
64/// 当使用 + 运算符时,会将新标记添加到当前标记的列表中
65impl<'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().into(), &vec![mark])?;
72        Ok(MarkRef::new(self.tree, self.key.clone()))
73    }
74}
75
76/// 为 MarkRef 实现自定义的 + 运算符,用于添加多个标记
77/// 当使用 + 运算符时,会将多个新标记添加到当前标记的列表中
78impl<'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().into(), &marks)?;
85        Ok(MarkRef::new(self.tree, self.key.clone()))
86    }
87}
88
89/// 为 AttrsRef 实现自定义的 + 运算符,用于添加属性
90/// 当使用 + 运算符时,会更新当前节点的属性
91impl<'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().into(), 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().into(), im::hashmap! {key=>value})?;
109        Ok(AttrsRef::new(self.tree, self.key.clone()))
110    }
111}
112
113/// 为 AttrsRef 实现自定义的 + 运算符,用于直接添加属性映射
114/// 当使用 + 运算符时,会直接使用提供的属性映射更新当前节点的属性
115impl<'a> Add<im::HashMap<String, Value>> for AttrsRef<'a> {
116    type Output = PoolResult<AttrsRef<'a>>;
117    fn add(
118        self,
119        attrs: im::HashMap<String, Value>,
120    ) -> Self::Output {
121        self.tree.update_attr(&self.key.clone().into(), attrs)?;
122        Ok(AttrsRef::new(self.tree, self.key.clone()))
123    }
124}