Skip to main content

excelize_rs/calc/
common.rs

1//! Shared helpers for the formula function submodules.
2//!
3//! Most helpers live in `crate::calc::arg`; this file is reserved for helpers
4//! that are specific to the function-category submodules.
5
6use crate::calc::arg::*;
7
8/// Return the first error found in a slice of arguments, if any.
9pub fn first_error(args: &[FormulaArg]) -> Option<FormulaArg> {
10    args.iter().find(|a| a.is_error()).cloned()
11}
12
13/// Count the number of numeric values in a (possibly nested) argument.
14pub fn count_numeric(arg: &FormulaArg) -> usize {
15    match arg.typ {
16        ArgType::Number => 1,
17        ArgType::List => arg.list.iter().map(count_numeric).sum(),
18        ArgType::Matrix => arg
19            .matrix
20            .iter()
21            .flat_map(|r| r.iter())
22            .map(count_numeric)
23            .sum(),
24        _ => 0,
25    }
26}