FIX: Consolidate two structs

This commit is contained in:
Nicolas Hatcher
2024-02-20 15:19:05 +01:00
parent 55a963e360
commit ab2f1f10fc
26 changed files with 379 additions and 395 deletions

View File

@@ -1,9 +1,7 @@
use crate::constants::{LAST_COLUMN, LAST_ROW};
use crate::expressions::types::CellReferenceIndex;
use crate::{
calc_result::{CalcResult, CellReference},
expressions::parser::Node,
expressions::token::Error,
model::Model,
calc_result::CalcResult, expressions::parser::Node, expressions::token::Error, model::Model,
};
use std::f64::consts::PI;
@@ -19,7 +17,7 @@ pub fn random() -> f64 {
}
impl Model {
pub(crate) fn fn_min(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_min(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
let mut result = f64::NAN;
for arg in args {
match self.evaluate_node_in_context(arg, cell) {
@@ -34,7 +32,7 @@ impl Model {
}
for row in left.row..(right.row + 1) {
for column in left.column..(right.column + 1) {
match self.evaluate_cell(CellReference {
match self.evaluate_cell(CellReferenceIndex {
sheet: left.sheet,
row,
column,
@@ -62,7 +60,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_max(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_max(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
let mut result = f64::NAN;
for arg in args {
match self.evaluate_node_in_context(arg, cell) {
@@ -77,7 +75,7 @@ impl Model {
}
for row in left.row..(right.row + 1) {
for column in left.column..(right.column + 1) {
match self.evaluate_cell(CellReference {
match self.evaluate_cell(CellReferenceIndex {
sheet: left.sheet,
row,
column,
@@ -105,7 +103,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_sum(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_sum(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.is_empty() {
return CalcResult::new_args_number_error(cell);
}
@@ -147,7 +145,7 @@ impl Model {
}
for row in row1..row2 + 1 {
for column in column1..(column2 + 1) {
match self.evaluate_cell(CellReference {
match self.evaluate_cell(CellReferenceIndex {
sheet: left.sheet,
row,
column,
@@ -172,7 +170,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_product(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_product(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.is_empty() {
return CalcResult::new_args_number_error(cell);
}
@@ -214,7 +212,7 @@ impl Model {
}
for row in row1..row2 + 1 {
for column in column1..(column2 + 1) {
match self.evaluate_cell(CellReference {
match self.evaluate_cell(CellReferenceIndex {
sheet: left.sheet,
row,
column,
@@ -245,7 +243,7 @@ impl Model {
/// SUMIF(criteria_range, criteria, [sum_range])
/// if sum_rage is missing then criteria_range will be used
pub(crate) fn fn_sumif(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_sumif(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() == 2 {
let arguments = vec![args[0].clone(), args[0].clone(), args[1].clone()];
self.fn_sumifs(&arguments, cell)
@@ -258,7 +256,7 @@ impl Model {
}
/// SUMIFS(sum_range, criteria_range1, criteria1, [criteria_range2, criteria2], ...)
pub(crate) fn fn_sumifs(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_sumifs(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
let mut total = 0.0;
let sum = |value| total += value;
if let Err(e) = self.apply_ifs(args, cell, sum) {
@@ -267,7 +265,7 @@ impl Model {
CalcResult::Number(total)
}
pub(crate) fn fn_round(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_round(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 2 {
// Incorrect number of arguments
return CalcResult::new_args_number_error(cell);
@@ -289,7 +287,7 @@ impl Model {
let scale = 10.0_f64.powf(number_of_digits);
CalcResult::Number((value * scale).round() / scale)
}
pub(crate) fn fn_roundup(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_roundup(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 2 {
return CalcResult::new_args_number_error(cell);
}
@@ -314,7 +312,7 @@ impl Model {
CalcResult::Number((value * scale).floor() / scale)
}
}
pub(crate) fn fn_rounddown(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_rounddown(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 2 {
return CalcResult::new_args_number_error(cell);
}
@@ -340,7 +338,7 @@ impl Model {
}
}
pub(crate) fn fn_sin(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_sin(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -351,7 +349,7 @@ impl Model {
let result = value.sin();
CalcResult::Number(result)
}
pub(crate) fn fn_cos(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_cos(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -363,7 +361,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_tan(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_tan(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -375,7 +373,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_sinh(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_sinh(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -386,7 +384,7 @@ impl Model {
let result = value.sinh();
CalcResult::Number(result)
}
pub(crate) fn fn_cosh(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_cosh(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -398,7 +396,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_tanh(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_tanh(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -410,7 +408,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_asin(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_asin(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -428,7 +426,7 @@ impl Model {
}
CalcResult::Number(result)
}
pub(crate) fn fn_acos(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_acos(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -447,7 +445,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_atan(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_atan(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -466,7 +464,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_asinh(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_asinh(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -484,7 +482,7 @@ impl Model {
}
CalcResult::Number(result)
}
pub(crate) fn fn_acosh(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_acosh(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -503,7 +501,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_atanh(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_atanh(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -522,14 +520,14 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_pi(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_pi(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if !args.is_empty() {
return CalcResult::new_args_number_error(cell);
}
CalcResult::Number(PI)
}
pub(crate) fn fn_abs(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_abs(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -540,7 +538,7 @@ impl Model {
CalcResult::Number(value.abs())
}
pub(crate) fn fn_sqrtpi(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_sqrtpi(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -558,7 +556,7 @@ impl Model {
CalcResult::Number((value * PI).sqrt())
}
pub(crate) fn fn_sqrt(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_sqrt(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
@@ -576,7 +574,7 @@ impl Model {
CalcResult::Number(value.sqrt())
}
pub(crate) fn fn_atan2(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_atan2(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 2 {
return CalcResult::new_args_number_error(cell);
}
@@ -598,7 +596,7 @@ impl Model {
CalcResult::Number(f64::atan2(y, x))
}
pub(crate) fn fn_power(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_power(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 2 {
return CalcResult::new_args_number_error(cell);
}
@@ -639,7 +637,7 @@ impl Model {
CalcResult::Number(result)
}
pub(crate) fn fn_rand(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_rand(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if !args.is_empty() {
return CalcResult::new_args_number_error(cell);
}
@@ -647,7 +645,7 @@ impl Model {
}
// TODO: Add tests for RANDBETWEEN
pub(crate) fn fn_randbetween(&mut self, args: &[Node], cell: CellReference) -> CalcResult {
pub(crate) fn fn_randbetween(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 2 {
return CalcResult::new_args_number_error(cell);
}