@@ -101,6 +101,16 @@ impl LinkedChunkBuilder {
101
101
}
102
102
}
103
103
104
+ // New chunk doesn't create a cycle.
105
+ if let Some ( previous_chunk) = new_first_chunk. previous {
106
+ if linked_chunk. chunks ( ) . any ( |chunk| chunk. identifier ( ) == previous_chunk) {
107
+ return Err ( LinkedChunkBuilderError :: Cycle {
108
+ new_chunk : new_first_chunk. identifier ,
109
+ with_chunk : previous_chunk,
110
+ } ) ;
111
+ }
112
+ }
113
+
104
114
let expected_next_chunk = linked_chunk. links . first_chunk ( ) . identifier ( ) ;
105
115
106
116
// New chunk has a next chunk.
@@ -118,8 +128,7 @@ impl LinkedChunkBuilder {
118
128
} ) ;
119
129
}
120
130
121
- // Alright. It's not possible to have a cycle within the chunks or
122
- // multiple connected components here. All checks are made.
131
+ // Alright. All checks are made.
123
132
}
124
133
125
134
// Insert the new first chunk.
@@ -208,6 +217,9 @@ pub enum LinkedChunkBuilderError {
208
217
#[ error( "chunk with id {} has a next chunk, it is supposed to be the last chunk" , id. index( ) ) ]
209
218
ChunkIsNotLast { id : ChunkIdentifier } ,
210
219
220
+ #[ error( "chunk with id {} forms a cycle with chunk with id {}" , new_chunk. index( ) , with_chunk. index( ) ) ]
221
+ Cycle { new_chunk : ChunkIdentifier , with_chunk : ChunkIdentifier } ,
222
+
211
223
#[ error( "chunk with id {} is supposed to have a next chunk" , id. index( ) ) ]
212
224
MissingNextChunk { id : ChunkIdentifier } ,
213
225
@@ -343,6 +355,24 @@ mod tests {
343
355
} ) ;
344
356
}
345
357
358
+ #[ test]
359
+ fn test_insert_new_first_chunk_err_cycle ( ) {
360
+ let new_first_chunk = RawChunk {
361
+ previous : Some ( ChunkIdentifier :: new ( 0 ) ) ,
362
+ identifier : ChunkIdentifier :: new ( 1 ) ,
363
+ next : Some ( ChunkIdentifier ( 0 ) ) ,
364
+ content : ChunkContent :: Gap ( ( ) ) ,
365
+ } ;
366
+
367
+ let mut linked_chunk = LinkedChunk :: < 2 , char , ( ) > :: new ( ) ;
368
+ let result = LinkedChunkBuilder :: insert_new_first_chunk ( & mut linked_chunk, new_first_chunk) ;
369
+
370
+ assert_matches ! ( result, Err ( LinkedChunkBuilderError :: Cycle { new_chunk, with_chunk } ) => {
371
+ assert_eq!( new_chunk, 1 ) ;
372
+ assert_eq!( with_chunk, 0 ) ;
373
+ } ) ;
374
+ }
375
+
346
376
#[ test]
347
377
fn test_insert_new_first_chunk_err_missing_next_chunk ( ) {
348
378
let new_first_chunk = RawChunk {
0 commit comments