11use crate :: core:: document:: position:: Position ;
2- use crate :: core:: {
3- DeleteOperation , DocumentOperation , InsertOperation , NodeAttributes , NodeData , TextEditOperation , Transaction ,
4- UpdateOperation ,
5- } ;
2+ use crate :: core:: { DocumentOperation , NodeAttributes , NodeData , OperationTransform , TextDelta , Transaction } ;
3+ use crate :: errors:: { ErrorBuilder , OTError , OTErrorCode } ;
64use indextree:: { Arena , NodeId } ;
75
86pub struct DocumentTree {
@@ -86,42 +84,48 @@ impl DocumentTree {
8684 None
8785 }
8886
89- pub fn apply ( & mut self , transaction : Transaction ) {
87+ pub fn apply ( & mut self , transaction : Transaction ) -> Result < ( ) , OTError > {
9088 for op in & transaction. operations {
91- self . apply_op ( op) ;
89+ self . apply_op ( op) ? ;
9290 }
91+ Ok ( ( ) )
9392 }
9493
95- fn apply_op ( & mut self , op : & DocumentOperation ) {
94+ fn apply_op ( & mut self , op : & DocumentOperation ) -> Result < ( ) , OTError > {
9695 match op {
97- DocumentOperation :: Insert ( op ) => self . apply_insert ( op ) ,
98- DocumentOperation :: Update ( op ) => self . apply_update ( op ) ,
99- DocumentOperation :: Delete ( op ) => self . apply_delete ( op ) ,
100- DocumentOperation :: TextEdit ( op ) => self . apply_text_edit ( op ) ,
96+ DocumentOperation :: Insert { path , nodes } => self . apply_insert ( path , nodes ) ,
97+ DocumentOperation :: Update { path , attributes , .. } => self . apply_update ( path , attributes ) ,
98+ DocumentOperation :: Delete { path , nodes } => self . apply_delete ( path , nodes . len ( ) ) ,
99+ DocumentOperation :: TextEdit { path , delta , .. } => self . apply_text_edit ( path , delta ) ,
101100 }
102101 }
103102
104- fn apply_insert ( & mut self , op : & InsertOperation ) {
105- let parent_path = & op. path . 0 [ 0 ..( op. path . 0 . len ( ) - 1 ) ] ;
106- let last_index = op. path . 0 [ op. path . 0 . len ( ) - 1 ] ;
107- let parent_node = self . node_at_path ( & Position ( parent_path. to_vec ( ) ) ) ;
108- if let Some ( parent_node) = parent_node {
109- let mut inserted_nodes = Vec :: new ( ) ;
103+ fn apply_insert ( & mut self , path : & Position , nodes : & Vec < NodeData > ) -> Result < ( ) , OTError > {
104+ let parent_path = & path. 0 [ 0 ..( path. 0 . len ( ) - 1 ) ] ;
105+ let last_index = path. 0 [ path. 0 . len ( ) - 1 ] ;
106+ let parent_node = self
107+ . node_at_path ( & Position ( parent_path. to_vec ( ) ) )
108+ . ok_or ( ErrorBuilder :: new ( OTErrorCode :: PathNotFound ) . build ( ) ) ?;
109+ let mut inserted_nodes = Vec :: new ( ) ;
110110
111- for node in & op. nodes {
112- inserted_nodes. push ( self . arena . new_node ( node. clone ( ) ) ) ;
113- }
114-
115- self . insert_child_at_index ( parent_node, last_index, & inserted_nodes) ;
111+ for node in nodes {
112+ inserted_nodes. push ( self . arena . new_node ( node. clone ( ) ) ) ;
116113 }
114+
115+ self . insert_child_at_index ( parent_node, last_index, & inserted_nodes)
117116 }
118117
119- fn insert_child_at_index ( & mut self , parent : NodeId , index : usize , insert_children : & [ NodeId ] ) {
118+ fn insert_child_at_index (
119+ & mut self ,
120+ parent : NodeId ,
121+ index : usize ,
122+ insert_children : & [ NodeId ] ,
123+ ) -> Result < ( ) , OTError > {
120124 if index == 0 && parent. children ( & self . arena ) . next ( ) . is_none ( ) {
121125 for id in insert_children {
122126 parent. append ( * id, & mut self . arena ) ;
123127 }
124- return ;
128+ return Ok ( ( ) ) ;
125129 }
126130
127131 let children_length = parent. children ( & self . arena ) . fold ( 0 , |counter, _| counter + 1 ) ;
@@ -130,32 +134,72 @@ impl DocumentTree {
130134 for id in insert_children {
131135 parent. append ( * id, & mut self . arena ) ;
132136 }
133- return ;
137+ return Ok ( ( ) ) ;
134138 }
135139
136- let node_to_insert = self . child_at_index_of_path ( parent, index) . unwrap ( ) ;
140+ let node_to_insert = self
141+ . child_at_index_of_path ( parent, index)
142+ . ok_or ( ErrorBuilder :: new ( OTErrorCode :: PathNotFound ) . build ( ) ) ?;
137143
138144 for id in insert_children {
139145 node_to_insert. insert_before ( * id, & mut self . arena ) ;
140146 }
147+ Ok ( ( ) )
141148 }
142149
143- fn apply_update ( & self , op : & UpdateOperation ) {
144- let update_node = self . node_at_path ( & op. path ) . unwrap ( ) ;
145- let node_data = self . arena . get ( update_node) . unwrap ( ) ;
146- let new_attributes = {
147- let old_attributes = node_data. get ( ) . attributes . borrow ( ) ;
148- NodeAttributes :: compose ( & old_attributes, & op. attributes )
150+ fn apply_update ( & mut self , path : & Position , attributes : & NodeAttributes ) -> Result < ( ) , OTError > {
151+ let update_node = self
152+ . node_at_path ( path)
153+ . ok_or ( ErrorBuilder :: new ( OTErrorCode :: PathNotFound ) . build ( ) ) ?;
154+ let node_data = self . arena . get_mut ( update_node) . unwrap ( ) ;
155+ // let new_node = NodeData {
156+ // ..node_data.get().clone()
157+ // attributes:
158+ // };
159+ let new_node = {
160+ let old_attributes = & node_data. get ( ) . attributes ;
161+ let new_attributes = NodeAttributes :: compose ( & old_attributes, attributes) ;
162+ NodeData {
163+ attributes : new_attributes,
164+ ..node_data. get ( ) . clone ( )
165+ }
149166 } ;
150- node_data. get ( ) . attributes . replace ( new_attributes) ;
167+ * node_data. get_mut ( ) = new_node;
168+ Ok ( ( ) )
151169 }
152170
153- fn apply_delete ( & mut self , op : & DeleteOperation ) {
154- let update_node = self . node_at_path ( & op. path ) . unwrap ( ) ;
155- update_node. remove_subtree ( & mut self . arena ) ;
171+ fn apply_delete ( & mut self , path : & Position , len : usize ) -> Result < ( ) , OTError > {
172+ let mut update_node = self
173+ . node_at_path ( path)
174+ . ok_or ( ErrorBuilder :: new ( OTErrorCode :: PathNotFound ) . build ( ) ) ?;
175+ for _ in 0 ..len {
176+ let next = update_node. following_siblings ( & self . arena ) . next ( ) ;
177+ update_node. remove_subtree ( & mut self . arena ) ;
178+ if let Some ( next_id) = next {
179+ update_node = next_id;
180+ } else {
181+ break ;
182+ }
183+ }
184+ Ok ( ( ) )
156185 }
157186
158- fn apply_text_edit ( & self , _op : & TextEditOperation ) {
159- unimplemented ! ( )
187+ fn apply_text_edit ( & mut self , path : & Position , delta : & TextDelta ) -> Result < ( ) , OTError > {
188+ let edit_node = self
189+ . node_at_path ( path)
190+ . ok_or ( ErrorBuilder :: new ( OTErrorCode :: PathNotFound ) . build ( ) ) ?;
191+ let node_data = self . arena . get_mut ( edit_node) . unwrap ( ) ;
192+ let new_delta = if let Some ( old_delta) = & node_data. get ( ) . delta {
193+ Some ( old_delta. compose ( delta) ?)
194+ } else {
195+ None
196+ } ;
197+ if let Some ( new_delta) = new_delta {
198+ * node_data. get_mut ( ) = NodeData {
199+ delta : Some ( new_delta) ,
200+ ..node_data. get ( ) . clone ( )
201+ } ;
202+ } ;
203+ Ok ( ( ) )
160204 }
161205}
0 commit comments