200 lines
5.8 KiB
Rust
Raw Normal View History

2022-09-12 11:30:02 +08:00
use crate::core::attributes::Attributes;
2022-09-10 10:06:44 +08:00
use crate::core::document::path::Path;
2022-09-12 11:30:02 +08:00
use crate::core::{NodeData, NodeOperation, NodeTree};
use indextree::NodeId;
2022-08-17 16:48:45 +08:00
2022-09-11 12:59:01 +08:00
use super::{NodeBodyChangeset, NodeOperationList};
2022-09-13 20:23:56 +08:00
#[derive(Debug, Clone)]
2022-08-17 16:48:45 +08:00
pub struct Transaction {
operations: NodeOperationList,
2022-08-17 16:48:45 +08:00
}
2022-08-18 16:19:50 +08:00
impl Transaction {
2022-09-13 20:23:56 +08:00
pub fn new() -> Self {
Transaction {
operations: vec![].into(),
}
}
pub fn from_operations<T: Into<NodeOperationList>>(operations: T) -> Self {
Self {
operations: operations.into(),
}
2022-08-18 16:19:50 +08:00
}
pub fn into_operations(self) -> Vec<NodeOperation> {
self.operations.into_inner()
}
2022-09-13 20:23:56 +08:00
pub fn transform(&self, other: &mut Transaction) {
for other_operation in other.iter_mut() {
for operation in self.operations.iter() {
operation.mut_transform(other_operation);
}
}
}
}
impl std::ops::Deref for Transaction {
type Target = NodeOperationList;
fn deref(&self) -> &Self::Target {
&self.operations
}
}
impl std::ops::DerefMut for Transaction {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.operations
}
2022-08-18 16:19:50 +08:00
}
pub struct TransactionBuilder<'a> {
2022-09-10 10:05:27 +08:00
node_tree: &'a NodeTree,
2022-09-11 12:59:01 +08:00
operations: NodeOperationList,
2022-08-17 16:48:45 +08:00
}
2022-08-18 16:19:50 +08:00
impl<'a> TransactionBuilder<'a> {
2022-09-10 10:05:27 +08:00
pub fn new(node_tree: &'a NodeTree) -> TransactionBuilder {
2022-08-18 16:19:50 +08:00
TransactionBuilder {
2022-09-10 10:05:27 +08:00
node_tree,
2022-09-11 12:59:01 +08:00
operations: NodeOperationList::default(),
2022-08-18 16:19:50 +08:00
}
2022-08-17 16:48:45 +08:00
}
2022-09-08 17:38:11 +08:00
///
///
/// # Arguments
///
/// * `path`: the path that is used to save the nodes
/// * `nodes`: the nodes you will be save in the path
///
/// # Examples
///
/// ```
2022-09-08 19:27:15 +08:00
/// // -- 0 (root)
/// // 0 -- text_1
/// // 1 -- text_2
2022-09-11 08:59:12 +08:00
/// use lib_ot::core::{NodeTree, NodeData, TransactionBuilder};
/// let mut node_tree = NodeTree::new("root");
/// let transaction = TransactionBuilder::new(&node_tree)
2022-09-11 08:59:12 +08:00
/// .insert_nodes_at_path(0,vec![ NodeData::new("text_1"), NodeData::new("text_2")])
/// .finalize();
/// node_tree.apply(transaction).unwrap();
2022-09-08 17:38:11 +08:00
///
/// node_tree.node_id_at_path(vec![0, 0]);
2022-09-08 17:38:11 +08:00
/// ```
///
2022-09-11 08:59:12 +08:00
pub fn insert_nodes_at_path<T: Into<Path>>(self, path: T, nodes: Vec<NodeData>) -> Self {
self.push(NodeOperation::Insert {
2022-09-08 17:38:11 +08:00
path: path.into(),
nodes,
})
2022-08-18 17:49:20 +08:00
}
2022-09-08 17:38:11 +08:00
///
///
/// # Arguments
///
/// * `path`: the path that is used to save the nodes
/// * `node`: the node data will be saved in the path
///
/// # Examples
///
/// ```
/// // 0
/// // -- 0
/// // |-- text
2022-09-11 08:59:12 +08:00
/// use lib_ot::core::{NodeTree, NodeData, TransactionBuilder};
/// let mut node_tree = NodeTree::new("root");
/// let transaction = TransactionBuilder::new(&node_tree)
2022-09-11 08:59:12 +08:00
/// .insert_node_at_path(0, NodeData::new("text"))
/// .finalize();
/// node_tree.apply(transaction).unwrap();
2022-09-08 17:38:11 +08:00
/// ```
///
2022-09-11 08:59:12 +08:00
pub fn insert_node_at_path<T: Into<Path>>(self, path: T, node: NodeData) -> Self {
self.insert_nodes_at_path(path, vec![node])
2022-09-08 17:38:11 +08:00
}
2022-09-12 11:30:02 +08:00
pub fn update_attributes_at_path(mut self, path: &Path, attributes: Attributes) -> Self {
2022-09-11 12:59:01 +08:00
match self.node_tree.get_node_at_path(path) {
Some(node) => {
2022-09-12 11:30:02 +08:00
let mut old_attributes = Attributes::new();
2022-09-11 12:59:01 +08:00
for key in attributes.keys() {
let old_attrs = &node.attributes;
if let Some(value) = old_attrs.get(key.as_str()) {
old_attributes.insert(key.clone(), value.clone());
}
}
2022-08-18 20:15:34 +08:00
2022-09-11 12:59:01 +08:00
self.operations.push(NodeOperation::UpdateAttributes {
path: path.clone(),
attributes,
old_attributes,
});
2022-09-10 08:42:53 +08:00
}
2022-09-11 12:59:01 +08:00
None => tracing::warn!("Update attributes at path: {:?} failed. Node is not exist", path),
2022-08-18 20:15:34 +08:00
}
2022-09-11 12:59:01 +08:00
self
}
2022-08-18 20:15:34 +08:00
2022-09-11 12:59:01 +08:00
pub fn update_body_at_path(mut self, path: &Path, changeset: NodeBodyChangeset) -> Self {
match self.node_tree.node_id_at_path(path) {
Some(_) => {
self.operations.push(NodeOperation::UpdateBody {
path: path.clone(),
changeset,
});
}
None => tracing::warn!("Update attributes at path: {:?} failed. Node is not exist", path),
}
self
2022-08-18 20:15:34 +08:00
}
pub fn delete_node_at_path(self, path: &Path) -> Self {
self.delete_nodes_at_path(path, 1)
2022-08-18 17:49:20 +08:00
}
pub fn delete_nodes_at_path(mut self, path: &Path, length: usize) -> Self {
2022-09-11 12:59:01 +08:00
let mut node = self.node_tree.node_id_at_path(path).unwrap();
let mut deleted_nodes = vec![];
2022-08-18 17:49:20 +08:00
for _ in 0..length {
deleted_nodes.push(self.get_deleted_nodes(node));
2022-09-10 10:05:27 +08:00
node = self.node_tree.following_siblings(node).next().unwrap();
2022-08-18 17:49:20 +08:00
}
self.operations.push(NodeOperation::Delete {
2022-08-18 17:49:20 +08:00
path: path.clone(),
nodes: deleted_nodes,
});
self
2022-08-18 17:49:20 +08:00
}
2022-09-11 08:59:12 +08:00
fn get_deleted_nodes(&self, node_id: NodeId) -> NodeData {
let node_data = self.node_tree.get_node(node_id).unwrap();
let mut children = vec![];
2022-09-10 10:05:27 +08:00
self.node_tree.children_from_node(node_id).for_each(|child_id| {
children.push(self.get_deleted_nodes(child_id));
});
2022-09-11 08:59:12 +08:00
NodeData {
2022-09-10 20:34:00 +08:00
node_type: node_data.node_type.clone(),
attributes: node_data.attributes.clone(),
2022-09-10 20:34:00 +08:00
body: node_data.body.clone(),
children,
}
}
pub fn push(mut self, op: NodeOperation) -> Self {
2022-08-17 16:48:45 +08:00
self.operations.push(op);
self
2022-08-17 16:48:45 +08:00
}
pub fn finalize(self) -> Transaction {
2022-09-13 20:23:56 +08:00
Transaction::from_operations(self.operations)
2022-08-17 16:48:45 +08:00
}
}