sheets-diff 2.5.0

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
//! Cell-level value and formula comparison (RFC-010, RFC-018, RFC-019).

use crate::model::{
    CellDateTime, CellValue, FormulaChange, FormulaText, ValueChange, ValueDifferenceKind,
};
use crate::options::{
    DateComparePolicy, FormulaCompareMode, NumberComparePolicy, NumericTypePolicy,
    TypeMismatchPolicy, ValueCompareOptions,
};

// ---------------------------------------------------------------------------
// Value comparison
// ---------------------------------------------------------------------------

/// Compare two `CellValue`s under the supplied options.
///
/// Returns `Some(ValueChange)` when the values differ; `None` when equal.
pub fn compare_values(
    old: &CellValue,
    new: &CellValue,
    opts: &ValueCompareOptions,
) -> Option<ValueChange> {
    use CellValue::*;

    let reason = match (old, new) {
        // Same variant comparisons
        (Empty, Empty) => return None,
        (Text(a), Text(b)) if a == b => return None,
        (Text(a), Text(b)) => {
            debug_assert!(a != b);
            ValueDifferenceKind::ContentChanged
        }
        (Bool(a), Bool(b)) if a == b => return None,
        (Bool(_), Bool(_)) => ValueDifferenceKind::ContentChanged,
        (Integer(a), Integer(b)) if a == b => return None,
        (Integer(_), Integer(_)) => ValueDifferenceKind::ContentChanged,
        (Number(a), Number(b)) => compare_floats(*a, *b, &opts.number)?,
        (Error(a), Error(b)) if a == b => return None,
        (Error(_), Error(_)) => ValueDifferenceKind::ErrorKindChanged,
        (DateTime(a), DateTime(b)) => {
            if datetime_equal(a, b) {
                return None;
            }
            match opts.date {
                DateComparePolicy::ExactRepresentation => ValueDifferenceKind::DateTimeChanged,
                DateComparePolicy::NormalizeEquivalentDateTimes => {
                    // Attempt serial normalization across 1900/1904 systems.
                    // Only meaningful when both sides carry a genuine serial
                    // (D-01) — an ISO-only value has no epoch to normalise.
                    if a.has_serial
                        && b.has_serial
                        && normalized_serial_eq(a.serial, a.is_1904, b.serial, b.is_1904)
                    {
                        return None;
                    }
                    ValueDifferenceKind::DateTimeChanged
                }
            }
        }
        (Duration(a), Duration(b)) => {
            let equal = match (&a.iso, &b.iso) {
                // Both carry an ISO string: it is the authoritative
                // representation for a duration (RFC-019 / D-01 — `serial`
                // is currently always a `0.0` placeholder here; comparing
                // `iso` is what actually distinguishes two durations).
                (Some(ai), Some(bi)) => ai == bi,
                (None, None) => a.serial == b.serial,
                // One side has an ISO string and the other doesn't: never
                // silently equal (D-01) — there is no reliable common
                // representation to compare through.
                _ => false,
            };
            if equal {
                return None;
            }
            ValueDifferenceKind::ContentChanged
        }
        (Unsupported { display: a, .. }, Unsupported { display: b, .. }) if a == b => return None,
        (Unsupported { .. }, Unsupported { .. }) => ValueDifferenceKind::ContentChanged,

        // Cross-type: Integer vs Number
        (Integer(i), Number(f)) | (Number(f), Integer(i)) => match opts.numeric_type {
            NumericTypePolicy::PreserveType => ValueDifferenceKind::TypeChanged,
            NumericTypePolicy::CompareMathematicalValue => {
                if *i as f64 == *f {
                    return None;
                }
                ValueDifferenceKind::ContentChanged
            }
        },

        // Cross-type: everything else
        _ => match opts.type_mismatch {
            TypeMismatchPolicy::Different => ValueDifferenceKind::TypeChanged,
            TypeMismatchPolicy::CompareDisplayString => {
                let a_str = old.display_string();
                let b_str = new.display_string();
                if a_str == b_str {
                    return None;
                }
                ValueDifferenceKind::DisplayStringChanged
            }
        },
    };

    Some(ValueChange {
        old: old.clone(),
        new: new.clone(),
        reason,
    })
}

/// Equality for the default (`ExactRepresentation`) date/time comparison.
///
/// D-01: a value from `Data::DateTimeIso` has no genuine Excel serial — its
/// `serial` field is a `0.0` placeholder (`has_serial: false`) and `iso` is
/// the only meaningful representation. A value from `Data::DateTime` always
/// has a genuine serial (`has_serial: true`); when the `chrono` feature is
/// enabled it may *also* carry a synthesized `iso` string, but that string
/// is redundant with the serial, not authoritative — comparing it instead
/// of the serial would risk losing precision (the synthesized string has
/// only second resolution) and would make the comparison result depend on
/// whether `chrono` is enabled, which must not happen.
///
/// So: two genuine serials compare via serial/`is_1904`/`kind`, unchanged
/// from before. Two ISO-only values compare via `iso`. A serial-based value
/// against an ISO-only value has no shared representation to compare
/// through and is never silently equal.
fn datetime_equal(a: &CellDateTime, b: &CellDateTime) -> bool {
    match (a.has_serial, b.has_serial) {
        (true, true) => a.serial == b.serial && a.is_1904 == b.is_1904 && a.kind == b.kind,
        (false, false) => a.iso == b.iso,
        _ => false,
    }
}

fn compare_floats(a: f64, b: f64, policy: &NumberComparePolicy) -> Option<ValueDifferenceKind> {
    let equal = match policy {
        NumberComparePolicy::Exact => a == b || (a.is_nan() && b.is_nan()),
        NumberComparePolicy::AbsoluteTolerance(tol) => (a - b).abs() <= *tol,
        NumberComparePolicy::RelativeTolerance(tol) => {
            let denom = a.abs().max(b.abs());
            denom == 0.0 || (a - b).abs() / denom <= *tol
        }
        NumberComparePolicy::AbsoluteOrRelative { abs, rel } => {
            let abs_ok = (a - b).abs() <= *abs;
            let denom = a.abs().max(b.abs());
            let rel_ok = denom == 0.0 || (a - b).abs() / denom <= *rel;
            abs_ok || rel_ok
        }
    };
    if equal {
        None
    } else {
        Some(ValueDifferenceKind::NumericOutsideTolerance)
    }
}

/// Normalise serials across 1900 / 1904 date systems.
/// The offset between the two systems is 1462 days.
fn normalized_serial_eq(a_serial: f64, a_1904: bool, b_serial: f64, b_1904: bool) -> bool {
    const OFFSET: f64 = 1462.0;
    let a_norm = if a_1904 { a_serial + OFFSET } else { a_serial };
    let b_norm = if b_1904 { b_serial + OFFSET } else { b_serial };
    a_norm == b_norm
}

// ---------------------------------------------------------------------------
// Formula comparison (RFC-018)
// ---------------------------------------------------------------------------

/// Compare formula strings under the configured mode.
///
/// Returns `Some(FormulaChange)` when the formulas differ; `None` when equal or
/// when the mode is `Ignore`.
pub fn compare_formulas(
    old_formula: Option<&str>,
    new_formula: Option<&str>,
    mode: FormulaCompareMode,
) -> Option<FormulaChange> {
    if mode == FormulaCompareMode::Ignore {
        return None;
    }

    let old_text = old_formula.map(|r| FormulaText {
        raw: r.to_owned(),
        normalized: None, // NormalizedText mode guard is in options validation
    });
    let new_text = new_formula.map(|r| FormulaText {
        raw: r.to_owned(),
        normalized: None,
    });

    // Equal?
    let old_raw = old_formula.unwrap_or("");
    let new_raw = new_formula.unwrap_or("");
    if old_raw == new_raw {
        return None;
    }

    Some(FormulaChange {
        old: old_text,
        new: new_text,
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{CellDuration, CellValue, DateTimeKind};
    use crate::options::{DateComparePolicy, ValueCompareOptions};

    fn opts() -> ValueCompareOptions {
        ValueCompareOptions::default()
    }

    fn iso_only_dt(iso: &str) -> CellDateTime {
        CellDateTime {
            serial: 0.0,
            is_1904: false,
            kind: DateTimeKind::DateTime,
            iso: Some(iso.to_string()),
            has_serial: false,
        }
    }

    fn serial_dt(serial: f64, is_1904: bool) -> CellDateTime {
        CellDateTime {
            serial,
            is_1904,
            kind: DateTimeKind::DateTime,
            iso: None,
            has_serial: true,
        }
    }

    // D-01: ISO date/time and duration values must not always compare equal ---

    #[test]
    fn iso_only_datetimes_with_same_iso_are_equal() {
        let a = iso_only_dt("2024-01-01T00:00:00");
        let b = iso_only_dt("2024-01-01T00:00:00");
        assert!(
            compare_values(&CellValue::DateTime(a), &CellValue::DateTime(b), &opts()).is_none()
        );
    }

    #[test]
    fn iso_only_datetimes_with_different_iso_are_reported_changed() {
        // The exact pair from the RFC-035 Handoff 05 audit: before the fix,
        // both normalise to serial 0.0 / is_1904 false / kind DateTime, so
        // this compared equal no matter how different the two dates are.
        let a = iso_only_dt("2024-01-01T00:00:00");
        let b = iso_only_dt("2099-12-31T23:59:59");
        let r = compare_values(&CellValue::DateTime(a), &CellValue::DateTime(b), &opts()).unwrap();
        assert_eq!(r.reason, ValueDifferenceKind::DateTimeChanged);
    }

    #[test]
    fn iso_only_durations_with_different_iso_are_reported_changed() {
        // The second pair from the same audit finding.
        let a = CellValue::Duration(CellDuration {
            serial: 0.0,
            iso: Some("PT1H".to_string()),
        });
        let b = CellValue::Duration(CellDuration {
            serial: 0.0,
            iso: Some("PT99H".to_string()),
        });
        let r = compare_values(&a, &b, &opts()).unwrap();
        assert_eq!(r.reason, ValueDifferenceKind::ContentChanged);
    }

    #[test]
    fn iso_only_durations_with_same_iso_are_equal() {
        let a = CellValue::Duration(CellDuration {
            serial: 0.0,
            iso: Some("PT1H30M".to_string()),
        });
        let b = CellValue::Duration(CellDuration {
            serial: 0.0,
            iso: Some("PT1H30M".to_string()),
        });
        assert!(compare_values(&a, &b, &opts()).is_none());
    }

    #[test]
    fn mixed_serial_and_iso_datetime_never_silently_equal() {
        // A genuine serial-based value whose serial happens to be 0.0 (a
        // legitimate date, 1899-12-30 in the 1900 system) against an
        // ISO-only value whose serial is *also* 0.0 but as a placeholder.
        // Before `has_serial`, these were indistinguishable and compared
        // equal under the old (serial, is_1904, kind) check.
        let serial_based = serial_dt(0.0, false);
        let iso_only = iso_only_dt("2024-01-01T00:00:00");
        let mut o = opts();

        o.date = DateComparePolicy::ExactRepresentation;
        let r = compare_values(
            &CellValue::DateTime(serial_based.clone()),
            &CellValue::DateTime(iso_only.clone()),
            &o,
        );
        assert!(
            r.is_some(),
            "mixed representation must not be silently equal"
        );

        // Must not become equal under the normalisation policy either.
        o.date = DateComparePolicy::NormalizeEquivalentDateTimes;
        let r = compare_values(
            &CellValue::DateTime(serial_based),
            &CellValue::DateTime(iso_only),
            &o,
        );
        assert!(
            r.is_some(),
            "mixed representation must not be silently equal under NormalizeEquivalentDateTimes either"
        );
    }

    #[test]
    fn normalize_equivalent_datetimes_reconciles_1900_and_1904_systems() {
        // Same real-world date, represented once under the 1900 system and
        // once under the 1904 system: serials differ by exactly the 1462-day
        // offset. `ExactRepresentation` must see them as different;
        // `NormalizeEquivalentDateTimes` must see them as the same instant.
        let system_1900 = serial_dt(45000.0, false);
        let system_1904 = serial_dt(45000.0 - 1462.0, true);

        let mut o = opts();
        o.date = DateComparePolicy::ExactRepresentation;
        let r = compare_values(
            &CellValue::DateTime(system_1900.clone()),
            &CellValue::DateTime(system_1904.clone()),
            &o,
        );
        assert!(
            r.is_some(),
            "ExactRepresentation must not conflate the two epochs"
        );

        o.date = DateComparePolicy::NormalizeEquivalentDateTimes;
        let r = compare_values(
            &CellValue::DateTime(system_1900),
            &CellValue::DateTime(system_1904),
            &o,
        );
        assert!(
            r.is_none(),
            "NormalizeEquivalentDateTimes must recognise the same instant across epochs"
        );
    }

    #[test]
    fn equal_texts_produce_no_change() {
        let r = compare_values(
            &CellValue::Text("x".into()),
            &CellValue::Text("x".into()),
            &opts(),
        );
        assert!(r.is_none());
    }

    #[test]
    fn different_texts_produce_content_changed() {
        let r = compare_values(
            &CellValue::Text("a".into()),
            &CellValue::Text("b".into()),
            &opts(),
        )
        .unwrap();
        assert_eq!(r.reason, ValueDifferenceKind::ContentChanged);
    }

    #[test]
    fn integer_vs_number_is_type_changed_by_default() {
        let r = compare_values(&CellValue::Integer(1), &CellValue::Number(1.0), &opts()).unwrap();
        assert_eq!(r.reason, ValueDifferenceKind::TypeChanged);
    }

    #[test]
    fn integer_vs_number_equal_when_math_policy() {
        let mut o = opts();
        o.numeric_type = NumericTypePolicy::CompareMathematicalValue;
        let r = compare_values(&CellValue::Integer(1), &CellValue::Number(1.0), &o);
        assert!(r.is_none());
    }

    #[test]
    fn text_vs_integer_is_type_changed() {
        let r = compare_values(
            &CellValue::Text("100".into()),
            &CellValue::Integer(100),
            &opts(),
        )
        .unwrap();
        assert_eq!(r.reason, ValueDifferenceKind::TypeChanged);
    }

    #[test]
    fn equal_booleans_produce_no_change() {
        let r = compare_values(&CellValue::Bool(true), &CellValue::Bool(true), &opts());
        assert!(r.is_none());
    }

    #[test]
    fn empty_vs_empty_produces_no_change() {
        let r = compare_values(&CellValue::Empty, &CellValue::Empty, &opts());
        assert!(r.is_none());
    }

    #[test]
    fn formula_ignore_returns_none() {
        let r = compare_formulas(Some("=A1"), Some("=B1"), FormulaCompareMode::Ignore);
        assert!(r.is_none());
    }

    #[test]
    fn equal_formulas_return_none() {
        let r = compare_formulas(Some("=A1+B1"), Some("=A1+B1"), FormulaCompareMode::RawText);
        assert!(r.is_none());
    }

    #[test]
    fn different_formulas_return_change() {
        let r = compare_formulas(
            Some("=A1+B1"),
            Some("=A1+B1+C1"),
            FormulaCompareMode::RawText,
        )
        .unwrap();
        assert_eq!(r.old.as_ref().unwrap().raw, "=A1+B1");
        assert_eq!(r.new.as_ref().unwrap().raw, "=A1+B1+C1");
    }

    #[test]
    fn formula_added() {
        let r = compare_formulas(None, Some("=SUM(A1:A10)"), FormulaCompareMode::RawText).unwrap();
        assert!(r.old.is_none());
        assert!(r.new.is_some());
    }
}