use std::ops::Add;
use rpds::{ht_map_sync, HashTrieMapSync};
use serde_json::Value;
use crate::{
attrs::Attrs,
error::{PoolResult},
mark::Mark,
node::Node,
node_definition::NodeTree,
};
use super::{AttrsRef, MarkRef, NodeRef};
impl<'a> Add<Node> for NodeRef<'a> {
type Output = PoolResult<NodeRef<'a>>;
fn add(
self,
node: Node,
) -> Self::Output {
self.tree.add_node(&self.key.clone(), &vec![node])?;
Ok(NodeRef::new(self.tree, self.key.clone()))
}
}
impl<'a> Add<(usize, Node)> for NodeRef<'a> {
type Output = PoolResult<NodeRef<'a>>;
fn add(
self,
(index, node): (usize, Node),
) -> Self::Output {
self.tree.add_at_index(&self.key.clone(), index, &node)?;
Ok(NodeRef::new(self.tree, self.key.clone()))
}
}
impl<'a> Add<Vec<Node>> for NodeRef<'a> {
type Output = PoolResult<NodeRef<'a>>;
fn add(
self,
nodes: Vec<Node>,
) -> Self::Output {
self.tree.add_node(&self.key.clone(), &nodes)?;
Ok(NodeRef::new(self.tree, self.key.clone()))
}
}
impl<'a> Add<NodeTree> for NodeRef<'a> {
type Output = PoolResult<NodeRef<'a>>;
fn add(
self,
nodes: NodeTree,
) -> Self::Output {
self.tree.add(&self.key.clone(), vec![nodes])?;
Ok(NodeRef::new(self.tree, self.key.clone()))
}
}
impl<'a> Add<Mark> for MarkRef<'a> {
type Output = PoolResult<MarkRef<'a>>;
fn add(
self,
mark: Mark,
) -> Self::Output {
self.tree.add_mark(&self.key.clone(), &[mark])?;
Ok(MarkRef::new(self.tree, self.key.clone()))
}
}
impl<'a> Add<Vec<Mark>> for MarkRef<'a> {
type Output = PoolResult<MarkRef<'a>>;
fn add(
self,
marks: Vec<Mark>,
) -> Self::Output {
self.tree.add_mark(&self.key.clone(), &marks)?;
Ok(MarkRef::new(self.tree, self.key.clone()))
}
}
impl<'a> Add<Attrs> for AttrsRef<'a> {
type Output = PoolResult<AttrsRef<'a>>;
fn add(
self,
attrs: Attrs,
) -> Self::Output {
self.tree.update_attr(&self.key.clone(), attrs.attrs)?;
Ok(AttrsRef::new(self.tree, self.key.clone()))
}
}
impl<'a> Add<(String, Value)> for AttrsRef<'a> {
type Output = PoolResult<AttrsRef<'a>>;
fn add(
self,
(key, value): (String, Value),
) -> Self::Output {
let map = ht_map_sync![key=>value];
self.tree.update_attr(&self.key.clone(), map)?;
Ok(AttrsRef::new(self.tree, self.key.clone()))
}
}
impl<'a> Add<HashTrieMapSync<String, Value>> for AttrsRef<'a> {
type Output = PoolResult<AttrsRef<'a>>;
fn add(
self,
attrs: HashTrieMapSync<String, Value>,
) -> Self::Output {
self.tree.update_attr(&self.key.clone(), attrs)?;
Ok(AttrsRef::new(self.tree, self.key.clone()))
}
}