cellrune 0.1.18

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
use super::super::ArithmeticSemantics;
use super::super::ast::Expr;
use super::super::decimal::{DecimalTrace, RationalTrace, is_excel_near_zero_cancellation};
use super::super::eval::{Engine, EvalContext};
use super::super::limits::CalculationLimitKind;
use super::super::runtime::Rect;
use super::super::scope::ScopeValue;
use super::super::sheet_span::SheetSpanPolicy;
use super::super::value::{ErrorKind, Value};
use super::{DynamicFunction, Evaluator, function_evaluator, let_scope_value};

#[derive(Debug, Clone)]
pub(super) struct ArgumentValue {
    pub(super) value: Value,
    pub(super) decimal_trace: Option<DecimalTrace>,
    pub(super) from_collection: bool,
    pub(super) from_single_cell_reference: bool,
}

pub(super) fn collect_argument_values(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[Expr],
) -> Result<Vec<ArgumentValue>, ErrorKind> {
    collect_argument_values_with_policy(engine, context, args, SheetSpanPolicy::Unsupported)
}

pub(super) fn collect_argument_values_with_policy(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[Expr],
    sheet_span_policy: SheetSpanPolicy,
) -> Result<Vec<ArgumentValue>, ErrorKind> {
    let mut visited_cells = 0_u64;
    collect_argument_values_with_counter_and_policy(
        engine,
        context,
        args,
        &mut visited_cells,
        sheet_span_policy,
    )
}

pub(super) fn collect_argument_values_with_counter(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[Expr],
    visited_cells: &mut u64,
) -> Result<Vec<ArgumentValue>, ErrorKind> {
    collect_argument_values_with_counter_and_policy(
        engine,
        context,
        args,
        visited_cells,
        SheetSpanPolicy::Unsupported,
    )
}

pub(super) fn collect_argument_values_with_counter_and_policy(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[Expr],
    visited_cells: &mut u64,
    sheet_span_policy: SheetSpanPolicy,
) -> Result<Vec<ArgumentValue>, ErrorKind> {
    let mut values = Vec::new();
    for arg in args {
        if let Some(scoped) = collection_preserving_scope_value(engine, context, arg) {
            collect_scope_values(
                engine,
                context,
                scoped,
                visited_cells,
                sheet_span_policy,
                true,
                &mut values,
            )?;
            continue;
        }
        if let Ok(reference) = engine.resolve_reference_value_expr(context, arg) {
            if matches!(&reference, super::super::runtime::ReferenceValue::Empty) {
                return Err(ErrorKind::Ref);
            }
            if reference.has_sheet_span() {
                match sheet_span_policy {
                    SheetSpanPolicy::CollectAcrossSheets => {}
                    SheetSpanPolicy::ReturnExcelError(kind) => return Err(kind),
                    SheetSpanPolicy::Unsupported => return Err(ErrorKind::Unsupported),
                }
            }
            for rect in reference.rects() {
                collect_rect_values(engine, context, rect, visited_cells, &mut values)?;
            }
        } else {
            let evaluated = engine.eval_array_with_trace(context, arg)?;
            let from_collection = !evaluated.array.is_scalar() || matches!(arg, Expr::Array(_));
            *visited_cells = visited_cells
                .checked_add(evaluated.array.data.len() as u64)
                .ok_or(ErrorKind::ResourceLimit(CalculationLimitKind::ArrayCells))?;
            engine.ensure_array_cells(*visited_cells)?;
            values.extend(
                evaluated
                    .array
                    .data
                    .into_iter()
                    .zip(evaluated.decimal_traces)
                    .map(|(value, decimal_trace)| ArgumentValue {
                        value,
                        decimal_trace,
                        from_collection,
                        from_single_cell_reference: false,
                    }),
            );
        }
    }
    Ok(values)
}

fn collection_preserving_scope_value(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    expr: &Expr,
) -> Option<ScopeValue> {
    match expr {
        Expr::Paren(inner) => collection_preserving_scope_value(engine, context, inner),
        Expr::Array(_) | Expr::Name(_) | Expr::BuiltinCallable(_) => {
            Some(engine.eval_scope_value(context, expr))
        }
        Expr::Call { name, args }
            if context.binding(name).is_none()
                && engine
                    .resolve_name_expr_with_id_in_context(context, name)
                    .is_none()
                && function_evaluator(name) == Some(Evaluator::Dynamic(DynamicFunction::Let)) =>
        {
            Some(let_scope_value(engine, context, args))
        }
        _ => None,
    }
}

pub(super) fn collect_callable_argument_values(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[ScopeValue],
) -> Result<Vec<ArgumentValue>, ErrorKind> {
    let mut visited_cells = 0_u64;
    let mut values = Vec::new();
    for value in args {
        collect_scope_values(
            engine,
            context,
            value.clone(),
            &mut visited_cells,
            SheetSpanPolicy::CollectAcrossSheets,
            true,
            &mut values,
        )?;
    }
    Ok(values)
}

fn collect_scope_values(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    scoped: ScopeValue,
    visited_cells: &mut u64,
    sheet_span_policy: SheetSpanPolicy,
    arrays_are_collections: bool,
    values: &mut Vec<ArgumentValue>,
) -> Result<(), ErrorKind> {
    match scoped {
        ScopeValue::Missing => {
            charge_array_cells(engine, visited_cells, 1)?;
            values.push(ArgumentValue {
                value: Value::Blank,
                decimal_trace: None,
                from_collection: false,
                from_single_cell_reference: false,
            });
        }
        ScopeValue::Scalar(evaluated) => {
            charge_array_cells(engine, visited_cells, 1)?;
            values.push(ArgumentValue {
                value: evaluated.value,
                decimal_trace: evaluated.decimal_trace,
                from_collection: false,
                from_single_cell_reference: false,
            });
        }
        ScopeValue::Array(evaluated) => {
            charge_array_cells(engine, visited_cells, evaluated.array.data.len() as u64)?;
            let from_collection = arrays_are_collections || !evaluated.array.is_scalar();
            values.extend(
                evaluated
                    .array
                    .data
                    .iter()
                    .cloned()
                    .zip(evaluated.decimal_traces.iter().copied())
                    .map(|(value, decimal_trace)| ArgumentValue {
                        value,
                        decimal_trace,
                        from_collection,
                        from_single_cell_reference: false,
                    }),
            );
        }
        ScopeValue::Reference(reference) => {
            if matches!(&reference, super::super::runtime::ReferenceValue::Empty) {
                return Err(ErrorKind::Ref);
            }
            if reference.has_sheet_span() {
                match sheet_span_policy {
                    SheetSpanPolicy::CollectAcrossSheets => {}
                    SheetSpanPolicy::ReturnExcelError(kind) => return Err(kind),
                    SheetSpanPolicy::Unsupported => return Err(ErrorKind::Unsupported),
                }
            }
            for rect in reference.rects() {
                collect_rect_values(engine, context, rect, visited_cells, values)?;
            }
        }
        ScopeValue::Callable(_) => return Err(ErrorKind::Value),
    }
    Ok(())
}

fn charge_array_cells(
    engine: &Engine<'_>,
    visited_cells: &mut u64,
    cells: u64,
) -> Result<(), ErrorKind> {
    *visited_cells = visited_cells
        .checked_add(cells)
        .ok_or(ErrorKind::ResourceLimit(CalculationLimitKind::ArrayCells))?;
    engine.ensure_array_cells(*visited_cells)
}

fn collect_rect_values(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    rect: Rect,
    visited_cells: &mut u64,
    values: &mut Vec<ArgumentValue>,
) -> Result<(), ErrorKind> {
    let rows = engine.operation_row_count([&rect]);
    if rows == 0 {
        return Ok(());
    }
    let cells = rows
        .checked_mul(rect.width())
        .ok_or(ErrorKind::ResourceLimit(CalculationLimitKind::ArrayCells))?;
    *visited_cells = visited_cells
        .checked_add(cells)
        .ok_or(ErrorKind::ResourceLimit(CalculationLimitKind::ArrayCells))?;
    engine.ensure_array_cells(*visited_cells)?;
    for row_offset in 0..rows as u32 {
        let row = rect.row_start + row_offset;
        for column in rect.col_start..=rect.col_end {
            let cell = (rect.sheet, row, column);
            let value = engine.read_reference_cell(context, cell)?;
            let decimal_trace = match &value {
                Value::Number(_) => engine.numeric_decimal_trace(cell),
                _ => None,
            };
            values.push(ArgumentValue {
                value,
                decimal_trace,
                from_collection: true,
                from_single_cell_reference: rect.is_single_cell(),
            });
        }
    }
    Ok(())
}

pub(super) fn required_number(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    expr: &Expr,
) -> Result<f64, ErrorKind> {
    required_number_with_trace(engine, context, expr).map(|(number, _)| number)
}

pub(super) fn required_number_with_trace(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    expr: &Expr,
) -> Result<(f64, Option<DecimalTrace>), ErrorKind> {
    engine.eval_number_with_trace(context, expr)
}

pub(super) fn required_text(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    expr: &Expr,
) -> Result<String, ErrorKind> {
    super::super::coerce::to_text(&engine.eval_scalar(context, expr))
}

/// Exact running value an [`ExcelSum`] can carry beside its `f64` total.
///
/// Two representations implement it: parsed decimals, for kernels that add their inputs as spelled;
/// and rationals, for kernels such as `NPV` that transform each input before adding it. Both fail
/// closed — an operation that does not fit the bounded representation returns `None`, and the
/// accumulator then stops claiming to know the exact total rather than guessing that it is zero.
pub(super) trait ExactTrace: Copy {
    const EXACT_ZERO: Self;

    fn combined_with(self, right: Self) -> Option<Self>;

    fn is_exact_zero(self) -> bool;
}

impl ExactTrace for DecimalTrace {
    const EXACT_ZERO: Self = Self::ZERO;

    fn combined_with(self, right: Self) -> Option<Self> {
        self.add(right)
    }

    fn is_exact_zero(self) -> bool {
        self.is_zero()
    }
}

impl ExactTrace for RationalTrace {
    const EXACT_ZERO: Self = Self::ZERO;

    fn combined_with(self, right: Self) -> Option<Self> {
        self.add(right)
    }

    fn is_exact_zero(self) -> bool {
        self.is_zero()
    }
}

/// Policy-aware streaming total shared by every kernel that adds numbers up.
///
/// Two things make this the single place the sum is formed. First, `Iterator::sum` for `f64` folds
/// from `-0.0`, which is the true additive identity for floats because `-0.0 + x == x` holds even
/// when `x` is `-0.0`; that makes an empty sum negative zero, while Excel reports `0` for a sum over
/// no numbers. Second, a running total is a chain of additions, so it accumulates exactly the
/// residue the operator path corrects — without the same correction here, one release would answer
/// `=A1+A2+A3` and `=SUM(A1:A3)` differently, which is not a compatibility mode but a
/// contradiction.
///
/// The correction is applied at each step against the term that produced it, matching what
/// `a + b + c` does in the operator path, and only when the exact total is zero. Ordinary and
/// conditional aggregates, `SUMPRODUCT`, and `NPV` all read the policy through this one type, so
/// none of them can drift from the others.
pub(super) struct ExcelSum<Trace: ExactTrace = DecimalTrace> {
    excel_near_zero: bool,
    total: f64,
    exact_total: Option<Trace>,
}

impl<Trace: ExactTrace> ExcelSum<Trace> {
    pub(super) fn new(engine: &Engine<'_>) -> Self {
        Self {
            excel_near_zero: matches!(
                engine.arithmetic_semantics(),
                ArithmeticSemantics::ExcelNearZero
            ),
            total: 0.0,
            exact_total: Some(Trace::EXACT_ZERO),
        }
    }

    pub(super) fn add_with_trace(&mut self, value: f64, trace: Option<Trace>) {
        let next = self.total + value;
        if !self.excel_near_zero {
            self.total = next;
            return;
        }

        self.exact_total = self
            .exact_total
            .and_then(|total| total.combined_with(trace?));
        if self.exact_total.is_some_and(ExactTrace::is_exact_zero)
            && is_excel_near_zero_cancellation(self.total, value, next)
        {
            self.total = 0.0;
        } else {
            self.total = next;
        }
    }

    pub(super) const fn total(&self) -> f64 {
        self.total
    }
}

pub(super) fn excel_numeric_arguments(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[Expr],
) -> Result<Vec<f64>, ErrorKind> {
    excel_numeric_arguments_with_policy(engine, context, args, SheetSpanPolicy::Unsupported)
}

pub(super) fn excel_numeric_arguments_with_policy(
    engine: &Engine<'_>,
    context: EvalContext<'_>,
    args: &[Expr],
    sheet_span_policy: SheetSpanPolicy,
) -> Result<Vec<f64>, ErrorKind> {
    let mut numbers = Vec::new();
    for ArgumentValue {
        value,
        from_collection,
        ..
    } in collect_argument_values_with_policy(engine, context, args, sheet_span_policy)?
    {
        match value {
            Value::Number(number) => numbers.push(number),
            Value::Logical(logical) if !from_collection => {
                numbers.push(if logical { 1.0 } else { 0.0 });
            }
            Value::Text(text) if !from_collection => {
                let number = text
                    .trim()
                    .parse::<f64>()
                    .ok()
                    .filter(|number| number.is_finite())
                    .ok_or(ErrorKind::Value)?;
                numbers.push(number);
            }
            Value::Error(kind) => return Err(kind),
            Value::Blank | Value::Text(_) | Value::Logical(_) => {}
        }
    }
    Ok(numbers)
}