feat: transaction built der

This commit is contained in:
Vincent Chan 2022-08-17 16:48:45 +08:00
parent 2466b3eebc
commit aa90613bf6
4 changed files with 76 additions and 2 deletions

View File

@ -1,5 +1,5 @@
use crate::core::document::position::Position;
use crate::core::NodeData;
use crate::core::{NodeData, Transaction};
use indextree::{Arena, NodeId};
pub struct DocumentTree {
@ -35,6 +35,42 @@ impl DocumentTree {
Some(iterate_node)
}
pub fn path_of_node(&self, node_id: NodeId) -> Position {
let mut path: Vec<usize> = Vec::new();
let mut ancestors = node_id.ancestors(&self.arena);
let mut current_node = node_id;
let mut parent = ancestors.next();
while parent.is_some() {
let parent_node = parent.unwrap();
let counter = self.index_of_node(parent_node, current_node);
path.push(counter);
current_node = parent_node;
parent = ancestors.next();
}
Position(path)
}
fn index_of_node(&self, parent_node: NodeId, child_node: NodeId) -> usize {
let mut counter: usize = 0;
let mut children_iterator = parent_node.children(&self.arena);
let mut node = children_iterator.next();
while node.is_some() {
if node.unwrap() == child_node {
return counter;
}
node = children_iterator.next();
counter += 1;
}
counter
}
fn child_at_index_of_path(&self, at_node: NodeId, index: usize) -> Option<NodeId> {
let children = at_node.children(&self.arena);
@ -49,4 +85,8 @@ impl DocumentTree {
None
}
pub fn apply(&self, _transaction: Transaction) {
unimplemented!()
}
}

View File

@ -3,8 +3,10 @@ mod document;
mod document_operation;
mod node;
mod position;
mod transaction;
pub use attributes::*;
pub use document::*;
pub use document_operation::*;
pub use node::*;
pub use transaction::*;

View File

@ -0,0 +1,25 @@
use crate::core::DocumentOperation;
pub struct Transaction {
pub operations: Vec<DocumentOperation>,
}
pub struct TransactionBuilder {
operations: Vec<DocumentOperation>,
}
impl TransactionBuilder {
pub fn new() -> TransactionBuilder {
TransactionBuilder { operations: Vec::new() }
}
pub fn push(&mut self, op: DocumentOperation) {
self.operations.push(op);
}
pub fn finalize(self) -> Transaction {
Transaction {
operations: self.operations,
}
}
}

View File

@ -1,7 +1,14 @@
use lib_ot::core::DocumentTree;
use lib_ot::core::{DocumentTree, TransactionBuilder};
#[test]
fn main() {
// Create a new arena
let _document = DocumentTree::new();
}
#[test]
fn test_documents() {
let document = DocumentTree::new();
let tb = TransactionBuilder::new();
document.apply(tb.finalize());
}