Shared Rust kernels
catalogue/financial
All modules · quoted at commit 9e1884369a67 of DnaCalc/DnaCalc. Comments are the source's own.
required_number · 6 lines · calc-func/src/catalogue/financial.rs on GitHub/// Required scalar financial slots distinguish a Missing placeholder from a
/// blank cell. The latter still reaches the existing numeric coercion as zero.
pub(super) fn required_number(value: &CalcValue) -> Step<()> {
if value.is_missing() { return fail(W::NA); }
reject_logical(value)
}
Used by 7 functions.
required_num · 4 lines · calc-func/src/catalogue/financial.rs on GitHubpub(super) fn required_num(site: &Site<'_>, a: &[CalcValue], index: usize) -> Step<f64> {
required_number(&a[index])?;
num(site, a, index)
}
Used by 4 functions.
tv_out · 9 lines · calc-func/src/catalogue/financial.rs on GitHub/// A time-value kernel's result: its number, or the worksheet error its failure maps to.
fn tv_out(site: &Site<'_>, result: Result<f64, FinancialError>) -> Step<CalcValue> {
match result {
Ok(n) => site.number(n),
Err(FinancialError::Num | FinancialError::NoConvergence) => fail(W::Num),
Err(FinancialError::Div0) => fail(W::Div0),
Err(FinancialError::Value) => fail(W::Value),
}
}
Used by 15 functions.
timing · 9 lines · calc-func/src/catalogue/financial.rs on GitHub/// The payment timing argument `index`: 0 (the default) is the end of each period, any other
/// number truncated to non-zero its beginning.
fn timing(site: &Site<'_>, a: &[CalcValue], index: usize) -> Step<PaymentTiming> {
Ok(if opt(site, a, index, 0.0)?.trunc() == 0.0 {
PaymentTiming::EndOfPeriod
} else {
PaymentTiming::BeginningOfPeriod
})
}
Used by 7 functions.
resolved · 16 lines · calc-func/src/catalogue/financial.rs on GitHub/// Argument `index` as OxFunc's resolver gave it to the cash-flow collectors: a reference to one
/// cell as the cell's value, a larger reference as the array of its values, anything else as it
/// is.
fn resolved(site: &Site<'_>, a: &[CalcValue], index: usize, sv: &mut EvalServices<'_>) -> Step<CalcValue> {
if !matches!(a[index].core_ref(), CoreRef::Reference(_)) {
return Ok(a[index].clone());
}
let g = grid(site, a, index, sv)?;
if g.rows == 1 && g.cols == 1 {
return Ok(g.cells.into_iter().next().unwrap_or_else(CalcValue::empty));
}
let rows: Vec<Vec<CalcValue>> = g.cells.chunks(g.cols).map(<[CalcValue]>::to_vec).collect();
CalcArray::from_rows(rows)
.map(CalcValue::array)
.ok_or_else(|| site.not_modelled(Some(index)))
}
Used by 3 functions.
cash_err · 4 lines · calc-func/src/catalogue/financial.rs on GitHub/// A cash-flow collector's or kernel's failure as a worksheet error.
fn cash_err<T>(error: cash::CashflowRateEvalError) -> Step<T> {
fail(cash::map_cashflow_rate_error_to_ws(&error))
}
Used by 3 functions.
guess · 11 lines · calc-func/src/catalogue/financial.rs on GitHub/// The optional guess of IRR and XIRR: `None` when omitted, else the number OxFunc's rule reads.
fn guess(site: &Site<'_>, a: &[CalcValue], index: usize, sv: &mut EvalServices<'_>) -> Step<Option<f64>> {
if !given(a, index) {
return Ok(None);
}
let value = resolved(site, a, index, sv)?;
match cash::scalar_number_from_eval(&value) {
Ok(n) => Ok(Some(n)),
Err(error) => cash_err(error),
}
}
Used by 2 functions.
date_1900 · 12 lines · calc-func/src/catalogue/financial.rs on GitHub/// A date argument of AMORLINC or AMORDEGRC as a serial of the 1900 system, counted in the date
/// system of the call's environment.
pub(super) fn date_1900(site: &Site<'_>, a: &[CalcValue], index: usize, sv: &EvalServices<'_>) -> Step<f64> {
let Some(environment) = sv.environment() else {
return Err(site.not_modelled(None));
};
let serial = num(site, a, index)?;
Ok(match environment.date_system {
DateSystem::System1904 if serial >= 0.0 => serial + OFFSET_1904,
_ => serial,
})
}
Used by 28 functions.
amor_out · 6 lines · calc-func/src/catalogue/financial.rs on GitHubfn amor_out(site: &Site<'_>, result: Result<f64, amor::AmorDepreciationEvalError>) -> Step<CalcValue> {
match result {
Ok(n) => site.number(n),
Err(error) => fail(amor::map_amor_depreciation_error_to_ws(&error)),
}
}
Used by 2 functions.
cumulative_out · 9 lines · calc-func/src/catalogue/financial.rs on GitHubfn cumulative_out(
site: &Site<'_>,
result: Result<f64, cumulative::CumulativeFinanceEvalError>,
) -> Step<CalcValue> {
match result {
Ok(n) => site.number(n),
Err(error) => fail(cumulative::map_cumulative_finance_error_to_ws(&error)),
}
}
Used by 2 functions.
dollar_num · 8 lines · calc-func/src/catalogue/financial.rs on GitHub/// DOLLARDE's and DOLLARFR's argument: a logical is #VALUE! (OxFunc's surface admits numbers,
/// numeric text and blank cells only).
fn dollar_num(site: &Site<'_>, a: &[CalcValue], index: usize) -> Step<f64> {
match a[index].core_ref() {
CoreRef::Logical(_) => fail(W::Value),
_ => num(site, a, index),
}
}
Used by 2 functions.