sheets-diff 1.2.0

Collect diff between office sheets
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
use std::fmt;
use std::io::{Read, Seek};
use std::path::Path;

use calamine::{Data, Reader, Xlsx, open_workbook};
#[cfg(feature = "serde_derive")]
use serde::{Deserialize, Serialize};

use super::error::{SheetsDiffError, WorkbookSide};
use super::utils::{cell_pos_to_address, diff_range, filter_same_name_sheets};

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// Whether a cell diff concerns its displayed value or its formula.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub enum CellDiffKind {
    Value,
    Formula,
}

impl fmt::Display for CellDiffKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CellDiffKind::Value => write!(f, "value"),
            CellDiffKind::Formula => write!(f, "formula"),
        }
    }
}

/// Top-level diff result between two `.xlsx` workbooks.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct Diff {
    pub old_filepath: String,
    pub new_filepath: String,
    pub sheet_diff: Vec<SheetDiff>,
    pub cell_diffs: Vec<SheetCellDiff>,
}

/// Records a sheet that was added or removed between the two workbooks.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct SheetDiff {
    pub old: Option<String>,
    pub new: Option<String>,
}

/// All cell-level diffs for a single worksheet.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct SheetCellDiff {
    pub sheet: String,
    pub cells: Vec<CellDiff>,
}

/// A single changed cell.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct CellDiff {
    /// 1-based row index.
    pub row: usize,
    /// 1-based column index.
    pub col: usize,
    /// Excel A1 address (e.g. `"XFD1048576"`).
    pub addr: String,
    pub kind: CellDiffKind,
    pub old: Option<String>,
    pub new: Option<String>,
}

// ---------------------------------------------------------------------------
// Diff constructors
// ---------------------------------------------------------------------------

impl Diff {
    /// Panicking convenience constructor.
    ///
    /// Opens both workbooks and computes their diff. Panics with a diagnostic
    /// message if either workbook cannot be opened or read.
    ///
    /// Existing callers of v1.1.4's `Diff::new` can continue to use this
    /// without any source changes.
    ///
    /// For production embedders and GUI applications, prefer [`Diff::try_new`]
    /// to receive a structured [`SheetsDiffError`] instead of a panic.
    pub fn new(old_filepath: &str, new_filepath: &str) -> Self {
        match Self::try_new(old_filepath, new_filepath) {
            Ok(diff) => diff,
            Err(err) => panic!("failed to diff workbooks: {err}"),
        }
    }

    /// Fallible path-based constructor.
    ///
    /// Accepts any value that can be treated as a [`Path`], including `&str`,
    /// `String`, and `PathBuf`. Returns a structured error for missing,
    /// corrupt, locked, or non-`.xlsx` inputs without panicking.
    pub fn try_new(
        old_filepath: impl AsRef<Path>,
        new_filepath: impl AsRef<Path>,
    ) -> Result<Self, SheetsDiffError> {
        let old_path = old_filepath.as_ref();
        let new_path = new_filepath.as_ref();

        let mut old_workbook: Xlsx<_> =
            open_workbook(old_path).map_err(|source| SheetsDiffError::OpenWorkbook {
                side: WorkbookSide::Old,
                path: old_path.to_path_buf(),
                source,
            })?;

        let mut new_workbook: Xlsx<_> =
            open_workbook(new_path).map_err(|source| SheetsDiffError::OpenWorkbook {
                side: WorkbookSide::New,
                path: new_path.to_path_buf(),
                source,
            })?;

        let old_label = old_path.to_string_lossy().into_owned();
        let new_label = new_path.to_string_lossy().into_owned();

        Self::try_from_workbooks(old_label, new_label, &mut old_workbook, &mut new_workbook)
    }

    /// Fallible reader-based constructor with explicit display names.
    ///
    /// Accepts any `Read + Seek` stream (e.g. [`std::io::Cursor`]). The
    /// `old_name` / `new_name` strings are stored in the returned
    /// `Diff.old_filepath` / `Diff.new_filepath` fields and appear in diff
    /// output — supply meaningful labels (filenames, Git object hashes, etc.).
    ///
    /// This constructor lets GUI and VCS tools avoid double I/O and lossy
    /// path-to-string conversions.
    pub fn try_from_named_readers<R1, R2>(
        old_name: impl Into<String>,
        old_reader: R1,
        new_name: impl Into<String>,
        new_reader: R2,
    ) -> Result<Self, SheetsDiffError>
    where
        R1: Read + Seek,
        R2: Read + Seek,
    {
        let mut old_workbook = Xlsx::new(old_reader).map_err(|source| {
            SheetsDiffError::OpenReader {
                side: WorkbookSide::Old,
                source,
            }
        })?;

        let mut new_workbook = Xlsx::new(new_reader).map_err(|source| {
            SheetsDiffError::OpenReader {
                side: WorkbookSide::New,
                source,
            }
        })?;

        Self::try_from_workbooks(
            old_name.into(),
            new_name.into(),
            &mut old_workbook,
            &mut new_workbook,
        )
    }

    /// Returns a clone of this diff (kept for v1.1.4 source compatibility).
    pub fn diff(&mut self) -> Diff {
        self.clone()
    }
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

impl Diff {
    /// Returns an empty `Diff` with the given display labels.
    fn empty(old_filepath: String, new_filepath: String) -> Self {
        Diff {
            old_filepath,
            new_filepath,
            sheet_diff: vec![],
            cell_diffs: vec![],
        }
    }

    /// Shared internal engine used by all public constructors.
    fn try_from_workbooks<R1, R2>(
        old_label: String,
        new_label: String,
        old_workbook: &mut Xlsx<R1>,
        new_workbook: &mut Xlsx<R2>,
    ) -> Result<Self, SheetsDiffError>
    where
        R1: Read + Seek,
        R2: Read + Seek,
    {
        let mut diff = Self::empty(old_label, new_label);
        diff.collect_diff_from_workbooks(old_workbook, new_workbook)?;
        diff.normalize_cell_diffs();
        Ok(diff)
    }

    /// Collects all sheet-level and cell-level diffs into `self`.
    fn collect_diff_from_workbooks<R1, R2>(
        &mut self,
        old_workbook: &mut Xlsx<R1>,
        new_workbook: &mut Xlsx<R2>,
    ) -> Result<(), SheetsDiffError>
    where
        R1: Read + Seek,
        R2: Read + Seek,
    {
        let old_sheets = old_workbook.sheet_names().to_owned();
        let new_sheets = new_workbook.sheet_names().to_owned();

        self.collect_sheet_diff(&old_sheets, &new_sheets);

        let same_name_sheets = filter_same_name_sheets(&old_sheets, &new_sheets);
        self.collect_cell_value_diff(old_workbook, new_workbook, &same_name_sheets)?;
        self.collect_cell_formula_diff(old_workbook, new_workbook, &same_name_sheets)?;

        Ok(())
    }

    /// Detects sheets that were added or removed.
    fn collect_sheet_diff(&mut self, old_sheets: &[String], new_sheets: &[String]) {
        if old_sheets == new_sheets {
            return;
        }

        for sheet in old_sheets {
            if !new_sheets.contains(sheet) {
                self.sheet_diff.push(SheetDiff {
                    old: Some(sheet.clone()),
                    new: None,
                });
            }
        }
        for sheet in new_sheets {
            if !old_sheets.contains(sheet) {
                self.sheet_diff.push(SheetDiff {
                    old: None,
                    new: Some(sheet.clone()),
                });
            }
        }
    }

    /// Collects changed cell values for all shared sheets.
    fn collect_cell_value_diff<R1, R2>(
        &mut self,
        old_workbook: &mut Xlsx<R1>,
        new_workbook: &mut Xlsx<R2>,
        same_name_sheets: &[String],
    ) -> Result<(), SheetsDiffError>
    where
        R1: Read + Seek,
        R2: Read + Seek,
    {
        for sheet in same_name_sheets {
            let old_range =
                old_workbook
                    .worksheet_range(sheet)
                    .map_err(|source| SheetsDiffError::ReadSheetValues {
                        side: WorkbookSide::Old,
                        sheet: sheet.clone(),
                        source,
                    })?;

            let new_range =
                new_workbook
                    .worksheet_range(sheet)
                    .map_err(|source| SheetsDiffError::ReadSheetValues {
                        side: WorkbookSide::New,
                        sheet: sheet.clone(),
                        source,
                    })?;

            let mut cell_diffs: Vec<CellDiff> = vec![];

            let (start_row, start_col, end_row, end_col) = diff_range(
                old_range.start(),
                new_range.start(),
                old_range.end(),
                new_range.end(),
            );

            for row in start_row..end_row {
                for col in start_col..end_col {
                    let old_cell = old_range.get_value((row, col)).unwrap_or(&Data::Empty);
                    let new_cell = new_range.get_value((row, col)).unwrap_or(&Data::Empty);

                    if old_cell != new_cell {
                        let row1 = (row + 1) as usize;
                        let col1 = (col + 1) as usize;
                        cell_diffs.push(CellDiff {
                            row: row1,
                            col: col1,
                            addr: cell_pos_to_address(row1, col1),
                            kind: CellDiffKind::Value,
                            old: if old_cell != &Data::Empty {
                                Some(old_cell.to_string())
                            } else {
                                None
                            },
                            new: if new_cell != &Data::Empty {
                                Some(new_cell.to_string())
                            } else {
                                None
                            },
                        });
                    }
                }
            }

            if !cell_diffs.is_empty() {
                self.cell_diffs.push(SheetCellDiff {
                    sheet: sheet.clone(),
                    cells: cell_diffs,
                });
            }
        }

        Ok(())
    }

    /// Collects changed cell formulas for all shared sheets.
    fn collect_cell_formula_diff<R1, R2>(
        &mut self,
        old_workbook: &mut Xlsx<R1>,
        new_workbook: &mut Xlsx<R2>,
        same_name_sheets: &[String],
    ) -> Result<(), SheetsDiffError>
    where
        R1: Read + Seek,
        R2: Read + Seek,
    {
        for sheet in same_name_sheets {
            let old_range = old_workbook
                .worksheet_formula(sheet)
                .map_err(|source| SheetsDiffError::ReadSheetFormulas {
                    side: WorkbookSide::Old,
                    sheet: sheet.clone(),
                    source,
                })?;

            let new_range = new_workbook
                .worksheet_formula(sheet)
                .map_err(|source| SheetsDiffError::ReadSheetFormulas {
                    side: WorkbookSide::New,
                    sheet: sheet.clone(),
                    source,
                })?;

            let mut cell_diffs: Vec<CellDiff> = vec![];

            let (start_row, start_col, end_row, end_col) = diff_range(
                old_range.start(),
                new_range.start(),
                old_range.end(),
                new_range.end(),
            );

            for row in start_row..end_row {
                for col in start_col..end_col {
                    let empty = String::new();
                    let old_cell = old_range.get_value((row, col)).unwrap_or(&empty);
                    let new_cell = new_range.get_value((row, col)).unwrap_or(&empty);

                    if old_cell != new_cell {
                        let row1 = (row + 1) as usize;
                        let col1 = (col + 1) as usize;
                        cell_diffs.push(CellDiff {
                            row: row1,
                            col: col1,
                            addr: cell_pos_to_address(row1, col1),
                            kind: CellDiffKind::Formula,
                            old: if old_cell.is_empty() {
                                None
                            } else {
                                Some(old_cell.to_string())
                            },
                            new: if new_cell.is_empty() {
                                None
                            } else {
                                Some(new_cell.to_string())
                            },
                        });
                    }
                }
            }

            if !cell_diffs.is_empty() {
                self.cell_diffs.push(SheetCellDiff {
                    sheet: sheet.clone(),
                    cells: cell_diffs,
                });
            }
        }

        Ok(())
    }

    /// Merges per-sheet cell diff batches and sorts by `(sheet, row, col, kind)`.
    ///
    /// Sorting by numeric `(row, col)` avoids lexical ordering bugs such as
    /// `A10` appearing before `A2`.
    fn normalize_cell_diffs(&mut self) {
        self.cell_diffs.sort_by(|a, b| a.sheet.cmp(&b.sheet));

        let mut merged: Vec<SheetCellDiff> = vec![];
        for entry in self.cell_diffs.drain(..) {
            match merged.iter_mut().find(|m| m.sheet == entry.sheet) {
                Some(existing) => existing.cells.extend(entry.cells),
                None => merged.push(entry),
            }
        }

        for sheet_diff in &mut merged {
            sheet_diff.cells.sort_by(|a, b| {
                a.row
                    .cmp(&b.row)
                    .then_with(|| a.col.cmp(&b.col))
                    .then_with(|| a.kind.cmp(&b.kind))
            });
        }

        self.cell_diffs = merged;
    }
}