refactor: refactor transaction builder in rust style

This commit is contained in:
appflowy 2022-09-09 14:34:40 +08:00
parent ac23f81e24
commit a2918d251f
2 changed files with 64 additions and 88 deletions

View File

@ -26,7 +26,6 @@ impl<'a> TransactionBuilder<'a> {
} }
} }
/// ///
/// ///
/// # Arguments /// # Arguments
@ -42,21 +41,19 @@ impl<'a> TransactionBuilder<'a> {
/// // 1 -- text_2 /// // 1 -- text_2
/// use lib_ot::core::{DocumentTree, NodeSubTree, TransactionBuilder}; /// use lib_ot::core::{DocumentTree, NodeSubTree, TransactionBuilder};
/// let mut document = DocumentTree::new(); /// let mut document = DocumentTree::new();
/// let transaction = { /// let transaction = TransactionBuilder::new(&document)
/// let mut tb = TransactionBuilder::new(&document); /// .insert_nodes_at_path(0,vec![ NodeSubTree::new("text_1"), NodeSubTree::new("text_2")])
/// tb.insert_nodes_at_path(0,vec![ NodeSubTree::new("text_1"), NodeSubTree::new("text_2")]); /// .finalize();
/// tb.finalize()
/// };
/// document.apply(transaction).unwrap(); /// document.apply(transaction).unwrap();
/// ///
/// document.node_at_path(vec![0, 0]); /// document.node_at_path(vec![0, 0]);
/// ``` /// ```
/// ///
pub fn insert_nodes_at_path<T: Into<Path>>(&mut self, path: T, nodes: Vec<NodeSubTree>) { pub fn insert_nodes_at_path<T: Into<Path>>(self, path: T, nodes: Vec<NodeSubTree>) -> Self {
self.push(DocumentOperation::Insert { self.push(DocumentOperation::Insert {
path: path.into(), path: path.into(),
nodes, nodes,
}); })
} }
/// ///
@ -74,19 +71,17 @@ impl<'a> TransactionBuilder<'a> {
/// // |-- text /// // |-- text
/// use lib_ot::core::{DocumentTree, NodeSubTree, TransactionBuilder}; /// use lib_ot::core::{DocumentTree, NodeSubTree, TransactionBuilder};
/// let mut document = DocumentTree::new(); /// let mut document = DocumentTree::new();
/// let transaction = { /// let transaction = TransactionBuilder::new(&document)
/// let mut tb = TransactionBuilder::new(&document); /// .insert_node_at_path(0, NodeSubTree::new("text"))
/// tb.insert_node_at_path(0, NodeSubTree::new("text")); /// .finalize();
/// tb.finalize()
/// };
/// document.apply(transaction).unwrap(); /// document.apply(transaction).unwrap();
/// ``` /// ```
/// ///
pub fn insert_node_at_path<T: Into<Path>>(&mut self, path: T, node: NodeSubTree) { pub fn insert_node_at_path<T: Into<Path>>(self, path: T, node: NodeSubTree) -> Self {
self.insert_nodes_at_path(path, vec![node]); self.insert_nodes_at_path(path, vec![node])
} }
pub fn update_attributes_at_path(&mut self, path: &Path, attributes: HashMap<String, Option<String>>) { pub fn update_attributes_at_path(self, path: &Path, attributes: HashMap<String, Option<String>>) -> Self {
let mut old_attributes: HashMap<String, Option<String>> = HashMap::new(); let mut old_attributes: HashMap<String, Option<String>> = HashMap::new();
let node = self.document.node_at_path(path).unwrap(); let node = self.document.node_at_path(path).unwrap();
let node_data = self.document.get_node_data(node).unwrap(); let node_data = self.document.get_node_data(node).unwrap();
@ -107,28 +102,29 @@ impl<'a> TransactionBuilder<'a> {
}) })
} }
pub fn delete_node_at_path(&mut self, path: &Path) { pub fn delete_node_at_path(self, path: &Path) -> Self {
self.delete_nodes_at_path(path, 1); self.delete_nodes_at_path(path, 1)
} }
pub fn delete_nodes_at_path(&mut self, path: &Path, length: usize) { pub fn delete_nodes_at_path(mut self, path: &Path, length: usize) -> Self {
let mut node = self.document.node_at_path(path).unwrap(); let mut node = self.document.node_at_path(path).unwrap();
let mut deleted_nodes = vec![]; let mut deleted_nodes = vec![];
for _ in 0..length { for _ in 0..length {
deleted_nodes.push(self.get_deleted_nodes(node)); deleted_nodes.push(self.get_deleted_nodes(node));
node = self.document.following_siblings(node).next().unwrap(); node = self.document.following_siblings(node).next().unwrap();
} }
self.operations.push(DocumentOperation::Delete { self.operations.push(DocumentOperation::Delete {
path: path.clone(), path: path.clone(),
nodes: deleted_nodes, nodes: deleted_nodes,
}) });
self
} }
fn get_deleted_nodes(&self, node_id: NodeId) -> NodeSubTree { fn get_deleted_nodes(&self, node_id: NodeId) -> NodeSubTree {
let node_data = self.document.get_node_data(node_id).unwrap(); let node_data = self.document.get_node_data(node_id).unwrap();
let mut children = vec![]; let mut children = vec![];
self.document.children_from_node(node_id).for_each(|child_id| { self.document.children_from_node(node_id).for_each(|child_id| {
children.push(self.get_deleted_nodes(child_id)); children.push(self.get_deleted_nodes(child_id));
}); });
@ -141,8 +137,9 @@ impl<'a> TransactionBuilder<'a> {
} }
} }
pub fn push(&mut self, op: DocumentOperation) { pub fn push(mut self, op: DocumentOperation) -> Self {
self.operations.push(op); self.operations.push(op);
self
} }
pub fn finalize(self) -> Transaction { pub fn finalize(self) -> Transaction {

View File

@ -11,11 +11,10 @@ fn main() {
#[test] #[test]
fn test_documents() { fn test_documents() {
let mut document = DocumentTree::new(); let mut document = DocumentTree::new();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .insert_node_at_path(0, NodeSubTree::new("text"))
tb.insert_node_at_path(0, NodeSubTree::new("text")); .finalize();
tb.finalize()
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
assert!(document.node_at_path(0).is_some()); assert!(document.node_at_path(0).is_some());
@ -23,21 +22,17 @@ fn test_documents() {
let node_data = document.get_node_data(node).unwrap(); let node_data = document.get_node_data(node).unwrap();
assert_eq!(node_data.node_type, "text"); assert_eq!(node_data.node_type, "text");
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .update_attributes_at_path(
tb.update_attributes_at_path(
&vec![0].into(), &vec![0].into(),
HashMap::from([("subtype".into(), Some("bullet-list".into()))]), HashMap::from([("subtype".into(), Some("bullet-list".into()))]),
); )
tb.finalize() .finalize();
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .delete_node_at_path(&vec![0].into())
tb.delete_node_at_path(&vec![0].into()); .finalize();
tb.finalize()
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
assert!(document.node_at_path(0).is_none()); assert!(document.node_at_path(0).is_none());
} }
@ -45,29 +40,24 @@ fn test_documents() {
#[test] #[test]
fn test_inserts_nodes() { fn test_inserts_nodes() {
let mut document = DocumentTree::new(); let mut document = DocumentTree::new();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .insert_node_at_path(0, NodeSubTree::new("text"))
tb.insert_node_at_path(0, NodeSubTree::new("text")); .insert_node_at_path(1, NodeSubTree::new("text"))
tb.insert_node_at_path(1, NodeSubTree::new("text")); .insert_node_at_path(2, NodeSubTree::new("text"))
tb.insert_node_at_path(2, NodeSubTree::new("text")); .finalize();
tb.finalize()
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .insert_node_at_path(1, NodeSubTree::new("text"))
tb.insert_node_at_path(1, NodeSubTree::new("text")); .finalize();
tb.finalize()
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
} }
#[test] #[test]
fn test_inserts_subtrees() { fn test_inserts_subtrees() {
let mut document = DocumentTree::new(); let mut document = DocumentTree::new();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .insert_node_at_path(
tb.insert_node_at_path(
0, 0,
NodeSubTree { NodeSubTree {
note_type: "text".into(), note_type: "text".into(),
@ -75,9 +65,8 @@ fn test_inserts_subtrees() {
delta: None, delta: None,
children: vec![NodeSubTree::new("image")], children: vec![NodeSubTree::new("image")],
}, },
); )
tb.finalize() .finalize();
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
let node = document.node_at_path(&Path(vec![0, 0])).unwrap(); let node = document.node_at_path(&Path(vec![0, 0])).unwrap();
@ -88,20 +77,16 @@ fn test_inserts_subtrees() {
#[test] #[test]
fn test_update_nodes() { fn test_update_nodes() {
let mut document = DocumentTree::new(); let mut document = DocumentTree::new();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .insert_node_at_path(&vec![0], NodeSubTree::new("text"))
tb.insert_node_at_path(&vec![0], NodeSubTree::new("text")); .insert_node_at_path(&vec![1], NodeSubTree::new("text"))
tb.insert_node_at_path(&vec![1], NodeSubTree::new("text")); .insert_node_at_path(vec![2], NodeSubTree::new("text"))
tb.insert_node_at_path(vec![2], NodeSubTree::new("text")); .finalize();
tb.finalize()
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .update_attributes_at_path(&vec![1].into(), HashMap::from([("bolded".into(), Some("true".into()))]))
tb.update_attributes_at_path(&vec![1].into(), HashMap::from([("bolded".into(), Some("true".into()))])); .finalize();
tb.finalize()
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
let node = document.node_at_path(&Path(vec![1])).unwrap(); let node = document.node_at_path(&Path(vec![1])).unwrap();
@ -113,20 +98,16 @@ fn test_update_nodes() {
#[test] #[test]
fn test_delete_nodes() { fn test_delete_nodes() {
let mut document = DocumentTree::new(); let mut document = DocumentTree::new();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .insert_node_at_path(0, NodeSubTree::new("text"))
tb.insert_node_at_path(0, NodeSubTree::new("text")); .insert_node_at_path(1, NodeSubTree::new("text"))
tb.insert_node_at_path(1, NodeSubTree::new("text")); .insert_node_at_path(2, NodeSubTree::new("text"))
tb.insert_node_at_path(2, NodeSubTree::new("text")); .finalize();
tb.finalize()
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .delete_node_at_path(&Path(vec![1]))
tb.delete_node_at_path(&Path(vec![1])); .finalize();
tb.finalize()
};
document.apply(transaction).unwrap(); document.apply(transaction).unwrap();
let len = document.number_of_children(); let len = document.number_of_children();
@ -136,12 +117,10 @@ fn test_delete_nodes() {
#[test] #[test]
fn test_errors() { fn test_errors() {
let mut document = DocumentTree::new(); let mut document = DocumentTree::new();
let transaction = { let transaction = TransactionBuilder::new(&document)
let mut tb = TransactionBuilder::new(&document); .insert_node_at_path(0, NodeSubTree::new("text"))
tb.insert_node_at_path(0, NodeSubTree::new("text")); .insert_node_at_path(100, NodeSubTree::new("text"))
tb.insert_node_at_path(100, NodeSubTree::new("text")); .finalize();
tb.finalize()
};
let result = document.apply(transaction); let result = document.apply(transaction);
assert_eq!(result.err().unwrap().code, OTErrorCode::PathNotFound); assert_eq!(result.err().unwrap().code, OTErrorCode::PathNotFound);
} }