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
use logisheets_base::{
    BlockFieldId, BlockId, CellId, ColId, Cube, RowId, SheetId, TextId, errors::BasicError,
    index_fetcher::IndexFetcherTrait, name_fetcher::NameFetcherTrait,
};
use logisheets_workbook::workbook::Wb;

use crate::{
    Controller,
    block_manager::schema_manager::SchemaManager,
    container::DataContainer,
    cube_manager::CubeManager,
    ext_book_manager::ExtBooksManager,
    ext_ref_manager::ExtRefManager,
    file_saver::workbook::save_workbook,
    formula_manager::FormulaManager,
    id_manager::{FuncIdManager, NameIdManager, SheetIdManager, TextIdManager},
    navigator::Navigator,
    range_manager::RangeManager,
    workbook::sheet_info_manager::SheetInfoManager,
};

pub use self::error::SaveError;
type Result<T> = std::result::Result<T, BasicError>;

mod error;
mod external_links;
pub(crate) mod pivot_ooxml;
mod sst;
mod styles;
mod utils;
mod workbook;
mod worksheet;

/// Serialize the controller into a workbook.
///
/// `resolve_block_refs` chooses how block formulas are written. See
/// `FormulaFormat` on the public API for what each choice means.
pub fn save_file(
    controller: &Controller,
    resolve_block_refs: bool,
) -> std::result::Result<Wb, SaveError> {
    let func_manager = &controller.status.func_id_manager;
    let external_links_manager = &controller.status.external_links_manager;
    let text_id_manager = &controller.status.text_id_manager;
    let name_id_manager = &controller.status.name_id_manager;
    let formula_manager = &controller.status.formula_manager;
    let range_manager = &controller.status.range_manager;
    let cube_manager = &controller.status.cube_manager;
    let ext_ref_manager = &controller.status.ext_ref_manager;

    let data_container = &controller.status.container;
    let attachment_manager = &controller.status.cell_attachment_manager;
    let sheet_pos_manager = &controller.status.sheet_info_manager;
    let sheet_id_manager = &controller.status.sheet_id_manager;
    let style_manager = &controller.status.style_manager;
    let ext_book_manager = &controller.status.external_links_manager;
    let theme_manager = &controller.settings.theme;

    let settings = &controller.settings;

    let mut navigator_replication = controller.status.navigator.clone();
    let block_schema_manager = &controller.status.block_schema_manager;
    let field_render_manager = &controller.status.field_render_manager;
    let reserved_ids: std::collections::HashSet<String> = settings
        .unknown_package_parts
        .iter()
        .chain(settings.unknown_workbook_parts.iter())
        .chain(
            settings
                .preserved_parts
                .values()
                .flat_map(|p| p.unknown_parts.iter()),
        )
        .map(|u| u.rel.id.clone())
        // Preserved parts are not the only ones that keep a source id: a pivot
        // cache, a pivot table and a structured table all do, because the XML
        // that references them names that id. `calc_test.xlsx` showed the
        // consequence — a minted `rId4` landing on the pivot cache's.
        .chain(settings.pivot_caches.iter().map(|c| c.rel_id.clone()))
        .chain(
            settings
                .preserved_parts
                .values()
                .flat_map(|p| p.pivot_tables.iter().map(|t| t.rel_id.clone())),
        )
        .chain(
            settings
                .preserved_parts
                .values()
                .flat_map(|p| p.tables.iter().map(|t| t.rel_id.clone())),
        )
        .collect();
    let mut saver = Saver {
        part_count: 0,
        reserved_ids,
        _external_count: 0,
        func_manager,
        range_manager,
        cube_manager,
        ext_ref_manager,
        sheet_id_manager,
        external_links_manager,
        text_id_manager,
        name_id_manager,
        navigator: &mut navigator_replication,
        _formula_manager: formula_manager,
        sheet_pos_manager,
        block_schema_manager,
        container: data_container,
        resolve_block_refs,
    };

    save_workbook(
        data_container,
        formula_manager,
        attachment_manager,
        sheet_pos_manager,
        sheet_id_manager,
        style_manager,
        ext_book_manager,
        theme_manager,
        text_id_manager,
        &controller.status.navigator,
        settings,
        controller.app_data.clone(),
        block_schema_manager,
        field_render_manager,
        &controller.status.enum_set_manager,
        &controller.status.image_manager,
        &controller.status.chart_manager,
        &controller.status.data_validation_manager,
        &controller.status.conditional_formatting_manager,
        &controller.status.range_manager,
        &controller.status.exclusive_manager,
        &mut saver,
    )
}

pub trait SaverTrait: IndexFetcherTrait + NameFetcherTrait {
    fn fetch_part_id(&mut self) -> String;

    fn fetch_row_id(&mut self, sheet_id: SheetId, idx: usize) -> RowId;
}

pub struct Saver<'a> {
    pub part_count: u32,
    /// Relationship ids that preserved parts already answer to.
    ///
    /// A part we do not model keeps the id it arrived with, because the XML that
    /// references it says `r:id="…"` and renaming would break that. Minted ids
    /// therefore have to step around them: two relationships sharing an Id makes
    /// the package invalid, and it would only show up on a file whose vendor
    /// part happened to be numbered the way we number ours.
    pub reserved_ids: std::collections::HashSet<String>,
    pub _external_count: u32,

    pub func_manager: &'a FuncIdManager,
    pub sheet_id_manager: &'a SheetIdManager,
    pub external_links_manager: &'a ExtBooksManager,
    pub text_id_manager: &'a TextIdManager,
    pub name_id_manager: &'a NameIdManager,
    pub navigator: &'a mut Navigator,
    pub _formula_manager: &'a FormulaManager,
    pub range_manager: &'a RangeManager,
    pub cube_manager: &'a CubeManager,
    pub ext_ref_manager: &'a ExtRefManager,
    pub sheet_pos_manager: &'a SheetInfoManager,
    pub block_schema_manager: &'a SchemaManager,
    /// Needed to read block key cells when resolving a BLOCKREF to coordinates.
    pub container: &'a DataContainer,
    /// When set, `BLOCKREF` / `BLOCKREFS` are written as ordinary A1 references
    /// instead of function calls — see `FormulaFormat`.
    pub resolve_block_refs: bool,
}

impl<'a> IndexFetcherTrait for Saver<'a> {
    fn fetch_row_index(&self, sheet_id: &SheetId, row_id: &RowId) -> Result<usize> {
        self.navigator.fetch_row_idx(sheet_id, row_id)
    }

    fn fetch_col_index(&self, sheet_id: &SheetId, col_id: &ColId) -> Result<usize> {
        self.navigator.fetch_col_idx(sheet_id, col_id)
    }

    fn fetch_cell_index(
        &self,
        sheet_id: &SheetId,
        cell_id: &logisheets_base::CellId,
    ) -> Result<(usize, usize)> {
        self.navigator.fetch_cell_idx(sheet_id, cell_id)
    }

    fn fetch_sheet_index(&self, sheet_id: &SheetId) -> Result<usize> {
        self.sheet_pos_manager
            .get_sheet_idx(sheet_id)
            .ok_or(BasicError::SheetIdNotFound(*sheet_id))
    }

    fn fetch_normal_cell_index(
        &self,
        sheet_id: &SheetId,
        normal_cell_id: &logisheets_base::NormalCellId,
    ) -> Result<(usize, usize)> {
        self.navigator
            .fetch_normal_cell_idx(sheet_id, normal_cell_id)
    }

    fn fetch_block_cell_index(
        &self,
        sheet: &SheetId,
        block_cell_id: &logisheets_base::BlockCellId,
    ) -> Result<(usize, usize)> {
        self.navigator.fetch_block_cell_idx(sheet, block_cell_id)
    }
}

impl<'a> NameFetcherTrait for Saver<'a> {
    fn fetch_text(&self, text_id: &TextId) -> Result<String> {
        self.text_id_manager
            .get_string(text_id)
            .ok_or(BasicError::TextIdNotFound(*text_id))
    }

    fn fetch_func_name(&self, func_id: &logisheets_base::FuncId) -> Result<String> {
        self.func_manager
            .get_string(func_id)
            .ok_or(BasicError::FuncIdNotFound(*func_id))
    }

    fn fetch_sheet_name(&self, sheet_id: &SheetId) -> Result<String> {
        self.sheet_id_manager
            .get_string(sheet_id)
            .ok_or(BasicError::SheetIdNotFound(*sheet_id))
    }

    fn fetch_book_name(&self, book_id: &logisheets_base::ExtBookId) -> Result<String> {
        self.external_links_manager
            .fetch_book_name(book_id)
            .ok_or_else(|| BasicError::BookIdNotFound(*book_id))
    }

    fn fetch_defined_name(&self, nid: &logisheets_base::NameId) -> Result<String> {
        self.name_id_manager
            .get_string(nid)
            .and_then(|(_, s)| Some(s))
            .ok_or(BasicError::NameNotFound(*nid))
    }

    fn fetch_cell_idx(
        &self,
        sheet_id: &SheetId,
        cell_id: &logisheets_base::CellId,
    ) -> Result<(usize, usize)> {
        self.navigator.fetch_cell_idx(sheet_id, cell_id)
    }

    fn fetch_row_idx(&self, sheet_id: &SheetId, row_id: &RowId) -> Result<usize> {
        self.navigator.fetch_row_idx(sheet_id, row_id)
    }

    fn fetch_col_idx(&self, sheet_id: &SheetId, col_id: &ColId) -> Result<usize> {
        self.navigator.fetch_col_idx(sheet_id, col_id)
    }

    fn fetch_range(
        &self,
        sheet_id: &SheetId,
        range_id: &logisheets_base::RangeId,
    ) -> Result<logisheets_base::Range> {
        self.range_manager
            .get_range(sheet_id, range_id)
            .ok_or(BasicError::RangeIdNotFound(*range_id))
    }

    fn fetch_cube(&self, cube_id: &logisheets_base::CubeId) -> Result<Cube> {
        self.cube_manager
            .get_cube(cube_id)
            .ok_or(BasicError::CubeIdNotFound(*cube_id))
    }

    fn fetch_ext_ref(
        &mut self,
        ext_ref_id: &logisheets_base::ExtRefId,
    ) -> Result<logisheets_base::ExtRef> {
        self.ext_ref_manager
            .get_ext_ref(ext_ref_id)
            .ok_or(BasicError::ExtRefIdNotFound(*ext_ref_id))
    }

    fn fetch_block_ref_name_by_id(&self, sheet_id: SheetId, block_id: BlockId) -> Option<String> {
        self.block_schema_manager
            .fetch_block_ref_name(sheet_id, block_id)
    }

    fn fetch_block_field_name_by_id(
        &self,
        sheet_id: SheetId,
        block_id: BlockId,
        field_id: BlockFieldId,
    ) -> Option<String> {
        self.block_schema_manager
            .fetch_field_name(sheet_id, block_id, field_id)
    }

    fn resolve_block_ref_cell(
        &self,
        sheet_id: SheetId,
        block_id: BlockId,
        field_id: BlockFieldId,
        key: &str,
    ) -> Option<(usize, usize)> {
        if !self.resolve_block_refs {
            return None;
        }
        // Only a literal key names one row. Unparse renders a string literal
        // with its quotes, so strip them; anything else is a computed
        // expression and keeps the BLOCKREF form.
        let key = key.strip_prefix('"')?.strip_suffix('"')?;
        let (_, cell_id) = self.resolve_key(sheet_id, block_id, key)?;
        let target = self
            .block_schema_manager
            .partially_resolve_by_field_id(sheet_id, block_id, cell_id, field_id)?;
        self.navigator
            .fetch_cell_idx(&sheet_id, &CellId::BlockCell(target))
            .ok()
    }

    fn resolve_block_join(
        &self,
        sheet_id: SheetId,
        block_id: BlockId,
        field_id: logisheets_base::BlockFieldId,
    ) -> Option<(
        ((usize, usize), (usize, usize)),
        ((usize, usize), (usize, usize)),
    )> {
        if !self.resolve_block_refs {
            return None;
        }
        let bp = self
            .navigator
            .get_block_place(&sheet_id, &block_id)
            .ok()?
            .clone();
        let keys = self
            .block_schema_manager
            .get_all_key_cell_ids_by_block(sheet_id, block_id, &bp)?;
        let first = keys.first()?;
        let last = keys.last()?;

        // The key column: where the key cells themselves sit.
        let k0 = self
            .navigator
            .fetch_cell_idx(&sheet_id, &CellId::BlockCell(*first))
            .ok()?;
        let k1 = self
            .navigator
            .fetch_cell_idx(&sheet_id, &CellId::BlockCell(*last))
            .ok()?;

        // The field column, over the same rows.
        let ftop = self
            .block_schema_manager
            .partially_resolve_by_field_id(sheet_id, block_id, *first, field_id)?;
        let fbot = self
            .block_schema_manager
            .partially_resolve_by_field_id(sheet_id, block_id, *last, field_id)?;
        let f0 = self
            .navigator
            .fetch_cell_idx(&sheet_id, &CellId::BlockCell(ftop))
            .ok()?;
        let f1 = self
            .navigator
            .fetch_cell_idx(&sheet_id, &CellId::BlockCell(fbot))
            .ok()?;
        Some(((f0, f1), (k0, k1)))
    }

    fn resolve_block_refs_range(
        &self,
        sheet_id: SheetId,
        block_id: BlockId,
        key_condition: &str,
        field_condition: &str,
    ) -> Option<((usize, usize), (usize, usize))> {
        if !self.resolve_block_refs {
            return None;
        }
        // Only the statically-decidable shape: every row of one named field.
        // A real filter would have to be evaluated, and the rectangle it picks
        // out need not even be contiguous.
        if key_condition != "\"*\"" {
            return None;
        }
        let field = field_condition.strip_prefix('"')?.strip_suffix('"')?;
        let bp = self
            .navigator
            .get_block_place(&sheet_id, &block_id)
            .ok()?
            .clone();
        let keys = self
            .block_schema_manager
            .get_all_key_cell_ids_by_block(sheet_id, block_id, &bp)?;
        let first = keys.first()?;
        let last = keys.last()?;
        let top = self.block_schema_manager.partially_resolve_by_block(
            sheet_id,
            block_id,
            *first,
            &field.to_string(),
        )?;
        let bottom = self.block_schema_manager.partially_resolve_by_block(
            sheet_id,
            block_id,
            *last,
            &field.to_string(),
        )?;
        let top = self
            .navigator
            .fetch_cell_idx(&sheet_id, &CellId::BlockCell(top))
            .ok()?;
        let bottom = self
            .navigator
            .fetch_cell_idx(&sheet_id, &CellId::BlockCell(bottom))
            .ok()?;
        Some((top, bottom))
    }
}

impl<'a> Saver<'a> {
    /// The block key cell whose value equals `key`, mirroring how the calc
    /// engine resolves a BLOCKREF at evaluation time.
    fn resolve_key(
        &self,
        sheet_id: SheetId,
        block_id: BlockId,
        key: &str,
    ) -> Option<(SheetId, logisheets_base::BlockCellId)> {
        let bp = self
            .navigator
            .get_block_place(&sheet_id, &block_id)
            .ok()?
            .clone();
        let ids = self
            .block_schema_manager
            .get_all_key_cell_ids_by_block(sheet_id, block_id, &bp)?;
        let text =
            |id: logisheets_base::TextId| self.text_id_manager.get_string(&id).unwrap_or_default();
        ids.into_iter().find_map(|id| {
            let cell = self.container.get_cell(sheet_id, &CellId::BlockCell(id))?;
            if cell.value.to_string(&text) == key {
                Some((sheet_id, id))
            } else {
                None
            }
        })
    }
}

impl<'a> SaverTrait for Saver<'a> {
    fn fetch_part_id(&mut self) -> String {
        loop {
            self.part_count += 1;
            let candidate = format!("rId{}", self.part_count);
            if !self.reserved_ids.contains(&candidate) {
                return candidate;
            }
        }
    }

    fn fetch_row_id(&mut self, sheet_id: SheetId, idx: usize) -> RowId {
        self.navigator.fetch_row_id(&sheet_id, idx).unwrap()
    }
}