sheets-diff 2.2.2

Structured diff engine for Microsoft Excel .xlsx workbooks
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
//! Public result data model.
//!
//! All types here are normatively defined in RFC-033.  This module owns
//! construction and the summary / change-kind derivation logic; the field
//! shapes are fixed by the canonical lexicon.

use std::fmt;

#[cfg(feature = "serde")]
use serde::Serialize;

use crate::address::{CellAddress, ComparedRange};

// ---------------------------------------------------------------------------
// Side
// ---------------------------------------------------------------------------

/// Which workbook of the pair a piece of data refers to.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum Side {
    Old,
    New,
}

impl fmt::Display for Side {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Side::Old => f.write_str("old"),
            Side::New => f.write_str("new"),
        }
    }
}

// ---------------------------------------------------------------------------
// Source description
// ---------------------------------------------------------------------------

/// What kind of input source a workbook came from.
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum SourceKind {
    Path,
    Bytes,
    Reader,
    Unknown,
}

/// Caller-visible description of a workbook input source.
///
/// `display_name` is never an absolute path unless the caller explicitly
/// provided it as such.
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct SourceDescription {
    pub kind: SourceKind,
    pub display_name: Option<String>,
}

// ---------------------------------------------------------------------------
// Per-side workbook metadata
// ---------------------------------------------------------------------------

#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct WorkbookSideInfo {
    pub source: SourceDescription,
    pub workbook_name: Option<String>,
    pub sheet_count: usize,
}

// ---------------------------------------------------------------------------
// Sheet identity
// ---------------------------------------------------------------------------

/// A reference to a specific sheet in one workbook.
///
/// `index` is **0-based** workbook order (as returned by calamine).
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct SheetRef {
    pub name: String,
    pub index: usize,
}

// ---------------------------------------------------------------------------
// Sheet change classification (RFC-009 / RFC-033 §6)
// ---------------------------------------------------------------------------

/// How confident the sheet-matching algorithm is about a non-exact pairing.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum MatchConfidence {
    Exact,
    High,
    Medium,
    Low,
}

/// The reason a non-exact sheet pair was formed.
#[non_exhaustive]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum SheetMatchReason {
    ExactName,
    IndexAndContent,
    ContentSimilarity,
}

/// How a sheet pair was classified.
///
/// Names and indices live in `SheetDiff.old_sheet` / `SheetDiff.new_sheet`;
/// they are **not** duplicated inside the variant payloads.
#[non_exhaustive]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum SheetChange {
    /// Name-matched, index unchanged, no cell differences.
    Unchanged,
    /// Name-matched (or rename-matched), has cell differences.
    Modified,
    /// New sheet with no counterpart in the old workbook.
    Added,
    /// Old sheet with no counterpart in the new workbook.
    Removed,
    /// Name-matched, but the tab index moved between the two workbooks.
    Moved,
    /// Name changed; heuristically matched.
    Renamed { confidence: MatchConfidence, reason: SheetMatchReason },
    /// Both renamed and moved.
    RenamedAndMoved { confidence: MatchConfidence, reason: SheetMatchReason },
}

// ---------------------------------------------------------------------------
// CellValue and components (RFC-007 / RFC-033 §2–§3)
// ---------------------------------------------------------------------------

/// Spreadsheet-serial date/time value captured from calamine.
///
/// `serial` is the Excel date serial (days since 1900-01-00 or 1904-01-01).
/// `is_1904` distinguishes the two date systems.
/// `iso` is populated when calamine provides an ISO string directly or when the
/// `chrono` feature can synthesize one.
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct CellDateTime {
    pub serial: f64,
    pub is_1904: bool,
    pub kind: DateTimeKind,
    pub iso: Option<String>,
}

/// Whether an Excel date serial represents a date, time, or datetime.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum DateTimeKind {
    DateTime,
    Date,
    Time,
}

/// Spreadsheet-serial duration value (ISO 8601 duration string when available).
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct CellDuration {
    pub serial: f64,
    pub iso: Option<String>,
}

/// Typed spreadsheet cell error.
///
/// Maps 1-to-1 with calamine's `CellErrorType`; `Other` handles forward-compat.
#[non_exhaustive]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum CellError {
    Div0,
    NA,
    Name,
    Null,
    Num,
    Ref,
    Value,
    GettingData,
    Other(String),
}

impl fmt::Display for CellError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CellError::Div0 => f.write_str("#DIV/0!"),
            CellError::NA => f.write_str("#N/A"),
            CellError::Name => f.write_str("#NAME?"),
            CellError::Null => f.write_str("#NULL!"),
            CellError::Num => f.write_str("#NUM!"),
            CellError::Ref => f.write_str("#REF!"),
            CellError::Value => f.write_str("#VALUE!"),
            CellError::GettingData => f.write_str("#GETTING_DATA"),
            CellError::Other(s) => write!(f, "#{s}"),
        }
    }
}

/// Typed representation of a spreadsheet cell value (RFC-033 §2).
///
/// `Integer` and `Number` are kept distinct (reflecting calamine's `Data::Int`
/// / `Data::Float`).  Default comparison treats `Integer(1)` vs `Number(1.0)`
/// as a `TypeChanged` difference; cross-type numeric equality is opt-in
/// (RFC-019).
#[non_exhaustive]
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum CellValue {
    Empty,
    Text(String),
    Integer(i64),
    Number(f64),
    Bool(bool),
    DateTime(CellDateTime),
    Duration(CellDuration),
    Error(CellError),
    Unsupported { display: String, reason: String },
}

impl CellValue {
    /// A human-readable display string.  For use in reports only; never used
    /// as an equality key.
    pub fn display_string(&self) -> String {
        match self {
            CellValue::Empty => String::new(),
            CellValue::Text(s) => s.clone(),
            CellValue::Integer(i) => i.to_string(),
            CellValue::Number(f) => f.to_string(),
            CellValue::Bool(b) => b.to_string(),
            CellValue::DateTime(dt) => {
                dt.iso.clone().unwrap_or_else(|| dt.serial.to_string())
            }
            CellValue::Duration(d) => {
                d.iso.clone().unwrap_or_else(|| d.serial.to_string())
            }
            CellValue::Error(e) => e.to_string(),
            CellValue::Unsupported { display, .. } => display.clone(),
        }
    }

    /// True if the value is `Empty`.
    pub fn is_empty(&self) -> bool {
        matches!(self, CellValue::Empty)
    }

    /// Alias for `display_string` — preferred name per RFC-020.
    #[inline]
    pub fn display_default(&self) -> String {
        self.display_string()
    }
}

// ---------------------------------------------------------------------------
// Display metadata (RFC-020)
// ---------------------------------------------------------------------------

/// Where a display string originated.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum DisplaySource {
    /// Provided directly by the workbook reader.
    ReaderProvided,
    /// Synthesised by `sheets-diff` from the typed value.
    SheetsDiffDefault,
    /// Substituted by the calling application.
    ApplicationProvided,
}

/// A number-format identifier and/or code string captured from the workbook.
///
/// In calamine 0.35 neither field is available from cell data; both are
/// `None` in v2.2. The struct is reserved so RFC-022 can populate it
/// without an API break.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct CellNumberFormat {
    /// Excel built-in format ID (e.g. `4` for `#,##0.00`).
    pub id: Option<u32>,
    /// Raw format code string (e.g. `"#,##0.00"`).
    pub code: Option<String>,
}

/// Human-friendly display metadata attached to a cell value (RFC-020).
///
/// `text` is the primary display string. `format` and `source` are optional
/// metadata; consumers may use them for localisation or formatting hints.
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct CellDisplay {
    /// The display string — deterministic and locale-neutral by default.
    pub text: String,
    /// Number-format metadata when available (always `None` in calamine 0.35).
    pub format: Option<CellNumberFormat>,
    pub source: DisplaySource,
}

impl CellDisplay {
    /// Build a default display from a `CellValue`.
    pub fn from_value(value: &CellValue) -> Self {
        Self {
            text: value.display_default(),
            format: None,
            source: DisplaySource::SheetsDiffDefault,
        }
    }
}

/// A full snapshot of one cell: typed value + optional formula + optional display
/// metadata (RFC-020).
///
/// `display` is populated by default using `CellDisplay::from_value`; it can be
/// overridden by the calling application without touching the typed value.
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct CellSnapshot {
    pub value: CellValue,
    pub formula: Option<crate::model::FormulaText>,
    pub display: Option<CellDisplay>,
}

impl CellSnapshot {
    /// Return the best available display string: `display.text` when present,
    /// otherwise `value.display_default()`.
    pub fn preferred_display(&self) -> String {
        self.display
            .as_ref()
            .map(|d| d.text.clone())
            .unwrap_or_else(|| self.value.display_default())
    }
}

// ---------------------------------------------------------------------------
// Cell change model (RFC-010 / RFC-033 §5)
// ---------------------------------------------------------------------------

/// Why two `CellValue`s were considered different.
#[non_exhaustive]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum ValueDifferenceKind {
    /// The Rust enum variant changed (e.g. `Integer` → `Number`).
    TypeChanged,
    /// Same type, different content.
    ContentChanged,
    /// Same float type, outside the configured tolerance.
    NumericOutsideTolerance,
    /// Date/time serial or kind changed.
    DateTimeChanged,
    /// `CellError` variant changed.
    ErrorKindChanged,
    /// Compared as display strings (opt-in policy); strings differed.
    DisplayStringChanged,
}

/// A value-layer change at one cell address.
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ValueChange {
    pub old: CellValue,
    pub new: CellValue,
    pub reason: ValueDifferenceKind,
}

/// A formula's text, with an optional normalised form.
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct FormulaText {
    pub raw: String,
    /// `None` unless the `NormalizedText` formula-compare mode is enabled and
    /// a normaliser is available (RFC-018).
    pub normalized: Option<String>,
}

/// A formula-layer change at one cell address.
///
/// `None` in `old` or `new` means the formula was added or removed.
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct FormulaChange {
    pub old: Option<FormulaText>,
    pub new: Option<FormulaText>,
}

/// Reserved for RFC-022 (style/format diffs).  Always `None` in v2.0.
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct FormatChange {
    // Fields added in v2.x once RFC-022 is implemented.
}

/// Derived classification of a `CellDiff` entry.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum CellChangeKind {
    Added,
    Removed,
    Modified,
}

/// A merged per-cell diff entry (RFC-033 §5).
///
/// **One `CellDiff` per logical address.** This is the intended consumer model:
/// a value change and a formula change at the same address are *facets of one
/// change*, carried in the independent `value` and `formula` sub-fields, not
/// two separate entries. The `output::view::CellChangeRow` projection follows
/// the same rule (one row per address, with `formula_changed` / `old_formula` /
/// `new_formula` describing the formula facet). Consumers migrating from a
/// per-facet model should collapse to one row per address rather than preserve
/// the split.
///
/// `change_kind()` is derived from the sub-fields, not stored.
#[non_exhaustive]
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct CellDiff {
    pub address: CellAddress,
    pub value: Option<ValueChange>,
    pub formula: Option<FormulaChange>,
    /// Reserved until RFC-022.
    pub format: Option<FormatChange>,
    pub diagnostics: Vec<Diagnostic>,
}

impl CellDiff {
    /// Derive Added / Removed / Modified from the sub-change fields.
    ///
    /// - **Added**: every present sub-change has an empty/absent `old` side.
    /// - **Removed**: every present sub-change has an empty/absent `new` side.
    /// - **Modified**: otherwise.
    ///
    /// This derivation is **stable API**: the rule above will not change within
    /// a major version, so downstream code may depend on it rather than
    /// re-deriving presence classification from the sub-fields.
    pub fn change_kind(&self) -> CellChangeKind {
        let has_old = self.value.as_ref().map(|v| !v.old.is_empty()).unwrap_or(false)
            || self.formula.as_ref().map(|f| f.old.is_some()).unwrap_or(false);
        let has_new = self.value.as_ref().map(|v| !v.new.is_empty()).unwrap_or(false)
            || self.formula.as_ref().map(|f| f.new.is_some()).unwrap_or(false);
        match (has_old, has_new) {
            (false, true) => CellChangeKind::Added,
            (true, false) => CellChangeKind::Removed,
            _ => CellChangeKind::Modified,
        }
    }
}

// ---------------------------------------------------------------------------
// Diagnostics (RFC-005 / RFC-033 §8)
// ---------------------------------------------------------------------------

/// Severity of a diagnostic entry.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum Severity {
    Info,
    Warning,
    Error,
}

/// Which processing stage emitted a diagnostic.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum DiffStage {
    Open,
    Metadata,
    Match,
    Read,
    Normalize,
    Compare,
    Aggregate,
}

/// Location context attached to a diagnostic.
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct DiagnosticLocation {
    pub stage: DiffStage,
    /// 0-based sheet order (workbook index), if applicable.
    pub sheet_order: Option<usize>,
    pub sheet_name: Option<String>,
    pub address: Option<CellAddress>,
}

/// Structured diagnostic kind.
///
/// `code()` returns a stable string identifier for serde / localisation;
/// it is never renamed within a major version.
#[non_exhaustive]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum DiagnosticKind {
    FormulaUnavailable,
    FormulaCachedValueUnverified,
    AmbiguousSheetMatch { candidates: Vec<SheetRef> },
    UnsupportedCellValue { detail: String },
    UnsupportedWorkbookFeature { feature: String },
    UnsupportedWorkbookMetadata { category: String },
    DefinedNameScopeUnknown,
    DateTimeNotNormalized,
    LimitTruncatedCells { limit: String, observed: u64 },
}

impl DiagnosticKind {
    /// Stable code string for this diagnostic kind.
    ///
    /// **These strings are the stable programmatic surface for diagnostics.**
    /// Match on `code()` rather than on the `#[non_exhaustive]` enum variants:
    /// new variants may be added in a minor release (which would break an
    /// exhaustive `match` on the enum), but an existing code string is never
    /// renamed within a major version. Codes also appear verbatim in serialised
    /// JSON.
    ///
    /// The complete set of codes in this major version:
    ///
    /// | Code | Meaning |
    /// |---|---|
    /// | `formula_unavailable` | A cell's formula text could not be read |
    /// | `formula_cached_value_unverified` | A formula's cached value could not be verified |
    /// | `ambiguous_sheet_match` | Sheet rename detection found more than one candidate |
    /// | `unsupported_cell_value` | A cell value could not be normalised to a `CellValue` |
    /// | `unsupported_workbook_feature` | A non-cell object/sheet type is present but not compared |
    /// | `unsupported_workbook_metadata` | A defined-name / visibility / metadata change was detected |
    /// | `defined_name_scope_unknown` | Defined-name scope is unavailable from the reader |
    /// | `datetime_not_normalized` | A date/time value could not be normalised to ISO form |
    /// | `limit_truncated_cells` | A configured cell limit truncated the comparison |
    ///
    /// New codes added in later minor versions will extend this table; existing
    /// rows are stable.
    pub fn code(&self) -> &'static str {
        match self {
            DiagnosticKind::FormulaUnavailable => "formula_unavailable",
            DiagnosticKind::FormulaCachedValueUnverified => "formula_cached_value_unverified",
            DiagnosticKind::AmbiguousSheetMatch { .. } => "ambiguous_sheet_match",
            DiagnosticKind::UnsupportedCellValue { .. } => "unsupported_cell_value",
            DiagnosticKind::UnsupportedWorkbookFeature { .. } => "unsupported_workbook_feature",
            DiagnosticKind::UnsupportedWorkbookMetadata { .. } => "unsupported_workbook_metadata",
            DiagnosticKind::DefinedNameScopeUnknown => "defined_name_scope_unknown",
            DiagnosticKind::DateTimeNotNormalized => "datetime_not_normalized",
            DiagnosticKind::LimitTruncatedCells { .. } => "limit_truncated_cells",
        }
    }
}

/// A single structured diagnostic entry.
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Diagnostic {
    pub severity: Severity,
    pub kind: DiagnosticKind,
    pub location: DiagnosticLocation,
    /// Human-readable message — for display only, not for programmatic matching.
    pub message: String,
}

// ---------------------------------------------------------------------------
// Summary types
// ---------------------------------------------------------------------------

/// Per-sheet summary counts.
#[derive(Clone, Default, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct SheetSummary {
    pub cells_changed: usize,
    pub values_changed: usize,
    pub formulas_changed: usize,
}

/// Diagnostic counts rolled up at any level.
#[derive(Clone, Default, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct DiagnosticSummary {
    pub errors: usize,
    pub warnings: usize,
    pub info: usize,
}

/// Top-level workbook diff summary.
#[derive(Clone, Default, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct DiffSummary {
    pub sheets_added: usize,
    pub sheets_removed: usize,
    pub sheets_renamed: usize,
    pub sheets_moved: usize,
    pub sheets_changed: usize,
    pub cells_changed: usize,
    pub values_changed: usize,
    pub formulas_changed: usize,
    pub diagnostics: DiagnosticSummary,
}

/// Internal processing metrics (RFC-024, RFC-027).
///
/// Useful for benchmarking, performance analysis, and debugging.
/// Always populated; fields are cumulative across the whole comparison.
#[derive(Clone, Default, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct DiffMetrics {
    pub sheets_read: u32,
    pub cells_read: u64,
    pub cells_compared: u64,
    pub diffs_emitted: u64,
    pub diagnostics_emitted: u64,
}

// ---------------------------------------------------------------------------
// SheetDiff
// ---------------------------------------------------------------------------

/// Summary of row-alignment decisions for a sheet pair (RFC-011).
///
/// `None` on `SheetDiff.alignment_summary` when mode is `Positional`.
#[non_exhaustive]
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct AlignmentSummary {
    pub inserted_rows: usize,
    pub removed_rows: usize,
    pub matched_rows: usize,
    pub confidence: MatchConfidence,
}

/// The diff result for one logical sheet pair.
#[non_exhaustive]
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct SheetDiff {
    /// The sheet on the old side (`None` for Added sheets).
    pub old_sheet: Option<SheetRef>,
    /// The sheet on the new side (`None` for Removed sheets).
    pub new_sheet: Option<SheetRef>,
    pub change: SheetChange,
    /// Cell diffs sorted by `(row, col)`.
    pub cell_diffs: Vec<CellDiff>,
    pub compared_range: ComparedRange,
    /// Reserved until RFC-011.
    pub alignment_summary: Option<AlignmentSummary>,
    pub diagnostics: Vec<Diagnostic>,
    pub summary: SheetSummary,
}

// ---------------------------------------------------------------------------
// Workbook-level change placeholders (RFC-021/023, reserved in v2.0)
// ---------------------------------------------------------------------------

/// Reserved for RFC-021 (workbook metadata diffs).  Always empty in v2.0.
#[non_exhaustive]
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct WorkbookChange {
    // Populated by RFC-021 implementation.
}

/// Reserved for RFC-023 (non-cell object diffs).  Always empty in v2.0.
#[non_exhaustive]
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct WorkbookObjectChange {
    // Populated by RFC-023 implementation.
}

// ---------------------------------------------------------------------------
// Top-level result (RFC-033 §12)
// ---------------------------------------------------------------------------

/// The complete diff result for a workbook pair.
///
/// `workbook_changes` and `object_changes` are reserved for RFC-021/023 (v2.1+)
/// and are always empty in v2.0.  Because the struct is `#[non_exhaustive]` and
/// read-only for application code, those fields can be populated additively.
///
/// # Extracting a lightweight summary
///
/// `summary` ([`DiffSummary`]), `metrics` ([`DiffMetrics`]), and each sheet's
/// `change` ([`SheetChange`]) are all cheap, small, owned values. Memory-conscious
/// consumers that only need counts and the sheet-change list can clone those out
/// and drop the whole `WorkbookDiff` — including the potentially large
/// `sheets[..].cell_diffs` vectors — at their adapter boundary:
///
/// ```no_run
/// # use sheets_diff::compare_paths;
/// let diff = compare_paths("a.xlsx", "b.xlsx")?;
/// let summary = diff.summary.clone();        // cheap
/// let metrics = diff.metrics.clone();        // cheap
/// let sheet_changes: Vec<_> =
///     diff.sheets.iter().map(|s| s.change.clone()).collect();
/// drop(diff);                                 // releases all cell_diffs
/// # Ok::<(), sheets_diff::SheetsDiffError>(())
/// ```
#[non_exhaustive]
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct WorkbookDiff {
    pub old: WorkbookSideInfo,
    pub new: WorkbookSideInfo,
    /// Sheet diffs in old-workbook sheet order (then new-workbook order for
    /// added sheets).
    pub sheets: Vec<SheetDiff>,
    /// Reserved; empty until RFC-021.
    pub workbook_changes: Vec<WorkbookChange>,
    /// Reserved; empty until RFC-023.
    pub object_changes: Vec<WorkbookObjectChange>,
    pub diagnostics: Vec<Diagnostic>,
    pub summary: DiffSummary,
    /// Processing metrics for benchmarking and performance analysis (RFC-024/027).
    pub metrics: DiffMetrics,
}

// ---------------------------------------------------------------------------
// Summary derivation helpers
// ---------------------------------------------------------------------------

impl WorkbookDiff {
    pub(crate) fn derive_summary(sheets: &[SheetDiff], diagnostics: &[Diagnostic]) -> DiffSummary {
        let mut s = DiffSummary::default();
        for sd in sheets {
            match sd.change {
                SheetChange::Added => s.sheets_added += 1,
                SheetChange::Removed => s.sheets_removed += 1,
                SheetChange::Renamed { .. } => {
                    s.sheets_renamed += 1;
                    if !sd.cell_diffs.is_empty() {
                        s.sheets_changed += 1;
                    }
                }
                SheetChange::RenamedAndMoved { .. } => {
                    s.sheets_renamed += 1;
                    s.sheets_moved += 1;
                    if !sd.cell_diffs.is_empty() {
                        s.sheets_changed += 1;
                    }
                }
                SheetChange::Moved => {
                    s.sheets_moved += 1;
                    if !sd.cell_diffs.is_empty() {
                        s.sheets_changed += 1;
                    }
                }
                SheetChange::Modified => s.sheets_changed += 1,
                SheetChange::Unchanged => {}
            }
            s.cells_changed += sd.summary.cells_changed;
            s.values_changed += sd.summary.values_changed;
            s.formulas_changed += sd.summary.formulas_changed;
        }
        for d in diagnostics {
            match d.severity {
                Severity::Error => s.diagnostics.errors += 1,
                Severity::Warning => s.diagnostics.warnings += 1,
                Severity::Info => s.diagnostics.info += 1,
            }
        }
        s
    }
}