logisheets_controller 1.16.1

Core spreadsheet engine for LogiSheets: formula evaluation, dependency tracking, and undo/redo.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
use logisheets_base::{CellId, ColId, RowId, SheetId, errors::BasicError};

use super::sheet_nav::{MAX_COL_CNT, MAX_ROW_CNT};
use super::{BlockPlace, Navigator, SheetNav, ctx::NavExecCtx};
use crate::{Error, edit_action::EditPayload};

/// Cells a single block may cover.
///
/// Block cells are materialized eagerly, so the cost of creating one scales
/// with `row_cnt * col_cnt`: a million cells already takes seconds, ten million
/// takes half a minute, and a block the size of a whole sheet (1048576 x 16384)
/// overflows the allocation and panics — which under wasm kills the engine
/// instance. `create_block` is a first-class agent operation, so an unbounded
/// size request is a crash and a denial of service reachable from one tool call.
///
/// This is a policy limit, not a format one: xlsx itself would allow a block to
/// span the sheet. It is set at one full column's worth of cells, which is far
/// past any structured table a caller has a real use for.
pub const MAX_BLOCK_CELLS: u64 = 1_048_576;

/// Reject a row/column insert or delete that cannot fit the sheet.
///
/// Line counts arrive straight from the payload, and the work is proportional to
/// the count: `insert_cols` with `count = u32::MAX` never returns. A sheet holds
/// at most `MAX_ROW_CNT` rows and `MAX_COL_CNT` columns, so anything past that
/// is meaningless regardless of how long it would take.
fn check_line_range(start: usize, count: u32, limit: u32) -> Result<(), Error> {
    if start >= limit as usize || start as u64 + count as u64 > limit as u64 {
        return Err(BasicError::LineRangeOutOfSheet(count, start, limit).into());
    }
    Ok(())
}

/// Reject a block-line insert or delete whose range falls outside the block.
///
/// `BlockPlace`'s line helpers reach imbl's `split_at` / `remove` / `insert`,
/// which panic out of range, and the indices come straight from the payload.
/// `insert` may land just past the last line; `delete` may not.
fn check_block_line_range(
    start: usize,
    cnt: u32,
    len: usize,
    block_id: logisheets_base::BlockId,
    inserting: bool,
) -> Result<(), Error> {
    let end = start as u64 + cnt as u64;
    let ok = if inserting {
        start <= len && end <= len as u64 + cnt as u64
    } else {
        end <= len as u64
    };
    if !ok {
        return Err(BasicError::LineRangeOutOfBlock(start, cnt, block_id, len).into());
    }
    Ok(())
}

/// Interpret a pivot declaration arriving on a payload, refusing the two
/// states that would produce silently wrong numbers.
///
/// A pivot with **no source** is meaningless: every cell of it aggregates a
/// block that was never named, so the whole grid would read 0 with nothing to
/// say why. And a pivot whose source is **itself** is the cycle the separate-
/// block design exists to avoid — refused here as well as in the `analyzes`
/// check above, because a pivot reaches its source through the recipe too.
///
/// A recipe this build cannot interpret (an unknown aggregate function, a
/// missing dimension) is dropped rather than guessed — see
/// `PivotSpec::from_parts` — so the block stays a plain analysis block instead
/// of quietly inventing an aggregate.
fn pivot_from(
    parts: Option<&crate::block_manager::schema_manager::field_type::PivotSpecParts>,
    analyzes: Option<usize>,
    block_id: usize,
) -> Result<Option<crate::block_manager::schema_manager::field_type::PivotSpec>, Error> {
    use crate::block_manager::schema_manager::field_type::PivotSpec;
    let Some(parts) = parts else {
        return Ok(None);
    };
    match analyzes {
        None => Err(Error::PayloadError(format!(
            "block {block_id} declares a pivot but analyses nothing — a pivot \
             needs a source block to aggregate"
        ))),
        Some(src) if src == block_id => Err(Error::PayloadError(format!(
            "block {block_id} cannot pivot over itself"
        ))),
        Some(_) => Ok(PivotSpec::from_parts(parts)),
    }
}

/// Reject block dimensions that don't fit the sheet, or whose cell count is
/// past what we're willing to materialize.
fn check_block_size(row_cnt: u32, col_cnt: u32) -> Result<(), Error> {
    let cells = row_cnt as u64 * col_cnt as u64;
    if row_cnt > MAX_ROW_CNT || col_cnt > MAX_COL_CNT || cells > MAX_BLOCK_CELLS {
        return Err(BasicError::BlockTooLarge(
            row_cnt,
            col_cnt,
            MAX_ROW_CNT,
            MAX_COL_CNT,
            MAX_BLOCK_CELLS,
        )
        .into());
    }
    Ok(())
}

pub struct NavExecutor {
    pub nav: Navigator,
    pub row_inserted: Vec<(SheetId, RowId)>,
    pub row_removed: Vec<(SheetId, RowId)>,
    pub col_inserted: Vec<(SheetId, ColId)>,
    pub col_removed: Vec<(SheetId, ColId)>,
}

impl NavExecutor {
    pub fn new(nav: Navigator) -> Self {
        NavExecutor {
            nav,
            row_inserted: vec![],
            row_removed: vec![],
            col_inserted: vec![],
            col_removed: vec![],
        }
    }

    pub fn execute<C: NavExecCtx>(
        mut self,
        ctx: &mut C,
        payload: EditPayload,
    ) -> Result<(Self, bool), Error> {
        match payload {
            EditPayload::MoveBlock(move_block) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(move_block.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let cell_id = self.nav.fetch_cell_id(
                    &sheet_id,
                    move_block.new_master_row,
                    move_block.new_master_col,
                )?;
                if let CellId::BlockCell(id) = cell_id {
                    if id.block_id != move_block.id {
                        return Err(BasicError::CreatingBlockOn(id.block_id).into());
                    }
                }
                self.nav.move_block(
                    &sheet_id,
                    &move_block.id,
                    move_block.new_master_row,
                    move_block.new_master_col,
                );
                Ok((self, true))
            }
            EditPayload::MoveBlockLine(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let bp = self.nav.get_block_place(&sheet_id, &p.block_id)?.clone();
                let len = if p.is_row {
                    bp.rows.len()
                } else {
                    bp.cols.len()
                };
                if p.from >= len || p.to >= len {
                    return Err(BasicError::LineRangeOutOfBlock(
                        p.from.max(p.to),
                        1,
                        p.block_id,
                        len,
                    )
                    .into());
                }
                let new_bp = bp.move_line(p.from, p.to, p.is_row);
                let nav = self.nav.add_block_place(sheet_id, p.block_id, new_bp);
                Ok((
                    NavExecutor {
                        nav,
                        row_inserted: self.row_inserted,
                        row_removed: self.row_removed,
                        col_inserted: self.col_inserted,
                        col_removed: self.col_removed,
                    },
                    true,
                ))
            }
            EditPayload::ReorderBlockLines(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let bp = self.nav.get_block_place(&sheet_id, &p.block_id)?.clone();
                let len = if p.is_row {
                    bp.rows.len()
                } else {
                    bp.cols.len()
                };
                if p.new_order.len() != len || p.new_order.iter().any(|&i| i >= len) {
                    return Err(BasicError::BadBlockLineOrder(p.block_id, len).into());
                }
                let new_bp = bp.reorder_lines(&p.new_order, p.is_row);
                let nav = self.nav.add_block_place(sheet_id, p.block_id, new_bp);
                Ok((
                    NavExecutor {
                        nav,
                        row_inserted: self.row_inserted,
                        row_removed: self.row_removed,
                        col_inserted: self.col_inserted,
                        col_removed: self.col_removed,
                    },
                    true,
                ))
            }
            EditPayload::RemoveBlock(remove_block) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(remove_block.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                self.nav.remove_block(&sheet_id, &remove_block.id);
                Ok((self, true))
            }
            EditPayload::CreateBlock(create_block) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(create_block.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let cell_id = self.nav.fetch_cell_id(
                    &sheet_id,
                    create_block.master_row,
                    create_block.master_col,
                )?;
                let cell_id = match cell_id {
                    CellId::NormalCell(c) => c,
                    CellId::BlockCell(id) => {
                        return Err(BasicError::CreatingBlockOn(id.block_id).into());
                    }
                    CellId::EphemeralCell(id) => {
                        return Err(BasicError::CreatingBlockOnEphemeral(id).into());
                    }
                };
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id);
                if sheet_nav.data.has_block_id(&create_block.id) {
                    return Err(BasicError::BlockIdHasAlreadyExisted(create_block.id).into());
                }
                check_block_size(create_block.row_cnt as u32, create_block.col_cnt as u32)?;
                let block_place = BlockPlace::new(
                    cell_id,
                    create_block.row_cnt as u32,
                    create_block.col_cnt as u32,
                    create_block.owner.clone().unwrap_or_default(),
                    create_block.modify_policy.unwrap_or_default(),
                )
                .with_pivot(pivot_from(
                    create_block.pivot.as_ref(),
                    create_block.analyzes,
                    create_block.id,
                )?)
                .with_description(create_block.description.clone().unwrap_or_default())
                .with_permissions(create_block.permissions.clone().unwrap_or_default())
                .with_analyzes(create_block.analyzes);
                let block_id = create_block.id;
                sheet_nav.data.blocks.insert(block_id, block_place);
                sheet_nav.cache = Default::default();
                Ok((self, true))
            }
            EditPayload::SetBlockDescription(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id);
                let Some(bp) = sheet_nav.data.blocks.get(&p.block_id) else {
                    return Err(BasicError::BlockIdNotFound(sheet_id, p.block_id).into());
                };
                let mut bp = bp.clone();
                bp.description = p.description.clone();
                sheet_nav.data.blocks.insert(p.block_id, bp);
                Ok((self, true))
            }
            EditPayload::SetBlockAnalyzes(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id);
                // A block cannot analyse itself: its cells would aggregate a
                // set they belong to, which is the cycle the whole separate-
                // block design exists to avoid.
                if p.analyzes == Some(p.block_id) {
                    return Err(Error::PayloadError(format!(
                        "block {} cannot analyse itself",
                        p.block_id
                    )));
                }
                // And it has to analyse something that exists — a marker
                // pointing at nothing would make the block's formulas read
                // empty with no way for a reader to see why.
                if let Some(source) = p.analyzes {
                    if !sheet_nav.data.has_block_id(&source) {
                        return Err(BasicError::BlockIdNotFound(sheet_id, source).into());
                    }
                }
                let Some(bp) = sheet_nav.data.blocks.get(&p.block_id) else {
                    return Err(BasicError::BlockIdNotFound(sheet_id, p.block_id).into());
                };
                let pivot = pivot_from(p.pivot.as_ref(), p.analyzes, p.block_id)?;
                let bp = bp.clone().with_analyzes(p.analyzes).with_pivot(pivot);
                sheet_nav.data.blocks.insert(p.block_id, bp);
                Ok((self, true))
            }
            EditPayload::SetBlockPermissions(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id);
                let Some(bp) = sheet_nav.data.blocks.get(&p.block_id) else {
                    return Err(BasicError::BlockIdNotFound(sheet_id, p.block_id).into());
                };
                let mut bp = bp.clone();
                bp.permissions = p.permissions.clone();
                if let Some(policy) = p.modify_policy {
                    bp.modify_policy = policy;
                }
                sheet_nav.data.blocks.insert(p.block_id, bp);
                Ok((self, true))
            }
            EditPayload::ConvertBlock(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let cell_id = self
                    .nav
                    .fetch_cell_id(&sheet_id, p.master_row, p.master_col)?;
                let cell_id = match cell_id {
                    CellId::NormalCell(c) => c,
                    CellId::BlockCell(id) => {
                        return Err(BasicError::CreatingBlockOn(id.block_id).into());
                    }
                    CellId::EphemeralCell(id) => {
                        return Err(BasicError::CreatingBlockOnEphemeral(id).into());
                    }
                };
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id);
                if sheet_nav.data.has_block_id(&p.id) {
                    return Err(BasicError::BlockIdHasAlreadyExisted(p.id).into());
                }
                check_block_size(p.row_cnt as u32, p.col_cnt as u32)?;
                let block_place = BlockPlace::new(
                    cell_id,
                    p.row_cnt as u32,
                    p.col_cnt as u32,
                    String::new(),
                    Default::default(),
                );
                let block_id = p.id;
                sheet_nav.data.blocks.insert(block_id, block_place);
                sheet_nav.cache = Default::default();
                Ok((self, true))
            }
            EditPayload::ResizeBlock(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id);
                if !sheet_nav.data.has_block_id(&p.id) {
                    return Err(BasicError::BlockIdDoesNotExist(p.id).into());
                }
                let block_place = sheet_nav
                    .data
                    .blocks
                    .get_mut(&p.id)
                    .ok_or(BasicError::BlockIdNotFound(sheet_id, p.id))?
                    .clone();
                check_block_size(
                    p.new_row_cnt.unwrap_or(block_place.rows.len()) as u32,
                    p.new_col_cnt.unwrap_or(block_place.cols.len()) as u32,
                )?;
                let new_block_place = block_place.resize(p.new_row_cnt, p.new_col_cnt);
                sheet_nav.data.blocks.insert(p.id, new_block_place);
                sheet_nav.cache = Default::default();

                Ok((self, true))
            }
            EditPayload::CreateSheet(p) => {
                let sheet_id = ctx.get_sheet_id(p.new_name)?;
                self.nav.create_sheet(sheet_id);
                Ok((self, true))
            }
            EditPayload::DeleteSheet(delete_sheet) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(delete_sheet.idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                self.nav.delete_sheet(&sheet_id);
                Ok((self, true))
            }
            EditPayload::InsertCols(insert_cols) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(insert_cols.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                check_line_range(insert_cols.start, insert_cols.count as u32, MAX_COL_CNT)?;
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id).clone();
                let (new_sheet_nav, inserted) =
                    insert_new_cols(sheet_nav, insert_cols.start, insert_cols.count as u32);
                let sheet_navs = self.nav.sheet_navs.update(sheet_id, new_sheet_nav);
                let mut col_inserted = self.col_inserted;
                col_inserted.extend(inserted.into_iter().map(|id| (sheet_id, id)));
                let res = NavExecutor {
                    nav: Navigator { sheet_navs },
                    row_inserted: self.row_inserted,
                    row_removed: self.row_removed,
                    col_inserted,
                    col_removed: self.col_removed,
                };
                Ok((res, true))
            }
            EditPayload::DeleteCols(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                check_line_range(p.start, p.count as u32, MAX_COL_CNT)?;
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id).clone();
                let (new_sheet_nav, removed) = delete_cols(sheet_nav, p.start, p.count as u32);
                let sheet_navs = self.nav.sheet_navs.update(sheet_id, new_sheet_nav);
                let mut col_removed = self.col_removed;
                col_removed.extend(removed.into_iter().map(|id| (sheet_id, id)));
                let res = NavExecutor {
                    nav: Navigator { sheet_navs },
                    row_inserted: self.row_inserted,
                    row_removed: self.row_removed,
                    col_inserted: self.col_inserted,
                    col_removed,
                };
                Ok((res, true))
            }
            EditPayload::InsertRows(insert_rows) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(insert_rows.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                check_line_range(insert_rows.start, insert_rows.count as u32, MAX_ROW_CNT)?;
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id).clone();
                let (new_sheet_nav, inserted) =
                    insert_new_rows(sheet_nav, insert_rows.start, insert_rows.count as u32);
                let sheet_navs = self.nav.sheet_navs.update(sheet_id, new_sheet_nav);
                let mut row_inserted = self.row_inserted;
                row_inserted.extend(inserted.into_iter().map(|id| (sheet_id, id)));
                let res = NavExecutor {
                    nav: Navigator { sheet_navs },
                    row_inserted,
                    row_removed: self.row_removed,
                    col_inserted: self.col_inserted,
                    col_removed: self.col_removed,
                };
                Ok((res, true))
            }
            EditPayload::DeleteRows(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                check_line_range(p.start, p.count as u32, MAX_ROW_CNT)?;
                let sheet_nav = self.nav.get_sheet_nav_mut(&sheet_id).clone();
                let (new_sheet_nav, removed) = delete_rows(sheet_nav, p.start, p.count as u32);
                let sheet_navs = self.nav.sheet_navs.update(sheet_id, new_sheet_nav);
                let mut row_removed = self.row_removed;
                row_removed.extend(removed.into_iter().map(|id| (sheet_id, id)));
                let res = NavExecutor {
                    nav: Navigator { sheet_navs },
                    row_inserted: self.row_inserted,
                    row_removed,
                    col_inserted: self.col_inserted,
                    col_removed: self.col_removed,
                };
                Ok((res, true))
            }
            EditPayload::InsertColsInBlock(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let bp = self.nav.get_block_place(&sheet_id, &p.block_id)?.clone();
                check_block_line_range(p.start, p.cnt as u32, bp.cols.len(), p.block_id, true)?;
                let new_bp = bp.add_new_cols(p.start, p.cnt as u32);
                let nav = self.nav.add_block_place(sheet_id, p.block_id, new_bp);
                let result = NavExecutor {
                    nav,
                    row_inserted: self.row_inserted,
                    row_removed: self.row_removed,
                    col_inserted: self.col_inserted,
                    col_removed: self.col_removed,
                };
                Ok((result, true))
            }
            EditPayload::DeleteColsInBlock(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let bp = self.nav.get_block_place(&sheet_id, &p.block_id)?.clone();
                check_block_line_range(p.start, p.cnt as u32, bp.cols.len(), p.block_id, false)?;
                let new_bp = bp.delete_cols(p.start, p.cnt as u32);
                let nav = self.nav.add_block_place(sheet_id, p.block_id, new_bp);
                let result = NavExecutor {
                    nav,
                    row_inserted: self.row_inserted,
                    row_removed: self.row_removed,
                    col_inserted: self.col_inserted,
                    col_removed: self.col_removed,
                };
                Ok((result, true))
            }
            EditPayload::InsertRowsInBlock(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let bp = self.nav.get_block_place(&sheet_id, &p.block_id)?.clone();
                check_block_line_range(p.start, p.cnt as u32, bp.rows.len(), p.block_id, true)?;
                let new_bp = bp.add_new_rows(p.start, p.cnt as u32);
                let nav = self.nav.add_block_place(sheet_id, p.block_id, new_bp);
                let result = NavExecutor {
                    nav,
                    row_inserted: self.row_inserted,
                    row_removed: self.row_removed,
                    col_inserted: self.col_inserted,
                    col_removed: self.col_removed,
                };
                Ok((result, true))
            }
            EditPayload::DeleteRowsInBlock(p) => {
                let sheet_id = ctx
                    .fetch_sheet_id_by_index(p.sheet_idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?;
                let bp = self.nav.get_block_place(&sheet_id, &p.block_id)?.clone();
                check_block_line_range(p.start, p.cnt as u32, bp.rows.len(), p.block_id, false)?;
                let new_bp = bp.delete_rows(p.start, p.cnt as u32);
                let nav = self.nav.add_block_place(sheet_id, p.block_id, new_bp);
                let result = NavExecutor {
                    nav,
                    row_inserted: self.row_inserted,
                    row_removed: self.row_removed,
                    col_inserted: self.col_inserted,
                    col_removed: self.col_removed,
                };
                Ok((result, true))
            }
            _ => Ok((self, false)),
        }
    }
}

fn insert_new_rows(sheet_nav: SheetNav, idx: usize, cnt: u32) -> (SheetNav, Vec<RowId>) {
    let mut new_id_manager = sheet_nav.id_manager.clone();
    let new_ids = new_id_manager.get_row_ids(cnt);
    let inserted: Vec<RowId> = new_ids.iter().cloned().collect();
    let new_rows = {
        let rows = sheet_nav.data.rows.clone();
        let row_max = rows.len();
        let (mut left, right) = rows.split_at(idx);
        left.append(new_ids.clone());
        left.append(right);
        left.truncate(row_max);
        left
    };
    let sheet_nav = SheetNav {
        sheet_id: sheet_nav.sheet_id,
        data: sheet_nav.data.update_rows(new_rows),
        cache: Default::default(),
        id_manager: new_id_manager,
    };
    (sheet_nav, inserted)
}

fn insert_new_cols(sheet_nav: SheetNav, idx: usize, cnt: u32) -> (SheetNav, Vec<ColId>) {
    let mut new_id_manager = sheet_nav.id_manager.clone();
    let new_ids = new_id_manager.get_col_ids(cnt);
    let inserted: Vec<ColId> = new_ids.iter().cloned().collect();
    let new_cols = {
        let cols = sheet_nav.data.cols.clone();
        let col_max = cols.len();
        let (mut left, right) = cols.split_at(idx);
        left.append(new_ids.clone());
        left.append(right);
        left.truncate(col_max);
        left
    };
    let sheet_nav = SheetNav {
        sheet_id: sheet_nav.sheet_id,
        data: sheet_nav.data.update_cols(new_cols),
        cache: Default::default(),
        id_manager: new_id_manager,
    };
    (sheet_nav, inserted)
}

fn delete_rows(sheet_nav: SheetNav, idx: usize, cnt: u32) -> (SheetNav, Vec<RowId>) {
    let sheet_id = sheet_nav.sheet_id;
    let result = sheet_nav;
    let mut new_id_manager = result.id_manager.clone();
    let new_ids = new_id_manager.get_row_ids(cnt);
    let removed: Vec<RowId>;
    let new_rows = {
        let rows = result.data.rows.clone();
        let removed_cnt = std::cmp::min(rows.len() - idx, cnt as usize);
        let (mut left, right) = rows.split_at(idx);
        removed = right.iter().take(removed_cnt).cloned().collect();
        left.append(right.skip(removed_cnt));
        left.append(new_ids.clone());
        left
    };
    let new_blocks = {
        let mut old_blocks = result.data.blocks.clone();
        old_blocks.iter_mut().for_each(|(_, bp)| {
            // One stale block is not a reason to abandon the deletion; leave
            // it where it is, as the new-anchor lookup below already does.
            let Ok((row, col)) = result.get_fetcher().get_norm_cell_idx(&bp.master) else {
                return;
            };
            if row >= idx && row <= idx + cnt as usize - 1 {
                let new_row = idx + cnt as usize;
                if let Ok(new_master_id) = result.get_fetcher().get_norm_cell_id(new_row, col) {
                    bp.master = new_master_id;
                }
            }
        });
        old_blocks
    };
    let sheet_nav = SheetNav {
        sheet_id,
        data: result.data.update_rows(new_rows).update_blocks(new_blocks),
        cache: Default::default(),
        id_manager: new_id_manager,
    };
    (sheet_nav, removed)
}

fn delete_cols(sheet_nav: SheetNav, idx: usize, cnt: u32) -> (SheetNav, Vec<ColId>) {
    let sheet_id = sheet_nav.sheet_id;
    let result = sheet_nav;
    let mut new_id_manager = result.id_manager.clone();
    let new_ids = new_id_manager.get_col_ids(cnt);
    let removed: Vec<ColId>;
    let new_cols = {
        let cols = result.data.cols.clone();
        let removed_cnt = std::cmp::min(cols.len() - idx, cnt as usize);
        let (mut left, right) = cols.split_at(idx);
        removed = right.iter().take(removed_cnt).cloned().collect();
        left.append(right.skip(removed_cnt));
        left.append(new_ids.clone());
        left
    };
    let new_blocks = {
        let mut old_blocks = result.data.blocks.clone();
        old_blocks.iter_mut().for_each(|(_, bp)| {
            // See `delete_rows`.
            let Ok((row, col)) = result.get_fetcher().get_norm_cell_idx(&bp.master) else {
                return;
            };
            if col >= idx && col <= idx + cnt as usize - 1 {
                let new_col = idx + cnt as usize;
                if let Ok(new_master_id) = result.get_fetcher().get_norm_cell_id(row, new_col) {
                    bp.master = new_master_id;
                }
            }
        });
        old_blocks
    };
    let sheet_nav = SheetNav {
        sheet_id,
        data: result.data.update_cols(new_cols).update_blocks(new_blocks),
        cache: Default::default(),
        id_manager: new_id_manager,
    };
    (sheet_nav, removed)
}

#[cfg(test)]
mod tests {
    use imbl::Vector;

    use super::{delete_cols, delete_rows};
    use crate::navigator::sheet_nav::SheetNav;

    #[test]
    fn delete_row_test() {
        let sheet_nav = SheetNav::init(5, 5, 0);
        assert_eq!(&sheet_nav.data.rows, &Vector::from(vec![0, 1, 2, 3, 4]));
        let (new_sheet_nav, _) = delete_rows(sheet_nav, 1, 1);
        assert_eq!(&new_sheet_nav.data.rows, &Vector::from(vec![0, 2, 3, 4, 5]));
    }

    #[test]
    fn delete_col_test() {
        let sheet_nav = SheetNav::init(5, 5, 0);
        assert_eq!(&sheet_nav.data.cols, &Vector::from(vec![0, 1, 2, 3, 4]));
        let (new_sheet_nav, _) = delete_cols(sheet_nav, 1, 1);
        assert_eq!(&new_sheet_nav.data.cols, &Vector::from(vec![0, 2, 3, 4, 5]));
    }
}