cellrune 0.1.17

Bounded XLSX/XLSM reading, deterministic calculation, editing, and writing for Rust
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::num::NonZeroU32;
use std::sync::{Arc, OnceLock};

mod table_index;

pub(crate) use table_index::TableRangeIndex;
use table_index::{TableColumnLocation, TableIndex, TableIndexBuildError, TableLocation};

use crate::calculation::persistent_store::{PersistentRadixMap, PersistentRadixValues};
use crate::{
    Cell, CellAddress, CellContent, CellRange, Column, DefinedName, DefinedNameScope, Diagnostic,
    NumberFormat, Provenance, Row, Table, TableColumn, TableColumnId, TableId, ValidationError,
};

#[cfg(test)]
fn clone_map_cancellable<K, V>(
    source: &BTreeMap<K, V>,
    cancelled: &impl Fn() -> bool,
) -> Result<BTreeMap<K, V>, ()>
where
    K: Clone + Ord,
    V: Clone,
{
    let mut cloned = BTreeMap::new();
    for (key, value) in source {
        if cancelled() {
            return Err(());
        }
        cloned.insert(key.clone(), value.clone());
    }
    Ok(cloned)
}

/// A validated, non-zero workbook-local sheet identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SheetId(NonZeroU32);

impl SheetId {
    /// Validates and constructs a sheet ID.
    ///
    /// # Errors
    ///
    /// Returns [`ValidationError::SheetIdZero`] when `value` is zero.
    pub fn new(value: u32) -> Result<Self, ValidationError> {
        NonZeroU32::new(value)
            .map(Self)
            .ok_or(ValidationError::SheetIdZero)
    }

    /// Returns the workbook-local numeric ID.
    pub const fn get(self) -> u32 {
        self.0.get()
    }
}

impl TryFrom<u32> for SheetId {
    type Error = ValidationError;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

/// A validated sheet name with its original spelling preserved.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SheetName {
    original: Box<str>,
    lookup_key: Box<str>,
}

impl SheetName {
    /// Applies Excel's length, apostrophe, and forbidden-character constraints.
    ///
    /// # Errors
    ///
    /// Returns a [`ValidationError`] when the name violates an Excel sheet-name constraint.
    pub fn new(value: impl Into<String>) -> Result<Self, ValidationError> {
        let value = value.into();
        if value.is_empty() {
            return Err(ValidationError::SheetNameEmpty);
        }
        let utf16_len = value.encode_utf16().count();
        if utf16_len > 31 {
            return Err(ValidationError::SheetNameTooLong { utf16_len });
        }
        if value.starts_with('\'') || value.ends_with('\'') {
            return Err(ValidationError::SheetNameApostropheBoundary);
        }
        if let Some(character) = value.chars().find(|character| {
            character.is_control() || matches!(character, ':' | '\\' | '/' | '?' | '*' | '[' | ']')
        }) {
            return Err(ValidationError::SheetNameInvalidCharacter { character });
        }
        let lookup_key = case_insensitive_key(&value).into_boxed_str();
        Ok(Self {
            original: value.into_boxed_str(),
            lookup_key,
        })
    }

    /// Returns the original spelling.
    pub fn as_str(&self) -> &str {
        &self.original
    }

    pub(crate) fn lookup_key(&self) -> &str {
        &self.lookup_key
    }
}

/// Sheet visibility as represented by `SpreadsheetML`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SheetVisibility {
    /// Visible in the workbook UI.
    #[default]
    Visible,
    /// Hidden, but users can make the sheet visible through the normal UI.
    Hidden,
    /// Hidden and unavailable to the normal unhide UI.
    VeryHidden,
}

#[derive(Debug, Clone, Default)]
struct CellStore {
    cells: PersistentRadixMap<Cell>,
    len: usize,
}

struct CellStoreValues<'a> {
    inner: PersistentRadixValues<'a, Cell>,
    remaining: usize,
}

impl<'a> Iterator for CellStoreValues<'a> {
    type Item = &'a Cell;

    fn next(&mut self) -> Option<Self::Item> {
        let value = self.inner.next()?;
        self.remaining -= 1;
        Some(value)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.remaining, Some(self.remaining))
    }
}

impl DoubleEndedIterator for CellStoreValues<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let value = self.inner.next_back()?;
        self.remaining -= 1;
        Some(value)
    }
}

impl ExactSizeIterator for CellStoreValues<'_> {}

impl CellStore {
    fn key(address: CellAddress) -> u128 {
        u128::from(address.row().get() - 1) * u128::from(crate::EXCEL_MAX_COLUMNS)
            + u128::from(address.column().get() - 1)
    }

    fn contains_key(&self, address: &CellAddress) -> bool {
        self.get(address).is_some()
    }

    fn get(&self, address: &CellAddress) -> Option<&Cell> {
        self.cells.get(Self::key(*address))
    }

    fn insert(&mut self, address: CellAddress, cell: Cell) -> bool {
        let previous = self.cells.insert(Self::key(address), cell);
        if previous.is_none() {
            self.len += 1;
        }
        previous.is_some()
    }

    fn remove(&mut self, address: &CellAddress) -> bool {
        if self.cells.get(Self::key(*address)).is_none() {
            return false;
        }
        let removed = self.cells.remove(Self::key(*address));
        if removed.is_some() {
            self.len -= 1;
        }
        removed.is_some()
    }

    fn values(&self) -> CellStoreValues<'_> {
        CellStoreValues {
            inner: self.cells.ordered_values(),
            remaining: self.len,
        }
    }

    const fn len(&self) -> usize {
        self.len
    }

    const fn is_empty(&self) -> bool {
        self.len == 0
    }

    fn semantic_fingerprint_cancellable(
        &self,
        cancelled: &impl Fn() -> bool,
    ) -> Result<[u8; 32], ()> {
        self.cells.semantic_fingerprint_cancellable(
            &|entries| {
                crate::calculation::identity::cell_chunk_fingerprint_cancellable(
                    entries.entries().map(|(_, cell)| cell),
                    entries.len(),
                    cancelled,
                )
            },
            &crate::calculation::identity::cell_store_node_fingerprint,
            cancelled,
        )
    }
}

/// A sparse, format-neutral worksheet.
#[derive(Debug, Clone)]
pub struct Sheet {
    id: SheetId,
    name: SheetName,
    visibility: SheetVisibility,
    cells: CellStore,
    formula_addresses: Arc<BTreeSet<CellAddress>>,
    column_max_rows: Arc<BTreeMap<u32, u32>>,
    bounds_dirty: bool,
    min_row: Option<Row>,
    min_column: Option<Column>,
    max_row: Option<Row>,
    max_column: Option<Column>,
    merged_ranges: Arc<Vec<CellRange>>,
    tables: Arc<Vec<Table>>,
    semantic_fingerprint: OnceLock<[u8; 32]>,
}

impl Sheet {
    pub(crate) fn clone_cancellable(&self, cancelled: &impl Fn() -> bool) -> Result<Self, ()> {
        if cancelled() {
            return Err(());
        }
        Ok(Self {
            id: self.id,
            name: self.name.clone(),
            visibility: self.visibility,
            cells: self.cells.clone(),
            formula_addresses: Arc::clone(&self.formula_addresses),
            column_max_rows: Arc::clone(&self.column_max_rows),
            bounds_dirty: self.bounds_dirty,
            min_row: self.min_row,
            min_column: self.min_column,
            max_row: self.max_row,
            max_column: self.max_column,
            merged_ranges: Arc::clone(&self.merged_ranges),
            tables: Arc::clone(&self.tables),
            semantic_fingerprint: self.semantic_fingerprint.clone(),
        })
    }

    /// Constructs an empty sparse sheet.
    pub fn new(id: SheetId, name: SheetName, visibility: SheetVisibility) -> Self {
        Self {
            id,
            name,
            visibility,
            cells: CellStore::default(),
            formula_addresses: Arc::new(BTreeSet::new()),
            column_max_rows: Arc::new(BTreeMap::new()),
            bounds_dirty: false,
            min_row: None,
            min_column: None,
            max_row: None,
            max_column: None,
            merged_ranges: Arc::new(Vec::new()),
            tables: Arc::new(Vec::new()),
            semantic_fingerprint: OnceLock::new(),
        }
    }

    /// Returns the sheet ID.
    pub const fn id(&self) -> SheetId {
        self.id
    }

    /// Returns the original validated name.
    pub const fn name(&self) -> &SheetName {
        &self.name
    }

    /// Returns the sheet visibility.
    pub const fn visibility(&self) -> SheetVisibility {
        self.visibility
    }

    /// Inserts a sparse cell and rejects duplicate addresses.
    ///
    /// # Errors
    ///
    /// Returns [`ValidationError::DuplicateCell`] when the address is already occupied.
    pub fn insert_cell(
        &mut self,
        address: CellAddress,
        content: CellContent,
    ) -> Result<(), ValidationError> {
        self.insert_cell_with_number_format(address, content, NumberFormat::default())
    }

    pub(crate) fn insert_cell_with_number_format(
        &mut self,
        address: CellAddress,
        content: CellContent,
        number_format: NumberFormat,
    ) -> Result<(), ValidationError> {
        if self.cells.contains_key(&address) {
            return Err(ValidationError::DuplicateCell {
                row: address.row().get(),
                column: address.column().get(),
            });
        }
        let is_formula = matches!(content, CellContent::Formula(_));
        self.semantic_fingerprint = OnceLock::new();
        self.update_bounds(address);
        self.update_column_extent(address);
        self.cells.insert(
            address,
            Cell::with_number_format(address, content, number_format),
        );
        if is_formula {
            Arc::make_mut(&mut self.formula_addresses).insert(address);
        }
        Ok(())
    }

    /// Returns a cell by typed address.
    pub fn cell(&self, address: CellAddress) -> Option<&Cell> {
        self.cells.get(&address)
    }

    /// Parses an unqualified A1 address and returns the corresponding sparse cell.
    ///
    /// # Errors
    ///
    /// Returns a [`ValidationError`] when `address` is not a valid unqualified A1 address.
    pub fn cell_by_a1(&self, address: &str) -> Result<Option<&Cell>, ValidationError> {
        Ok(self.cell(CellAddress::from_a1(address)?))
    }

    /// Iterates sparse cells in deterministic row-major order.
    pub fn cells(&self) -> impl ExactSizeIterator<Item = &Cell> + DoubleEndedIterator {
        self.cells.values()
    }

    pub(crate) fn column_max_rows(&self) -> &BTreeMap<u32, u32> {
        &self.column_max_rows
    }

    pub(crate) fn semantic_cell_store_fingerprint_cancellable(
        &self,
        cancelled: &impl Fn() -> bool,
    ) -> Result<[u8; 32], ()> {
        self.cells.semantic_fingerprint_cancellable(cancelled)
    }

    pub(crate) fn semantic_fingerprint_cancellable(
        &self,
        cancelled: &impl Fn() -> bool,
    ) -> Result<[u8; 32], ()> {
        if let Some(fingerprint) = self.semantic_fingerprint.get() {
            return Ok(*fingerprint);
        }
        let fingerprint =
            crate::calculation::identity::sheet_fingerprint_cancellable(self, cancelled)?;
        let _ = self.semantic_fingerprint.set(fingerprint);
        Ok(*self
            .semantic_fingerprint
            .get()
            .expect("sheet fingerprint was initialized"))
    }

    pub(crate) fn next_formula_cell_after(&self, after: Option<CellAddress>) -> Option<Cell> {
        let address = match after {
            Some(address) => self
                .formula_addresses
                .range(address..)
                .find(|candidate| **candidate > address),
            None => self.formula_addresses.first(),
        }?;
        self.cells.get(address).cloned()
    }

    /// Returns the number of stored sparse cells.
    pub fn len(&self) -> usize {
        self.cells.len()
    }

    /// Returns whether the sheet stores no cells.
    pub fn is_empty(&self) -> bool {
        self.cells.is_empty()
    }

    /// Returns merged ranges sorted by top-left address, then by bottom-right address.
    ///
    /// The reader stores only validated, pairwise non-overlapping, multi-cell ranges; entries
    /// that fail those checks are reported as diagnostics and dropped.
    pub fn merged_ranges(&self) -> &[CellRange] {
        self.merged_ranges.as_slice()
    }

    /// Stores the validated merged ranges for this sheet.
    ///
    /// Callers must pass ranges already sorted by `(start, end)` and pairwise non-overlapping;
    /// the reader's merge parser is the only producer of that order.
    pub(crate) fn set_merged_ranges(&mut self, merged_ranges: Vec<CellRange>) {
        self.semantic_fingerprint = OnceLock::new();
        self.merged_ranges = Arc::new(merged_ranges);
    }

    /// Returns this sheet's tables in XLSX declaration order.
    pub fn tables(&self) -> &[Table] {
        self.tables.as_slice()
    }

    /// Stores the validated tables for this sheet.
    ///
    /// Workbook-wide name uniqueness is enforced when the snapshot is constructed, not here.
    pub(crate) fn set_tables(&mut self, tables: Vec<Table>) {
        self.semantic_fingerprint = OnceLock::new();
        self.tables = Arc::new(tables);
    }

    pub(crate) fn tables_mut(&mut self) -> &mut [Table] {
        self.semantic_fingerprint = OnceLock::new();
        Arc::make_mut(&mut self.tables).as_mut_slice()
    }

    /// Returns the smallest bounding rectangle containing all sparse cells.
    pub fn used_range(&self) -> Option<CellRange> {
        let start = CellAddress::new(self.min_row?, self.min_column?);
        let end = CellAddress::new(self.max_row?, self.max_column?);
        Some(CellRange::from_ordered(start, end))
    }

    fn update_bounds(&mut self, address: CellAddress) {
        self.min_row = Some(
            self.min_row
                .map_or(address.row(), |row| row.min(address.row())),
        );
        self.min_column = Some(
            self.min_column
                .map_or(address.column(), |column| column.min(address.column())),
        );
        self.max_row = Some(
            self.max_row
                .map_or(address.row(), |row| row.max(address.row())),
        );
        self.max_column = Some(
            self.max_column
                .map_or(address.column(), |column| column.max(address.column())),
        );
    }

    fn update_column_extent(&mut self, address: CellAddress) {
        Arc::make_mut(&mut self.column_max_rows)
            .entry(address.column().get())
            .and_modify(|row| *row = (*row).max(address.row().get()))
            .or_insert(address.row().get());
    }

    pub(crate) fn upsert_cell(
        &mut self,
        address: CellAddress,
        content: CellContent,
        number_format: NumberFormat,
    ) {
        self.upsert_cell_deferred(address, content, number_format);
        self.finish_deferred_cell_edits();
    }

    pub(crate) fn upsert_cell_deferred(
        &mut self,
        address: CellAddress,
        content: CellContent,
        number_format: NumberFormat,
    ) {
        self.semantic_fingerprint = OnceLock::new();
        self.track_formula_address(address, &content);
        let is_new = !self.cells.insert(
            address,
            Cell::with_content_and_number_format(address, content, number_format),
        );
        if is_new {
            self.update_bounds(address);
            self.update_column_extent(address);
        }
    }

    pub(crate) fn upsert_cell_instance_deferred(&mut self, cell: Cell) {
        let address = cell.address();
        self.semantic_fingerprint = OnceLock::new();
        self.track_formula_address(address, cell.content());
        let is_new = !self.cells.insert(address, cell);
        if is_new {
            self.update_bounds(address);
            self.update_column_extent(address);
        }
    }

    pub(crate) fn remove_cell_deferred(&mut self, address: CellAddress) -> bool {
        self.semantic_fingerprint = OnceLock::new();
        if self.formula_addresses.contains(&address) {
            Arc::make_mut(&mut self.formula_addresses).remove(&address);
        }
        let removed = self.cells.remove(&address);
        if removed
            && (self.min_row == Some(address.row())
                || self.max_row == Some(address.row())
                || self.min_column == Some(address.column())
                || self.max_column == Some(address.column())
                || self
                    .column_max_rows
                    .get(&address.column().get())
                    .is_some_and(|row| *row == address.row().get()))
        {
            self.bounds_dirty = true;
        }
        removed
    }

    pub(crate) fn finish_deferred_cell_edits(&mut self) {
        if self.bounds_dirty {
            self.rebuild_bounds();
        }
    }

    pub(crate) fn rename(&mut self, name: SheetName) {
        self.semantic_fingerprint = OnceLock::new();
        self.name = name;
    }

    pub(crate) fn set_visibility(&mut self, visibility: SheetVisibility) {
        self.semantic_fingerprint = OnceLock::new();
        self.visibility = visibility;
    }

    fn rebuild_bounds(&mut self) {
        self.min_row = None;
        self.min_column = None;
        self.max_row = None;
        self.max_column = None;
        self.column_max_rows = Arc::new(BTreeMap::new());
        let addresses = self.cells.values().map(Cell::address).collect::<Vec<_>>();
        for address in addresses {
            self.update_bounds(address);
            self.update_column_extent(address);
        }
        self.bounds_dirty = false;
    }

    fn track_formula_address(&mut self, address: CellAddress, content: &CellContent) {
        if matches!(content, CellContent::Formula(_)) {
            if !self.formula_addresses.contains(&address) {
                Arc::make_mut(&mut self.formula_addresses).insert(address);
            }
        } else if self.formula_addresses.contains(&address) {
            Arc::make_mut(&mut self.formula_addresses).remove(&address);
        }
    }
}

/// Excel's serial date epoch selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum DateSystem {
    /// The default 1900 date system, including Excel's historical leap-year behavior.
    #[default]
    Excel1900,
    /// The alternative 1904 date system.
    Excel1904,
}

/// Workbook calculation mode metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CalculationMode {
    /// Recalculate automatically.
    Automatic,
    /// Recalculate automatically except for data tables.
    AutomaticExceptDataTables,
    /// Recalculate only when explicitly requested.
    Manual,
}

/// Calculation hints read from workbook metadata without triggering calculation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct CalculationHints {
    mode: Option<CalculationMode>,
    calculation_id: Option<u32>,
    full_calculation_on_load: Option<bool>,
    force_full_calculation: Option<bool>,
    iterative_calculation: Option<bool>,
}

impl CalculationHints {
    /// Constructs workbook calculation hints.
    pub const fn new(
        mode: Option<CalculationMode>,
        calculation_id: Option<u32>,
        full_calculation_on_load: Option<bool>,
        force_full_calculation: Option<bool>,
    ) -> Self {
        Self {
            mode,
            calculation_id,
            full_calculation_on_load,
            force_full_calculation,
            iterative_calculation: None,
        }
    }

    /// Returns a copy with the declared iterative-calculation flag.
    pub const fn with_iterative_calculation(mut self, iterative_calculation: Option<bool>) -> Self {
        self.iterative_calculation = iterative_calculation;
        self
    }

    /// Returns the declared calculation mode.
    pub const fn mode(self) -> Option<CalculationMode> {
        self.mode
    }

    /// Returns the producer's calculation engine ID.
    pub const fn calculation_id(self) -> Option<u32> {
        self.calculation_id
    }

    /// Returns the full-calculation-on-load flag.
    pub const fn full_calculation_on_load(self) -> Option<bool> {
        self.full_calculation_on_load
    }

    /// Returns the force-full-calculation flag.
    pub const fn force_full_calculation(self) -> Option<bool> {
        self.force_full_calculation
    }

    /// Returns the declared iterative-calculation flag.
    pub const fn iterative_calculation(self) -> Option<bool> {
        self.iterative_calculation
    }
}

/// The caller-facing input adapter that supplied workbook bytes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum WorkbookSourceKind {
    /// Source details are not available.
    #[default]
    Unknown,
    /// A filesystem path adapter supplied the bytes.
    Path,
    /// An in-memory byte buffer supplied the bytes.
    Bytes,
    /// A generic `Read + Seek` adapter supplied the bytes.
    Reader,
}

/// Non-sensitive source metadata retained by the snapshot.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct WorkbookSource {
    kind: WorkbookSourceKind,
    byte_length: Option<u64>,
}

impl WorkbookSource {
    /// Constructs source metadata without retaining a host path.
    pub const fn new(kind: WorkbookSourceKind, byte_length: Option<u64>) -> Self {
        Self { kind, byte_length }
    }

    /// Returns the input adapter kind.
    pub const fn kind(self) -> WorkbookSourceKind {
        self.kind
    }

    /// Returns the input byte length, when known.
    pub const fn byte_length(self) -> Option<u64> {
        self.byte_length
    }
}

/// A versioned, history-independent digest of workbook semantics.
///
/// The digest is an equality and stale-state aid. It is not an input-file hash, electronic
/// signature, or authenticity proof.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WorkbookFingerprint {
    schema_version: u16,
    bytes: [u8; 32],
}

impl WorkbookFingerprint {
    /// The semantic hashing schema used by this CellRune release.
    pub const CURRENT_SCHEMA_VERSION: u16 =
        crate::calculation::identity::WORKBOOK_FINGERPRINT_SCHEMA_VERSION as u16;

    pub(crate) const fn current(bytes: [u8; 32]) -> Self {
        Self {
            schema_version: Self::CURRENT_SCHEMA_VERSION,
            bytes,
        }
    }

    /// Returns the semantic hashing schema version.
    pub const fn schema_version(self) -> u16 {
        self.schema_version
    }

    /// Returns the stable digest bytes.
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.bytes
    }

    /// Returns the digest as lower-case hexadecimal without a prefix.
    pub fn to_hex(self) -> String {
        self.to_string()
    }
}

impl fmt::Display for WorkbookFingerprint {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in self.bytes {
            write!(formatter, "{byte:02x}")?;
        }
        Ok(())
    }
}

/// An immutable workbook snapshot with deterministic sheet lookup and order.
#[derive(Debug, Clone)]
pub struct WorkbookSnapshot {
    sheets: Vec<Sheet>,
    sheet_identity: PersistentRadixMap<Sheet>,
    sheet_id_index: BTreeMap<SheetId, usize>,
    sheet_name_index: BTreeMap<Box<str>, usize>,
    table_index: TableIndex,
    defined_names: Vec<DefinedName>,
    defined_name_index: BTreeMap<DefinedNameScope, BTreeMap<Box<str>, usize>>,
    diagnostics: Vec<Diagnostic>,
    date_system: DateSystem,
    calculation_hints: CalculationHints,
    source: WorkbookSource,
    provenance: Provenance,
    semantic_revision: u64,
    semantic_fingerprint: OnceLock<[u8; 32]>,
}

pub(crate) enum WorkbookBuildError {
    Validation(ValidationError),
    Cancelled,
}

pub(crate) struct WorkbookSnapshotInput {
    pub(crate) sheets: Vec<Sheet>,
    pub(crate) defined_names: Vec<DefinedName>,
    pub(crate) diagnostics: Vec<Diagnostic>,
    pub(crate) date_system: DateSystem,
    pub(crate) calculation_hints: CalculationHints,
    pub(crate) source: WorkbookSource,
    pub(crate) provenance: Provenance,
}

impl From<ValidationError> for WorkbookBuildError {
    fn from(error: ValidationError) -> Self {
        Self::Validation(error)
    }
}

impl WorkbookSnapshot {
    #[cfg(test)]
    pub(crate) fn clone_cancellable(&self, cancelled: &impl Fn() -> bool) -> Result<Self, ()> {
        let mut sheets = Vec::with_capacity(self.sheets.len());
        for sheet in &self.sheets {
            if cancelled() {
                return Err(());
            }
            sheets.push(sheet.clone_cancellable(cancelled)?);
        }
        let mut defined_names = Vec::with_capacity(self.defined_names.len());
        for name in &self.defined_names {
            if cancelled() {
                return Err(());
            }
            defined_names.push(name.clone());
        }
        let mut defined_name_index = BTreeMap::new();
        for (scope, names) in &self.defined_name_index {
            if cancelled() {
                return Err(());
            }
            defined_name_index.insert(*scope, clone_map_cancellable(names, cancelled)?);
        }
        let mut diagnostics = Vec::with_capacity(self.diagnostics.len());
        for diagnostic in &self.diagnostics {
            if cancelled() {
                return Err(());
            }
            diagnostics.push(diagnostic.clone());
        }
        if cancelled() {
            return Err(());
        }
        Ok(Self {
            sheet_identity: self.sheet_identity.clone(),
            sheets,
            sheet_id_index: clone_map_cancellable(&self.sheet_id_index, cancelled)?,
            sheet_name_index: clone_map_cancellable(&self.sheet_name_index, cancelled)?,
            table_index: self.table_index.clone_cancellable(cancelled)?,
            defined_names,
            defined_name_index,
            diagnostics,
            date_system: self.date_system,
            calculation_hints: self.calculation_hints,
            source: self.source,
            provenance: self.provenance.clone(),
            semantic_revision: self.semantic_revision,
            semantic_fingerprint: self.semantic_fingerprint.clone(),
        })
    }

    pub(crate) fn new_draft() -> Self {
        let sheet_id = SheetId(NonZeroU32::MIN);
        let sheet_name = SheetName {
            original: Box::from("Sheet1"),
            lookup_key: Box::from("sheet1"),
        };
        let sheet = Sheet::new(sheet_id, sheet_name, SheetVisibility::Visible);
        let mut sheet_id_index = BTreeMap::new();
        sheet_id_index.insert(sheet_id, 0);
        let mut sheet_name_index = BTreeMap::new();
        sheet_name_index.insert(Box::from("sheet1"), 0);
        Self {
            sheet_identity: sheet_identity_store_cancellable(std::slice::from_ref(&sheet), &|| {
                false
            })
            .expect("the draft identity store cannot be cancelled"),
            sheets: vec![sheet],
            sheet_id_index,
            sheet_name_index,
            table_index: TableIndex::default(),
            defined_names: Vec::new(),
            defined_name_index: BTreeMap::new(),
            diagnostics: Vec::new(),
            date_system: DateSystem::Excel1900,
            calculation_hints: CalculationHints::default(),
            source: WorkbookSource::default(),
            provenance: Provenance::new(crate::ProviderIdentity::writer(), None),
            semantic_revision: 0,
            semantic_fingerprint: OnceLock::new(),
        }
    }

    /// Validates workbook-level uniqueness and constructs an immutable snapshot.
    ///
    /// # Errors
    ///
    /// Returns a [`ValidationError`] when sheet IDs or names are not unique.
    pub fn new(
        sheets: Vec<Sheet>,
        date_system: DateSystem,
        calculation_hints: CalculationHints,
        source: WorkbookSource,
        provenance: Provenance,
    ) -> Result<Self, ValidationError> {
        Self::new_with_metadata(
            sheets,
            Vec::new(),
            Vec::new(),
            date_system,
            calculation_hints,
            source,
            provenance,
        )
    }

    /// Validates and constructs a snapshot with names and compatibility diagnostics.
    ///
    /// # Errors
    ///
    /// Returns a [`ValidationError`] when sheet IDs, sheet names, defined names, or table
    /// identities violate workbook-level uniqueness and scope constraints.
    pub fn new_with_metadata(
        sheets: Vec<Sheet>,
        defined_names: Vec<DefinedName>,
        diagnostics: Vec<Diagnostic>,
        date_system: DateSystem,
        calculation_hints: CalculationHints,
        source: WorkbookSource,
        provenance: Provenance,
    ) -> Result<Self, ValidationError> {
        match Self::new_with_metadata_cancellable(
            WorkbookSnapshotInput {
                sheets,
                defined_names,
                diagnostics,
                date_system,
                calculation_hints,
                source,
                provenance,
            },
            &|| false,
        ) {
            Ok(workbook) => Ok(workbook),
            Err(WorkbookBuildError::Validation(error)) => Err(error),
            Err(WorkbookBuildError::Cancelled) => {
                unreachable!("the non-cancellable snapshot builder cannot be cancelled")
            }
        }
    }

    pub(crate) fn new_with_metadata_cancellable(
        input: WorkbookSnapshotInput,
        cancelled: &impl Fn() -> bool,
    ) -> Result<Self, WorkbookBuildError> {
        Self::new_with_metadata_cancellable_reusing_identity(input, None, cancelled)
    }

    pub(crate) fn new_with_metadata_cancellable_from_previous(
        input: WorkbookSnapshotInput,
        previous: &Self,
        touched_sheets: &BTreeSet<SheetId>,
        cancelled: &impl Fn() -> bool,
    ) -> Result<Self, WorkbookBuildError> {
        Self::new_with_metadata_cancellable_reusing_identity(
            input,
            Some((previous, touched_sheets)),
            cancelled,
        )
    }

    fn new_with_metadata_cancellable_reusing_identity(
        input: WorkbookSnapshotInput,
        identity_base: Option<(&Self, &BTreeSet<SheetId>)>,
        cancelled: &impl Fn() -> bool,
    ) -> Result<Self, WorkbookBuildError> {
        let WorkbookSnapshotInput {
            sheets,
            defined_names,
            diagnostics,
            date_system,
            calculation_hints,
            source,
            provenance,
        } = input;
        let mut sheet_id_index = BTreeMap::new();
        let mut sheet_name_index = BTreeMap::new();
        let mut identity_order_unchanged =
            identity_base.is_some_and(|(previous, _)| previous.sheets.len() == sheets.len());
        for (index, sheet) in sheets.iter().enumerate() {
            if cancelled() {
                return Err(WorkbookBuildError::Cancelled);
            }
            if identity_order_unchanged
                && identity_base
                    .and_then(|(previous, _)| previous.sheets.get(index))
                    .is_none_or(|previous| previous.id() != sheet.id())
            {
                identity_order_unchanged = false;
            }
            if sheet_id_index.insert(sheet.id(), index).is_some() {
                return Err(ValidationError::DuplicateSheetId {
                    value: sheet.id().get(),
                }
                .into());
            }
            let name_key = Box::<str>::from(sheet.name().lookup_key());
            if sheet_name_index.insert(name_key, index).is_some() {
                return Err(ValidationError::DuplicateSheetName {
                    name: sheet.name().as_str().to_owned(),
                }
                .into());
            }
        }
        let mut defined_name_index = BTreeMap::<DefinedNameScope, BTreeMap<Box<str>, usize>>::new();
        let mut defined_name_keys = std::collections::BTreeSet::<Box<str>>::new();
        for (index, defined_name) in defined_names.iter().enumerate() {
            if cancelled() {
                return Err(WorkbookBuildError::Cancelled);
            }
            if let DefinedNameScope::Sheet(sheet_id) = defined_name.scope()
                && !sheet_id_index.contains_key(&sheet_id)
            {
                return Err(ValidationError::DefinedNameUnknownSheet {
                    sheet_id: sheet_id.get(),
                }
                .into());
            }
            let previous = defined_name_index
                .entry(defined_name.scope())
                .or_default()
                .insert(Box::from(defined_name.lookup_key()), index);
            if previous.is_some() {
                return Err(ValidationError::DuplicateDefinedName {
                    name: defined_name.name().to_owned(),
                }
                .into());
            }
            defined_name_keys.insert(Box::from(defined_name.lookup_key()));
        }
        let table_index = TableIndex::new_cancellable(&sheets, &defined_name_keys, cancelled)
            .map_err(|error| match error {
                TableIndexBuildError::Validation(error) => WorkbookBuildError::Validation(error),
                TableIndexBuildError::Cancelled => WorkbookBuildError::Cancelled,
            })?;
        if cancelled() {
            return Err(WorkbookBuildError::Cancelled);
        }
        let sheet_identity = if identity_order_unchanged {
            let (previous, touched_sheets) =
                identity_base.expect("unchanged identity order requires a previous workbook");
            let mut identity = previous.sheet_identity.clone();
            for sheet_id in touched_sheets {
                if cancelled() {
                    return Err(WorkbookBuildError::Cancelled);
                }
                let Some(index) = sheet_id_index.get(sheet_id).copied() else {
                    return Err(ValidationError::UnknownSheetId {
                        value: sheet_id.get(),
                    }
                    .into());
                };
                identity.insert(index as u128, sheets[index].clone());
            }
            identity
        } else {
            sheet_identity_store_cancellable(&sheets, cancelled)
                .map_err(|()| WorkbookBuildError::Cancelled)?
        };
        Ok(Self {
            sheet_identity,
            sheets,
            sheet_id_index,
            sheet_name_index,
            table_index,
            defined_names,
            defined_name_index,
            diagnostics,
            date_system,
            calculation_hints,
            source,
            provenance,
            semantic_revision: 0,
            semantic_fingerprint: OnceLock::new(),
        })
    }

    /// Returns sheets in workbook order.
    pub fn sheets(&self) -> &[Sheet] {
        &self.sheets
    }

    /// Returns defined names in workbook XML order.
    pub fn defined_names(&self) -> &[DefinedName] {
        &self.defined_names
    }

    pub(crate) fn defined_name(
        &self,
        scope: DefinedNameScope,
        name: &str,
    ) -> Option<(usize, &DefinedName)> {
        let key = case_insensitive_key(name);
        let index = *self.defined_name_index.get(&scope)?.get(key.as_str())?;
        Some((index, &self.defined_names[index]))
    }

    /// Returns deterministic read-time compatibility diagnostics.
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Returns a sheet by stable ID.
    pub fn sheet_by_id(&self, id: SheetId) -> Option<&Sheet> {
        self.sheet_id_index
            .get(&id)
            .map(|index| &self.sheets[*index])
    }

    pub(crate) fn sheet_position(&self, id: SheetId) -> Option<usize> {
        self.sheet_id_index.get(&id).copied()
    }

    /// Returns a table by its workbook-global formula/UI display name.
    ///
    /// OOXML `displayName` values are workbook-global and case-insensitive even though each table
    /// is owned by its worksheet. The worksheet-local programmatic `name` is not a lookup key.
    pub fn table(&self, name: &str) -> Option<&Table> {
        let location = self.table_location(name)?;
        Some(&self.sheets[location.sheet_index].tables()[location.table_index])
    }

    /// Returns a table by its stable workbook-local identifier.
    pub fn table_by_id(&self, id: TableId) -> Option<&Table> {
        let location = self.table_location_by_id(id)?;
        Some(&self.sheets[location.sheet_index].tables()[location.table_index])
    }

    /// Returns a table column by the stable identities of its table and column.
    pub fn table_column_by_id(
        &self,
        table_id: TableId,
        column_id: TableColumnId,
    ) -> Option<&TableColumn> {
        let location = self.table_column_location_by_id(table_id, column_id)?;
        Some(
            &self.sheets[location.table.sheet_index].tables()[location.table.table_index].columns()
                [location.column_index],
        )
    }

    /// Returns a table column by stable table ID and case-insensitive column name.
    pub fn table_column(&self, table_id: TableId, name: &str) -> Option<&TableColumn> {
        let location = self.table_column_location(table_id, name)?;
        Some(
            &self.sheets[location.table.sheet_index].tables()[location.table.table_index].columns()
                [location.column_index],
        )
    }

    /// Returns the table whose full range contains one worksheet address.
    pub fn containing_table(&self, sheet_id: SheetId, address: CellAddress) -> Option<&Table> {
        let location = self.containing_table_location(sheet_id, address)?;
        Some(&self.sheets[location.sheet_index].tables()[location.table_index])
    }

    pub(crate) fn table_location(&self, name: &str) -> Option<TableLocation> {
        self.table_index.by_display_name(name)
    }

    pub(crate) fn table_location_by_id(&self, table_id: TableId) -> Option<TableLocation> {
        self.table_index.by_id(table_id)
    }

    pub(crate) fn table_column_location_by_id(
        &self,
        table_id: TableId,
        column_id: TableColumnId,
    ) -> Option<TableColumnLocation> {
        self.table_index.column_by_id(table_id, column_id)
    }

    pub(crate) fn table_column_location(
        &self,
        table_id: TableId,
        name: &str,
    ) -> Option<TableColumnLocation> {
        self.table_index.column_by_name(table_id, name)
    }

    pub(crate) fn containing_table_location(
        &self,
        sheet_id: SheetId,
        address: CellAddress,
    ) -> Option<TableLocation> {
        self.table_index.containing(sheet_id, address)
    }

    /// Returns a sheet using deterministic case-insensitive lookup.
    pub fn sheet_by_name(&self, name: &str) -> Option<&Sheet> {
        self.sheet_index_by_name(name)
            .map(|index| &self.sheets[index])
    }

    pub(crate) fn sheet_index_by_name(&self, name: &str) -> Option<usize> {
        let key = case_insensitive_key(name);
        self.sheet_name_index.get(key.as_str()).copied()
    }

    /// Returns the workbook date system.
    pub const fn date_system(&self) -> DateSystem {
        self.date_system
    }

    /// Returns calculation metadata without initiating recalculation.
    pub const fn calculation_hints(&self) -> CalculationHints {
        self.calculation_hints
    }

    /// Returns non-sensitive source metadata.
    pub const fn source(&self) -> WorkbookSource {
        self.source
    }

    /// Returns snapshot provenance.
    pub const fn provenance(&self) -> &Provenance {
        &self.provenance
    }

    /// Returns the monotonic semantic revision associated with this snapshot.
    pub const fn semantic_revision(&self) -> u64 {
        self.semantic_revision
    }

    /// Returns the versioned, history-independent semantic fingerprint.
    pub fn fingerprint(&self) -> WorkbookFingerprint {
        WorkbookFingerprint::current(
            self.semantic_fingerprint_cancellable(&|| false)
                .expect("non-cancellable fingerprinting cannot be cancelled"),
        )
    }

    pub(crate) fn semantic_fingerprint_cancellable(
        &self,
        cancelled: &impl Fn() -> bool,
    ) -> Result<[u8; 32], ()> {
        if let Some(fingerprint) = self.semantic_fingerprint.get() {
            return Ok(*fingerprint);
        }
        let fingerprint =
            crate::calculation::identity::workbook_fingerprint_cancellable(self, cancelled)?;
        let _ = self.semantic_fingerprint.set(fingerprint);
        Ok(*self
            .semantic_fingerprint
            .get()
            .expect("semantic fingerprint was initialized"))
    }

    pub(crate) fn semantic_sheet_tree_fingerprint_cancellable(
        &self,
        cancelled: &impl Fn() -> bool,
    ) -> Result<[u8; 32], ()> {
        self.sheet_identity.semantic_fingerprint_cancellable(
            &|entries| {
                crate::calculation::identity::sheet_tree_leaf_fingerprint_cancellable(
                    entries.entries(),
                    entries.len(),
                    cancelled,
                )
            },
            &crate::calculation::identity::sheet_tree_node_fingerprint,
            cancelled,
        )
    }

    pub(crate) const fn with_semantic_revision(mut self, semantic_revision: u64) -> Self {
        self.semantic_revision = semantic_revision;
        self
    }
}

fn sheet_identity_store_cancellable(
    sheets: &[Sheet],
    cancelled: &impl Fn() -> bool,
) -> Result<PersistentRadixMap<Sheet>, ()> {
    PersistentRadixMap::from_sorted_iter_cancellable(
        sheets
            .iter()
            .enumerate()
            .map(|(index, sheet)| (index as u128, sheet.clone())),
        cancelled,
    )
}

fn case_insensitive_key(value: &str) -> String {
    value.chars().flat_map(char::to_lowercase).collect()
}