@@ -3,6 +3,7 @@ use crate::core::document::path::Path;
33use crate :: core:: { Node , NodeBodyChangeset , NodeData , NodeOperation , OperationTransform , Transaction } ;
44use crate :: errors:: { ErrorBuilder , OTError , OTErrorCode } ;
55use indextree:: { Arena , Children , FollowingSiblings , NodeId } ;
6+ use std:: rc:: Rc ;
67
78use super :: NodeOperationList ;
89
@@ -26,14 +27,13 @@ impl NodeTree {
2627 }
2728
2829 pub fn from_bytes ( root_name : & str , bytes : Vec < u8 > ) -> Result < Self , OTError > {
29- let operations = NodeOperationList :: from_bytes ( bytes) ?. into_inner ( ) ;
30+ let operations = NodeOperationList :: from_bytes ( bytes) ?;
3031 Self :: from_operations ( root_name, operations)
3132 }
3233
33- pub fn from_operations ( root_name : & str , operations : Vec < NodeOperation > ) -> Result < Self , OTError > {
34+ pub fn from_operations ( root_name : & str , operations : NodeOperationList ) -> Result < Self , OTError > {
3435 let mut node_tree = NodeTree :: new ( root_name) ;
35-
36- for operation in operations {
36+ for operation in operations. into_inner ( ) . into_iter ( ) {
3737 let _ = node_tree. apply_op ( operation) ?;
3838 }
3939 Ok ( node_tree)
@@ -54,13 +54,14 @@ impl NodeTree {
5454 /// # Examples
5555 ///
5656 /// ```
57+ /// use std::rc::Rc;
5758 /// use lib_ot::core::{NodeOperation, NodeTree, NodeData, Path};
5859 /// let nodes = vec![NodeData::new("text".to_string())];
5960 /// let root_path: Path = vec![0].into();
6061 /// let op = NodeOperation::Insert {path: root_path.clone(),nodes };
6162 ///
6263 /// let mut node_tree = NodeTree::new("root");
63- /// node_tree.apply_op(op ).unwrap();
64+ /// node_tree.apply_op(Rc::new(op) ).unwrap();
6465 /// let node_id = node_tree.node_id_at_path(&root_path).unwrap();
6566 /// let node_path = node_tree.path_from_node_id(node_id);
6667 /// debug_assert_eq!(node_path, root_path);
@@ -105,23 +106,25 @@ impl NodeTree {
105106 counter
106107 }
107108
108- ///
109+ /// Returns the note_id at the position of the tree with id note_id
109110 /// # Arguments
110111 ///
111- /// * `node_id`:
112- /// * `index`:
112+ /// * `node_id`: the node id of the child's parent
113+ /// * `index`: index of the node in parent children list
113114 ///
114115 /// returns: Option<NodeId>
115116 ///
116117 /// # Examples
117118 ///
118119 /// ```
120+ /// use std::rc::Rc;
119121 /// use lib_ot::core::{NodeOperation, NodeTree, NodeData, Path};
120122 /// let node_1 = NodeData::new("text".to_string());
121123 /// let inserted_path: Path = vec![0].into();
122124 ///
123125 /// let mut node_tree = NodeTree::new("root");
124- /// node_tree.apply_op(NodeOperation::Insert {path: inserted_path.clone(),nodes: vec![node_1.clone()] }).unwrap();
126+ /// let op = NodeOperation::Insert {path: inserted_path.clone(),nodes: vec![node_1.clone()] };
127+ /// node_tree.apply_op(Rc::new(op)).unwrap();
125128 ///
126129 /// let node_2 = node_tree.get_node_at_path(&inserted_path).unwrap();
127130 /// assert_eq!(node_2.node_type, node_1.node_type);
@@ -137,6 +140,10 @@ impl NodeTree {
137140 None
138141 }
139142
143+ /// Returns all children whose parent node id is node_id
144+ ///
145+ /// * `node_id`: the children's parent node id
146+ ///
140147 pub fn children_from_node ( & self , node_id : NodeId ) -> Children < ' _ , Node > {
141148 node_id. children ( & self . arena )
142149 }
@@ -159,18 +166,23 @@ impl NodeTree {
159166 node_id. following_siblings ( & self . arena )
160167 }
161168
162- pub fn apply ( & mut self , transaction : Transaction ) -> Result < ( ) , OTError > {
169+ pub fn apply_transaction ( & mut self , transaction : Transaction ) -> Result < ( ) , OTError > {
163170 let operations = transaction. into_operations ( ) ;
164171 for operation in operations {
165172 self . apply_op ( operation) ?;
166173 }
167174 Ok ( ( ) )
168175 }
169176
170- pub fn apply_op ( & mut self , op : NodeOperation ) -> Result < ( ) , OTError > {
177+ pub fn apply_op ( & mut self , op : Rc < NodeOperation > ) -> Result < ( ) , OTError > {
178+ let op = match Rc :: try_unwrap ( op) {
179+ Ok ( op) => op,
180+ Err ( op) => op. as_ref ( ) . clone ( ) ,
181+ } ;
182+
171183 match op {
172184 NodeOperation :: Insert { path, nodes } => self . insert_nodes ( & path, nodes) ,
173- NodeOperation :: UpdateAttributes { path, attributes , .. } => self . update_attributes ( & path, attributes ) ,
185+ NodeOperation :: UpdateAttributes { path, new , .. } => self . update_attributes ( & path, new ) ,
174186 NodeOperation :: UpdateBody { path, changeset } => self . update_body ( & path, changeset) ,
175187 NodeOperation :: Delete { path, nodes } => self . delete_node ( & path, nodes) ,
176188 }
0 commit comments